blob: fd092068ef3b90ca35469c32f2df3735646b3bf4 [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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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"
cristyd43a46b2010-01-21 02:13:41 +000044#include "magick/accelerate.h"
cristy3ed852e2009-09-05 21:47:34 +000045#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"
cristy6771f1e2010-03-05 19:43:39 +000067#include "magick/morphology.h"
cristy3ed852e2009-09-05 21:47:34 +000068#include "magick/paint.h"
69#include "magick/pixel-private.h"
70#include "magick/property.h"
71#include "magick/quantize.h"
72#include "magick/quantum.h"
73#include "magick/random_.h"
74#include "magick/random-private.h"
75#include "magick/resample.h"
76#include "magick/resample-private.h"
77#include "magick/resize.h"
78#include "magick/resource_.h"
79#include "magick/segment.h"
80#include "magick/shear.h"
81#include "magick/signature-private.h"
82#include "magick/string_.h"
83#include "magick/thread-private.h"
84#include "magick/transform.h"
85#include "magick/threshold.h"
86
87/*
88%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89% %
90% %
91% %
92% A d a p t i v e B l u r I m a g e %
93% %
94% %
95% %
96%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97%
98% AdaptiveBlurImage() adaptively blurs the image by blurring less
99% intensely near image edges and more intensely far from edges. We blur the
100% image with a Gaussian operator of the given radius and standard deviation
101% (sigma). For reasonable results, radius should be larger than sigma. Use a
102% radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
103%
104% The format of the AdaptiveBlurImage method is:
105%
106% Image *AdaptiveBlurImage(const Image *image,const double radius,
107% const double sigma,ExceptionInfo *exception)
108% Image *AdaptiveBlurImageChannel(const Image *image,
109% const ChannelType channel,double radius,const double sigma,
110% ExceptionInfo *exception)
111%
112% A description of each parameter follows:
113%
114% o image: the image.
115%
116% o channel: the channel type.
117%
118% o radius: the radius of the Gaussian, in pixels, not counting the center
119% pixel.
120%
121% o sigma: the standard deviation of the Laplacian, in pixels.
122%
123% o exception: return any errors or warnings in this structure.
124%
125*/
126
127MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
128 const double sigma,ExceptionInfo *exception)
129{
130 Image
131 *blur_image;
132
133 blur_image=AdaptiveBlurImageChannel(image,DefaultChannels,radius,sigma,
134 exception);
135 return(blur_image);
136}
137
138MagickExport Image *AdaptiveBlurImageChannel(const Image *image,
139 const ChannelType channel,const double radius,const double sigma,
140 ExceptionInfo *exception)
141{
142#define AdaptiveBlurImageTag "Convolve/Image"
143#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
144
cristyc4c8d132010-01-07 01:58:38 +0000145 CacheView
146 *blur_view,
147 *edge_view,
148 *image_view;
149
cristy3ed852e2009-09-05 21:47:34 +0000150 double
cristy47e00502009-12-17 19:19:57 +0000151 **kernel,
152 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000153
154 Image
155 *blur_image,
156 *edge_image,
157 *gaussian_image;
158
cristy3ed852e2009-09-05 21:47:34 +0000159 MagickBooleanType
160 status;
161
cristybb503372010-05-27 20:51:26 +0000162 MagickOffsetType
163 progress;
164
cristy3ed852e2009-09-05 21:47:34 +0000165 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +0000166 bias;
cristy3ed852e2009-09-05 21:47:34 +0000167
cristybb503372010-05-27 20:51:26 +0000168 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000169 i;
cristy3ed852e2009-09-05 21:47:34 +0000170
cristybb503372010-05-27 20:51:26 +0000171 size_t
cristy3ed852e2009-09-05 21:47:34 +0000172 width;
173
cristybb503372010-05-27 20:51:26 +0000174 ssize_t
175 j,
176 k,
177 u,
178 v,
179 y;
180
cristy3ed852e2009-09-05 21:47:34 +0000181 assert(image != (const Image *) NULL);
182 assert(image->signature == MagickSignature);
183 if (image->debug != MagickFalse)
184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
185 assert(exception != (ExceptionInfo *) NULL);
186 assert(exception->signature == MagickSignature);
187 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
188 if (blur_image == (Image *) NULL)
189 return((Image *) NULL);
190 if (fabs(sigma) <= MagickEpsilon)
191 return(blur_image);
192 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
193 {
194 InheritException(exception,&blur_image->exception);
195 blur_image=DestroyImage(blur_image);
196 return((Image *) NULL);
197 }
198 /*
199 Edge detect the image brighness channel, level, blur, and level again.
200 */
201 edge_image=EdgeImage(image,radius,exception);
202 if (edge_image == (Image *) NULL)
203 {
204 blur_image=DestroyImage(blur_image);
205 return((Image *) NULL);
206 }
207 (void) LevelImage(edge_image,"20%,95%");
208 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
209 if (gaussian_image != (Image *) NULL)
210 {
211 edge_image=DestroyImage(edge_image);
212 edge_image=gaussian_image;
213 }
214 (void) LevelImage(edge_image,"10%,95%");
215 /*
216 Create a set of kernels from maximum (radius,sigma) to minimum.
217 */
218 width=GetOptimalKernelWidth2D(radius,sigma);
219 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
220 if (kernel == (double **) NULL)
221 {
222 edge_image=DestroyImage(edge_image);
223 blur_image=DestroyImage(blur_image);
224 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
225 }
226 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000227 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000228 {
229 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
230 sizeof(**kernel));
231 if (kernel[i] == (double *) NULL)
232 break;
cristy47e00502009-12-17 19:19:57 +0000233 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000234 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000235 k=0;
236 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000237 {
cristy47e00502009-12-17 19:19:57 +0000238 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000239 {
cristy4205a3c2010-09-12 20:19:59 +0000240 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
241 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000242 normalize+=kernel[i][k];
243 k++;
cristy3ed852e2009-09-05 21:47:34 +0000244 }
245 }
cristy3ed852e2009-09-05 21:47:34 +0000246 if (fabs(normalize) <= MagickEpsilon)
247 normalize=1.0;
248 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000249 for (k=0; k < (j*j); k++)
250 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000251 }
cristybb503372010-05-27 20:51:26 +0000252 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000253 {
254 for (i-=2; i >= 0; i-=2)
255 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
256 kernel=(double **) RelinquishMagickMemory(kernel);
257 edge_image=DestroyImage(edge_image);
258 blur_image=DestroyImage(blur_image);
259 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
260 }
261 /*
262 Adaptively blur image.
263 */
264 status=MagickTrue;
265 progress=0;
cristyddd82202009-11-03 20:14:50 +0000266 GetMagickPixelPacket(image,&bias);
267 SetMagickPixelPacketBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000268 image_view=AcquireCacheView(image);
269 edge_view=AcquireCacheView(edge_image);
270 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000271#if defined(MAGICKCORE_OPENMP_SUPPORT)
272 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000273#endif
cristybb503372010-05-27 20:51:26 +0000274 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000275 {
276 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000277 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +0000278
279 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000280 *restrict p,
281 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000282
283 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000284 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +0000285
cristy3ed852e2009-09-05 21:47:34 +0000286 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000287 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000288
cristy117ff172010-08-15 21:35:32 +0000289 register ssize_t
290 x;
291
cristy3ed852e2009-09-05 21:47:34 +0000292 if (status == MagickFalse)
293 continue;
294 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
295 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
296 exception);
297 if ((r == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
298 {
299 status=MagickFalse;
300 continue;
301 }
302 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +0000303 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000304 {
305 MagickPixelPacket
306 pixel;
307
308 MagickRealType
309 alpha,
310 gamma;
311
312 register const double
cristyc47d1f82009-11-26 01:44:43 +0000313 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000314
cristybb503372010-05-27 20:51:26 +0000315 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000316 i,
317 u,
318 v;
319
320 gamma=0.0;
cristybb503372010-05-27 20:51:26 +0000321 i=(ssize_t) ceil((double) width*QuantumScale*PixelIntensity(r)-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000322 if (i < 0)
323 i=0;
324 else
cristybb503372010-05-27 20:51:26 +0000325 if (i > (ssize_t) width)
326 i=(ssize_t) width;
cristy3ed852e2009-09-05 21:47:34 +0000327 if ((i & 0x01) != 0)
328 i--;
cristya21afde2010-07-02 00:45:40 +0000329 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
330 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
cristy3ed852e2009-09-05 21:47:34 +0000331 if (p == (const PixelPacket *) NULL)
332 break;
333 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristyddd82202009-11-03 20:14:50 +0000334 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000335 k=kernel[i];
cristybb503372010-05-27 20:51:26 +0000336 for (v=0; v < (ssize_t) (width-i); v++)
cristy3ed852e2009-09-05 21:47:34 +0000337 {
cristybb503372010-05-27 20:51:26 +0000338 for (u=0; u < (ssize_t) (width-i); u++)
cristy3ed852e2009-09-05 21:47:34 +0000339 {
340 alpha=1.0;
341 if (((channel & OpacityChannel) != 0) &&
342 (image->matte != MagickFalse))
cristy46f08202010-01-10 04:04:21 +0000343 alpha=(MagickRealType) (QuantumScale*GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000344 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000345 pixel.red+=(*k)*alpha*GetRedPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000346 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000347 pixel.green+=(*k)*alpha*GetGreenPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000348 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000349 pixel.blue+=(*k)*alpha*GetBluePixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000350 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000351 pixel.opacity+=(*k)*GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000352 if (((channel & IndexChannel) != 0) &&
353 (image->colorspace == CMYKColorspace))
354 pixel.index+=(*k)*alpha*indexes[x+(width-i)*v+u];
355 gamma+=(*k)*alpha;
356 k++;
357 p++;
358 }
359 }
360 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
361 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000362 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000363 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000364 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000365 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000366 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000367 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000368 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000369 if (((channel & IndexChannel) != 0) &&
370 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +0000371 blur_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000372 q++;
373 r++;
374 }
375 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
376 status=MagickFalse;
377 if (image->progress_monitor != (MagickProgressMonitor) NULL)
378 {
379 MagickBooleanType
380 proceed;
381
cristyb5d5f722009-11-04 03:03:49 +0000382#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000383 #pragma omp critical (MagickCore_AdaptiveBlurImageChannel)
384#endif
385 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
386 image->rows);
387 if (proceed == MagickFalse)
388 status=MagickFalse;
389 }
390 }
391 blur_image->type=image->type;
392 blur_view=DestroyCacheView(blur_view);
393 edge_view=DestroyCacheView(edge_view);
394 image_view=DestroyCacheView(image_view);
395 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000396 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000397 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
398 kernel=(double **) RelinquishMagickMemory(kernel);
399 if (status == MagickFalse)
400 blur_image=DestroyImage(blur_image);
401 return(blur_image);
402}
403
404/*
405%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406% %
407% %
408% %
409% A d a p t i v e S h a r p e n I m a g e %
410% %
411% %
412% %
413%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414%
415% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
416% intensely near image edges and less intensely far from edges. We sharpen the
417% image with a Gaussian operator of the given radius and standard deviation
418% (sigma). For reasonable results, radius should be larger than sigma. Use a
419% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
420%
421% The format of the AdaptiveSharpenImage method is:
422%
423% Image *AdaptiveSharpenImage(const Image *image,const double radius,
424% const double sigma,ExceptionInfo *exception)
425% Image *AdaptiveSharpenImageChannel(const Image *image,
426% const ChannelType channel,double radius,const double sigma,
427% ExceptionInfo *exception)
428%
429% A description of each parameter follows:
430%
431% o image: the image.
432%
433% o channel: the channel type.
434%
435% o radius: the radius of the Gaussian, in pixels, not counting the center
436% pixel.
437%
438% o sigma: the standard deviation of the Laplacian, in pixels.
439%
440% o exception: return any errors or warnings in this structure.
441%
442*/
443
444MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
445 const double sigma,ExceptionInfo *exception)
446{
447 Image
448 *sharp_image;
449
450 sharp_image=AdaptiveSharpenImageChannel(image,DefaultChannels,radius,sigma,
451 exception);
452 return(sharp_image);
453}
454
455MagickExport Image *AdaptiveSharpenImageChannel(const Image *image,
456 const ChannelType channel,const double radius,const double sigma,
457 ExceptionInfo *exception)
458{
459#define AdaptiveSharpenImageTag "Convolve/Image"
460#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
461
cristyc4c8d132010-01-07 01:58:38 +0000462 CacheView
463 *sharp_view,
464 *edge_view,
465 *image_view;
466
cristy3ed852e2009-09-05 21:47:34 +0000467 double
cristy47e00502009-12-17 19:19:57 +0000468 **kernel,
469 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000470
471 Image
472 *sharp_image,
473 *edge_image,
474 *gaussian_image;
475
cristy3ed852e2009-09-05 21:47:34 +0000476 MagickBooleanType
477 status;
478
cristybb503372010-05-27 20:51:26 +0000479 MagickOffsetType
480 progress;
481
cristy3ed852e2009-09-05 21:47:34 +0000482 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +0000483 bias;
cristy3ed852e2009-09-05 21:47:34 +0000484
cristybb503372010-05-27 20:51:26 +0000485 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000486 i;
cristy3ed852e2009-09-05 21:47:34 +0000487
cristybb503372010-05-27 20:51:26 +0000488 size_t
cristy3ed852e2009-09-05 21:47:34 +0000489 width;
490
cristybb503372010-05-27 20:51:26 +0000491 ssize_t
492 j,
493 k,
494 u,
495 v,
496 y;
497
cristy3ed852e2009-09-05 21:47:34 +0000498 assert(image != (const Image *) NULL);
499 assert(image->signature == MagickSignature);
500 if (image->debug != MagickFalse)
501 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
502 assert(exception != (ExceptionInfo *) NULL);
503 assert(exception->signature == MagickSignature);
504 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
505 if (sharp_image == (Image *) NULL)
506 return((Image *) NULL);
507 if (fabs(sigma) <= MagickEpsilon)
508 return(sharp_image);
509 if (SetImageStorageClass(sharp_image,DirectClass) == MagickFalse)
510 {
511 InheritException(exception,&sharp_image->exception);
512 sharp_image=DestroyImage(sharp_image);
513 return((Image *) NULL);
514 }
515 /*
516 Edge detect the image brighness channel, level, sharp, and level again.
517 */
518 edge_image=EdgeImage(image,radius,exception);
519 if (edge_image == (Image *) NULL)
520 {
521 sharp_image=DestroyImage(sharp_image);
522 return((Image *) NULL);
523 }
524 (void) LevelImage(edge_image,"20%,95%");
525 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
526 if (gaussian_image != (Image *) NULL)
527 {
528 edge_image=DestroyImage(edge_image);
529 edge_image=gaussian_image;
530 }
531 (void) LevelImage(edge_image,"10%,95%");
532 /*
533 Create a set of kernels from maximum (radius,sigma) to minimum.
534 */
535 width=GetOptimalKernelWidth2D(radius,sigma);
536 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
537 if (kernel == (double **) NULL)
538 {
539 edge_image=DestroyImage(edge_image);
540 sharp_image=DestroyImage(sharp_image);
541 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
542 }
543 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000544 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
546 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
547 sizeof(**kernel));
548 if (kernel[i] == (double *) NULL)
549 break;
cristy47e00502009-12-17 19:19:57 +0000550 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000551 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000552 k=0;
553 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000554 {
cristy47e00502009-12-17 19:19:57 +0000555 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000556 {
cristy4205a3c2010-09-12 20:19:59 +0000557 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
558 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000559 normalize+=kernel[i][k];
560 k++;
cristy3ed852e2009-09-05 21:47:34 +0000561 }
562 }
cristy3ed852e2009-09-05 21:47:34 +0000563 if (fabs(normalize) <= MagickEpsilon)
564 normalize=1.0;
565 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000566 for (k=0; k < (j*j); k++)
567 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000568 }
cristybb503372010-05-27 20:51:26 +0000569 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000570 {
571 for (i-=2; i >= 0; i-=2)
572 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
573 kernel=(double **) RelinquishMagickMemory(kernel);
574 edge_image=DestroyImage(edge_image);
575 sharp_image=DestroyImage(sharp_image);
576 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
577 }
578 /*
579 Adaptively sharpen image.
580 */
581 status=MagickTrue;
582 progress=0;
cristyddd82202009-11-03 20:14:50 +0000583 GetMagickPixelPacket(image,&bias);
584 SetMagickPixelPacketBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000585 image_view=AcquireCacheView(image);
586 edge_view=AcquireCacheView(edge_image);
587 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000588#if defined(MAGICKCORE_OPENMP_SUPPORT)
589 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000590#endif
cristybb503372010-05-27 20:51:26 +0000591 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000592 {
593 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000594 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +0000595
596 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000597 *restrict p,
598 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000599
600 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000601 *restrict sharp_indexes;
cristy3ed852e2009-09-05 21:47:34 +0000602
cristy3ed852e2009-09-05 21:47:34 +0000603 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000604 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000605
cristy117ff172010-08-15 21:35:32 +0000606 register ssize_t
607 x;
608
cristy3ed852e2009-09-05 21:47:34 +0000609 if (status == MagickFalse)
610 continue;
611 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
612 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
613 exception);
614 if ((r == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
615 {
616 status=MagickFalse;
617 continue;
618 }
619 sharp_indexes=GetCacheViewAuthenticIndexQueue(sharp_view);
cristybb503372010-05-27 20:51:26 +0000620 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000621 {
622 MagickPixelPacket
623 pixel;
624
625 MagickRealType
626 alpha,
627 gamma;
628
629 register const double
cristyc47d1f82009-11-26 01:44:43 +0000630 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000631
cristybb503372010-05-27 20:51:26 +0000632 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000633 i,
634 u,
635 v;
636
637 gamma=0.0;
cristybb503372010-05-27 20:51:26 +0000638 i=(ssize_t) ceil((double) width*(QuantumRange-QuantumScale*
cristy1f9ce9f2010-04-28 11:55:12 +0000639 PixelIntensity(r))-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000640 if (i < 0)
641 i=0;
642 else
cristybb503372010-05-27 20:51:26 +0000643 if (i > (ssize_t) width)
644 i=(ssize_t) width;
cristy3ed852e2009-09-05 21:47:34 +0000645 if ((i & 0x01) != 0)
646 i--;
cristy117ff172010-08-15 21:35:32 +0000647 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
648 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
cristy3ed852e2009-09-05 21:47:34 +0000649 if (p == (const PixelPacket *) NULL)
650 break;
651 indexes=GetCacheViewVirtualIndexQueue(image_view);
652 k=kernel[i];
cristyddd82202009-11-03 20:14:50 +0000653 pixel=bias;
cristybb503372010-05-27 20:51:26 +0000654 for (v=0; v < (ssize_t) (width-i); v++)
cristy3ed852e2009-09-05 21:47:34 +0000655 {
cristybb503372010-05-27 20:51:26 +0000656 for (u=0; u < (ssize_t) (width-i); u++)
cristy3ed852e2009-09-05 21:47:34 +0000657 {
658 alpha=1.0;
659 if (((channel & OpacityChannel) != 0) &&
660 (image->matte != MagickFalse))
cristy46f08202010-01-10 04:04:21 +0000661 alpha=(MagickRealType) (QuantumScale*GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000662 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000663 pixel.red+=(*k)*alpha*GetRedPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000664 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000665 pixel.green+=(*k)*alpha*GetGreenPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000666 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000667 pixel.blue+=(*k)*alpha*GetBluePixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000668 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000669 pixel.opacity+=(*k)*GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000670 if (((channel & IndexChannel) != 0) &&
671 (image->colorspace == CMYKColorspace))
672 pixel.index+=(*k)*alpha*indexes[x+(width-i)*v+u];
673 gamma+=(*k)*alpha;
674 k++;
675 p++;
676 }
677 }
678 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
679 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000680 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000681 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000682 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000683 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000684 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000685 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000686 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000687 if (((channel & IndexChannel) != 0) &&
688 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +0000689 sharp_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000690 q++;
691 r++;
692 }
693 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
694 status=MagickFalse;
695 if (image->progress_monitor != (MagickProgressMonitor) NULL)
696 {
697 MagickBooleanType
698 proceed;
699
cristyb5d5f722009-11-04 03:03:49 +0000700#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000701 #pragma omp critical (MagickCore_AdaptiveSharpenImageChannel)
702#endif
703 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
704 image->rows);
705 if (proceed == MagickFalse)
706 status=MagickFalse;
707 }
708 }
709 sharp_image->type=image->type;
710 sharp_view=DestroyCacheView(sharp_view);
711 edge_view=DestroyCacheView(edge_view);
712 image_view=DestroyCacheView(image_view);
713 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000714 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000715 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
716 kernel=(double **) RelinquishMagickMemory(kernel);
717 if (status == MagickFalse)
718 sharp_image=DestroyImage(sharp_image);
719 return(sharp_image);
720}
721
722/*
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724% %
725% %
726% %
727% B l u r I m a g e %
728% %
729% %
730% %
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732%
733% BlurImage() blurs an image. We convolve the image with a Gaussian operator
734% of the given radius and standard deviation (sigma). For reasonable results,
735% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
736% selects a suitable radius for you.
737%
738% BlurImage() differs from GaussianBlurImage() in that it uses a separable
739% kernel which is faster but mathematically equivalent to the non-separable
740% kernel.
741%
742% The format of the BlurImage method is:
743%
744% Image *BlurImage(const Image *image,const double radius,
745% const double sigma,ExceptionInfo *exception)
746% Image *BlurImageChannel(const Image *image,const ChannelType channel,
747% const double radius,const double sigma,ExceptionInfo *exception)
748%
749% A description of each parameter follows:
750%
751% o image: the image.
752%
753% o channel: the channel type.
754%
755% o radius: the radius of the Gaussian, in pixels, not counting the center
756% pixel.
757%
758% o sigma: the standard deviation of the Gaussian, in pixels.
759%
760% o exception: return any errors or warnings in this structure.
761%
762*/
763
764MagickExport Image *BlurImage(const Image *image,const double radius,
765 const double sigma,ExceptionInfo *exception)
766{
767 Image
768 *blur_image;
769
770 blur_image=BlurImageChannel(image,DefaultChannels,radius,sigma,exception);
771 return(blur_image);
772}
773
cristybb503372010-05-27 20:51:26 +0000774static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000775{
cristy3ed852e2009-09-05 21:47:34 +0000776 double
cristy47e00502009-12-17 19:19:57 +0000777 *kernel,
778 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000779
cristy117ff172010-08-15 21:35:32 +0000780 register ssize_t
781 i;
782
cristybb503372010-05-27 20:51:26 +0000783 ssize_t
cristy47e00502009-12-17 19:19:57 +0000784 j,
785 k;
cristy3ed852e2009-09-05 21:47:34 +0000786
cristy3ed852e2009-09-05 21:47:34 +0000787 /*
788 Generate a 1-D convolution kernel.
789 */
790 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
791 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
792 if (kernel == (double *) NULL)
793 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000794 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000795 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000796 i=0;
797 for (k=(-j); k <= j; k++)
798 {
cristy4205a3c2010-09-12 20:19:59 +0000799 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
800 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000801 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000802 i++;
803 }
cristybb503372010-05-27 20:51:26 +0000804 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000805 kernel[i]/=normalize;
806 return(kernel);
807}
808
809MagickExport Image *BlurImageChannel(const Image *image,
810 const ChannelType channel,const double radius,const double sigma,
811 ExceptionInfo *exception)
812{
813#define BlurImageTag "Blur/Image"
814
cristyc4c8d132010-01-07 01:58:38 +0000815 CacheView
816 *blur_view,
817 *image_view;
818
cristy3ed852e2009-09-05 21:47:34 +0000819 double
820 *kernel;
821
822 Image
823 *blur_image;
824
cristy3ed852e2009-09-05 21:47:34 +0000825 MagickBooleanType
826 status;
827
cristybb503372010-05-27 20:51:26 +0000828 MagickOffsetType
829 progress;
830
cristy3ed852e2009-09-05 21:47:34 +0000831 MagickPixelPacket
cristy3ed852e2009-09-05 21:47:34 +0000832 bias;
833
cristybb503372010-05-27 20:51:26 +0000834 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000835 i;
836
cristybb503372010-05-27 20:51:26 +0000837 size_t
cristy3ed852e2009-09-05 21:47:34 +0000838 width;
839
cristybb503372010-05-27 20:51:26 +0000840 ssize_t
841 x,
842 y;
843
cristy3ed852e2009-09-05 21:47:34 +0000844 /*
845 Initialize blur image attributes.
846 */
847 assert(image != (Image *) NULL);
848 assert(image->signature == MagickSignature);
849 if (image->debug != MagickFalse)
850 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
851 assert(exception != (ExceptionInfo *) NULL);
852 assert(exception->signature == MagickSignature);
853 blur_image=CloneImage(image,0,0,MagickTrue,exception);
854 if (blur_image == (Image *) NULL)
855 return((Image *) NULL);
856 if (fabs(sigma) <= MagickEpsilon)
857 return(blur_image);
858 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
859 {
860 InheritException(exception,&blur_image->exception);
861 blur_image=DestroyImage(blur_image);
862 return((Image *) NULL);
863 }
864 width=GetOptimalKernelWidth1D(radius,sigma);
865 kernel=GetBlurKernel(width,sigma);
866 if (kernel == (double *) NULL)
867 {
868 blur_image=DestroyImage(blur_image);
869 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
870 }
871 if (image->debug != MagickFalse)
872 {
873 char
874 format[MaxTextExtent],
875 *message;
876
877 register const double
878 *k;
879
880 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000881 " BlurImage with %.20g kernel:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000882 message=AcquireString("");
883 k=kernel;
cristybb503372010-05-27 20:51:26 +0000884 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000885 {
886 *message='\0';
cristye8c25f92010-06-03 00:53:06 +0000887 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000888 (void) ConcatenateString(&message,format);
cristye7f51092010-01-17 00:39:37 +0000889 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000890 (void) ConcatenateString(&message,format);
891 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
892 }
893 message=DestroyString(message);
894 }
895 /*
896 Blur rows.
897 */
898 status=MagickTrue;
899 progress=0;
cristyddd82202009-11-03 20:14:50 +0000900 GetMagickPixelPacket(image,&bias);
901 SetMagickPixelPacketBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000902 image_view=AcquireCacheView(image);
903 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000904#if defined(MAGICKCORE_OPENMP_SUPPORT)
905 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000906#endif
cristybb503372010-05-27 20:51:26 +0000907 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000908 {
909 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000910 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +0000911
912 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000913 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000914
915 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000916 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +0000917
cristy3ed852e2009-09-05 21:47:34 +0000918 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000919 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000920
cristy117ff172010-08-15 21:35:32 +0000921 register ssize_t
922 x;
923
cristy3ed852e2009-09-05 21:47:34 +0000924 if (status == MagickFalse)
925 continue;
cristy117ff172010-08-15 21:35:32 +0000926 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
927 image->columns+width,1,exception);
cristy3ed852e2009-09-05 21:47:34 +0000928 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
929 exception);
930 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
931 {
932 status=MagickFalse;
933 continue;
934 }
935 indexes=GetCacheViewVirtualIndexQueue(image_view);
936 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +0000937 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000938 {
939 MagickPixelPacket
940 pixel;
941
942 register const double
cristyc47d1f82009-11-26 01:44:43 +0000943 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000944
945 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000946 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000947
cristybb503372010-05-27 20:51:26 +0000948 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000949 i;
950
cristyddd82202009-11-03 20:14:50 +0000951 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000952 k=kernel;
953 kernel_pixels=p;
954 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
955 {
cristybb503372010-05-27 20:51:26 +0000956 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000957 {
958 pixel.red+=(*k)*kernel_pixels->red;
959 pixel.green+=(*k)*kernel_pixels->green;
960 pixel.blue+=(*k)*kernel_pixels->blue;
961 k++;
962 kernel_pixels++;
963 }
964 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000965 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000966 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000967 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000968 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000969 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000970 if ((channel & OpacityChannel) != 0)
971 {
972 k=kernel;
973 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +0000974 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000975 {
976 pixel.opacity+=(*k)*kernel_pixels->opacity;
977 k++;
978 kernel_pixels++;
979 }
cristyce70c172010-01-07 17:15:30 +0000980 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000981 }
982 if (((channel & IndexChannel) != 0) &&
983 (image->colorspace == CMYKColorspace))
984 {
985 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000986 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +0000987
988 k=kernel;
989 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +0000990 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000991 {
992 pixel.index+=(*k)*(*kernel_indexes);
993 k++;
994 kernel_indexes++;
995 }
cristyce70c172010-01-07 17:15:30 +0000996 blur_indexes[x]=ClampToQuantum(pixel.index);
cristy3ed852e2009-09-05 21:47:34 +0000997 }
998 }
999 else
1000 {
1001 MagickRealType
1002 alpha,
1003 gamma;
1004
1005 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001006 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001007 {
cristy46f08202010-01-10 04:04:21 +00001008 alpha=(MagickRealType) (QuantumScale*
1009 GetAlphaPixelComponent(kernel_pixels));
cristy3ed852e2009-09-05 21:47:34 +00001010 pixel.red+=(*k)*alpha*kernel_pixels->red;
1011 pixel.green+=(*k)*alpha*kernel_pixels->green;
1012 pixel.blue+=(*k)*alpha*kernel_pixels->blue;
1013 gamma+=(*k)*alpha;
1014 k++;
1015 kernel_pixels++;
1016 }
1017 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1018 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001019 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001020 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001021 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001022 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001023 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001024 if ((channel & OpacityChannel) != 0)
1025 {
1026 k=kernel;
1027 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001028 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001029 {
1030 pixel.opacity+=(*k)*kernel_pixels->opacity;
1031 k++;
1032 kernel_pixels++;
1033 }
cristyce70c172010-01-07 17:15:30 +00001034 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001035 }
1036 if (((channel & IndexChannel) != 0) &&
1037 (image->colorspace == CMYKColorspace))
1038 {
1039 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001040 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001041
1042 k=kernel;
1043 kernel_pixels=p;
1044 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00001045 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001046 {
cristy46f08202010-01-10 04:04:21 +00001047 alpha=(MagickRealType) (QuantumScale*
1048 GetAlphaPixelComponent(kernel_pixels));
cristy3ed852e2009-09-05 21:47:34 +00001049 pixel.index+=(*k)*alpha*(*kernel_indexes);
1050 k++;
1051 kernel_pixels++;
1052 kernel_indexes++;
1053 }
cristy46f08202010-01-10 04:04:21 +00001054 blur_indexes[x]=ClampToQuantum(gamma*
1055 GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001056 }
1057 }
1058 p++;
1059 q++;
1060 }
1061 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1062 status=MagickFalse;
1063 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1064 {
1065 MagickBooleanType
1066 proceed;
1067
cristyb5d5f722009-11-04 03:03:49 +00001068#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001069 #pragma omp critical (MagickCore_BlurImageChannel)
1070#endif
1071 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1072 blur_image->columns);
1073 if (proceed == MagickFalse)
1074 status=MagickFalse;
1075 }
1076 }
1077 blur_view=DestroyCacheView(blur_view);
1078 image_view=DestroyCacheView(image_view);
1079 /*
1080 Blur columns.
1081 */
1082 image_view=AcquireCacheView(blur_image);
1083 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001084#if defined(MAGICKCORE_OPENMP_SUPPORT)
1085 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001086#endif
cristybb503372010-05-27 20:51:26 +00001087 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001088 {
1089 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001090 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001091
1092 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001093 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001094
1095 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001096 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001097
cristy3ed852e2009-09-05 21:47:34 +00001098 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001099 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001100
cristy117ff172010-08-15 21:35:32 +00001101 register ssize_t
1102 y;
1103
cristy3ed852e2009-09-05 21:47:34 +00001104 if (status == MagickFalse)
1105 continue;
cristy117ff172010-08-15 21:35:32 +00001106 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1107 image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001108 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1109 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1110 {
1111 status=MagickFalse;
1112 continue;
1113 }
1114 indexes=GetCacheViewVirtualIndexQueue(image_view);
1115 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +00001116 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001117 {
1118 MagickPixelPacket
1119 pixel;
1120
1121 register const double
cristyc47d1f82009-11-26 01:44:43 +00001122 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00001123
1124 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001125 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001126
cristybb503372010-05-27 20:51:26 +00001127 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001128 i;
1129
cristyddd82202009-11-03 20:14:50 +00001130 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00001131 k=kernel;
1132 kernel_pixels=p;
1133 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1134 {
cristybb503372010-05-27 20:51:26 +00001135 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001136 {
1137 pixel.red+=(*k)*kernel_pixels->red;
1138 pixel.green+=(*k)*kernel_pixels->green;
1139 pixel.blue+=(*k)*kernel_pixels->blue;
1140 k++;
1141 kernel_pixels++;
1142 }
1143 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001144 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001145 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001146 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001147 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001148 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001149 if ((channel & OpacityChannel) != 0)
1150 {
1151 k=kernel;
1152 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001153 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001154 {
1155 pixel.opacity+=(*k)*kernel_pixels->opacity;
1156 k++;
1157 kernel_pixels++;
1158 }
cristyce70c172010-01-07 17:15:30 +00001159 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001160 }
1161 if (((channel & IndexChannel) != 0) &&
1162 (image->colorspace == CMYKColorspace))
1163 {
1164 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001165 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001166
1167 k=kernel;
1168 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00001169 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001170 {
1171 pixel.index+=(*k)*(*kernel_indexes);
1172 k++;
1173 kernel_indexes++;
1174 }
cristyce70c172010-01-07 17:15:30 +00001175 blur_indexes[y]=ClampToQuantum(pixel.index);
cristy3ed852e2009-09-05 21:47:34 +00001176 }
1177 }
1178 else
1179 {
1180 MagickRealType
1181 alpha,
1182 gamma;
1183
1184 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001185 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001186 {
cristy46f08202010-01-10 04:04:21 +00001187 alpha=(MagickRealType) (QuantumScale*
1188 GetAlphaPixelComponent(kernel_pixels));
cristy3ed852e2009-09-05 21:47:34 +00001189 pixel.red+=(*k)*alpha*kernel_pixels->red;
1190 pixel.green+=(*k)*alpha*kernel_pixels->green;
1191 pixel.blue+=(*k)*alpha*kernel_pixels->blue;
1192 gamma+=(*k)*alpha;
1193 k++;
1194 kernel_pixels++;
1195 }
1196 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1197 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001198 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001199 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001200 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001201 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001202 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001203 if ((channel & OpacityChannel) != 0)
1204 {
1205 k=kernel;
1206 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001207 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001208 {
1209 pixel.opacity+=(*k)*kernel_pixels->opacity;
1210 k++;
1211 kernel_pixels++;
1212 }
cristyce70c172010-01-07 17:15:30 +00001213 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001214 }
1215 if (((channel & IndexChannel) != 0) &&
1216 (image->colorspace == CMYKColorspace))
1217 {
1218 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001219 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001220
1221 k=kernel;
1222 kernel_pixels=p;
1223 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00001224 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001225 {
cristy46f08202010-01-10 04:04:21 +00001226 alpha=(MagickRealType) (QuantumScale*
1227 GetAlphaPixelComponent(kernel_pixels));
cristy3ed852e2009-09-05 21:47:34 +00001228 pixel.index+=(*k)*alpha*(*kernel_indexes);
1229 k++;
1230 kernel_pixels++;
1231 kernel_indexes++;
1232 }
cristy46f08202010-01-10 04:04:21 +00001233 blur_indexes[y]=ClampToQuantum(gamma*
1234 GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001235 }
1236 }
1237 p++;
1238 q++;
1239 }
1240 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1241 status=MagickFalse;
1242 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1243 {
1244 MagickBooleanType
1245 proceed;
1246
cristyb5d5f722009-11-04 03:03:49 +00001247#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001248 #pragma omp critical (MagickCore_BlurImageChannel)
1249#endif
1250 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1251 blur_image->columns);
1252 if (proceed == MagickFalse)
1253 status=MagickFalse;
1254 }
1255 }
1256 blur_view=DestroyCacheView(blur_view);
1257 image_view=DestroyCacheView(image_view);
1258 kernel=(double *) RelinquishMagickMemory(kernel);
1259 if (status == MagickFalse)
1260 blur_image=DestroyImage(blur_image);
1261 blur_image->type=image->type;
1262 return(blur_image);
1263}
1264
1265/*
1266%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1267% %
1268% %
1269% %
cristyfccdab92009-11-30 16:43:57 +00001270% C o n v o l v e I m a g e %
1271% %
1272% %
1273% %
1274%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1275%
1276% ConvolveImage() applies a custom convolution kernel to the image.
1277%
1278% The format of the ConvolveImage method is:
1279%
cristybb503372010-05-27 20:51:26 +00001280% Image *ConvolveImage(const Image *image,const size_t order,
cristyfccdab92009-11-30 16:43:57 +00001281% const double *kernel,ExceptionInfo *exception)
1282% Image *ConvolveImageChannel(const Image *image,const ChannelType channel,
cristy117ff172010-08-15 21:35:32 +00001283% const size_t order,const double *kernel,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001284%
1285% A description of each parameter follows:
1286%
1287% o image: the image.
1288%
1289% o channel: the channel type.
1290%
1291% o order: the number of columns and rows in the filter kernel.
1292%
1293% o kernel: An array of double representing the convolution kernel.
1294%
1295% o exception: return any errors or warnings in this structure.
1296%
1297*/
1298
cristybb503372010-05-27 20:51:26 +00001299MagickExport Image *ConvolveImage(const Image *image,const size_t order,
cristyfccdab92009-11-30 16:43:57 +00001300 const double *kernel,ExceptionInfo *exception)
1301{
1302 Image
1303 *convolve_image;
1304
1305 convolve_image=ConvolveImageChannel(image,DefaultChannels,order,kernel,
1306 exception);
1307 return(convolve_image);
1308}
1309
1310MagickExport Image *ConvolveImageChannel(const Image *image,
cristybb503372010-05-27 20:51:26 +00001311 const ChannelType channel,const size_t order,const double *kernel,
cristyfccdab92009-11-30 16:43:57 +00001312 ExceptionInfo *exception)
1313{
1314#define ConvolveImageTag "Convolve/Image"
1315
cristyc4c8d132010-01-07 01:58:38 +00001316 CacheView
1317 *convolve_view,
1318 *image_view;
1319
cristyfccdab92009-11-30 16:43:57 +00001320 double
1321 *normal_kernel;
1322
1323 Image
1324 *convolve_image;
1325
cristyfccdab92009-11-30 16:43:57 +00001326 MagickBooleanType
1327 status;
1328
cristybb503372010-05-27 20:51:26 +00001329 MagickOffsetType
1330 progress;
1331
cristyfccdab92009-11-30 16:43:57 +00001332 MagickPixelPacket
1333 bias;
1334
1335 MagickRealType
1336 gamma;
1337
cristybb503372010-05-27 20:51:26 +00001338 register ssize_t
cristyfccdab92009-11-30 16:43:57 +00001339 i;
1340
cristybb503372010-05-27 20:51:26 +00001341 size_t
cristyfccdab92009-11-30 16:43:57 +00001342 width;
1343
cristybb503372010-05-27 20:51:26 +00001344 ssize_t
1345 y;
1346
cristyfccdab92009-11-30 16:43:57 +00001347 /*
1348 Initialize convolve image attributes.
1349 */
1350 assert(image != (Image *) NULL);
1351 assert(image->signature == MagickSignature);
1352 if (image->debug != MagickFalse)
1353 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1354 assert(exception != (ExceptionInfo *) NULL);
1355 assert(exception->signature == MagickSignature);
1356 width=order;
1357 if ((width % 2) == 0)
1358 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
1359 convolve_image=CloneImage(image,0,0,MagickTrue,exception);
1360 if (convolve_image == (Image *) NULL)
1361 return((Image *) NULL);
1362 if (SetImageStorageClass(convolve_image,DirectClass) == MagickFalse)
1363 {
1364 InheritException(exception,&convolve_image->exception);
1365 convolve_image=DestroyImage(convolve_image);
1366 return((Image *) NULL);
1367 }
1368 if (image->debug != MagickFalse)
1369 {
1370 char
1371 format[MaxTextExtent],
1372 *message;
1373
cristy117ff172010-08-15 21:35:32 +00001374 register const double
1375 *k;
1376
cristybb503372010-05-27 20:51:26 +00001377 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001378 u,
1379 v;
1380
cristyfccdab92009-11-30 16:43:57 +00001381 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001382 " ConvolveImage with %.20gx%.20g kernel:",(double) width,(double)
1383 width);
cristyfccdab92009-11-30 16:43:57 +00001384 message=AcquireString("");
1385 k=kernel;
cristybb503372010-05-27 20:51:26 +00001386 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001387 {
1388 *message='\0';
cristye8c25f92010-06-03 00:53:06 +00001389 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001390 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00001391 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001392 {
cristye7f51092010-01-17 00:39:37 +00001393 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001394 (void) ConcatenateString(&message,format);
1395 }
1396 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1397 }
1398 message=DestroyString(message);
1399 }
1400 /*
1401 Normalize kernel.
1402 */
1403 normal_kernel=(double *) AcquireQuantumMemory(width*width,
1404 sizeof(*normal_kernel));
1405 if (normal_kernel == (double *) NULL)
1406 {
1407 convolve_image=DestroyImage(convolve_image);
1408 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1409 }
1410 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001411 for (i=0; i < (ssize_t) (width*width); i++)
cristyfccdab92009-11-30 16:43:57 +00001412 gamma+=kernel[i];
1413 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristybb503372010-05-27 20:51:26 +00001414 for (i=0; i < (ssize_t) (width*width); i++)
cristyfccdab92009-11-30 16:43:57 +00001415 normal_kernel[i]=gamma*kernel[i];
1416 /*
1417 Convolve image.
1418 */
1419 status=MagickTrue;
1420 progress=0;
1421 GetMagickPixelPacket(image,&bias);
1422 SetMagickPixelPacketBias(image,&bias);
1423 image_view=AcquireCacheView(image);
1424 convolve_view=AcquireCacheView(convolve_image);
1425#if defined(MAGICKCORE_OPENMP_SUPPORT)
1426 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1427#endif
cristybb503372010-05-27 20:51:26 +00001428 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001429 {
1430 MagickBooleanType
1431 sync;
1432
1433 register const IndexPacket
1434 *restrict indexes;
1435
1436 register const PixelPacket
1437 *restrict p;
1438
1439 register IndexPacket
1440 *restrict convolve_indexes;
1441
cristyfccdab92009-11-30 16:43:57 +00001442 register PixelPacket
1443 *restrict q;
1444
cristy117ff172010-08-15 21:35:32 +00001445 register ssize_t
1446 x;
1447
cristyfccdab92009-11-30 16:43:57 +00001448 if (status == MagickFalse)
1449 continue;
cristyce889302010-06-30 19:16:36 +00001450 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
1451 (width/2L),image->columns+width,width,exception);
cristyfccdab92009-11-30 16:43:57 +00001452 q=GetCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
1453 exception);
1454 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1455 {
1456 status=MagickFalse;
1457 continue;
1458 }
1459 indexes=GetCacheViewVirtualIndexQueue(image_view);
1460 convolve_indexes=GetCacheViewAuthenticIndexQueue(convolve_view);
cristybb503372010-05-27 20:51:26 +00001461 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001462 {
cristyfccdab92009-11-30 16:43:57 +00001463 MagickPixelPacket
1464 pixel;
1465
1466 register const double
1467 *restrict k;
1468
1469 register const PixelPacket
1470 *restrict kernel_pixels;
1471
cristybb503372010-05-27 20:51:26 +00001472 register ssize_t
cristyfccdab92009-11-30 16:43:57 +00001473 u;
1474
cristy117ff172010-08-15 21:35:32 +00001475 ssize_t
1476 v;
1477
cristyfccdab92009-11-30 16:43:57 +00001478 pixel=bias;
1479 k=normal_kernel;
1480 kernel_pixels=p;
1481 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1482 {
cristybb503372010-05-27 20:51:26 +00001483 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001484 {
cristybb503372010-05-27 20:51:26 +00001485 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001486 {
1487 pixel.red+=(*k)*kernel_pixels[u].red;
1488 pixel.green+=(*k)*kernel_pixels[u].green;
1489 pixel.blue+=(*k)*kernel_pixels[u].blue;
1490 k++;
1491 }
1492 kernel_pixels+=image->columns+width;
1493 }
1494 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001495 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001496 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001497 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001498 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001499 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001500 if ((channel & OpacityChannel) != 0)
1501 {
1502 k=normal_kernel;
1503 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001504 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001505 {
cristybb503372010-05-27 20:51:26 +00001506 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001507 {
1508 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1509 k++;
1510 }
1511 kernel_pixels+=image->columns+width;
1512 }
cristyce70c172010-01-07 17:15:30 +00001513 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001514 }
1515 if (((channel & IndexChannel) != 0) &&
1516 (image->colorspace == CMYKColorspace))
1517 {
1518 register const IndexPacket
1519 *restrict kernel_indexes;
1520
1521 k=normal_kernel;
1522 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00001523 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001524 {
cristybb503372010-05-27 20:51:26 +00001525 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001526 {
1527 pixel.index+=(*k)*kernel_indexes[u];
1528 k++;
1529 }
1530 kernel_indexes+=image->columns+width;
1531 }
cristyce70c172010-01-07 17:15:30 +00001532 convolve_indexes[x]=ClampToQuantum(pixel.index);
cristyfccdab92009-11-30 16:43:57 +00001533 }
1534 }
1535 else
1536 {
1537 MagickRealType
1538 alpha,
1539 gamma;
1540
1541 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001542 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001543 {
cristybb503372010-05-27 20:51:26 +00001544 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001545 {
1546 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1547 kernel_pixels[u].opacity));
1548 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
1549 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
1550 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
cristyfccdab92009-11-30 16:43:57 +00001551 gamma+=(*k)*alpha;
1552 k++;
1553 }
1554 kernel_pixels+=image->columns+width;
1555 }
1556 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1557 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001558 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001559 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001560 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001561 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001562 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001563 if ((channel & OpacityChannel) != 0)
1564 {
1565 k=normal_kernel;
1566 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001567 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001568 {
cristybb503372010-05-27 20:51:26 +00001569 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001570 {
1571 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1572 k++;
1573 }
1574 kernel_pixels+=image->columns+width;
1575 }
cristyce70c172010-01-07 17:15:30 +00001576 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001577 }
1578 if (((channel & IndexChannel) != 0) &&
1579 (image->colorspace == CMYKColorspace))
1580 {
1581 register const IndexPacket
1582 *restrict kernel_indexes;
1583
1584 k=normal_kernel;
1585 kernel_pixels=p;
1586 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00001587 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001588 {
cristybb503372010-05-27 20:51:26 +00001589 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001590 {
1591 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1592 kernel_pixels[u].opacity));
1593 pixel.index+=(*k)*alpha*kernel_indexes[u];
1594 k++;
1595 }
1596 kernel_pixels+=image->columns+width;
1597 kernel_indexes+=image->columns+width;
1598 }
cristy24b06da2010-01-09 23:05:56 +00001599 convolve_indexes[x]=ClampToQuantum(gamma*
1600 GetIndexPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001601 }
1602 }
1603 p++;
1604 q++;
1605 }
1606 sync=SyncCacheViewAuthenticPixels(convolve_view,exception);
1607 if (sync == MagickFalse)
1608 status=MagickFalse;
1609 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1610 {
1611 MagickBooleanType
1612 proceed;
1613
1614#if defined(MAGICKCORE_OPENMP_SUPPORT)
1615 #pragma omp critical (MagickCore_ConvolveImageChannel)
1616#endif
1617 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1618 if (proceed == MagickFalse)
1619 status=MagickFalse;
1620 }
1621 }
1622 convolve_image->type=image->type;
1623 convolve_view=DestroyCacheView(convolve_view);
1624 image_view=DestroyCacheView(image_view);
1625 normal_kernel=(double *) RelinquishMagickMemory(normal_kernel);
1626 if (status == MagickFalse)
1627 convolve_image=DestroyImage(convolve_image);
1628 return(convolve_image);
1629}
1630
1631/*
1632%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1633% %
1634% %
1635% %
cristy3ed852e2009-09-05 21:47:34 +00001636% D e s p e c k l e I m a g e %
1637% %
1638% %
1639% %
1640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1641%
1642% DespeckleImage() reduces the speckle noise in an image while perserving the
1643% edges of the original image.
1644%
1645% The format of the DespeckleImage method is:
1646%
1647% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1648%
1649% A description of each parameter follows:
1650%
1651% o image: the image.
1652%
1653% o exception: return any errors or warnings in this structure.
1654%
1655*/
1656
cristybb503372010-05-27 20:51:26 +00001657static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1658 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
cristy3ed852e2009-09-05 21:47:34 +00001659 const int polarity)
1660{
cristy3ed852e2009-09-05 21:47:34 +00001661 MagickRealType
1662 v;
1663
cristy3ed852e2009-09-05 21:47:34 +00001664 register Quantum
1665 *p,
1666 *q,
1667 *r,
1668 *s;
1669
cristy117ff172010-08-15 21:35:32 +00001670 register ssize_t
1671 x;
1672
1673 ssize_t
1674 y;
1675
cristy3ed852e2009-09-05 21:47:34 +00001676 assert(f != (Quantum *) NULL);
1677 assert(g != (Quantum *) NULL);
1678 p=f+(columns+2);
1679 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001680 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1681 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001682 {
1683 p++;
1684 q++;
1685 r++;
1686 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001687 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001688 {
1689 v=(MagickRealType) (*p);
1690 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1691 v+=ScaleCharToQuantum(1);
1692 *q=(Quantum) v;
1693 p++;
1694 q++;
1695 r++;
1696 }
1697 else
cristybb503372010-05-27 20:51:26 +00001698 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001699 {
1700 v=(MagickRealType) (*p);
1701 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
cristybb503372010-05-27 20:51:26 +00001702 v-=(ssize_t) ScaleCharToQuantum(1);
cristy3ed852e2009-09-05 21:47:34 +00001703 *q=(Quantum) v;
1704 p++;
1705 q++;
1706 r++;
1707 }
1708 p++;
1709 q++;
1710 r++;
1711 }
1712 p=f+(columns+2);
1713 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001714 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1715 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1716 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001717 {
1718 p++;
1719 q++;
1720 r++;
1721 s++;
1722 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001723 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001724 {
1725 v=(MagickRealType) (*q);
1726 if (((MagickRealType) *s >=
1727 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1728 ((MagickRealType) *r > v))
1729 v+=ScaleCharToQuantum(1);
1730 *p=(Quantum) v;
1731 p++;
1732 q++;
1733 r++;
1734 s++;
1735 }
1736 else
cristybb503372010-05-27 20:51:26 +00001737 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001738 {
1739 v=(MagickRealType) (*q);
1740 if (((MagickRealType) *s <=
1741 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1742 ((MagickRealType) *r < v))
1743 v-=(MagickRealType) ScaleCharToQuantum(1);
1744 *p=(Quantum) v;
1745 p++;
1746 q++;
1747 r++;
1748 s++;
1749 }
1750 p++;
1751 q++;
1752 r++;
1753 s++;
1754 }
1755}
1756
1757MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1758{
1759#define DespeckleImageTag "Despeckle/Image"
1760
cristy2407fc22009-09-11 00:55:25 +00001761 CacheView
1762 *despeckle_view,
1763 *image_view;
1764
cristy3ed852e2009-09-05 21:47:34 +00001765 Image
1766 *despeckle_image;
1767
cristy3ed852e2009-09-05 21:47:34 +00001768 MagickBooleanType
1769 status;
1770
cristya58c3172011-02-19 19:23:11 +00001771 register ssize_t
1772 i;
1773
cristy3ed852e2009-09-05 21:47:34 +00001774 Quantum
cristy65b9f392011-02-22 14:22:54 +00001775 *restrict buffers,
1776 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +00001777
1778 size_t
cristya58c3172011-02-19 19:23:11 +00001779 length,
1780 number_channels;
cristy117ff172010-08-15 21:35:32 +00001781
cristybb503372010-05-27 20:51:26 +00001782 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001783 X[4] = {0, 1, 1,-1},
1784 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001785
cristy3ed852e2009-09-05 21:47:34 +00001786 /*
1787 Allocate despeckled image.
1788 */
1789 assert(image != (const Image *) NULL);
1790 assert(image->signature == MagickSignature);
1791 if (image->debug != MagickFalse)
1792 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1793 assert(exception != (ExceptionInfo *) NULL);
1794 assert(exception->signature == MagickSignature);
1795 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1796 exception);
1797 if (despeckle_image == (Image *) NULL)
1798 return((Image *) NULL);
1799 if (SetImageStorageClass(despeckle_image,DirectClass) == MagickFalse)
1800 {
1801 InheritException(exception,&despeckle_image->exception);
1802 despeckle_image=DestroyImage(despeckle_image);
1803 return((Image *) NULL);
1804 }
1805 /*
1806 Allocate image buffers.
1807 */
1808 length=(size_t) ((image->columns+2)*(image->rows+2));
cristy65b9f392011-02-22 14:22:54 +00001809 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1810 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1811 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001812 {
cristy65b9f392011-02-22 14:22:54 +00001813 if (buffers != (Quantum *) NULL)
1814 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1815 if (pixels != (Quantum *) NULL)
1816 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001817 despeckle_image=DestroyImage(despeckle_image);
1818 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1819 }
1820 /*
1821 Reduce speckle in the image.
1822 */
1823 status=MagickTrue;
cristy109695a2011-02-19 19:38:14 +00001824 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
cristy3ed852e2009-09-05 21:47:34 +00001825 image_view=AcquireCacheView(image);
1826 despeckle_view=AcquireCacheView(despeckle_image);
cristy8df3d002011-02-19 19:40:59 +00001827 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001828 {
cristy3ed852e2009-09-05 21:47:34 +00001829 register Quantum
1830 *buffer,
1831 *pixel;
1832
cristyc1488b52011-02-19 18:54:15 +00001833 register ssize_t
cristya58c3172011-02-19 19:23:11 +00001834 k,
cristyc1488b52011-02-19 18:54:15 +00001835 x;
1836
cristy117ff172010-08-15 21:35:32 +00001837 ssize_t
1838 j,
1839 y;
1840
cristy3ed852e2009-09-05 21:47:34 +00001841 if (status == MagickFalse)
1842 continue;
cristy65b9f392011-02-22 14:22:54 +00001843 pixel=pixels;
cristy3ed852e2009-09-05 21:47:34 +00001844 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
cristy65b9f392011-02-22 14:22:54 +00001845 buffer=buffers;
cristybb503372010-05-27 20:51:26 +00001846 j=(ssize_t) image->columns+2;
1847 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001848 {
cristya58c3172011-02-19 19:23:11 +00001849 register const IndexPacket
1850 *restrict indexes;
1851
cristy3ed852e2009-09-05 21:47:34 +00001852 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001853 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001854
1855 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1856 if (p == (const PixelPacket *) NULL)
1857 break;
cristya58c3172011-02-19 19:23:11 +00001858 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001859 j++;
cristybb503372010-05-27 20:51:26 +00001860 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001861 {
cristya58c3172011-02-19 19:23:11 +00001862 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001863 {
cristyce70c172010-01-07 17:15:30 +00001864 case 0: pixel[j]=GetRedPixelComponent(p); break;
1865 case 1: pixel[j]=GetGreenPixelComponent(p); break;
1866 case 2: pixel[j]=GetBluePixelComponent(p); break;
1867 case 3: pixel[j]=GetOpacityPixelComponent(p); break;
cristya58c3172011-02-19 19:23:11 +00001868 case 4: pixel[j]=GetBlackPixelComponent(indexes,x); break;
cristy3ed852e2009-09-05 21:47:34 +00001869 default: break;
1870 }
1871 p++;
1872 j++;
1873 }
1874 j++;
1875 }
cristy3ed852e2009-09-05 21:47:34 +00001876 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
cristya58c3172011-02-19 19:23:11 +00001877 for (k=0; k < 4; k++)
cristy3ed852e2009-09-05 21:47:34 +00001878 {
cristya58c3172011-02-19 19:23:11 +00001879 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1880 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1881 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1882 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
cristy3ed852e2009-09-05 21:47:34 +00001883 }
cristybb503372010-05-27 20:51:26 +00001884 j=(ssize_t) image->columns+2;
1885 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001886 {
1887 MagickBooleanType
1888 sync;
1889
cristya58c3172011-02-19 19:23:11 +00001890 register IndexPacket
1891 *restrict indexes;
1892
cristy3ed852e2009-09-05 21:47:34 +00001893 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001894 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001895
1896 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1897 1,exception);
1898 if (q == (PixelPacket *) NULL)
1899 break;
cristya58c3172011-02-19 19:23:11 +00001900 indexes=GetCacheViewAuthenticIndexQueue(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001901 j++;
cristybb503372010-05-27 20:51:26 +00001902 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001903 {
cristya58c3172011-02-19 19:23:11 +00001904 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001905 {
1906 case 0: q->red=pixel[j]; break;
1907 case 1: q->green=pixel[j]; break;
1908 case 2: q->blue=pixel[j]; break;
1909 case 3: q->opacity=pixel[j]; break;
cristya58c3172011-02-19 19:23:11 +00001910 case 4: indexes[x]=pixel[j]; break;
cristy3ed852e2009-09-05 21:47:34 +00001911 default: break;
1912 }
1913 q++;
1914 j++;
1915 }
1916 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1917 if (sync == MagickFalse)
1918 {
1919 status=MagickFalse;
1920 break;
1921 }
1922 j++;
1923 }
1924 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1925 {
1926 MagickBooleanType
1927 proceed;
1928
cristya58c3172011-02-19 19:23:11 +00001929 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1930 number_channels);
cristy3ed852e2009-09-05 21:47:34 +00001931 if (proceed == MagickFalse)
1932 status=MagickFalse;
1933 }
1934 }
1935 despeckle_view=DestroyCacheView(despeckle_view);
1936 image_view=DestroyCacheView(image_view);
cristy65b9f392011-02-22 14:22:54 +00001937 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1938 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001939 despeckle_image->type=image->type;
1940 if (status == MagickFalse)
1941 despeckle_image=DestroyImage(despeckle_image);
1942 return(despeckle_image);
1943}
1944
1945/*
1946%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1947% %
1948% %
1949% %
1950% E d g e I m a g e %
1951% %
1952% %
1953% %
1954%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1955%
1956% EdgeImage() finds edges in an image. Radius defines the radius of the
1957% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1958% radius for you.
1959%
1960% The format of the EdgeImage method is:
1961%
1962% Image *EdgeImage(const Image *image,const double radius,
1963% ExceptionInfo *exception)
1964%
1965% A description of each parameter follows:
1966%
1967% o image: the image.
1968%
1969% o radius: the radius of the pixel neighborhood.
1970%
1971% o exception: return any errors or warnings in this structure.
1972%
1973*/
1974MagickExport Image *EdgeImage(const Image *image,const double radius,
1975 ExceptionInfo *exception)
1976{
1977 Image
1978 *edge_image;
1979
1980 double
1981 *kernel;
1982
cristybb503372010-05-27 20:51:26 +00001983 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001984 i;
1985
cristybb503372010-05-27 20:51:26 +00001986 size_t
cristy3ed852e2009-09-05 21:47:34 +00001987 width;
1988
1989 assert(image != (const Image *) NULL);
1990 assert(image->signature == MagickSignature);
1991 if (image->debug != MagickFalse)
1992 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1993 assert(exception != (ExceptionInfo *) NULL);
1994 assert(exception->signature == MagickSignature);
1995 width=GetOptimalKernelWidth1D(radius,0.5);
1996 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
1997 if (kernel == (double *) NULL)
1998 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00001999 for (i=0; i < (ssize_t) (width*width); i++)
cristy3ed852e2009-09-05 21:47:34 +00002000 kernel[i]=(-1.0);
2001 kernel[i/2]=(double) (width*width-1.0);
2002 edge_image=ConvolveImage(image,width,kernel,exception);
2003 kernel=(double *) RelinquishMagickMemory(kernel);
2004 return(edge_image);
2005}
2006
2007/*
2008%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2009% %
2010% %
2011% %
2012% E m b o s s I m a g e %
2013% %
2014% %
2015% %
2016%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017%
2018% EmbossImage() returns a grayscale image with a three-dimensional effect.
2019% We convolve the image with a Gaussian operator of the given radius and
2020% standard deviation (sigma). For reasonable results, radius should be
2021% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
2022% radius for you.
2023%
2024% The format of the EmbossImage method is:
2025%
2026% Image *EmbossImage(const Image *image,const double radius,
2027% const double sigma,ExceptionInfo *exception)
2028%
2029% A description of each parameter follows:
2030%
2031% o image: the image.
2032%
2033% o radius: the radius of the pixel neighborhood.
2034%
2035% o sigma: the standard deviation of the Gaussian, in pixels.
2036%
2037% o exception: return any errors or warnings in this structure.
2038%
2039*/
2040MagickExport Image *EmbossImage(const Image *image,const double radius,
2041 const double sigma,ExceptionInfo *exception)
2042{
2043 double
2044 *kernel;
2045
2046 Image
2047 *emboss_image;
2048
cristybb503372010-05-27 20:51:26 +00002049 register ssize_t
cristy47e00502009-12-17 19:19:57 +00002050 i;
2051
cristybb503372010-05-27 20:51:26 +00002052 size_t
cristy3ed852e2009-09-05 21:47:34 +00002053 width;
2054
cristy117ff172010-08-15 21:35:32 +00002055 ssize_t
2056 j,
2057 k,
2058 u,
2059 v;
2060
cristy3ed852e2009-09-05 21:47:34 +00002061 assert(image != (Image *) NULL);
2062 assert(image->signature == MagickSignature);
2063 if (image->debug != MagickFalse)
2064 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2065 assert(exception != (ExceptionInfo *) NULL);
2066 assert(exception->signature == MagickSignature);
2067 width=GetOptimalKernelWidth2D(radius,sigma);
2068 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2069 if (kernel == (double *) NULL)
2070 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00002071 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +00002072 k=j;
2073 i=0;
2074 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00002075 {
cristy47e00502009-12-17 19:19:57 +00002076 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00002077 {
cristy4205a3c2010-09-12 20:19:59 +00002078 kernel[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00002079 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00002080 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00002081 if (u != k)
cristy3ed852e2009-09-05 21:47:34 +00002082 kernel[i]=0.0;
2083 i++;
2084 }
cristy47e00502009-12-17 19:19:57 +00002085 k--;
cristy3ed852e2009-09-05 21:47:34 +00002086 }
2087 emboss_image=ConvolveImage(image,width,kernel,exception);
2088 if (emboss_image != (Image *) NULL)
2089 (void) EqualizeImage(emboss_image);
2090 kernel=(double *) RelinquishMagickMemory(kernel);
2091 return(emboss_image);
2092}
2093
2094/*
2095%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2096% %
2097% %
2098% %
cristy56a9e512010-01-06 18:18:55 +00002099% F i l t e r I m a g e %
2100% %
2101% %
2102% %
2103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2104%
2105% FilterImage() applies a custom convolution kernel to the image.
2106%
2107% The format of the FilterImage method is:
2108%
cristy2be15382010-01-21 02:38:03 +00002109% Image *FilterImage(const Image *image,const KernelInfo *kernel,
cristy56a9e512010-01-06 18:18:55 +00002110% ExceptionInfo *exception)
2111% Image *FilterImageChannel(const Image *image,const ChannelType channel,
cristy2be15382010-01-21 02:38:03 +00002112% const KernelInfo *kernel,ExceptionInfo *exception)
cristy56a9e512010-01-06 18:18:55 +00002113%
2114% A description of each parameter follows:
2115%
2116% o image: the image.
2117%
2118% o channel: the channel type.
2119%
2120% o kernel: the filtering kernel.
2121%
2122% o exception: return any errors or warnings in this structure.
2123%
2124*/
2125
cristy2be15382010-01-21 02:38:03 +00002126MagickExport Image *FilterImage(const Image *image,const KernelInfo *kernel,
cristy56a9e512010-01-06 18:18:55 +00002127 ExceptionInfo *exception)
2128{
2129 Image
2130 *filter_image;
2131
2132 filter_image=FilterImageChannel(image,DefaultChannels,kernel,exception);
2133 return(filter_image);
2134}
2135
2136MagickExport Image *FilterImageChannel(const Image *image,
cristy2be15382010-01-21 02:38:03 +00002137 const ChannelType channel,const KernelInfo *kernel,ExceptionInfo *exception)
cristy56a9e512010-01-06 18:18:55 +00002138{
2139#define FilterImageTag "Filter/Image"
2140
2141 CacheView
2142 *filter_view,
2143 *image_view;
2144
cristy56a9e512010-01-06 18:18:55 +00002145 Image
2146 *filter_image;
2147
cristy56a9e512010-01-06 18:18:55 +00002148 MagickBooleanType
2149 status;
2150
cristybb503372010-05-27 20:51:26 +00002151 MagickOffsetType
2152 progress;
2153
cristy56a9e512010-01-06 18:18:55 +00002154 MagickPixelPacket
2155 bias;
2156
cristybb503372010-05-27 20:51:26 +00002157 ssize_t
2158 y;
2159
cristy56a9e512010-01-06 18:18:55 +00002160 /*
2161 Initialize filter image attributes.
2162 */
2163 assert(image != (Image *) NULL);
2164 assert(image->signature == MagickSignature);
2165 if (image->debug != MagickFalse)
2166 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2167 assert(exception != (ExceptionInfo *) NULL);
2168 assert(exception->signature == MagickSignature);
2169 if ((kernel->width % 2) == 0)
2170 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
2171 filter_image=CloneImage(image,0,0,MagickTrue,exception);
2172 if (filter_image == (Image *) NULL)
2173 return((Image *) NULL);
2174 if (SetImageStorageClass(filter_image,DirectClass) == MagickFalse)
2175 {
2176 InheritException(exception,&filter_image->exception);
2177 filter_image=DestroyImage(filter_image);
2178 return((Image *) NULL);
2179 }
2180 if (image->debug != MagickFalse)
2181 {
2182 char
2183 format[MaxTextExtent],
2184 *message;
2185
cristy117ff172010-08-15 21:35:32 +00002186 register const double
2187 *k;
2188
cristybb503372010-05-27 20:51:26 +00002189 ssize_t
cristy56a9e512010-01-06 18:18:55 +00002190 u,
2191 v;
2192
cristy56a9e512010-01-06 18:18:55 +00002193 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00002194 " FilterImage with %.20gx%.20g kernel:",(double) kernel->width,(double)
2195 kernel->height);
cristy56a9e512010-01-06 18:18:55 +00002196 message=AcquireString("");
2197 k=kernel->values;
cristybb503372010-05-27 20:51:26 +00002198 for (v=0; v < (ssize_t) kernel->height; v++)
cristy56a9e512010-01-06 18:18:55 +00002199 {
2200 *message='\0';
cristye8c25f92010-06-03 00:53:06 +00002201 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy56a9e512010-01-06 18:18:55 +00002202 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00002203 for (u=0; u < (ssize_t) kernel->width; u++)
cristy56a9e512010-01-06 18:18:55 +00002204 {
cristye7f51092010-01-17 00:39:37 +00002205 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
cristy56a9e512010-01-06 18:18:55 +00002206 (void) ConcatenateString(&message,format);
2207 }
2208 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2209 }
2210 message=DestroyString(message);
2211 }
cristy36826ab2010-03-06 01:29:30 +00002212 status=AccelerateConvolveImage(image,kernel,filter_image,exception);
cristyd43a46b2010-01-21 02:13:41 +00002213 if (status == MagickTrue)
2214 return(filter_image);
cristy56a9e512010-01-06 18:18:55 +00002215 /*
2216 Filter image.
2217 */
2218 status=MagickTrue;
2219 progress=0;
2220 GetMagickPixelPacket(image,&bias);
2221 SetMagickPixelPacketBias(image,&bias);
2222 image_view=AcquireCacheView(image);
2223 filter_view=AcquireCacheView(filter_image);
2224#if defined(MAGICKCORE_OPENMP_SUPPORT)
2225 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2226#endif
cristybb503372010-05-27 20:51:26 +00002227 for (y=0; y < (ssize_t) image->rows; y++)
cristy56a9e512010-01-06 18:18:55 +00002228 {
2229 MagickBooleanType
2230 sync;
2231
2232 register const IndexPacket
2233 *restrict indexes;
2234
2235 register const PixelPacket
2236 *restrict p;
2237
2238 register IndexPacket
2239 *restrict filter_indexes;
2240
cristy56a9e512010-01-06 18:18:55 +00002241 register PixelPacket
2242 *restrict q;
2243
cristy117ff172010-08-15 21:35:32 +00002244 register ssize_t
2245 x;
2246
cristy56a9e512010-01-06 18:18:55 +00002247 if (status == MagickFalse)
2248 continue;
cristybb503372010-05-27 20:51:26 +00002249 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel->width/2L),
cristy117ff172010-08-15 21:35:32 +00002250 y-(ssize_t) (kernel->height/2L),image->columns+kernel->width,
2251 kernel->height,exception);
cristy56a9e512010-01-06 18:18:55 +00002252 q=GetCacheViewAuthenticPixels(filter_view,0,y,filter_image->columns,1,
2253 exception);
2254 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2255 {
2256 status=MagickFalse;
2257 continue;
2258 }
2259 indexes=GetCacheViewVirtualIndexQueue(image_view);
2260 filter_indexes=GetCacheViewAuthenticIndexQueue(filter_view);
cristybb503372010-05-27 20:51:26 +00002261 for (x=0; x < (ssize_t) image->columns; x++)
cristy56a9e512010-01-06 18:18:55 +00002262 {
cristy56a9e512010-01-06 18:18:55 +00002263 MagickPixelPacket
2264 pixel;
2265
2266 register const double
2267 *restrict k;
2268
2269 register const PixelPacket
2270 *restrict kernel_pixels;
2271
cristybb503372010-05-27 20:51:26 +00002272 register ssize_t
cristy56a9e512010-01-06 18:18:55 +00002273 u;
2274
cristy117ff172010-08-15 21:35:32 +00002275 ssize_t
2276 v;
2277
cristy56a9e512010-01-06 18:18:55 +00002278 pixel=bias;
cristy36826ab2010-03-06 01:29:30 +00002279 k=kernel->values;
cristy56a9e512010-01-06 18:18:55 +00002280 kernel_pixels=p;
2281 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
2282 {
cristybb503372010-05-27 20:51:26 +00002283 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002284 {
cristybb503372010-05-27 20:51:26 +00002285 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002286 {
2287 pixel.red+=(*k)*kernel_pixels[u].red;
2288 pixel.green+=(*k)*kernel_pixels[u].green;
2289 pixel.blue+=(*k)*kernel_pixels[u].blue;
2290 k++;
2291 }
cristy36826ab2010-03-06 01:29:30 +00002292 kernel_pixels+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002293 }
2294 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002295 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002296 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002297 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002298 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002299 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002300 if ((channel & OpacityChannel) != 0)
2301 {
cristy36826ab2010-03-06 01:29:30 +00002302 k=kernel->values;
cristy56a9e512010-01-06 18:18:55 +00002303 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00002304 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002305 {
cristybb503372010-05-27 20:51:26 +00002306 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002307 {
2308 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2309 k++;
2310 }
cristy36826ab2010-03-06 01:29:30 +00002311 kernel_pixels+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002312 }
cristyce70c172010-01-07 17:15:30 +00002313 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002314 }
2315 if (((channel & IndexChannel) != 0) &&
2316 (image->colorspace == CMYKColorspace))
2317 {
2318 register const IndexPacket
2319 *restrict kernel_indexes;
2320
cristy36826ab2010-03-06 01:29:30 +00002321 k=kernel->values;
cristy56a9e512010-01-06 18:18:55 +00002322 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00002323 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002324 {
cristybb503372010-05-27 20:51:26 +00002325 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002326 {
2327 pixel.index+=(*k)*kernel_indexes[u];
2328 k++;
2329 }
cristy36826ab2010-03-06 01:29:30 +00002330 kernel_indexes+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002331 }
cristyce70c172010-01-07 17:15:30 +00002332 filter_indexes[x]=ClampToQuantum(pixel.index);
cristy56a9e512010-01-06 18:18:55 +00002333 }
2334 }
2335 else
2336 {
2337 MagickRealType
2338 alpha,
2339 gamma;
2340
2341 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00002342 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002343 {
cristybb503372010-05-27 20:51:26 +00002344 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002345 {
2346 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2347 kernel_pixels[u].opacity));
2348 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
2349 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
2350 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
2351 gamma+=(*k)*alpha;
2352 k++;
2353 }
cristy36826ab2010-03-06 01:29:30 +00002354 kernel_pixels+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002355 }
2356 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2357 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002358 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002359 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002360 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002361 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002362 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002363 if ((channel & OpacityChannel) != 0)
2364 {
cristy36826ab2010-03-06 01:29:30 +00002365 k=kernel->values;
cristy56a9e512010-01-06 18:18:55 +00002366 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00002367 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002368 {
cristybb503372010-05-27 20:51:26 +00002369 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002370 {
2371 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2372 k++;
2373 }
cristy36826ab2010-03-06 01:29:30 +00002374 kernel_pixels+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002375 }
cristyce70c172010-01-07 17:15:30 +00002376 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002377 }
2378 if (((channel & IndexChannel) != 0) &&
2379 (image->colorspace == CMYKColorspace))
2380 {
2381 register const IndexPacket
2382 *restrict kernel_indexes;
2383
cristy36826ab2010-03-06 01:29:30 +00002384 k=kernel->values;
cristy56a9e512010-01-06 18:18:55 +00002385 kernel_pixels=p;
2386 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00002387 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002388 {
cristybb503372010-05-27 20:51:26 +00002389 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002390 {
2391 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2392 kernel_pixels[u].opacity));
2393 pixel.index+=(*k)*alpha*kernel_indexes[u];
2394 k++;
2395 }
cristy36826ab2010-03-06 01:29:30 +00002396 kernel_pixels+=image->columns+kernel->width;
2397 kernel_indexes+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002398 }
cristy2115aea2010-01-09 23:16:08 +00002399 filter_indexes[x]=ClampToQuantum(gamma*
2400 GetIndexPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002401 }
2402 }
2403 p++;
2404 q++;
2405 }
2406 sync=SyncCacheViewAuthenticPixels(filter_view,exception);
2407 if (sync == MagickFalse)
2408 status=MagickFalse;
2409 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2410 {
2411 MagickBooleanType
2412 proceed;
2413
2414#if defined(MAGICKCORE_OPENMP_SUPPORT)
2415 #pragma omp critical (MagickCore_FilterImageChannel)
2416#endif
2417 proceed=SetImageProgress(image,FilterImageTag,progress++,image->rows);
2418 if (proceed == MagickFalse)
2419 status=MagickFalse;
2420 }
2421 }
2422 filter_image->type=image->type;
2423 filter_view=DestroyCacheView(filter_view);
2424 image_view=DestroyCacheView(image_view);
cristy56a9e512010-01-06 18:18:55 +00002425 if (status == MagickFalse)
2426 filter_image=DestroyImage(filter_image);
2427 return(filter_image);
2428}
2429
2430/*
2431%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2432% %
2433% %
2434% %
cristy3ed852e2009-09-05 21:47:34 +00002435% G a u s s i a n B l u r I m a g e %
2436% %
2437% %
2438% %
2439%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2440%
2441% GaussianBlurImage() blurs an image. We convolve the image with a
2442% Gaussian operator of the given radius and standard deviation (sigma).
2443% For reasonable results, the radius should be larger than sigma. Use a
2444% radius of 0 and GaussianBlurImage() selects a suitable radius for you
2445%
2446% The format of the GaussianBlurImage method is:
2447%
2448% Image *GaussianBlurImage(const Image *image,onst double radius,
2449% const double sigma,ExceptionInfo *exception)
2450% Image *GaussianBlurImageChannel(const Image *image,
2451% const ChannelType channel,const double radius,const double sigma,
2452% ExceptionInfo *exception)
2453%
2454% A description of each parameter follows:
2455%
2456% o image: the image.
2457%
2458% o channel: the channel type.
2459%
2460% o radius: the radius of the Gaussian, in pixels, not counting the center
2461% pixel.
2462%
2463% o sigma: the standard deviation of the Gaussian, in pixels.
2464%
2465% o exception: return any errors or warnings in this structure.
2466%
2467*/
2468
2469MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
2470 const double sigma,ExceptionInfo *exception)
2471{
2472 Image
2473 *blur_image;
2474
2475 blur_image=GaussianBlurImageChannel(image,DefaultChannels,radius,sigma,
2476 exception);
2477 return(blur_image);
2478}
2479
2480MagickExport Image *GaussianBlurImageChannel(const Image *image,
2481 const ChannelType channel,const double radius,const double sigma,
2482 ExceptionInfo *exception)
2483{
2484 double
2485 *kernel;
2486
2487 Image
2488 *blur_image;
2489
cristybb503372010-05-27 20:51:26 +00002490 register ssize_t
cristy47e00502009-12-17 19:19:57 +00002491 i;
2492
cristybb503372010-05-27 20:51:26 +00002493 size_t
cristy3ed852e2009-09-05 21:47:34 +00002494 width;
2495
cristy117ff172010-08-15 21:35:32 +00002496 ssize_t
2497 j,
2498 u,
2499 v;
2500
cristy3ed852e2009-09-05 21:47:34 +00002501 assert(image != (const Image *) NULL);
2502 assert(image->signature == MagickSignature);
2503 if (image->debug != MagickFalse)
2504 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2505 assert(exception != (ExceptionInfo *) NULL);
2506 assert(exception->signature == MagickSignature);
2507 width=GetOptimalKernelWidth2D(radius,sigma);
2508 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2509 if (kernel == (double *) NULL)
2510 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00002511 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00002512 i=0;
cristy47e00502009-12-17 19:19:57 +00002513 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00002514 {
cristy47e00502009-12-17 19:19:57 +00002515 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00002516 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2517 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002518 }
2519 blur_image=ConvolveImageChannel(image,channel,width,kernel,exception);
2520 kernel=(double *) RelinquishMagickMemory(kernel);
2521 return(blur_image);
2522}
2523
2524/*
2525%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2526% %
2527% %
2528% %
2529% M e d i a n F i l t e r I m a g e %
2530% %
2531% %
2532% %
2533%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2534%
2535% MedianFilterImage() applies a digital filter that improves the quality
2536% of a noisy image. Each pixel is replaced by the median in a set of
2537% neighboring pixels as defined by radius.
2538%
2539% The algorithm was contributed by Mike Edmonds and implements an insertion
2540% sort for selecting median color-channel values. For more on this algorithm
2541% see "Skip Lists: A probabilistic Alternative to Balanced Trees" by William
2542% Pugh in the June 1990 of Communications of the ACM.
2543%
2544% The format of the MedianFilterImage method is:
2545%
2546% Image *MedianFilterImage(const Image *image,const double radius,
2547% ExceptionInfo *exception)
2548%
2549% A description of each parameter follows:
2550%
2551% o image: the image.
2552%
2553% o radius: the radius of the pixel neighborhood.
2554%
2555% o exception: return any errors or warnings in this structure.
2556%
2557*/
2558
cristy69ec32d2011-02-27 23:57:09 +00002559#define ListChannels 5
cristy3ed852e2009-09-05 21:47:34 +00002560
cristy69ec32d2011-02-27 23:57:09 +00002561typedef struct _ListNode
cristy3ed852e2009-09-05 21:47:34 +00002562{
cristybb503372010-05-27 20:51:26 +00002563 size_t
cristy3ed852e2009-09-05 21:47:34 +00002564 next[9],
2565 count,
2566 signature;
cristy69ec32d2011-02-27 23:57:09 +00002567} ListNode;
cristy3ed852e2009-09-05 21:47:34 +00002568
cristy69ec32d2011-02-27 23:57:09 +00002569typedef struct _SkipList
cristy3ed852e2009-09-05 21:47:34 +00002570{
cristybb503372010-05-27 20:51:26 +00002571 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002572 level;
2573
cristy69ec32d2011-02-27 23:57:09 +00002574 ListNode
cristy3ed852e2009-09-05 21:47:34 +00002575 *nodes;
cristy69ec32d2011-02-27 23:57:09 +00002576} SkipList;
cristy3ed852e2009-09-05 21:47:34 +00002577
cristy69ec32d2011-02-27 23:57:09 +00002578typedef struct _PixelList
cristy3ed852e2009-09-05 21:47:34 +00002579{
cristybb503372010-05-27 20:51:26 +00002580 size_t
cristy3ed852e2009-09-05 21:47:34 +00002581 center,
2582 seed,
2583 signature;
2584
cristy69ec32d2011-02-27 23:57:09 +00002585 SkipList
2586 lists[ListChannels];
2587} PixelList;
cristy3ed852e2009-09-05 21:47:34 +00002588
cristy69ec32d2011-02-27 23:57:09 +00002589static PixelList *DestroyPixelList(PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002590{
cristybb503372010-05-27 20:51:26 +00002591 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002592 i;
2593
cristy69ec32d2011-02-27 23:57:09 +00002594 if (pixel_list == (PixelList *) NULL)
2595 return((PixelList *) NULL);
2596 for (i=0; i < ListChannels; i++)
2597 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
2598 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
cristy3ed852e2009-09-05 21:47:34 +00002599 pixel_list->lists[i].nodes);
cristy69ec32d2011-02-27 23:57:09 +00002600 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
cristy3ed852e2009-09-05 21:47:34 +00002601 return(pixel_list);
2602}
2603
cristy69ec32d2011-02-27 23:57:09 +00002604static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002605{
cristybb503372010-05-27 20:51:26 +00002606 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002607 i;
2608
cristy69ec32d2011-02-27 23:57:09 +00002609 assert(pixel_list != (PixelList **) NULL);
cristybb503372010-05-27 20:51:26 +00002610 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy69ec32d2011-02-27 23:57:09 +00002611 if (pixel_list[i] != (PixelList *) NULL)
2612 pixel_list[i]=DestroyPixelList(pixel_list[i]);
2613 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
cristy3ed852e2009-09-05 21:47:34 +00002614 return(pixel_list);
2615}
2616
cristy69ec32d2011-02-27 23:57:09 +00002617static PixelList *AcquirePixelList(const size_t width)
cristy3ed852e2009-09-05 21:47:34 +00002618{
cristy69ec32d2011-02-27 23:57:09 +00002619 PixelList
cristy3ed852e2009-09-05 21:47:34 +00002620 *pixel_list;
2621
cristybb503372010-05-27 20:51:26 +00002622 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002623 i;
2624
cristy69ec32d2011-02-27 23:57:09 +00002625 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
2626 if (pixel_list == (PixelList *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002627 return(pixel_list);
2628 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
2629 pixel_list->center=width*width/2;
cristy69ec32d2011-02-27 23:57:09 +00002630 for (i=0; i < ListChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00002631 {
cristy69ec32d2011-02-27 23:57:09 +00002632 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
cristy3ed852e2009-09-05 21:47:34 +00002633 sizeof(*pixel_list->lists[i].nodes));
cristy69ec32d2011-02-27 23:57:09 +00002634 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
2635 return(DestroyPixelList(pixel_list));
cristy3ed852e2009-09-05 21:47:34 +00002636 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
2637 sizeof(*pixel_list->lists[i].nodes));
2638 }
2639 pixel_list->signature=MagickSignature;
2640 return(pixel_list);
2641}
2642
cristy69ec32d2011-02-27 23:57:09 +00002643static PixelList **AcquirePixelListThreadSet(const size_t width)
cristy3ed852e2009-09-05 21:47:34 +00002644{
cristy69ec32d2011-02-27 23:57:09 +00002645 PixelList
cristy3ed852e2009-09-05 21:47:34 +00002646 **pixel_list;
2647
cristy117ff172010-08-15 21:35:32 +00002648 register ssize_t
2649 i;
2650
cristybb503372010-05-27 20:51:26 +00002651 size_t
cristy3ed852e2009-09-05 21:47:34 +00002652 number_threads;
2653
2654 number_threads=GetOpenMPMaximumThreads();
cristy69ec32d2011-02-27 23:57:09 +00002655 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
cristy3ed852e2009-09-05 21:47:34 +00002656 sizeof(*pixel_list));
cristy69ec32d2011-02-27 23:57:09 +00002657 if (pixel_list == (PixelList **) NULL)
2658 return((PixelList **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002659 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
cristybb503372010-05-27 20:51:26 +00002660 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002661 {
cristy69ec32d2011-02-27 23:57:09 +00002662 pixel_list[i]=AcquirePixelList(width);
2663 if (pixel_list[i] == (PixelList *) NULL)
2664 return(DestroyPixelListThreadSet(pixel_list));
cristy3ed852e2009-09-05 21:47:34 +00002665 }
2666 return(pixel_list);
2667}
2668
cristy69ec32d2011-02-27 23:57:09 +00002669static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
2670 const size_t color)
cristy3ed852e2009-09-05 21:47:34 +00002671{
cristy69ec32d2011-02-27 23:57:09 +00002672 register SkipList
cristy3ed852e2009-09-05 21:47:34 +00002673 *list;
2674
cristy117ff172010-08-15 21:35:32 +00002675 register ssize_t
2676 level;
2677
cristybb503372010-05-27 20:51:26 +00002678 size_t
cristy3ed852e2009-09-05 21:47:34 +00002679 search,
2680 update[9];
2681
2682 /*
2683 Initialize the node.
2684 */
2685 list=pixel_list->lists+channel;
2686 list->nodes[color].signature=pixel_list->signature;
2687 list->nodes[color].count=1;
2688 /*
cristy33c53022010-06-25 12:17:27 +00002689 Determine where it belongs in the list.
cristy3ed852e2009-09-05 21:47:34 +00002690 */
2691 search=65536UL;
2692 for (level=list->level; level >= 0; level--)
2693 {
2694 while (list->nodes[search].next[level] < color)
2695 search=list->nodes[search].next[level];
2696 update[level]=search;
2697 }
2698 /*
2699 Generate a pseudo-random level for this node.
2700 */
2701 for (level=0; ; level++)
2702 {
2703 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
2704 if ((pixel_list->seed & 0x300) != 0x300)
2705 break;
2706 }
2707 if (level > 8)
2708 level=8;
2709 if (level > (list->level+2))
2710 level=list->level+2;
2711 /*
2712 If we're raising the list's level, link back to the root node.
2713 */
2714 while (level > list->level)
2715 {
2716 list->level++;
2717 update[list->level]=65536UL;
2718 }
2719 /*
2720 Link the node into the skip-list.
2721 */
2722 do
2723 {
2724 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
2725 list->nodes[update[level]].next[level]=color;
2726 }
2727 while (level-- > 0);
2728}
2729
cristy69ec32d2011-02-27 23:57:09 +00002730static MagickPixelPacket GetPixelList(PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002731{
2732 MagickPixelPacket
2733 pixel;
2734
cristy69ec32d2011-02-27 23:57:09 +00002735 register SkipList
cristy3ed852e2009-09-05 21:47:34 +00002736 *list;
2737
cristy117ff172010-08-15 21:35:32 +00002738 register ssize_t
2739 channel;
2740
cristybb503372010-05-27 20:51:26 +00002741 size_t
cristy3ed852e2009-09-05 21:47:34 +00002742 center,
2743 color,
2744 count;
2745
2746 unsigned short
cristy69ec32d2011-02-27 23:57:09 +00002747 channels[ListChannels];
cristy3ed852e2009-09-05 21:47:34 +00002748
2749 /*
2750 Find the median value for each of the color.
2751 */
2752 center=pixel_list->center;
2753 for (channel=0; channel < 5; channel++)
2754 {
2755 list=pixel_list->lists+channel;
2756 color=65536UL;
2757 count=0;
2758 do
2759 {
2760 color=list->nodes[color].next[0];
2761 count+=list->nodes[color].count;
2762 }
2763 while (count <= center);
2764 channels[channel]=(unsigned short) color;
2765 }
2766 GetMagickPixelPacket((const Image *) NULL,&pixel);
2767 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
2768 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
2769 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
2770 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
2771 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
2772 return(pixel);
2773}
2774
cristy69ec32d2011-02-27 23:57:09 +00002775static inline void InsertPixelList(const Image *image,const PixelPacket *pixel,
2776 const IndexPacket *indexes,PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002777{
cristybb503372010-05-27 20:51:26 +00002778 size_t
cristy3ed852e2009-09-05 21:47:34 +00002779 signature;
2780
2781 unsigned short
2782 index;
2783
2784 index=ScaleQuantumToShort(pixel->red);
2785 signature=pixel_list->lists[0].nodes[index].signature;
2786 if (signature == pixel_list->signature)
2787 pixel_list->lists[0].nodes[index].count++;
2788 else
cristy69ec32d2011-02-27 23:57:09 +00002789 AddNodePixelList(pixel_list,0,index);
cristy3ed852e2009-09-05 21:47:34 +00002790 index=ScaleQuantumToShort(pixel->green);
2791 signature=pixel_list->lists[1].nodes[index].signature;
2792 if (signature == pixel_list->signature)
2793 pixel_list->lists[1].nodes[index].count++;
2794 else
cristy69ec32d2011-02-27 23:57:09 +00002795 AddNodePixelList(pixel_list,1,index);
cristy3ed852e2009-09-05 21:47:34 +00002796 index=ScaleQuantumToShort(pixel->blue);
2797 signature=pixel_list->lists[2].nodes[index].signature;
2798 if (signature == pixel_list->signature)
2799 pixel_list->lists[2].nodes[index].count++;
2800 else
cristy69ec32d2011-02-27 23:57:09 +00002801 AddNodePixelList(pixel_list,2,index);
cristy3ed852e2009-09-05 21:47:34 +00002802 index=ScaleQuantumToShort(pixel->opacity);
2803 signature=pixel_list->lists[3].nodes[index].signature;
2804 if (signature == pixel_list->signature)
2805 pixel_list->lists[3].nodes[index].count++;
2806 else
cristy69ec32d2011-02-27 23:57:09 +00002807 AddNodePixelList(pixel_list,3,index);
cristy3ed852e2009-09-05 21:47:34 +00002808 if (image->colorspace == CMYKColorspace)
2809 index=ScaleQuantumToShort(*indexes);
2810 signature=pixel_list->lists[4].nodes[index].signature;
2811 if (signature == pixel_list->signature)
2812 pixel_list->lists[4].nodes[index].count++;
2813 else
cristy69ec32d2011-02-27 23:57:09 +00002814 AddNodePixelList(pixel_list,4,index);
cristy3ed852e2009-09-05 21:47:34 +00002815}
2816
cristy69ec32d2011-02-27 23:57:09 +00002817static void ResetPixelList(PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002818{
2819 int
2820 level;
2821
cristy69ec32d2011-02-27 23:57:09 +00002822 register ListNode
cristy3ed852e2009-09-05 21:47:34 +00002823 *root;
2824
cristy69ec32d2011-02-27 23:57:09 +00002825 register SkipList
cristy3ed852e2009-09-05 21:47:34 +00002826 *list;
2827
cristy117ff172010-08-15 21:35:32 +00002828 register ssize_t
2829 channel;
2830
cristy3ed852e2009-09-05 21:47:34 +00002831 /*
2832 Reset the skip-list.
2833 */
2834 for (channel=0; channel < 5; channel++)
2835 {
2836 list=pixel_list->lists+channel;
2837 root=list->nodes+65536UL;
2838 list->level=0;
2839 for (level=0; level < 9; level++)
2840 root->next[level]=65536UL;
2841 }
2842 pixel_list->seed=pixel_list->signature++;
2843}
2844
2845MagickExport Image *MedianFilterImage(const Image *image,const double radius,
2846 ExceptionInfo *exception)
2847{
2848#define MedianFilterImageTag "MedianFilter/Image"
2849
cristyc4c8d132010-01-07 01:58:38 +00002850 CacheView
2851 *image_view,
2852 *median_view;
2853
cristy3ed852e2009-09-05 21:47:34 +00002854 Image
2855 *median_image;
2856
cristy3ed852e2009-09-05 21:47:34 +00002857 MagickBooleanType
2858 status;
2859
cristybb503372010-05-27 20:51:26 +00002860 MagickOffsetType
2861 progress;
2862
cristy69ec32d2011-02-27 23:57:09 +00002863 PixelList
cristyfa112112010-01-04 17:48:07 +00002864 **restrict pixel_list;
cristy3ed852e2009-09-05 21:47:34 +00002865
cristybb503372010-05-27 20:51:26 +00002866 size_t
cristy3ed852e2009-09-05 21:47:34 +00002867 width;
2868
cristybb503372010-05-27 20:51:26 +00002869 ssize_t
2870 y;
2871
cristy3ed852e2009-09-05 21:47:34 +00002872 /*
2873 Initialize median image attributes.
2874 */
2875 assert(image != (Image *) NULL);
2876 assert(image->signature == MagickSignature);
2877 if (image->debug != MagickFalse)
2878 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2879 assert(exception != (ExceptionInfo *) NULL);
2880 assert(exception->signature == MagickSignature);
2881 width=GetOptimalKernelWidth2D(radius,0.5);
2882 if ((image->columns < width) || (image->rows < width))
2883 ThrowImageException(OptionError,"ImageSmallerThanKernelRadius");
2884 median_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2885 exception);
2886 if (median_image == (Image *) NULL)
2887 return((Image *) NULL);
2888 if (SetImageStorageClass(median_image,DirectClass) == MagickFalse)
2889 {
2890 InheritException(exception,&median_image->exception);
2891 median_image=DestroyImage(median_image);
2892 return((Image *) NULL);
2893 }
cristy69ec32d2011-02-27 23:57:09 +00002894 pixel_list=AcquirePixelListThreadSet(width);
2895 if (pixel_list == (PixelList **) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002896 {
2897 median_image=DestroyImage(median_image);
2898 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2899 }
2900 /*
2901 Median filter each image row.
2902 */
2903 status=MagickTrue;
2904 progress=0;
2905 image_view=AcquireCacheView(image);
2906 median_view=AcquireCacheView(median_image);
cristyb5d5f722009-11-04 03:03:49 +00002907#if defined(MAGICKCORE_OPENMP_SUPPORT)
2908 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002909#endif
cristybb503372010-05-27 20:51:26 +00002910 for (y=0; y < (ssize_t) median_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002911 {
cristy5c9e6f22010-09-17 17:31:01 +00002912 const int
2913 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00002914
cristy3ed852e2009-09-05 21:47:34 +00002915 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00002916 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00002917
2918 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002919 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002920
2921 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00002922 *restrict median_indexes;
cristy3ed852e2009-09-05 21:47:34 +00002923
cristy3ed852e2009-09-05 21:47:34 +00002924 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002925 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002926
cristy117ff172010-08-15 21:35:32 +00002927 register ssize_t
2928 x;
2929
cristy3ed852e2009-09-05 21:47:34 +00002930 if (status == MagickFalse)
2931 continue;
cristy6ebe97c2010-07-03 01:17:28 +00002932 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
2933 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00002934 q=QueueCacheViewAuthenticPixels(median_view,0,y,median_image->columns,1,
2935 exception);
2936 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2937 {
2938 status=MagickFalse;
2939 continue;
2940 }
2941 indexes=GetCacheViewVirtualIndexQueue(image_view);
2942 median_indexes=GetCacheViewAuthenticIndexQueue(median_view);
cristybb503372010-05-27 20:51:26 +00002943 for (x=0; x < (ssize_t) median_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002944 {
2945 MagickPixelPacket
2946 pixel;
2947
cristy3ed852e2009-09-05 21:47:34 +00002948 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00002949 *restrict s;
cristy3ed852e2009-09-05 21:47:34 +00002950
cristy117ff172010-08-15 21:35:32 +00002951 register const PixelPacket
2952 *restrict r;
2953
cristybb503372010-05-27 20:51:26 +00002954 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002955 u,
2956 v;
2957
2958 r=p;
2959 s=indexes+x;
cristy69ec32d2011-02-27 23:57:09 +00002960 ResetPixelList(pixel_list[id]);
cristybb503372010-05-27 20:51:26 +00002961 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00002962 {
cristybb503372010-05-27 20:51:26 +00002963 for (u=0; u < (ssize_t) width; u++)
cristy69ec32d2011-02-27 23:57:09 +00002964 InsertPixelList(image,r+u,s+u,pixel_list[id]);
cristy3ed852e2009-09-05 21:47:34 +00002965 r+=image->columns+width;
2966 s+=image->columns+width;
2967 }
cristy69ec32d2011-02-27 23:57:09 +00002968 pixel=GetPixelList(pixel_list[id]);
cristy3ed852e2009-09-05 21:47:34 +00002969 SetPixelPacket(median_image,&pixel,q,median_indexes+x);
2970 p++;
2971 q++;
2972 }
2973 if (SyncCacheViewAuthenticPixels(median_view,exception) == MagickFalse)
2974 status=MagickFalse;
2975 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2976 {
2977 MagickBooleanType
2978 proceed;
2979
cristyb5d5f722009-11-04 03:03:49 +00002980#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00002981 #pragma omp critical (MagickCore_MedianFilterImage)
2982#endif
2983 proceed=SetImageProgress(image,MedianFilterImageTag,progress++,
2984 image->rows);
2985 if (proceed == MagickFalse)
2986 status=MagickFalse;
2987 }
2988 }
2989 median_view=DestroyCacheView(median_view);
2990 image_view=DestroyCacheView(image_view);
cristy69ec32d2011-02-27 23:57:09 +00002991 pixel_list=DestroyPixelListThreadSet(pixel_list);
cristy3ed852e2009-09-05 21:47:34 +00002992 return(median_image);
2993}
2994
2995/*
2996%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2997% %
2998% %
2999% %
cristy69ec32d2011-02-27 23:57:09 +00003000% M o d e I m a g e %
3001% %
3002% %
3003% %
3004%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3005%
3006% ModeImage() makes each pixel the 'predominate color' of the neighborhood
3007% if the specified radius.
3008%
3009% The format of the ModeImage method is:
3010%
3011% Image *ModeImage(const Image *image,const double radius,
3012% ExceptionInfo *exception)
3013%
3014% A description of each parameter follows:
3015%
3016% o image: the image.
3017%
3018% o radius: the radius of the pixel neighborhood.
3019%
3020% o exception: return any errors or warnings in this structure.
3021%
3022*/
3023
3024static MagickPixelPacket GetModePixelList(PixelList *pixel_list)
3025{
3026 MagickPixelPacket
3027 pixel;
3028
3029 register SkipList
3030 *list;
3031
3032 register ssize_t
3033 channel;
3034
3035 size_t
3036 center,
3037 color,
3038 count,
3039 previous,
3040 next;
3041
3042 unsigned short
3043 channels[5];
3044
3045 /*
3046 Finds the median value for each of the color.
3047 */
3048 center=pixel_list->center;
3049 for (channel=0; channel < 5; channel++)
3050 {
3051 list=pixel_list->lists+channel;
3052 color=65536UL;
3053 next=list->nodes[color].next[0];
3054 count=0;
3055 do
3056 {
3057 previous=color;
3058 color=next;
3059 next=list->nodes[color].next[0];
3060 count+=list->nodes[color].count;
3061 }
3062 while (count <= center);
3063 if ((previous == 65536UL) && (next != 65536UL))
3064 color=next;
3065 else
3066 if ((previous != 65536UL) && (next == 65536UL))
3067 color=previous;
3068 channels[channel]=(unsigned short) color;
3069 }
3070 GetMagickPixelPacket((const Image *) NULL,&pixel);
3071 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
3072 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
3073 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
3074 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
3075 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
3076 return(pixel);
3077}
3078
3079MagickExport Image *ModeImage(const Image *image,const double radius,
3080 ExceptionInfo *exception)
3081{
3082#define ModeImageTag "Mode/Image"
3083
3084 CacheView
3085 *image_view,
3086 *mode_view;
3087
3088 Image
3089 *mode_image;
3090
3091 MagickBooleanType
3092 status;
3093
3094 MagickOffsetType
3095 progress;
3096
3097 PixelList
3098 **restrict pixel_list;
3099
3100 size_t
3101 width;
3102
3103 ssize_t
3104 y;
3105
3106 /*
3107 Initialize mode image attributes.
3108 */
3109 assert(image != (Image *) NULL);
3110 assert(image->signature == MagickSignature);
3111 if (image->debug != MagickFalse)
3112 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3113 assert(exception != (ExceptionInfo *) NULL);
3114 assert(exception->signature == MagickSignature);
3115 width=GetOptimalKernelWidth2D(radius,0.5);
3116 if ((image->columns < width) || (image->rows < width))
3117 ThrowImageException(OptionError,"ImageSmallerThanKernelRadius");
3118 mode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3119 exception);
3120 if (mode_image == (Image *) NULL)
3121 return((Image *) NULL);
3122 if (SetImageStorageClass(mode_image,DirectClass) == MagickFalse)
3123 {
3124 InheritException(exception,&mode_image->exception);
3125 mode_image=DestroyImage(mode_image);
3126 return((Image *) NULL);
3127 }
3128 pixel_list=AcquirePixelListThreadSet(width);
3129 if (pixel_list == (PixelList **) NULL)
3130 {
3131 mode_image=DestroyImage(mode_image);
3132 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3133 }
3134 /*
3135 Reduce mode image.
3136 */
3137 status=MagickTrue;
3138 progress=0;
3139 image_view=AcquireCacheView(image);
3140 mode_view=AcquireCacheView(mode_image);
3141#if defined(MAGICKCORE_OPENMP_SUPPORT)
3142 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3143#endif
3144 for (y=0; y < (ssize_t) mode_image->rows; y++)
3145 {
3146 const int
3147 id = GetOpenMPThreadId();
3148
3149 register const IndexPacket
3150 *restrict indexes;
3151
3152 register const PixelPacket
3153 *restrict p;
3154
3155 register IndexPacket
3156 *restrict mode_indexes;
3157
3158 register PixelPacket
3159 *restrict q;
3160
3161 register ssize_t
3162 x;
3163
3164 if (status == MagickFalse)
3165 continue;
3166 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3167 (width/2L),image->columns+width,width,exception);
3168 q=QueueCacheViewAuthenticPixels(mode_view,0,y,mode_image->columns,1,
3169 exception);
3170 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
3171 {
3172 status=MagickFalse;
3173 continue;
3174 }
3175 indexes=GetCacheViewVirtualIndexQueue(image_view);
3176 mode_indexes=GetCacheViewAuthenticIndexQueue(mode_view);
3177 for (x=0; x < (ssize_t) mode_image->columns; x++)
3178 {
3179 MagickPixelPacket
3180 pixel;
3181
3182 register const PixelPacket
3183 *restrict r;
3184
3185 register const IndexPacket
3186 *restrict s;
3187
3188 register ssize_t
3189 u,
3190 v;
3191
3192 r=p;
3193 s=indexes+x;
3194 ResetPixelList(pixel_list[id]);
3195 for (v=0; v < (ssize_t) width; v++)
3196 {
3197 for (u=0; u < (ssize_t) width; u++)
3198 InsertPixelList(image,r+u,s+u,pixel_list[id]);
3199 r+=image->columns+width;
3200 s+=image->columns+width;
3201 }
3202 pixel=GetModePixelList(pixel_list[id]);
3203 SetPixelPacket(mode_image,&pixel,q,mode_indexes+x);
3204 p++;
3205 q++;
3206 }
3207 if (SyncCacheViewAuthenticPixels(mode_view,exception) == MagickFalse)
3208 status=MagickFalse;
3209 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3210 {
3211 MagickBooleanType
3212 proceed;
3213
3214#if defined(MAGICKCORE_OPENMP_SUPPORT)
3215 #pragma omp critical (MagickCore_ModeImage)
3216#endif
3217 proceed=SetImageProgress(image,ModeImageTag,progress++,image->rows);
3218 if (proceed == MagickFalse)
3219 status=MagickFalse;
3220 }
3221 }
3222 mode_view=DestroyCacheView(mode_view);
3223 image_view=DestroyCacheView(image_view);
3224 pixel_list=DestroyPixelListThreadSet(pixel_list);
3225 return(mode_image);
3226}
3227
3228/*
3229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3230% %
3231% %
3232% %
cristy3ed852e2009-09-05 21:47:34 +00003233% M o t i o n B l u r I m a g e %
3234% %
3235% %
3236% %
3237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3238%
3239% MotionBlurImage() simulates motion blur. We convolve the image with a
3240% Gaussian operator of the given radius and standard deviation (sigma).
3241% For reasonable results, radius should be larger than sigma. Use a
3242% radius of 0 and MotionBlurImage() selects a suitable radius for you.
3243% Angle gives the angle of the blurring motion.
3244%
3245% Andrew Protano contributed this effect.
3246%
3247% The format of the MotionBlurImage method is:
3248%
3249% Image *MotionBlurImage(const Image *image,const double radius,
3250% const double sigma,const double angle,ExceptionInfo *exception)
3251% Image *MotionBlurImageChannel(const Image *image,const ChannelType channel,
3252% const double radius,const double sigma,const double angle,
3253% ExceptionInfo *exception)
3254%
3255% A description of each parameter follows:
3256%
3257% o image: the image.
3258%
3259% o channel: the channel type.
3260%
3261% o radius: the radius of the Gaussian, in pixels, not counting the center
3262% o radius: the radius of the Gaussian, in pixels, not counting
3263% the center pixel.
3264%
3265% o sigma: the standard deviation of the Gaussian, in pixels.
3266%
cristycee97112010-05-28 00:44:52 +00003267% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003268%
3269% o exception: return any errors or warnings in this structure.
3270%
3271*/
3272
cristybb503372010-05-27 20:51:26 +00003273static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00003274{
cristy3ed852e2009-09-05 21:47:34 +00003275 double
cristy47e00502009-12-17 19:19:57 +00003276 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00003277 normalize;
3278
cristybb503372010-05-27 20:51:26 +00003279 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003280 i;
3281
3282 /*
cristy47e00502009-12-17 19:19:57 +00003283 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00003284 */
3285 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3286 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
3287 if (kernel == (double *) NULL)
3288 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00003289 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00003290 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00003291 {
cristy4205a3c2010-09-12 20:19:59 +00003292 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
3293 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003294 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00003295 }
cristybb503372010-05-27 20:51:26 +00003296 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00003297 kernel[i]/=normalize;
3298 return(kernel);
3299}
3300
3301MagickExport Image *MotionBlurImage(const Image *image,const double radius,
3302 const double sigma,const double angle,ExceptionInfo *exception)
3303{
3304 Image
3305 *motion_blur;
3306
3307 motion_blur=MotionBlurImageChannel(image,DefaultChannels,radius,sigma,angle,
3308 exception);
3309 return(motion_blur);
3310}
3311
3312MagickExport Image *MotionBlurImageChannel(const Image *image,
3313 const ChannelType channel,const double radius,const double sigma,
3314 const double angle,ExceptionInfo *exception)
3315{
cristyc4c8d132010-01-07 01:58:38 +00003316 CacheView
3317 *blur_view,
3318 *image_view;
3319
cristy3ed852e2009-09-05 21:47:34 +00003320 double
3321 *kernel;
3322
3323 Image
3324 *blur_image;
3325
cristy3ed852e2009-09-05 21:47:34 +00003326 MagickBooleanType
3327 status;
3328
cristybb503372010-05-27 20:51:26 +00003329 MagickOffsetType
3330 progress;
3331
cristy3ed852e2009-09-05 21:47:34 +00003332 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00003333 bias;
cristy3ed852e2009-09-05 21:47:34 +00003334
3335 OffsetInfo
3336 *offset;
3337
3338 PointInfo
3339 point;
3340
cristybb503372010-05-27 20:51:26 +00003341 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003342 i;
3343
cristybb503372010-05-27 20:51:26 +00003344 size_t
cristy3ed852e2009-09-05 21:47:34 +00003345 width;
3346
cristybb503372010-05-27 20:51:26 +00003347 ssize_t
3348 y;
3349
cristy3ed852e2009-09-05 21:47:34 +00003350 assert(image != (Image *) NULL);
3351 assert(image->signature == MagickSignature);
3352 if (image->debug != MagickFalse)
3353 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3354 assert(exception != (ExceptionInfo *) NULL);
3355 width=GetOptimalKernelWidth1D(radius,sigma);
3356 kernel=GetMotionBlurKernel(width,sigma);
3357 if (kernel == (double *) NULL)
3358 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3359 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
3360 if (offset == (OffsetInfo *) NULL)
3361 {
3362 kernel=(double *) RelinquishMagickMemory(kernel);
3363 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3364 }
3365 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3366 if (blur_image == (Image *) NULL)
3367 {
3368 kernel=(double *) RelinquishMagickMemory(kernel);
3369 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3370 return((Image *) NULL);
3371 }
3372 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3373 {
3374 kernel=(double *) RelinquishMagickMemory(kernel);
3375 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3376 InheritException(exception,&blur_image->exception);
3377 blur_image=DestroyImage(blur_image);
3378 return((Image *) NULL);
3379 }
3380 point.x=(double) width*sin(DegreesToRadians(angle));
3381 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00003382 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00003383 {
cristybb503372010-05-27 20:51:26 +00003384 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
3385 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003386 }
3387 /*
3388 Motion blur image.
3389 */
3390 status=MagickTrue;
3391 progress=0;
cristyddd82202009-11-03 20:14:50 +00003392 GetMagickPixelPacket(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003393 image_view=AcquireCacheView(image);
3394 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00003395#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00003396 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00003397#endif
cristybb503372010-05-27 20:51:26 +00003398 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003399 {
3400 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003401 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00003402
cristy3ed852e2009-09-05 21:47:34 +00003403 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003404 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003405
cristy117ff172010-08-15 21:35:32 +00003406 register ssize_t
3407 x;
3408
cristy3ed852e2009-09-05 21:47:34 +00003409 if (status == MagickFalse)
3410 continue;
3411 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3412 exception);
3413 if (q == (PixelPacket *) NULL)
3414 {
3415 status=MagickFalse;
3416 continue;
3417 }
3418 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +00003419 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003420 {
3421 MagickPixelPacket
3422 qixel;
3423
3424 PixelPacket
3425 pixel;
3426
cristy117ff172010-08-15 21:35:32 +00003427 register const IndexPacket
3428 *restrict indexes;
3429
cristy3ed852e2009-09-05 21:47:34 +00003430 register double
cristyc47d1f82009-11-26 01:44:43 +00003431 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00003432
cristybb503372010-05-27 20:51:26 +00003433 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003434 i;
3435
cristy3ed852e2009-09-05 21:47:34 +00003436 k=kernel;
cristyddd82202009-11-03 20:14:50 +00003437 qixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00003438 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
3439 {
cristybb503372010-05-27 20:51:26 +00003440 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00003441 {
3442 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
3443 offset[i].y,&pixel,exception);
3444 qixel.red+=(*k)*pixel.red;
3445 qixel.green+=(*k)*pixel.green;
3446 qixel.blue+=(*k)*pixel.blue;
3447 qixel.opacity+=(*k)*pixel.opacity;
3448 if (image->colorspace == CMYKColorspace)
3449 {
3450 indexes=GetCacheViewVirtualIndexQueue(image_view);
3451 qixel.index+=(*k)*(*indexes);
3452 }
3453 k++;
3454 }
3455 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003456 q->red=ClampToQuantum(qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00003457 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003458 q->green=ClampToQuantum(qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00003459 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003460 q->blue=ClampToQuantum(qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00003461 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003462 q->opacity=ClampToQuantum(qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00003463 if (((channel & IndexChannel) != 0) &&
3464 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00003465 blur_indexes[x]=(IndexPacket) ClampToQuantum(qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00003466 }
3467 else
3468 {
3469 MagickRealType
3470 alpha,
3471 gamma;
3472
3473 alpha=0.0;
3474 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00003475 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00003476 {
3477 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
3478 offset[i].y,&pixel,exception);
cristy8a7ea362010-03-10 20:31:43 +00003479 alpha=(MagickRealType) (QuantumScale*
3480 GetAlphaPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00003481 qixel.red+=(*k)*alpha*pixel.red;
3482 qixel.green+=(*k)*alpha*pixel.green;
3483 qixel.blue+=(*k)*alpha*pixel.blue;
3484 qixel.opacity+=(*k)*pixel.opacity;
3485 if (image->colorspace == CMYKColorspace)
3486 {
3487 indexes=GetCacheViewVirtualIndexQueue(image_view);
3488 qixel.index+=(*k)*alpha*(*indexes);
3489 }
3490 gamma+=(*k)*alpha;
3491 k++;
3492 }
3493 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3494 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003495 q->red=ClampToQuantum(gamma*qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00003496 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003497 q->green=ClampToQuantum(gamma*qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00003498 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003499 q->blue=ClampToQuantum(gamma*qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00003500 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003501 q->opacity=ClampToQuantum(qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00003502 if (((channel & IndexChannel) != 0) &&
3503 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00003504 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00003505 }
3506 q++;
3507 }
3508 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3509 status=MagickFalse;
3510 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3511 {
3512 MagickBooleanType
3513 proceed;
3514
cristyb557a152011-02-22 12:14:30 +00003515#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003516 #pragma omp critical (MagickCore_MotionBlurImageChannel)
3517#endif
3518 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3519 if (proceed == MagickFalse)
3520 status=MagickFalse;
3521 }
3522 }
3523 blur_view=DestroyCacheView(blur_view);
3524 image_view=DestroyCacheView(image_view);
3525 kernel=(double *) RelinquishMagickMemory(kernel);
3526 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3527 if (status == MagickFalse)
3528 blur_image=DestroyImage(blur_image);
3529 return(blur_image);
3530}
3531
3532/*
3533%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3534% %
3535% %
3536% %
3537% P r e v i e w I m a g e %
3538% %
3539% %
3540% %
3541%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3542%
3543% PreviewImage() tiles 9 thumbnails of the specified image with an image
3544% processing operation applied with varying parameters. This may be helpful
3545% pin-pointing an appropriate parameter for a particular image processing
3546% operation.
3547%
3548% The format of the PreviewImages method is:
3549%
3550% Image *PreviewImages(const Image *image,const PreviewType preview,
3551% ExceptionInfo *exception)
3552%
3553% A description of each parameter follows:
3554%
3555% o image: the image.
3556%
3557% o preview: the image processing operation.
3558%
3559% o exception: return any errors or warnings in this structure.
3560%
3561*/
3562MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
3563 ExceptionInfo *exception)
3564{
3565#define NumberTiles 9
3566#define PreviewImageTag "Preview/Image"
3567#define DefaultPreviewGeometry "204x204+10+10"
3568
3569 char
3570 factor[MaxTextExtent],
3571 label[MaxTextExtent];
3572
3573 double
3574 degrees,
3575 gamma,
3576 percentage,
3577 radius,
3578 sigma,
3579 threshold;
3580
3581 Image
3582 *images,
3583 *montage_image,
3584 *preview_image,
3585 *thumbnail;
3586
3587 ImageInfo
3588 *preview_info;
3589
cristy3ed852e2009-09-05 21:47:34 +00003590 MagickBooleanType
3591 proceed;
3592
3593 MontageInfo
3594 *montage_info;
3595
3596 QuantizeInfo
3597 quantize_info;
3598
3599 RectangleInfo
3600 geometry;
3601
cristybb503372010-05-27 20:51:26 +00003602 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003603 i,
3604 x;
3605
cristybb503372010-05-27 20:51:26 +00003606 size_t
cristy3ed852e2009-09-05 21:47:34 +00003607 colors;
3608
cristy117ff172010-08-15 21:35:32 +00003609 ssize_t
3610 y;
3611
cristy3ed852e2009-09-05 21:47:34 +00003612 /*
3613 Open output image file.
3614 */
3615 assert(image != (Image *) NULL);
3616 assert(image->signature == MagickSignature);
3617 if (image->debug != MagickFalse)
3618 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3619 colors=2;
3620 degrees=0.0;
3621 gamma=(-0.2f);
3622 preview_info=AcquireImageInfo();
3623 SetGeometry(image,&geometry);
3624 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
3625 &geometry.width,&geometry.height);
3626 images=NewImageList();
3627 percentage=12.5;
3628 GetQuantizeInfo(&quantize_info);
3629 radius=0.0;
3630 sigma=1.0;
3631 threshold=0.0;
3632 x=0;
3633 y=0;
3634 for (i=0; i < NumberTiles; i++)
3635 {
3636 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
3637 if (thumbnail == (Image *) NULL)
3638 break;
3639 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
3640 (void *) NULL);
3641 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
3642 if (i == (NumberTiles/2))
3643 {
3644 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
3645 AppendImageToList(&images,thumbnail);
3646 continue;
3647 }
3648 switch (preview)
3649 {
3650 case RotatePreview:
3651 {
3652 degrees+=45.0;
3653 preview_image=RotateImage(thumbnail,degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003654 (void) FormatMagickString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00003655 break;
3656 }
3657 case ShearPreview:
3658 {
3659 degrees+=5.0;
3660 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003661 (void) FormatMagickString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00003662 degrees,2.0*degrees);
3663 break;
3664 }
3665 case RollPreview:
3666 {
cristybb503372010-05-27 20:51:26 +00003667 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
3668 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00003669 preview_image=RollImage(thumbnail,x,y,exception);
cristye8c25f92010-06-03 00:53:06 +00003670 (void) FormatMagickString(label,MaxTextExtent,"roll %+.20gx%+.20g",
3671 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00003672 break;
3673 }
3674 case HuePreview:
3675 {
3676 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3677 if (preview_image == (Image *) NULL)
3678 break;
cristye7f51092010-01-17 00:39:37 +00003679 (void) FormatMagickString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00003680 2.0*percentage);
3681 (void) ModulateImage(preview_image,factor);
3682 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3683 break;
3684 }
3685 case SaturationPreview:
3686 {
3687 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3688 if (preview_image == (Image *) NULL)
3689 break;
cristye7f51092010-01-17 00:39:37 +00003690 (void) FormatMagickString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00003691 2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00003692 (void) ModulateImage(preview_image,factor);
3693 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3694 break;
3695 }
3696 case BrightnessPreview:
3697 {
3698 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3699 if (preview_image == (Image *) NULL)
3700 break;
cristye7f51092010-01-17 00:39:37 +00003701 (void) FormatMagickString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00003702 (void) ModulateImage(preview_image,factor);
3703 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3704 break;
3705 }
3706 case GammaPreview:
3707 default:
3708 {
3709 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3710 if (preview_image == (Image *) NULL)
3711 break;
3712 gamma+=0.4f;
3713 (void) GammaImageChannel(preview_image,DefaultChannels,gamma);
cristye7f51092010-01-17 00:39:37 +00003714 (void) FormatMagickString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00003715 break;
3716 }
3717 case SpiffPreview:
3718 {
3719 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3720 if (preview_image != (Image *) NULL)
3721 for (x=0; x < i; x++)
3722 (void) ContrastImage(preview_image,MagickTrue);
cristye8c25f92010-06-03 00:53:06 +00003723 (void) FormatMagickString(label,MaxTextExtent,"contrast (%.20g)",
3724 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00003725 break;
3726 }
3727 case DullPreview:
3728 {
3729 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3730 if (preview_image == (Image *) NULL)
3731 break;
3732 for (x=0; x < i; x++)
3733 (void) ContrastImage(preview_image,MagickFalse);
cristye8c25f92010-06-03 00:53:06 +00003734 (void) FormatMagickString(label,MaxTextExtent,"+contrast (%.20g)",
3735 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00003736 break;
3737 }
3738 case GrayscalePreview:
3739 {
3740 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3741 if (preview_image == (Image *) NULL)
3742 break;
3743 colors<<=1;
3744 quantize_info.number_colors=colors;
3745 quantize_info.colorspace=GRAYColorspace;
3746 (void) QuantizeImage(&quantize_info,preview_image);
3747 (void) FormatMagickString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00003748 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00003749 break;
3750 }
3751 case QuantizePreview:
3752 {
3753 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3754 if (preview_image == (Image *) NULL)
3755 break;
3756 colors<<=1;
3757 quantize_info.number_colors=colors;
3758 (void) QuantizeImage(&quantize_info,preview_image);
cristye8c25f92010-06-03 00:53:06 +00003759 (void) FormatMagickString(label,MaxTextExtent,"colors %.20g",(double)
3760 colors);
cristy3ed852e2009-09-05 21:47:34 +00003761 break;
3762 }
3763 case DespecklePreview:
3764 {
3765 for (x=0; x < (i-1); x++)
3766 {
3767 preview_image=DespeckleImage(thumbnail,exception);
3768 if (preview_image == (Image *) NULL)
3769 break;
3770 thumbnail=DestroyImage(thumbnail);
3771 thumbnail=preview_image;
3772 }
3773 preview_image=DespeckleImage(thumbnail,exception);
3774 if (preview_image == (Image *) NULL)
3775 break;
cristye8c25f92010-06-03 00:53:06 +00003776 (void) FormatMagickString(label,MaxTextExtent,"despeckle (%.20g)",
3777 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00003778 break;
3779 }
3780 case ReduceNoisePreview:
3781 {
3782 preview_image=ReduceNoiseImage(thumbnail,radius,exception);
cristye7f51092010-01-17 00:39:37 +00003783 (void) FormatMagickString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00003784 break;
3785 }
3786 case AddNoisePreview:
3787 {
3788 switch ((int) i)
3789 {
3790 case 0:
3791 {
3792 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
3793 break;
3794 }
3795 case 1:
3796 {
3797 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
3798 break;
3799 }
3800 case 2:
3801 {
3802 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
3803 break;
3804 }
3805 case 3:
3806 {
3807 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
3808 break;
3809 }
3810 case 4:
3811 {
3812 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
3813 break;
3814 }
3815 case 5:
3816 {
3817 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
3818 break;
3819 }
3820 default:
3821 {
3822 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
3823 break;
3824 }
3825 }
3826 preview_image=ReduceNoiseImage(thumbnail,(double) i,exception);
3827 (void) FormatMagickString(label,MaxTextExtent,"+noise %s",factor);
3828 break;
3829 }
3830 case SharpenPreview:
3831 {
3832 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
cristye7f51092010-01-17 00:39:37 +00003833 (void) FormatMagickString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00003834 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00003835 break;
3836 }
3837 case BlurPreview:
3838 {
3839 preview_image=BlurImage(thumbnail,radius,sigma,exception);
cristye7f51092010-01-17 00:39:37 +00003840 (void) FormatMagickString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00003841 sigma);
3842 break;
3843 }
3844 case ThresholdPreview:
3845 {
3846 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3847 if (preview_image == (Image *) NULL)
3848 break;
3849 (void) BilevelImage(thumbnail,
3850 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
cristye7f51092010-01-17 00:39:37 +00003851 (void) FormatMagickString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00003852 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
3853 break;
3854 }
3855 case EdgeDetectPreview:
3856 {
3857 preview_image=EdgeImage(thumbnail,radius,exception);
cristye7f51092010-01-17 00:39:37 +00003858 (void) FormatMagickString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00003859 break;
3860 }
3861 case SpreadPreview:
3862 {
3863 preview_image=SpreadImage(thumbnail,radius,exception);
cristye7f51092010-01-17 00:39:37 +00003864 (void) FormatMagickString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00003865 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00003866 break;
3867 }
3868 case SolarizePreview:
3869 {
3870 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3871 if (preview_image == (Image *) NULL)
3872 break;
3873 (void) SolarizeImage(preview_image,(double) QuantumRange*
3874 percentage/100.0);
cristye7f51092010-01-17 00:39:37 +00003875 (void) FormatMagickString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00003876 (QuantumRange*percentage)/100.0);
3877 break;
3878 }
3879 case ShadePreview:
3880 {
3881 degrees+=10.0;
3882 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
3883 exception);
cristye7f51092010-01-17 00:39:37 +00003884 (void) FormatMagickString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00003885 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00003886 break;
3887 }
3888 case RaisePreview:
3889 {
3890 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3891 if (preview_image == (Image *) NULL)
3892 break;
cristybb503372010-05-27 20:51:26 +00003893 geometry.width=(size_t) (2*i+2);
3894 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00003895 geometry.x=i/2;
3896 geometry.y=i/2;
3897 (void) RaiseImage(preview_image,&geometry,MagickTrue);
cristye8c25f92010-06-03 00:53:06 +00003898 (void) FormatMagickString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00003899 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00003900 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00003901 break;
3902 }
3903 case SegmentPreview:
3904 {
3905 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3906 if (preview_image == (Image *) NULL)
3907 break;
3908 threshold+=0.4f;
3909 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
3910 threshold);
cristye7f51092010-01-17 00:39:37 +00003911 (void) FormatMagickString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00003912 threshold,threshold);
3913 break;
3914 }
3915 case SwirlPreview:
3916 {
3917 preview_image=SwirlImage(thumbnail,degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003918 (void) FormatMagickString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00003919 degrees+=45.0;
3920 break;
3921 }
3922 case ImplodePreview:
3923 {
3924 degrees+=0.1f;
3925 preview_image=ImplodeImage(thumbnail,degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003926 (void) FormatMagickString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00003927 break;
3928 }
3929 case WavePreview:
3930 {
3931 degrees+=5.0f;
3932 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003933 (void) FormatMagickString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00003934 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00003935 break;
3936 }
3937 case OilPaintPreview:
3938 {
3939 preview_image=OilPaintImage(thumbnail,(double) radius,exception);
cristye7f51092010-01-17 00:39:37 +00003940 (void) FormatMagickString(label,MaxTextExtent,"paint %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00003941 break;
3942 }
3943 case CharcoalDrawingPreview:
3944 {
3945 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
3946 exception);
cristye7f51092010-01-17 00:39:37 +00003947 (void) FormatMagickString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00003948 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00003949 break;
3950 }
3951 case JPEGPreview:
3952 {
3953 char
3954 filename[MaxTextExtent];
3955
3956 int
3957 file;
3958
3959 MagickBooleanType
3960 status;
3961
3962 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3963 if (preview_image == (Image *) NULL)
3964 break;
cristybb503372010-05-27 20:51:26 +00003965 preview_info->quality=(size_t) percentage;
cristye8c25f92010-06-03 00:53:06 +00003966 (void) FormatMagickString(factor,MaxTextExtent,"%.20g",(double)
3967 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00003968 file=AcquireUniqueFileResource(filename);
3969 if (file != -1)
3970 file=close(file)-1;
3971 (void) FormatMagickString(preview_image->filename,MaxTextExtent,
3972 "jpeg:%s",filename);
3973 status=WriteImage(preview_info,preview_image);
3974 if (status != MagickFalse)
3975 {
3976 Image
3977 *quality_image;
3978
3979 (void) CopyMagickString(preview_info->filename,
3980 preview_image->filename,MaxTextExtent);
3981 quality_image=ReadImage(preview_info,exception);
3982 if (quality_image != (Image *) NULL)
3983 {
3984 preview_image=DestroyImage(preview_image);
3985 preview_image=quality_image;
3986 }
3987 }
3988 (void) RelinquishUniqueFileResource(preview_image->filename);
3989 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristye7f51092010-01-17 00:39:37 +00003990 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00003991 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
3992 1024.0/1024.0);
3993 else
3994 if (GetBlobSize(preview_image) >= 1024)
cristy8cd5b312010-01-07 01:10:24 +00003995 (void) FormatMagickString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00003996 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00003997 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00003998 else
cristye8c25f92010-06-03 00:53:06 +00003999 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%.20gb ",
4000 factor,(double) GetBlobSize(thumbnail));
cristy3ed852e2009-09-05 21:47:34 +00004001 break;
4002 }
4003 }
4004 thumbnail=DestroyImage(thumbnail);
4005 percentage+=12.5;
4006 radius+=0.5;
4007 sigma+=0.25;
4008 if (preview_image == (Image *) NULL)
4009 break;
4010 (void) DeleteImageProperty(preview_image,"label");
4011 (void) SetImageProperty(preview_image,"label",label);
4012 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00004013 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
4014 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00004015 if (proceed == MagickFalse)
4016 break;
4017 }
4018 if (images == (Image *) NULL)
4019 {
4020 preview_info=DestroyImageInfo(preview_info);
4021 return((Image *) NULL);
4022 }
4023 /*
4024 Create the montage.
4025 */
4026 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
4027 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
4028 montage_info->shadow=MagickTrue;
4029 (void) CloneString(&montage_info->tile,"3x3");
4030 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
4031 (void) CloneString(&montage_info->frame,DefaultTileFrame);
4032 montage_image=MontageImages(images,montage_info,exception);
4033 montage_info=DestroyMontageInfo(montage_info);
4034 images=DestroyImageList(images);
4035 if (montage_image == (Image *) NULL)
4036 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4037 if (montage_image->montage != (char *) NULL)
4038 {
4039 /*
4040 Free image directory.
4041 */
4042 montage_image->montage=(char *) RelinquishMagickMemory(
4043 montage_image->montage);
4044 if (image->directory != (char *) NULL)
4045 montage_image->directory=(char *) RelinquishMagickMemory(
4046 montage_image->directory);
4047 }
4048 preview_info=DestroyImageInfo(preview_info);
4049 return(montage_image);
4050}
4051
4052/*
4053%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4054% %
4055% %
4056% %
4057% R a d i a l B l u r I m a g e %
4058% %
4059% %
4060% %
4061%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4062%
4063% RadialBlurImage() applies a radial blur to the image.
4064%
4065% Andrew Protano contributed this effect.
4066%
4067% The format of the RadialBlurImage method is:
4068%
4069% Image *RadialBlurImage(const Image *image,const double angle,
4070% ExceptionInfo *exception)
4071% Image *RadialBlurImageChannel(const Image *image,const ChannelType channel,
4072% const double angle,ExceptionInfo *exception)
4073%
4074% A description of each parameter follows:
4075%
4076% o image: the image.
4077%
4078% o channel: the channel type.
4079%
4080% o angle: the angle of the radial blur.
4081%
4082% o exception: return any errors or warnings in this structure.
4083%
4084*/
4085
4086MagickExport Image *RadialBlurImage(const Image *image,const double angle,
4087 ExceptionInfo *exception)
4088{
4089 Image
4090 *blur_image;
4091
4092 blur_image=RadialBlurImageChannel(image,DefaultChannels,angle,exception);
4093 return(blur_image);
4094}
4095
4096MagickExport Image *RadialBlurImageChannel(const Image *image,
4097 const ChannelType channel,const double angle,ExceptionInfo *exception)
4098{
cristyc4c8d132010-01-07 01:58:38 +00004099 CacheView
4100 *blur_view,
4101 *image_view;
4102
cristy3ed852e2009-09-05 21:47:34 +00004103 Image
4104 *blur_image;
4105
cristy3ed852e2009-09-05 21:47:34 +00004106 MagickBooleanType
4107 status;
4108
cristybb503372010-05-27 20:51:26 +00004109 MagickOffsetType
4110 progress;
4111
cristy3ed852e2009-09-05 21:47:34 +00004112 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00004113 bias;
cristy3ed852e2009-09-05 21:47:34 +00004114
4115 MagickRealType
4116 blur_radius,
4117 *cos_theta,
4118 offset,
4119 *sin_theta,
4120 theta;
4121
4122 PointInfo
4123 blur_center;
4124
cristybb503372010-05-27 20:51:26 +00004125 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004126 i;
4127
cristybb503372010-05-27 20:51:26 +00004128 size_t
cristy3ed852e2009-09-05 21:47:34 +00004129 n;
4130
cristybb503372010-05-27 20:51:26 +00004131 ssize_t
4132 y;
4133
cristy3ed852e2009-09-05 21:47:34 +00004134 /*
4135 Allocate blur image.
4136 */
4137 assert(image != (Image *) NULL);
4138 assert(image->signature == MagickSignature);
4139 if (image->debug != MagickFalse)
4140 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4141 assert(exception != (ExceptionInfo *) NULL);
4142 assert(exception->signature == MagickSignature);
4143 blur_image=CloneImage(image,0,0,MagickTrue,exception);
4144 if (blur_image == (Image *) NULL)
4145 return((Image *) NULL);
4146 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
4147 {
4148 InheritException(exception,&blur_image->exception);
4149 blur_image=DestroyImage(blur_image);
4150 return((Image *) NULL);
4151 }
4152 blur_center.x=(double) image->columns/2.0;
4153 blur_center.y=(double) image->rows/2.0;
4154 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00004155 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00004156 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
4157 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
4158 sizeof(*cos_theta));
4159 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
4160 sizeof(*sin_theta));
4161 if ((cos_theta == (MagickRealType *) NULL) ||
4162 (sin_theta == (MagickRealType *) NULL))
4163 {
4164 blur_image=DestroyImage(blur_image);
4165 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4166 }
4167 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00004168 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00004169 {
4170 cos_theta[i]=cos((double) (theta*i-offset));
4171 sin_theta[i]=sin((double) (theta*i-offset));
4172 }
4173 /*
4174 Radial blur image.
4175 */
4176 status=MagickTrue;
4177 progress=0;
cristyddd82202009-11-03 20:14:50 +00004178 GetMagickPixelPacket(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00004179 image_view=AcquireCacheView(image);
4180 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00004181#if defined(MAGICKCORE_OPENMP_SUPPORT)
4182 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004183#endif
cristybb503372010-05-27 20:51:26 +00004184 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004185 {
4186 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004187 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00004188
4189 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004190 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00004191
cristy3ed852e2009-09-05 21:47:34 +00004192 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004193 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004194
cristy117ff172010-08-15 21:35:32 +00004195 register ssize_t
4196 x;
4197
cristy3ed852e2009-09-05 21:47:34 +00004198 if (status == MagickFalse)
4199 continue;
4200 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
4201 exception);
4202 if (q == (PixelPacket *) NULL)
4203 {
4204 status=MagickFalse;
4205 continue;
4206 }
4207 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +00004208 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004209 {
4210 MagickPixelPacket
4211 qixel;
4212
4213 MagickRealType
4214 normalize,
4215 radius;
4216
4217 PixelPacket
4218 pixel;
4219
4220 PointInfo
4221 center;
4222
cristybb503372010-05-27 20:51:26 +00004223 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004224 i;
4225
cristybb503372010-05-27 20:51:26 +00004226 size_t
cristy3ed852e2009-09-05 21:47:34 +00004227 step;
4228
4229 center.x=(double) x-blur_center.x;
4230 center.y=(double) y-blur_center.y;
4231 radius=hypot((double) center.x,center.y);
4232 if (radius == 0)
4233 step=1;
4234 else
4235 {
cristybb503372010-05-27 20:51:26 +00004236 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00004237 if (step == 0)
4238 step=1;
4239 else
4240 if (step >= n)
4241 step=n-1;
4242 }
4243 normalize=0.0;
cristyddd82202009-11-03 20:14:50 +00004244 qixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00004245 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
4246 {
cristyeaedf062010-05-29 22:36:02 +00004247 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00004248 {
cristyeaedf062010-05-29 22:36:02 +00004249 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
4250 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
4251 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
4252 cos_theta[i]+0.5),&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004253 qixel.red+=pixel.red;
4254 qixel.green+=pixel.green;
4255 qixel.blue+=pixel.blue;
4256 qixel.opacity+=pixel.opacity;
4257 if (image->colorspace == CMYKColorspace)
4258 {
4259 indexes=GetCacheViewVirtualIndexQueue(image_view);
4260 qixel.index+=(*indexes);
4261 }
4262 normalize+=1.0;
4263 }
4264 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
4265 normalize);
4266 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004267 q->red=ClampToQuantum(normalize*qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00004268 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004269 q->green=ClampToQuantum(normalize*qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00004270 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004271 q->blue=ClampToQuantum(normalize*qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00004272 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004273 q->opacity=ClampToQuantum(normalize*qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00004274 if (((channel & IndexChannel) != 0) &&
4275 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00004276 blur_indexes[x]=(IndexPacket) ClampToQuantum(normalize*qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00004277 }
4278 else
4279 {
4280 MagickRealType
4281 alpha,
4282 gamma;
4283
4284 alpha=1.0;
4285 gamma=0.0;
cristyeaedf062010-05-29 22:36:02 +00004286 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00004287 {
cristyeaedf062010-05-29 22:36:02 +00004288 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
4289 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
4290 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
4291 cos_theta[i]+0.5),&pixel,exception);
cristy46f08202010-01-10 04:04:21 +00004292 alpha=(MagickRealType) (QuantumScale*
4293 GetAlphaPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004294 qixel.red+=alpha*pixel.red;
4295 qixel.green+=alpha*pixel.green;
4296 qixel.blue+=alpha*pixel.blue;
4297 qixel.opacity+=pixel.opacity;
4298 if (image->colorspace == CMYKColorspace)
4299 {
4300 indexes=GetCacheViewVirtualIndexQueue(image_view);
4301 qixel.index+=alpha*(*indexes);
4302 }
4303 gamma+=alpha;
4304 normalize+=1.0;
4305 }
4306 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4307 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
4308 normalize);
4309 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004310 q->red=ClampToQuantum(gamma*qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00004311 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004312 q->green=ClampToQuantum(gamma*qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00004313 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004314 q->blue=ClampToQuantum(gamma*qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00004315 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004316 q->opacity=ClampToQuantum(normalize*qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00004317 if (((channel & IndexChannel) != 0) &&
4318 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00004319 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00004320 }
4321 q++;
4322 }
4323 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
4324 status=MagickFalse;
4325 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4326 {
4327 MagickBooleanType
4328 proceed;
4329
cristyb5d5f722009-11-04 03:03:49 +00004330#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004331 #pragma omp critical (MagickCore_RadialBlurImageChannel)
4332#endif
4333 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
4334 if (proceed == MagickFalse)
4335 status=MagickFalse;
4336 }
4337 }
4338 blur_view=DestroyCacheView(blur_view);
4339 image_view=DestroyCacheView(image_view);
4340 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
4341 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
4342 if (status == MagickFalse)
4343 blur_image=DestroyImage(blur_image);
4344 return(blur_image);
4345}
4346
4347/*
4348%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4349% %
4350% %
4351% %
4352% R e d u c e N o i s e I m a g e %
4353% %
4354% %
4355% %
4356%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4357%
4358% ReduceNoiseImage() smooths the contours of an image while still preserving
4359% edge information. The algorithm works by replacing each pixel with its
4360% neighbor closest in value. A neighbor is defined by radius. Use a radius
4361% of 0 and ReduceNoise() selects a suitable radius for you.
4362%
4363% The format of the ReduceNoiseImage method is:
4364%
4365% Image *ReduceNoiseImage(const Image *image,const double radius,
4366% ExceptionInfo *exception)
4367%
4368% A description of each parameter follows:
4369%
4370% o image: the image.
4371%
4372% o radius: the radius of the pixel neighborhood.
4373%
4374% o exception: return any errors or warnings in this structure.
4375%
4376*/
4377
cristy69ec32d2011-02-27 23:57:09 +00004378static MagickPixelPacket GetNonpeakPixelList(PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00004379{
4380 MagickPixelPacket
4381 pixel;
4382
cristy69ec32d2011-02-27 23:57:09 +00004383 register SkipList
cristy3ed852e2009-09-05 21:47:34 +00004384 *list;
4385
cristy117ff172010-08-15 21:35:32 +00004386 register ssize_t
4387 channel;
4388
cristybb503372010-05-27 20:51:26 +00004389 size_t
cristy3ed852e2009-09-05 21:47:34 +00004390 center,
4391 color,
4392 count,
4393 previous,
4394 next;
4395
4396 unsigned short
4397 channels[5];
4398
4399 /*
4400 Finds the median value for each of the color.
4401 */
4402 center=pixel_list->center;
4403 for (channel=0; channel < 5; channel++)
4404 {
4405 list=pixel_list->lists+channel;
4406 color=65536UL;
4407 next=list->nodes[color].next[0];
4408 count=0;
4409 do
4410 {
4411 previous=color;
4412 color=next;
4413 next=list->nodes[color].next[0];
4414 count+=list->nodes[color].count;
4415 }
4416 while (count <= center);
4417 if ((previous == 65536UL) && (next != 65536UL))
4418 color=next;
4419 else
4420 if ((previous != 65536UL) && (next == 65536UL))
4421 color=previous;
4422 channels[channel]=(unsigned short) color;
4423 }
4424 GetMagickPixelPacket((const Image *) NULL,&pixel);
4425 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4426 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4427 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4428 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
4429 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
4430 return(pixel);
4431}
4432
4433MagickExport Image *ReduceNoiseImage(const Image *image,const double radius,
4434 ExceptionInfo *exception)
4435{
4436#define ReduceNoiseImageTag "ReduceNoise/Image"
4437
cristyfa112112010-01-04 17:48:07 +00004438 CacheView
4439 *image_view,
4440 *noise_view;
4441
cristy3ed852e2009-09-05 21:47:34 +00004442 Image
4443 *noise_image;
4444
cristy3ed852e2009-09-05 21:47:34 +00004445 MagickBooleanType
4446 status;
4447
cristybb503372010-05-27 20:51:26 +00004448 MagickOffsetType
4449 progress;
4450
cristy69ec32d2011-02-27 23:57:09 +00004451 PixelList
cristyfa112112010-01-04 17:48:07 +00004452 **restrict pixel_list;
cristy3ed852e2009-09-05 21:47:34 +00004453
cristybb503372010-05-27 20:51:26 +00004454 size_t
cristy3ed852e2009-09-05 21:47:34 +00004455 width;
4456
cristybb503372010-05-27 20:51:26 +00004457 ssize_t
4458 y;
4459
cristy3ed852e2009-09-05 21:47:34 +00004460 /*
4461 Initialize noise image attributes.
4462 */
4463 assert(image != (Image *) NULL);
4464 assert(image->signature == MagickSignature);
4465 if (image->debug != MagickFalse)
4466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4467 assert(exception != (ExceptionInfo *) NULL);
4468 assert(exception->signature == MagickSignature);
4469 width=GetOptimalKernelWidth2D(radius,0.5);
4470 if ((image->columns < width) || (image->rows < width))
4471 ThrowImageException(OptionError,"ImageSmallerThanKernelRadius");
4472 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4473 exception);
4474 if (noise_image == (Image *) NULL)
4475 return((Image *) NULL);
4476 if (SetImageStorageClass(noise_image,DirectClass) == MagickFalse)
4477 {
4478 InheritException(exception,&noise_image->exception);
4479 noise_image=DestroyImage(noise_image);
4480 return((Image *) NULL);
4481 }
cristy69ec32d2011-02-27 23:57:09 +00004482 pixel_list=AcquirePixelListThreadSet(width);
4483 if (pixel_list == (PixelList **) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004484 {
4485 noise_image=DestroyImage(noise_image);
4486 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4487 }
4488 /*
4489 Reduce noise image.
4490 */
4491 status=MagickTrue;
4492 progress=0;
4493 image_view=AcquireCacheView(image);
4494 noise_view=AcquireCacheView(noise_image);
cristyb5d5f722009-11-04 03:03:49 +00004495#if defined(MAGICKCORE_OPENMP_SUPPORT)
4496 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004497#endif
cristybb503372010-05-27 20:51:26 +00004498 for (y=0; y < (ssize_t) noise_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004499 {
cristy5c9e6f22010-09-17 17:31:01 +00004500 const int
4501 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004502
cristy3ed852e2009-09-05 21:47:34 +00004503 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004504 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00004505
4506 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004507 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004508
4509 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004510 *restrict noise_indexes;
cristy3ed852e2009-09-05 21:47:34 +00004511
cristy3ed852e2009-09-05 21:47:34 +00004512 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004513 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004514
cristy117ff172010-08-15 21:35:32 +00004515 register ssize_t
4516 x;
4517
cristy3ed852e2009-09-05 21:47:34 +00004518 if (status == MagickFalse)
4519 continue;
cristy6ebe97c2010-07-03 01:17:28 +00004520 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
4521 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00004522 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
4523 exception);
4524 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4525 {
4526 status=MagickFalse;
4527 continue;
4528 }
4529 indexes=GetCacheViewVirtualIndexQueue(image_view);
4530 noise_indexes=GetCacheViewAuthenticIndexQueue(noise_view);
cristybb503372010-05-27 20:51:26 +00004531 for (x=0; x < (ssize_t) noise_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004532 {
4533 MagickPixelPacket
4534 pixel;
4535
4536 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004537 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004538
4539 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004540 *restrict s;
cristy3ed852e2009-09-05 21:47:34 +00004541
cristybb503372010-05-27 20:51:26 +00004542 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004543 u,
4544 v;
4545
4546 r=p;
4547 s=indexes+x;
cristy69ec32d2011-02-27 23:57:09 +00004548 ResetPixelList(pixel_list[id]);
cristybb503372010-05-27 20:51:26 +00004549 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004550 {
cristybb503372010-05-27 20:51:26 +00004551 for (u=0; u < (ssize_t) width; u++)
cristy69ec32d2011-02-27 23:57:09 +00004552 InsertPixelList(image,r+u,s+u,pixel_list[id]);
cristy3ed852e2009-09-05 21:47:34 +00004553 r+=image->columns+width;
4554 s+=image->columns+width;
4555 }
cristy69ec32d2011-02-27 23:57:09 +00004556 pixel=GetNonpeakPixelList(pixel_list[id]);
cristy3ed852e2009-09-05 21:47:34 +00004557 SetPixelPacket(noise_image,&pixel,q,noise_indexes+x);
4558 p++;
4559 q++;
4560 }
4561 if (SyncCacheViewAuthenticPixels(noise_view,exception) == MagickFalse)
4562 status=MagickFalse;
4563 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4564 {
4565 MagickBooleanType
4566 proceed;
4567
cristyb5d5f722009-11-04 03:03:49 +00004568#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004569 #pragma omp critical (MagickCore_ReduceNoiseImage)
4570#endif
4571 proceed=SetImageProgress(image,ReduceNoiseImageTag,progress++,
4572 image->rows);
4573 if (proceed == MagickFalse)
4574 status=MagickFalse;
4575 }
4576 }
4577 noise_view=DestroyCacheView(noise_view);
4578 image_view=DestroyCacheView(image_view);
cristy69ec32d2011-02-27 23:57:09 +00004579 pixel_list=DestroyPixelListThreadSet(pixel_list);
cristy3ed852e2009-09-05 21:47:34 +00004580 return(noise_image);
4581}
4582
4583/*
4584%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4585% %
4586% %
4587% %
4588% S e l e c t i v e B l u r I m a g e %
4589% %
4590% %
4591% %
4592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4593%
4594% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
4595% It is similar to the unsharpen mask that sharpens everything with contrast
4596% above a certain threshold.
4597%
4598% The format of the SelectiveBlurImage method is:
4599%
4600% Image *SelectiveBlurImage(const Image *image,const double radius,
4601% const double sigma,const double threshold,ExceptionInfo *exception)
4602% Image *SelectiveBlurImageChannel(const Image *image,
4603% const ChannelType channel,const double radius,const double sigma,
4604% const double threshold,ExceptionInfo *exception)
4605%
4606% A description of each parameter follows:
4607%
4608% o image: the image.
4609%
4610% o channel: the channel type.
4611%
4612% o radius: the radius of the Gaussian, in pixels, not counting the center
4613% pixel.
4614%
4615% o sigma: the standard deviation of the Gaussian, in pixels.
4616%
4617% o threshold: only pixels within this contrast threshold are included
4618% in the blur operation.
4619%
4620% o exception: return any errors or warnings in this structure.
4621%
4622*/
4623
4624static inline MagickBooleanType SelectiveContrast(const PixelPacket *p,
4625 const PixelPacket *q,const double threshold)
4626{
4627 if (fabs(PixelIntensity(p)-PixelIntensity(q)) < threshold)
4628 return(MagickTrue);
4629 return(MagickFalse);
4630}
4631
4632MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
4633 const double sigma,const double threshold,ExceptionInfo *exception)
4634{
4635 Image
4636 *blur_image;
4637
4638 blur_image=SelectiveBlurImageChannel(image,DefaultChannels,radius,sigma,
4639 threshold,exception);
4640 return(blur_image);
4641}
4642
4643MagickExport Image *SelectiveBlurImageChannel(const Image *image,
4644 const ChannelType channel,const double radius,const double sigma,
4645 const double threshold,ExceptionInfo *exception)
4646{
4647#define SelectiveBlurImageTag "SelectiveBlur/Image"
4648
cristy47e00502009-12-17 19:19:57 +00004649 CacheView
4650 *blur_view,
4651 *image_view;
4652
cristy3ed852e2009-09-05 21:47:34 +00004653 double
cristy3ed852e2009-09-05 21:47:34 +00004654 *kernel;
4655
4656 Image
4657 *blur_image;
4658
cristy3ed852e2009-09-05 21:47:34 +00004659 MagickBooleanType
4660 status;
4661
cristybb503372010-05-27 20:51:26 +00004662 MagickOffsetType
4663 progress;
4664
cristy3ed852e2009-09-05 21:47:34 +00004665 MagickPixelPacket
cristy3ed852e2009-09-05 21:47:34 +00004666 bias;
4667
cristybb503372010-05-27 20:51:26 +00004668 register ssize_t
cristy47e00502009-12-17 19:19:57 +00004669 i;
cristy3ed852e2009-09-05 21:47:34 +00004670
cristybb503372010-05-27 20:51:26 +00004671 size_t
cristy3ed852e2009-09-05 21:47:34 +00004672 width;
4673
cristybb503372010-05-27 20:51:26 +00004674 ssize_t
4675 j,
4676 u,
4677 v,
4678 y;
4679
cristy3ed852e2009-09-05 21:47:34 +00004680 /*
4681 Initialize blur image attributes.
4682 */
4683 assert(image != (Image *) NULL);
4684 assert(image->signature == MagickSignature);
4685 if (image->debug != MagickFalse)
4686 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4687 assert(exception != (ExceptionInfo *) NULL);
4688 assert(exception->signature == MagickSignature);
4689 width=GetOptimalKernelWidth1D(radius,sigma);
4690 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
4691 if (kernel == (double *) NULL)
4692 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00004693 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00004694 i=0;
cristy47e00502009-12-17 19:19:57 +00004695 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00004696 {
cristy47e00502009-12-17 19:19:57 +00004697 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00004698 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
4699 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00004700 }
4701 if (image->debug != MagickFalse)
4702 {
4703 char
4704 format[MaxTextExtent],
4705 *message;
4706
cristy117ff172010-08-15 21:35:32 +00004707 register const double
4708 *k;
4709
cristybb503372010-05-27 20:51:26 +00004710 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004711 u,
4712 v;
4713
cristy3ed852e2009-09-05 21:47:34 +00004714 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004715 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
4716 width);
cristy3ed852e2009-09-05 21:47:34 +00004717 message=AcquireString("");
4718 k=kernel;
cristybb503372010-05-27 20:51:26 +00004719 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004720 {
4721 *message='\0';
cristye8c25f92010-06-03 00:53:06 +00004722 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00004723 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00004724 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004725 {
4726 (void) FormatMagickString(format,MaxTextExtent,"%+f ",*k++);
4727 (void) ConcatenateString(&message,format);
4728 }
4729 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
4730 }
4731 message=DestroyString(message);
4732 }
4733 blur_image=CloneImage(image,0,0,MagickTrue,exception);
4734 if (blur_image == (Image *) NULL)
4735 return((Image *) NULL);
4736 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
4737 {
4738 InheritException(exception,&blur_image->exception);
4739 blur_image=DestroyImage(blur_image);
4740 return((Image *) NULL);
4741 }
4742 /*
4743 Threshold blur image.
4744 */
4745 status=MagickTrue;
4746 progress=0;
cristyddd82202009-11-03 20:14:50 +00004747 GetMagickPixelPacket(image,&bias);
4748 SetMagickPixelPacketBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00004749 image_view=AcquireCacheView(image);
4750 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00004751#if defined(MAGICKCORE_OPENMP_SUPPORT)
4752 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004753#endif
cristybb503372010-05-27 20:51:26 +00004754 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004755 {
4756 MagickBooleanType
4757 sync;
4758
4759 MagickRealType
4760 gamma;
4761
4762 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004763 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00004764
4765 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004766 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004767
4768 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004769 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00004770
cristy3ed852e2009-09-05 21:47:34 +00004771 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004772 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004773
cristy117ff172010-08-15 21:35:32 +00004774 register ssize_t
4775 x;
4776
cristy3ed852e2009-09-05 21:47:34 +00004777 if (status == MagickFalse)
4778 continue;
cristy117ff172010-08-15 21:35:32 +00004779 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
4780 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00004781 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
4782 exception);
4783 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4784 {
4785 status=MagickFalse;
4786 continue;
4787 }
4788 indexes=GetCacheViewVirtualIndexQueue(image_view);
4789 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +00004790 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004791 {
cristy3ed852e2009-09-05 21:47:34 +00004792 MagickPixelPacket
4793 pixel;
4794
4795 register const double
cristyc47d1f82009-11-26 01:44:43 +00004796 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00004797
cristybb503372010-05-27 20:51:26 +00004798 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004799 u;
4800
cristy117ff172010-08-15 21:35:32 +00004801 ssize_t
4802 j,
4803 v;
4804
cristyddd82202009-11-03 20:14:50 +00004805 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00004806 k=kernel;
4807 gamma=0.0;
4808 j=0;
4809 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
4810 {
cristybb503372010-05-27 20:51:26 +00004811 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004812 {
cristybb503372010-05-27 20:51:26 +00004813 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004814 {
4815 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4816 {
4817 pixel.red+=(*k)*(p+u+j)->red;
4818 pixel.green+=(*k)*(p+u+j)->green;
4819 pixel.blue+=(*k)*(p+u+j)->blue;
4820 gamma+=(*k);
4821 k++;
4822 }
4823 }
cristyd99b0962010-05-29 23:14:26 +00004824 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004825 }
4826 if (gamma != 0.0)
4827 {
4828 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4829 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004830 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004831 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004832 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004833 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004834 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004835 }
4836 if ((channel & OpacityChannel) != 0)
4837 {
4838 gamma=0.0;
4839 j=0;
cristybb503372010-05-27 20:51:26 +00004840 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004841 {
cristybb503372010-05-27 20:51:26 +00004842 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004843 {
4844 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4845 {
4846 pixel.opacity+=(*k)*(p+u+j)->opacity;
4847 gamma+=(*k);
4848 k++;
4849 }
4850 }
cristyeaedf062010-05-29 22:36:02 +00004851 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004852 }
4853 if (gamma != 0.0)
4854 {
4855 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4856 gamma);
cristyce70c172010-01-07 17:15:30 +00004857 SetOpacityPixelComponent(q,ClampToQuantum(gamma*
4858 GetOpacityPixelComponent(&pixel)));
cristy3ed852e2009-09-05 21:47:34 +00004859 }
4860 }
4861 if (((channel & IndexChannel) != 0) &&
4862 (image->colorspace == CMYKColorspace))
4863 {
4864 gamma=0.0;
4865 j=0;
cristybb503372010-05-27 20:51:26 +00004866 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004867 {
cristybb503372010-05-27 20:51:26 +00004868 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004869 {
4870 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4871 {
4872 pixel.index+=(*k)*indexes[x+u+j];
4873 gamma+=(*k);
4874 k++;
4875 }
4876 }
cristyeaedf062010-05-29 22:36:02 +00004877 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004878 }
4879 if (gamma != 0.0)
4880 {
4881 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4882 gamma);
cristy6db48122010-01-11 00:18:07 +00004883 blur_indexes[x]=ClampToQuantum(gamma*
4884 GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004885 }
4886 }
4887 }
4888 else
4889 {
4890 MagickRealType
4891 alpha;
4892
cristybb503372010-05-27 20:51:26 +00004893 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004894 {
cristybb503372010-05-27 20:51:26 +00004895 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004896 {
4897 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4898 {
cristy46f08202010-01-10 04:04:21 +00004899 alpha=(MagickRealType) (QuantumScale*
4900 GetAlphaPixelComponent(p+u+j));
cristy3ed852e2009-09-05 21:47:34 +00004901 pixel.red+=(*k)*alpha*(p+u+j)->red;
4902 pixel.green+=(*k)*alpha*(p+u+j)->green;
4903 pixel.blue+=(*k)*alpha*(p+u+j)->blue;
4904 pixel.opacity+=(*k)*(p+u+j)->opacity;
4905 gamma+=(*k)*alpha;
4906 k++;
4907 }
4908 }
cristyeaedf062010-05-29 22:36:02 +00004909 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004910 }
4911 if (gamma != 0.0)
4912 {
4913 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4914 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004915 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004916 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004917 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004918 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004919 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004920 }
4921 if ((channel & OpacityChannel) != 0)
4922 {
4923 gamma=0.0;
4924 j=0;
cristybb503372010-05-27 20:51:26 +00004925 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004926 {
cristybb503372010-05-27 20:51:26 +00004927 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004928 {
4929 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4930 {
4931 pixel.opacity+=(*k)*(p+u+j)->opacity;
4932 gamma+=(*k);
4933 k++;
4934 }
4935 }
cristyeaedf062010-05-29 22:36:02 +00004936 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004937 }
4938 if (gamma != 0.0)
4939 {
4940 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4941 gamma);
cristy6db48122010-01-11 00:18:07 +00004942 SetOpacityPixelComponent(q,
4943 ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004944 }
4945 }
4946 if (((channel & IndexChannel) != 0) &&
4947 (image->colorspace == CMYKColorspace))
4948 {
4949 gamma=0.0;
4950 j=0;
cristybb503372010-05-27 20:51:26 +00004951 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004952 {
cristybb503372010-05-27 20:51:26 +00004953 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004954 {
4955 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4956 {
cristy46f08202010-01-10 04:04:21 +00004957 alpha=(MagickRealType) (QuantumScale*
4958 GetAlphaPixelComponent(p+u+j));
cristy3ed852e2009-09-05 21:47:34 +00004959 pixel.index+=(*k)*alpha*indexes[x+u+j];
4960 gamma+=(*k);
4961 k++;
4962 }
4963 }
cristyeaedf062010-05-29 22:36:02 +00004964 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004965 }
4966 if (gamma != 0.0)
4967 {
4968 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4969 gamma);
cristy6db48122010-01-11 00:18:07 +00004970 blur_indexes[x]=ClampToQuantum(gamma*
4971 GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004972 }
4973 }
4974 }
4975 p++;
4976 q++;
4977 }
4978 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
4979 if (sync == MagickFalse)
4980 status=MagickFalse;
4981 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4982 {
4983 MagickBooleanType
4984 proceed;
4985
cristyb5d5f722009-11-04 03:03:49 +00004986#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004987 #pragma omp critical (MagickCore_SelectiveBlurImageChannel)
4988#endif
4989 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
4990 image->rows);
4991 if (proceed == MagickFalse)
4992 status=MagickFalse;
4993 }
4994 }
4995 blur_image->type=image->type;
4996 blur_view=DestroyCacheView(blur_view);
4997 image_view=DestroyCacheView(image_view);
4998 kernel=(double *) RelinquishMagickMemory(kernel);
4999 if (status == MagickFalse)
5000 blur_image=DestroyImage(blur_image);
5001 return(blur_image);
5002}
5003
5004/*
5005%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5006% %
5007% %
5008% %
5009% S h a d e I m a g e %
5010% %
5011% %
5012% %
5013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5014%
5015% ShadeImage() shines a distant light on an image to create a
5016% three-dimensional effect. You control the positioning of the light with
5017% azimuth and elevation; azimuth is measured in degrees off the x axis
5018% and elevation is measured in pixels above the Z axis.
5019%
5020% The format of the ShadeImage method is:
5021%
5022% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
5023% const double azimuth,const double elevation,ExceptionInfo *exception)
5024%
5025% A description of each parameter follows:
5026%
5027% o image: the image.
5028%
5029% o gray: A value other than zero shades the intensity of each pixel.
5030%
5031% o azimuth, elevation: Define the light source direction.
5032%
5033% o exception: return any errors or warnings in this structure.
5034%
5035*/
5036MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
5037 const double azimuth,const double elevation,ExceptionInfo *exception)
5038{
5039#define ShadeImageTag "Shade/Image"
5040
cristyc4c8d132010-01-07 01:58:38 +00005041 CacheView
5042 *image_view,
5043 *shade_view;
5044
cristy3ed852e2009-09-05 21:47:34 +00005045 Image
5046 *shade_image;
5047
cristy3ed852e2009-09-05 21:47:34 +00005048 MagickBooleanType
5049 status;
5050
cristybb503372010-05-27 20:51:26 +00005051 MagickOffsetType
5052 progress;
5053
cristy3ed852e2009-09-05 21:47:34 +00005054 PrimaryInfo
5055 light;
5056
cristybb503372010-05-27 20:51:26 +00005057 ssize_t
5058 y;
5059
cristy3ed852e2009-09-05 21:47:34 +00005060 /*
5061 Initialize shaded image attributes.
5062 */
5063 assert(image != (const Image *) NULL);
5064 assert(image->signature == MagickSignature);
5065 if (image->debug != MagickFalse)
5066 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5067 assert(exception != (ExceptionInfo *) NULL);
5068 assert(exception->signature == MagickSignature);
5069 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5070 if (shade_image == (Image *) NULL)
5071 return((Image *) NULL);
5072 if (SetImageStorageClass(shade_image,DirectClass) == MagickFalse)
5073 {
5074 InheritException(exception,&shade_image->exception);
5075 shade_image=DestroyImage(shade_image);
5076 return((Image *) NULL);
5077 }
5078 /*
5079 Compute the light vector.
5080 */
5081 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
5082 cos(DegreesToRadians(elevation));
5083 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
5084 cos(DegreesToRadians(elevation));
5085 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
5086 /*
5087 Shade image.
5088 */
5089 status=MagickTrue;
5090 progress=0;
5091 image_view=AcquireCacheView(image);
5092 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00005093#if defined(MAGICKCORE_OPENMP_SUPPORT)
5094 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005095#endif
cristybb503372010-05-27 20:51:26 +00005096 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005097 {
5098 MagickRealType
5099 distance,
5100 normal_distance,
5101 shade;
5102
5103 PrimaryInfo
5104 normal;
5105
5106 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005107 *restrict p,
5108 *restrict s0,
5109 *restrict s1,
5110 *restrict s2;
cristy3ed852e2009-09-05 21:47:34 +00005111
cristy3ed852e2009-09-05 21:47:34 +00005112 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005113 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005114
cristy117ff172010-08-15 21:35:32 +00005115 register ssize_t
5116 x;
5117
cristy3ed852e2009-09-05 21:47:34 +00005118 if (status == MagickFalse)
5119 continue;
5120 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
5121 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
5122 exception);
5123 if ((p == (PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
5124 {
5125 status=MagickFalse;
5126 continue;
5127 }
5128 /*
5129 Shade this row of pixels.
5130 */
5131 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
5132 s0=p+1;
5133 s1=s0+image->columns+2;
5134 s2=s1+image->columns+2;
cristybb503372010-05-27 20:51:26 +00005135 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005136 {
5137 /*
5138 Determine the surface normal and compute shading.
5139 */
5140 normal.x=(double) (PixelIntensity(s0-1)+PixelIntensity(s1-1)+
5141 PixelIntensity(s2-1)-PixelIntensity(s0+1)-PixelIntensity(s1+1)-
5142 PixelIntensity(s2+1));
5143 normal.y=(double) (PixelIntensity(s2-1)+PixelIntensity(s2)+
5144 PixelIntensity(s2+1)-PixelIntensity(s0-1)-PixelIntensity(s0)-
5145 PixelIntensity(s0+1));
5146 if ((normal.x == 0.0) && (normal.y == 0.0))
5147 shade=light.z;
5148 else
5149 {
5150 shade=0.0;
5151 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
5152 if (distance > MagickEpsilon)
5153 {
5154 normal_distance=
5155 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
5156 if (normal_distance > (MagickEpsilon*MagickEpsilon))
5157 shade=distance/sqrt((double) normal_distance);
5158 }
5159 }
5160 if (gray != MagickFalse)
5161 {
5162 q->red=(Quantum) shade;
5163 q->green=(Quantum) shade;
5164 q->blue=(Quantum) shade;
5165 }
5166 else
5167 {
cristyce70c172010-01-07 17:15:30 +00005168 q->red=ClampToQuantum(QuantumScale*shade*s1->red);
5169 q->green=ClampToQuantum(QuantumScale*shade*s1->green);
5170 q->blue=ClampToQuantum(QuantumScale*shade*s1->blue);
cristy3ed852e2009-09-05 21:47:34 +00005171 }
5172 q->opacity=s1->opacity;
5173 s0++;
5174 s1++;
5175 s2++;
5176 q++;
5177 }
5178 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
5179 status=MagickFalse;
5180 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5181 {
5182 MagickBooleanType
5183 proceed;
5184
cristyb5d5f722009-11-04 03:03:49 +00005185#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005186 #pragma omp critical (MagickCore_ShadeImage)
5187#endif
5188 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
5189 if (proceed == MagickFalse)
5190 status=MagickFalse;
5191 }
5192 }
5193 shade_view=DestroyCacheView(shade_view);
5194 image_view=DestroyCacheView(image_view);
5195 if (status == MagickFalse)
5196 shade_image=DestroyImage(shade_image);
5197 return(shade_image);
5198}
5199
5200/*
5201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5202% %
5203% %
5204% %
5205% S h a r p e n I m a g e %
5206% %
5207% %
5208% %
5209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5210%
5211% SharpenImage() sharpens the image. We convolve the image with a Gaussian
5212% operator of the given radius and standard deviation (sigma). For
5213% reasonable results, radius should be larger than sigma. Use a radius of 0
5214% and SharpenImage() selects a suitable radius for you.
5215%
5216% Using a separable kernel would be faster, but the negative weights cancel
5217% out on the corners of the kernel producing often undesirable ringing in the
5218% filtered result; this can be avoided by using a 2D gaussian shaped image
5219% sharpening kernel instead.
5220%
5221% The format of the SharpenImage method is:
5222%
5223% Image *SharpenImage(const Image *image,const double radius,
5224% const double sigma,ExceptionInfo *exception)
5225% Image *SharpenImageChannel(const Image *image,const ChannelType channel,
5226% const double radius,const double sigma,ExceptionInfo *exception)
5227%
5228% A description of each parameter follows:
5229%
5230% o image: the image.
5231%
5232% o channel: the channel type.
5233%
5234% o radius: the radius of the Gaussian, in pixels, not counting the center
5235% pixel.
5236%
5237% o sigma: the standard deviation of the Laplacian, in pixels.
5238%
5239% o exception: return any errors or warnings in this structure.
5240%
5241*/
5242
5243MagickExport Image *SharpenImage(const Image *image,const double radius,
5244 const double sigma,ExceptionInfo *exception)
5245{
5246 Image
5247 *sharp_image;
5248
5249 sharp_image=SharpenImageChannel(image,DefaultChannels,radius,sigma,exception);
5250 return(sharp_image);
5251}
5252
5253MagickExport Image *SharpenImageChannel(const Image *image,
5254 const ChannelType channel,const double radius,const double sigma,
5255 ExceptionInfo *exception)
5256{
5257 double
cristy47e00502009-12-17 19:19:57 +00005258 *kernel,
5259 normalize;
cristy3ed852e2009-09-05 21:47:34 +00005260
5261 Image
5262 *sharp_image;
5263
cristybb503372010-05-27 20:51:26 +00005264 register ssize_t
cristy47e00502009-12-17 19:19:57 +00005265 i;
5266
cristybb503372010-05-27 20:51:26 +00005267 size_t
cristy3ed852e2009-09-05 21:47:34 +00005268 width;
5269
cristy117ff172010-08-15 21:35:32 +00005270 ssize_t
5271 j,
5272 u,
5273 v;
5274
cristy3ed852e2009-09-05 21:47:34 +00005275 assert(image != (const Image *) NULL);
5276 assert(image->signature == MagickSignature);
5277 if (image->debug != MagickFalse)
5278 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5279 assert(exception != (ExceptionInfo *) NULL);
5280 assert(exception->signature == MagickSignature);
5281 width=GetOptimalKernelWidth2D(radius,sigma);
5282 kernel=(double *) AcquireQuantumMemory((size_t) width*width,sizeof(*kernel));
5283 if (kernel == (double *) NULL)
5284 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00005285 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00005286 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +00005287 i=0;
5288 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00005289 {
cristy47e00502009-12-17 19:19:57 +00005290 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00005291 {
cristy4205a3c2010-09-12 20:19:59 +00005292 kernel[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
5293 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00005294 normalize+=kernel[i];
5295 i++;
5296 }
5297 }
5298 kernel[i/2]=(double) ((-2.0)*normalize);
5299 sharp_image=ConvolveImageChannel(image,channel,width,kernel,exception);
5300 kernel=(double *) RelinquishMagickMemory(kernel);
5301 return(sharp_image);
5302}
5303
5304/*
5305%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5306% %
5307% %
5308% %
5309% S p r e a d I m a g e %
5310% %
5311% %
5312% %
5313%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5314%
5315% SpreadImage() is a special effects method that randomly displaces each
5316% pixel in a block defined by the radius parameter.
5317%
5318% The format of the SpreadImage method is:
5319%
5320% Image *SpreadImage(const Image *image,const double radius,
5321% ExceptionInfo *exception)
5322%
5323% A description of each parameter follows:
5324%
5325% o image: the image.
5326%
5327% o radius: Choose a random pixel in a neighborhood of this extent.
5328%
5329% o exception: return any errors or warnings in this structure.
5330%
5331*/
5332MagickExport Image *SpreadImage(const Image *image,const double radius,
5333 ExceptionInfo *exception)
5334{
5335#define SpreadImageTag "Spread/Image"
5336
cristyfa112112010-01-04 17:48:07 +00005337 CacheView
5338 *image_view;
5339
cristy3ed852e2009-09-05 21:47:34 +00005340 Image
5341 *spread_image;
5342
cristy3ed852e2009-09-05 21:47:34 +00005343 MagickBooleanType
5344 status;
5345
cristybb503372010-05-27 20:51:26 +00005346 MagickOffsetType
5347 progress;
5348
cristy3ed852e2009-09-05 21:47:34 +00005349 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00005350 bias;
cristy3ed852e2009-09-05 21:47:34 +00005351
5352 RandomInfo
cristyfa112112010-01-04 17:48:07 +00005353 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00005354
5355 ResampleFilter
cristyfa112112010-01-04 17:48:07 +00005356 **restrict resample_filter;
cristy3ed852e2009-09-05 21:47:34 +00005357
cristybb503372010-05-27 20:51:26 +00005358 size_t
cristy3ed852e2009-09-05 21:47:34 +00005359 width;
5360
cristybb503372010-05-27 20:51:26 +00005361 ssize_t
5362 y;
5363
cristy3ed852e2009-09-05 21:47:34 +00005364 /*
5365 Initialize spread image attributes.
5366 */
5367 assert(image != (Image *) NULL);
5368 assert(image->signature == MagickSignature);
5369 if (image->debug != MagickFalse)
5370 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5371 assert(exception != (ExceptionInfo *) NULL);
5372 assert(exception->signature == MagickSignature);
5373 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
5374 exception);
5375 if (spread_image == (Image *) NULL)
5376 return((Image *) NULL);
5377 if (SetImageStorageClass(spread_image,DirectClass) == MagickFalse)
5378 {
5379 InheritException(exception,&spread_image->exception);
5380 spread_image=DestroyImage(spread_image);
5381 return((Image *) NULL);
5382 }
5383 /*
5384 Spread image.
5385 */
5386 status=MagickTrue;
5387 progress=0;
cristyddd82202009-11-03 20:14:50 +00005388 GetMagickPixelPacket(spread_image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00005389 width=GetOptimalKernelWidth1D(radius,0.5);
cristyb2a11ae2010-02-22 00:53:36 +00005390 resample_filter=AcquireResampleFilterThreadSet(image,
5391 UndefinedVirtualPixelMethod,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005392 random_info=AcquireRandomInfoThreadSet();
5393 image_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00005394#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00005395 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00005396#endif
cristybb503372010-05-27 20:51:26 +00005397 for (y=0; y < (ssize_t) spread_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005398 {
cristy5c9e6f22010-09-17 17:31:01 +00005399 const int
5400 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00005401
cristy3ed852e2009-09-05 21:47:34 +00005402 MagickPixelPacket
5403 pixel;
5404
5405 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005406 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00005407
cristy3ed852e2009-09-05 21:47:34 +00005408 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005409 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005410
cristy117ff172010-08-15 21:35:32 +00005411 register ssize_t
5412 x;
5413
cristy3ed852e2009-09-05 21:47:34 +00005414 if (status == MagickFalse)
5415 continue;
5416 q=QueueCacheViewAuthenticPixels(image_view,0,y,spread_image->columns,1,
5417 exception);
5418 if (q == (PixelPacket *) NULL)
5419 {
5420 status=MagickFalse;
5421 continue;
5422 }
5423 indexes=GetCacheViewAuthenticIndexQueue(image_view);
cristyddd82202009-11-03 20:14:50 +00005424 pixel=bias;
cristybb503372010-05-27 20:51:26 +00005425 for (x=0; x < (ssize_t) spread_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005426 {
5427 (void) ResamplePixelColor(resample_filter[id],(double) x+width*
5428 (GetPseudoRandomValue(random_info[id])-0.5),(double) y+width*
5429 (GetPseudoRandomValue(random_info[id])-0.5),&pixel);
5430 SetPixelPacket(spread_image,&pixel,q,indexes+x);
5431 q++;
5432 }
5433 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5434 status=MagickFalse;
5435 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5436 {
5437 MagickBooleanType
5438 proceed;
5439
cristyb557a152011-02-22 12:14:30 +00005440#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005441 #pragma omp critical (MagickCore_SpreadImage)
5442#endif
5443 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
5444 if (proceed == MagickFalse)
5445 status=MagickFalse;
5446 }
5447 }
5448 image_view=DestroyCacheView(image_view);
5449 random_info=DestroyRandomInfoThreadSet(random_info);
5450 resample_filter=DestroyResampleFilterThreadSet(resample_filter);
5451 return(spread_image);
5452}
5453
5454/*
5455%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5456% %
5457% %
5458% %
5459% U n s h a r p M a s k I m a g e %
5460% %
5461% %
5462% %
5463%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5464%
5465% UnsharpMaskImage() sharpens one or more image channels. We convolve the
5466% image with a Gaussian operator of the given radius and standard deviation
5467% (sigma). For reasonable results, radius should be larger than sigma. Use a
5468% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
5469%
5470% The format of the UnsharpMaskImage method is:
5471%
5472% Image *UnsharpMaskImage(const Image *image,const double radius,
5473% const double sigma,const double amount,const double threshold,
5474% ExceptionInfo *exception)
5475% Image *UnsharpMaskImageChannel(const Image *image,
5476% const ChannelType channel,const double radius,const double sigma,
5477% const double amount,const double threshold,ExceptionInfo *exception)
5478%
5479% A description of each parameter follows:
5480%
5481% o image: the image.
5482%
5483% o channel: the channel type.
5484%
5485% o radius: the radius of the Gaussian, in pixels, not counting the center
5486% pixel.
5487%
5488% o sigma: the standard deviation of the Gaussian, in pixels.
5489%
5490% o amount: the percentage of the difference between the original and the
5491% blur image that is added back into the original.
5492%
5493% o threshold: the threshold in pixels needed to apply the diffence amount.
5494%
5495% o exception: return any errors or warnings in this structure.
5496%
5497*/
5498
5499MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
5500 const double sigma,const double amount,const double threshold,
5501 ExceptionInfo *exception)
5502{
5503 Image
5504 *sharp_image;
5505
5506 sharp_image=UnsharpMaskImageChannel(image,DefaultChannels,radius,sigma,amount,
5507 threshold,exception);
5508 return(sharp_image);
5509}
5510
5511MagickExport Image *UnsharpMaskImageChannel(const Image *image,
5512 const ChannelType channel,const double radius,const double sigma,
5513 const double amount,const double threshold,ExceptionInfo *exception)
5514{
5515#define SharpenImageTag "Sharpen/Image"
5516
cristyc4c8d132010-01-07 01:58:38 +00005517 CacheView
5518 *image_view,
5519 *unsharp_view;
5520
cristy3ed852e2009-09-05 21:47:34 +00005521 Image
5522 *unsharp_image;
5523
cristy3ed852e2009-09-05 21:47:34 +00005524 MagickBooleanType
5525 status;
5526
cristybb503372010-05-27 20:51:26 +00005527 MagickOffsetType
5528 progress;
5529
cristy3ed852e2009-09-05 21:47:34 +00005530 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00005531 bias;
cristy3ed852e2009-09-05 21:47:34 +00005532
5533 MagickRealType
5534 quantum_threshold;
5535
cristybb503372010-05-27 20:51:26 +00005536 ssize_t
5537 y;
5538
cristy3ed852e2009-09-05 21:47:34 +00005539 assert(image != (const Image *) NULL);
5540 assert(image->signature == MagickSignature);
5541 if (image->debug != MagickFalse)
5542 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5543 assert(exception != (ExceptionInfo *) NULL);
5544 unsharp_image=BlurImageChannel(image,channel,radius,sigma,exception);
5545 if (unsharp_image == (Image *) NULL)
5546 return((Image *) NULL);
5547 quantum_threshold=(MagickRealType) QuantumRange*threshold;
5548 /*
5549 Unsharp-mask image.
5550 */
5551 status=MagickTrue;
5552 progress=0;
cristyddd82202009-11-03 20:14:50 +00005553 GetMagickPixelPacket(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00005554 image_view=AcquireCacheView(image);
5555 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00005556#if defined(MAGICKCORE_OPENMP_SUPPORT)
5557 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005558#endif
cristybb503372010-05-27 20:51:26 +00005559 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005560 {
5561 MagickPixelPacket
5562 pixel;
5563
5564 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005565 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00005566
5567 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005568 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005569
5570 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005571 *restrict unsharp_indexes;
cristy3ed852e2009-09-05 21:47:34 +00005572
cristy3ed852e2009-09-05 21:47:34 +00005573 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005574 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005575
cristy117ff172010-08-15 21:35:32 +00005576 register ssize_t
5577 x;
5578
cristy3ed852e2009-09-05 21:47:34 +00005579 if (status == MagickFalse)
5580 continue;
5581 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5582 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
5583 exception);
5584 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
5585 {
5586 status=MagickFalse;
5587 continue;
5588 }
5589 indexes=GetCacheViewVirtualIndexQueue(image_view);
5590 unsharp_indexes=GetCacheViewAuthenticIndexQueue(unsharp_view);
cristyddd82202009-11-03 20:14:50 +00005591 pixel=bias;
cristybb503372010-05-27 20:51:26 +00005592 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005593 {
5594 if ((channel & RedChannel) != 0)
5595 {
5596 pixel.red=p->red-(MagickRealType) q->red;
5597 if (fabs(2.0*pixel.red) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005598 pixel.red=(MagickRealType) GetRedPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005599 else
5600 pixel.red=(MagickRealType) p->red+(pixel.red*amount);
cristyce70c172010-01-07 17:15:30 +00005601 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005602 }
5603 if ((channel & GreenChannel) != 0)
5604 {
5605 pixel.green=p->green-(MagickRealType) q->green;
5606 if (fabs(2.0*pixel.green) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005607 pixel.green=(MagickRealType) GetGreenPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005608 else
5609 pixel.green=(MagickRealType) p->green+(pixel.green*amount);
cristyce70c172010-01-07 17:15:30 +00005610 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005611 }
5612 if ((channel & BlueChannel) != 0)
5613 {
5614 pixel.blue=p->blue-(MagickRealType) q->blue;
5615 if (fabs(2.0*pixel.blue) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005616 pixel.blue=(MagickRealType) GetBluePixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005617 else
5618 pixel.blue=(MagickRealType) p->blue+(pixel.blue*amount);
cristyce70c172010-01-07 17:15:30 +00005619 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005620 }
5621 if ((channel & OpacityChannel) != 0)
5622 {
5623 pixel.opacity=p->opacity-(MagickRealType) q->opacity;
5624 if (fabs(2.0*pixel.opacity) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005625 pixel.opacity=(MagickRealType) GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005626 else
5627 pixel.opacity=p->opacity+(pixel.opacity*amount);
cristyce70c172010-01-07 17:15:30 +00005628 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005629 }
5630 if (((channel & IndexChannel) != 0) &&
5631 (image->colorspace == CMYKColorspace))
5632 {
5633 pixel.index=unsharp_indexes[x]-(MagickRealType) indexes[x];
5634 if (fabs(2.0*pixel.index) < quantum_threshold)
cristyb557a152011-02-22 12:14:30 +00005635 pixel.index=(MagickRealType) indexes[x];
cristy3ed852e2009-09-05 21:47:34 +00005636 else
cristyb557a152011-02-22 12:14:30 +00005637 pixel.index=(MagickRealType) indexes[x]+(pixel.index*amount);
cristyce70c172010-01-07 17:15:30 +00005638 unsharp_indexes[x]=ClampToQuantum(pixel.index);
cristy3ed852e2009-09-05 21:47:34 +00005639 }
5640 p++;
5641 q++;
5642 }
5643 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
5644 status=MagickFalse;
5645 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5646 {
5647 MagickBooleanType
5648 proceed;
5649
cristyb5d5f722009-11-04 03:03:49 +00005650#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005651 #pragma omp critical (MagickCore_UnsharpMaskImageChannel)
5652#endif
5653 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
5654 if (proceed == MagickFalse)
5655 status=MagickFalse;
5656 }
5657 }
5658 unsharp_image->type=image->type;
5659 unsharp_view=DestroyCacheView(unsharp_view);
5660 image_view=DestroyCacheView(image_view);
5661 if (status == MagickFalse)
5662 unsharp_image=DestroyImage(unsharp_image);
5663 return(unsharp_image);
5664}