blob: 9bc681d2e3ce59be0bd9ca057b305b468f58a440 [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;
cristy9d314ff2011-03-09 01:30:28 +0000989 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;
cristy9d314ff2011-03-09 01:30:28 +00001044 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 }
cristy9d314ff2011-03-09 01:30:28 +00001058 indexes++;
cristy3ed852e2009-09-05 21:47:34 +00001059 p++;
1060 q++;
1061 }
1062 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1063 status=MagickFalse;
1064 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1065 {
1066 MagickBooleanType
1067 proceed;
1068
cristyb5d5f722009-11-04 03:03:49 +00001069#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001070 #pragma omp critical (MagickCore_BlurImageChannel)
1071#endif
1072 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1073 blur_image->columns);
1074 if (proceed == MagickFalse)
1075 status=MagickFalse;
1076 }
1077 }
1078 blur_view=DestroyCacheView(blur_view);
1079 image_view=DestroyCacheView(image_view);
1080 /*
1081 Blur columns.
1082 */
1083 image_view=AcquireCacheView(blur_image);
1084 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001085#if defined(MAGICKCORE_OPENMP_SUPPORT)
1086 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001087#endif
cristybb503372010-05-27 20:51:26 +00001088 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001089 {
1090 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001091 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001092
1093 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001094 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001095
1096 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001097 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001098
cristy3ed852e2009-09-05 21:47:34 +00001099 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001100 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001101
cristy117ff172010-08-15 21:35:32 +00001102 register ssize_t
1103 y;
1104
cristy3ed852e2009-09-05 21:47:34 +00001105 if (status == MagickFalse)
1106 continue;
cristy117ff172010-08-15 21:35:32 +00001107 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1108 image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001109 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1110 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1111 {
1112 status=MagickFalse;
1113 continue;
1114 }
1115 indexes=GetCacheViewVirtualIndexQueue(image_view);
1116 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +00001117 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001118 {
1119 MagickPixelPacket
1120 pixel;
1121
1122 register const double
cristyc47d1f82009-11-26 01:44:43 +00001123 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00001124
1125 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001126 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001127
cristybb503372010-05-27 20:51:26 +00001128 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001129 i;
1130
cristyddd82202009-11-03 20:14:50 +00001131 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00001132 k=kernel;
1133 kernel_pixels=p;
1134 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1135 {
cristybb503372010-05-27 20:51:26 +00001136 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001137 {
1138 pixel.red+=(*k)*kernel_pixels->red;
1139 pixel.green+=(*k)*kernel_pixels->green;
1140 pixel.blue+=(*k)*kernel_pixels->blue;
1141 k++;
1142 kernel_pixels++;
1143 }
1144 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001145 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001146 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001147 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001148 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001149 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001150 if ((channel & OpacityChannel) != 0)
1151 {
1152 k=kernel;
1153 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001154 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001155 {
1156 pixel.opacity+=(*k)*kernel_pixels->opacity;
1157 k++;
1158 kernel_pixels++;
1159 }
cristyce70c172010-01-07 17:15:30 +00001160 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001161 }
1162 if (((channel & IndexChannel) != 0) &&
1163 (image->colorspace == CMYKColorspace))
1164 {
1165 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001166 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001167
1168 k=kernel;
cristy9d314ff2011-03-09 01:30:28 +00001169 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00001170 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001171 {
1172 pixel.index+=(*k)*(*kernel_indexes);
1173 k++;
1174 kernel_indexes++;
1175 }
cristyce70c172010-01-07 17:15:30 +00001176 blur_indexes[y]=ClampToQuantum(pixel.index);
cristy3ed852e2009-09-05 21:47:34 +00001177 }
1178 }
1179 else
1180 {
1181 MagickRealType
1182 alpha,
1183 gamma;
1184
1185 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001186 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001187 {
cristy46f08202010-01-10 04:04:21 +00001188 alpha=(MagickRealType) (QuantumScale*
1189 GetAlphaPixelComponent(kernel_pixels));
cristy3ed852e2009-09-05 21:47:34 +00001190 pixel.red+=(*k)*alpha*kernel_pixels->red;
1191 pixel.green+=(*k)*alpha*kernel_pixels->green;
1192 pixel.blue+=(*k)*alpha*kernel_pixels->blue;
1193 gamma+=(*k)*alpha;
1194 k++;
1195 kernel_pixels++;
1196 }
1197 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1198 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001199 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001200 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001201 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001202 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001203 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001204 if ((channel & OpacityChannel) != 0)
1205 {
1206 k=kernel;
1207 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001208 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001209 {
1210 pixel.opacity+=(*k)*kernel_pixels->opacity;
1211 k++;
1212 kernel_pixels++;
1213 }
cristyce70c172010-01-07 17:15:30 +00001214 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001215 }
1216 if (((channel & IndexChannel) != 0) &&
1217 (image->colorspace == CMYKColorspace))
1218 {
1219 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001220 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001221
1222 k=kernel;
1223 kernel_pixels=p;
cristy9d314ff2011-03-09 01:30:28 +00001224 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00001225 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001226 {
cristy46f08202010-01-10 04:04:21 +00001227 alpha=(MagickRealType) (QuantumScale*
1228 GetAlphaPixelComponent(kernel_pixels));
cristy3ed852e2009-09-05 21:47:34 +00001229 pixel.index+=(*k)*alpha*(*kernel_indexes);
1230 k++;
1231 kernel_pixels++;
1232 kernel_indexes++;
1233 }
cristy46f08202010-01-10 04:04:21 +00001234 blur_indexes[y]=ClampToQuantum(gamma*
1235 GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001236 }
1237 }
cristy9d314ff2011-03-09 01:30:28 +00001238 indexes++;
cristy3ed852e2009-09-05 21:47:34 +00001239 p++;
1240 q++;
1241 }
1242 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1243 status=MagickFalse;
1244 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1245 {
1246 MagickBooleanType
1247 proceed;
1248
cristyb5d5f722009-11-04 03:03:49 +00001249#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001250 #pragma omp critical (MagickCore_BlurImageChannel)
1251#endif
1252 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1253 blur_image->columns);
1254 if (proceed == MagickFalse)
1255 status=MagickFalse;
1256 }
1257 }
1258 blur_view=DestroyCacheView(blur_view);
1259 image_view=DestroyCacheView(image_view);
1260 kernel=(double *) RelinquishMagickMemory(kernel);
1261 if (status == MagickFalse)
1262 blur_image=DestroyImage(blur_image);
1263 blur_image->type=image->type;
1264 return(blur_image);
1265}
1266
1267/*
1268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1269% %
1270% %
1271% %
cristyfccdab92009-11-30 16:43:57 +00001272% C o n v o l v e I m a g e %
1273% %
1274% %
1275% %
1276%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1277%
1278% ConvolveImage() applies a custom convolution kernel to the image.
1279%
1280% The format of the ConvolveImage method is:
1281%
cristybb503372010-05-27 20:51:26 +00001282% Image *ConvolveImage(const Image *image,const size_t order,
cristyfccdab92009-11-30 16:43:57 +00001283% const double *kernel,ExceptionInfo *exception)
1284% Image *ConvolveImageChannel(const Image *image,const ChannelType channel,
cristy117ff172010-08-15 21:35:32 +00001285% const size_t order,const double *kernel,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001286%
1287% A description of each parameter follows:
1288%
1289% o image: the image.
1290%
1291% o channel: the channel type.
1292%
1293% o order: the number of columns and rows in the filter kernel.
1294%
1295% o kernel: An array of double representing the convolution kernel.
1296%
1297% o exception: return any errors or warnings in this structure.
1298%
1299*/
1300
cristybb503372010-05-27 20:51:26 +00001301MagickExport Image *ConvolveImage(const Image *image,const size_t order,
cristyfccdab92009-11-30 16:43:57 +00001302 const double *kernel,ExceptionInfo *exception)
1303{
1304 Image
1305 *convolve_image;
1306
1307 convolve_image=ConvolveImageChannel(image,DefaultChannels,order,kernel,
1308 exception);
1309 return(convolve_image);
1310}
1311
1312MagickExport Image *ConvolveImageChannel(const Image *image,
cristybb503372010-05-27 20:51:26 +00001313 const ChannelType channel,const size_t order,const double *kernel,
cristyfccdab92009-11-30 16:43:57 +00001314 ExceptionInfo *exception)
1315{
1316#define ConvolveImageTag "Convolve/Image"
1317
cristyc4c8d132010-01-07 01:58:38 +00001318 CacheView
1319 *convolve_view,
1320 *image_view;
1321
cristyfccdab92009-11-30 16:43:57 +00001322 double
1323 *normal_kernel;
1324
1325 Image
1326 *convolve_image;
1327
cristyfccdab92009-11-30 16:43:57 +00001328 MagickBooleanType
1329 status;
1330
cristybb503372010-05-27 20:51:26 +00001331 MagickOffsetType
1332 progress;
1333
cristyfccdab92009-11-30 16:43:57 +00001334 MagickPixelPacket
1335 bias;
1336
1337 MagickRealType
1338 gamma;
1339
cristybb503372010-05-27 20:51:26 +00001340 register ssize_t
cristyfccdab92009-11-30 16:43:57 +00001341 i;
1342
cristybb503372010-05-27 20:51:26 +00001343 size_t
cristyfccdab92009-11-30 16:43:57 +00001344 width;
1345
cristybb503372010-05-27 20:51:26 +00001346 ssize_t
1347 y;
1348
cristyfccdab92009-11-30 16:43:57 +00001349 /*
1350 Initialize convolve image attributes.
1351 */
1352 assert(image != (Image *) NULL);
1353 assert(image->signature == MagickSignature);
1354 if (image->debug != MagickFalse)
1355 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1356 assert(exception != (ExceptionInfo *) NULL);
1357 assert(exception->signature == MagickSignature);
1358 width=order;
1359 if ((width % 2) == 0)
1360 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
1361 convolve_image=CloneImage(image,0,0,MagickTrue,exception);
1362 if (convolve_image == (Image *) NULL)
1363 return((Image *) NULL);
1364 if (SetImageStorageClass(convolve_image,DirectClass) == MagickFalse)
1365 {
1366 InheritException(exception,&convolve_image->exception);
1367 convolve_image=DestroyImage(convolve_image);
1368 return((Image *) NULL);
1369 }
1370 if (image->debug != MagickFalse)
1371 {
1372 char
1373 format[MaxTextExtent],
1374 *message;
1375
cristy117ff172010-08-15 21:35:32 +00001376 register const double
1377 *k;
1378
cristybb503372010-05-27 20:51:26 +00001379 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001380 u,
1381 v;
1382
cristyfccdab92009-11-30 16:43:57 +00001383 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001384 " ConvolveImage with %.20gx%.20g kernel:",(double) width,(double)
1385 width);
cristyfccdab92009-11-30 16:43:57 +00001386 message=AcquireString("");
1387 k=kernel;
cristybb503372010-05-27 20:51:26 +00001388 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001389 {
1390 *message='\0';
cristye8c25f92010-06-03 00:53:06 +00001391 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001392 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00001393 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001394 {
cristye7f51092010-01-17 00:39:37 +00001395 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001396 (void) ConcatenateString(&message,format);
1397 }
1398 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1399 }
1400 message=DestroyString(message);
1401 }
1402 /*
1403 Normalize kernel.
1404 */
1405 normal_kernel=(double *) AcquireQuantumMemory(width*width,
1406 sizeof(*normal_kernel));
1407 if (normal_kernel == (double *) NULL)
1408 {
1409 convolve_image=DestroyImage(convolve_image);
1410 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1411 }
1412 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001413 for (i=0; i < (ssize_t) (width*width); i++)
cristyfccdab92009-11-30 16:43:57 +00001414 gamma+=kernel[i];
1415 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristybb503372010-05-27 20:51:26 +00001416 for (i=0; i < (ssize_t) (width*width); i++)
cristyfccdab92009-11-30 16:43:57 +00001417 normal_kernel[i]=gamma*kernel[i];
1418 /*
1419 Convolve image.
1420 */
1421 status=MagickTrue;
1422 progress=0;
1423 GetMagickPixelPacket(image,&bias);
1424 SetMagickPixelPacketBias(image,&bias);
1425 image_view=AcquireCacheView(image);
1426 convolve_view=AcquireCacheView(convolve_image);
1427#if defined(MAGICKCORE_OPENMP_SUPPORT)
1428 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1429#endif
cristybb503372010-05-27 20:51:26 +00001430 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001431 {
1432 MagickBooleanType
1433 sync;
1434
1435 register const IndexPacket
1436 *restrict indexes;
1437
1438 register const PixelPacket
1439 *restrict p;
1440
1441 register IndexPacket
1442 *restrict convolve_indexes;
1443
cristyfccdab92009-11-30 16:43:57 +00001444 register PixelPacket
1445 *restrict q;
1446
cristy117ff172010-08-15 21:35:32 +00001447 register ssize_t
1448 x;
1449
cristyfccdab92009-11-30 16:43:57 +00001450 if (status == MagickFalse)
1451 continue;
cristyce889302010-06-30 19:16:36 +00001452 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
1453 (width/2L),image->columns+width,width,exception);
cristyfccdab92009-11-30 16:43:57 +00001454 q=GetCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
1455 exception);
1456 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1457 {
1458 status=MagickFalse;
1459 continue;
1460 }
1461 indexes=GetCacheViewVirtualIndexQueue(image_view);
1462 convolve_indexes=GetCacheViewAuthenticIndexQueue(convolve_view);
cristybb503372010-05-27 20:51:26 +00001463 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001464 {
cristyfccdab92009-11-30 16:43:57 +00001465 MagickPixelPacket
1466 pixel;
1467
1468 register const double
1469 *restrict k;
1470
1471 register const PixelPacket
1472 *restrict kernel_pixels;
1473
cristybb503372010-05-27 20:51:26 +00001474 register ssize_t
cristyfccdab92009-11-30 16:43:57 +00001475 u;
1476
cristy117ff172010-08-15 21:35:32 +00001477 ssize_t
1478 v;
1479
cristyfccdab92009-11-30 16:43:57 +00001480 pixel=bias;
1481 k=normal_kernel;
1482 kernel_pixels=p;
1483 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1484 {
cristybb503372010-05-27 20:51:26 +00001485 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001486 {
cristybb503372010-05-27 20:51:26 +00001487 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001488 {
1489 pixel.red+=(*k)*kernel_pixels[u].red;
1490 pixel.green+=(*k)*kernel_pixels[u].green;
1491 pixel.blue+=(*k)*kernel_pixels[u].blue;
1492 k++;
1493 }
1494 kernel_pixels+=image->columns+width;
1495 }
1496 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001497 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001498 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001499 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001500 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001501 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001502 if ((channel & OpacityChannel) != 0)
1503 {
1504 k=normal_kernel;
1505 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001506 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001507 {
cristybb503372010-05-27 20:51:26 +00001508 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001509 {
1510 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1511 k++;
1512 }
1513 kernel_pixels+=image->columns+width;
1514 }
cristyce70c172010-01-07 17:15:30 +00001515 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001516 }
1517 if (((channel & IndexChannel) != 0) &&
1518 (image->colorspace == CMYKColorspace))
1519 {
1520 register const IndexPacket
1521 *restrict kernel_indexes;
1522
1523 k=normal_kernel;
cristy9d314ff2011-03-09 01:30:28 +00001524 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00001525 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001526 {
cristybb503372010-05-27 20:51:26 +00001527 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001528 {
1529 pixel.index+=(*k)*kernel_indexes[u];
1530 k++;
1531 }
1532 kernel_indexes+=image->columns+width;
1533 }
cristyce70c172010-01-07 17:15:30 +00001534 convolve_indexes[x]=ClampToQuantum(pixel.index);
cristyfccdab92009-11-30 16:43:57 +00001535 }
1536 }
1537 else
1538 {
1539 MagickRealType
1540 alpha,
1541 gamma;
1542
1543 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001544 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001545 {
cristybb503372010-05-27 20:51:26 +00001546 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001547 {
1548 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1549 kernel_pixels[u].opacity));
1550 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
1551 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
1552 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
cristyfccdab92009-11-30 16:43:57 +00001553 gamma+=(*k)*alpha;
1554 k++;
1555 }
1556 kernel_pixels+=image->columns+width;
1557 }
1558 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1559 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001560 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001561 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001562 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001563 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001564 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001565 if ((channel & OpacityChannel) != 0)
1566 {
1567 k=normal_kernel;
1568 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001569 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001570 {
cristybb503372010-05-27 20:51:26 +00001571 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001572 {
1573 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1574 k++;
1575 }
1576 kernel_pixels+=image->columns+width;
1577 }
cristyce70c172010-01-07 17:15:30 +00001578 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001579 }
1580 if (((channel & IndexChannel) != 0) &&
1581 (image->colorspace == CMYKColorspace))
1582 {
1583 register const IndexPacket
1584 *restrict kernel_indexes;
1585
1586 k=normal_kernel;
1587 kernel_pixels=p;
cristy9d314ff2011-03-09 01:30:28 +00001588 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00001589 for (v=0; v < (ssize_t) width; v++)
cristyfccdab92009-11-30 16:43:57 +00001590 {
cristybb503372010-05-27 20:51:26 +00001591 for (u=0; u < (ssize_t) width; u++)
cristyfccdab92009-11-30 16:43:57 +00001592 {
1593 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1594 kernel_pixels[u].opacity));
1595 pixel.index+=(*k)*alpha*kernel_indexes[u];
1596 k++;
1597 }
1598 kernel_pixels+=image->columns+width;
1599 kernel_indexes+=image->columns+width;
1600 }
cristy24b06da2010-01-09 23:05:56 +00001601 convolve_indexes[x]=ClampToQuantum(gamma*
1602 GetIndexPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001603 }
1604 }
cristy9d314ff2011-03-09 01:30:28 +00001605 indexes++;
cristyfccdab92009-11-30 16:43:57 +00001606 p++;
1607 q++;
1608 }
1609 sync=SyncCacheViewAuthenticPixels(convolve_view,exception);
1610 if (sync == MagickFalse)
1611 status=MagickFalse;
1612 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1613 {
1614 MagickBooleanType
1615 proceed;
1616
1617#if defined(MAGICKCORE_OPENMP_SUPPORT)
1618 #pragma omp critical (MagickCore_ConvolveImageChannel)
1619#endif
1620 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1621 if (proceed == MagickFalse)
1622 status=MagickFalse;
1623 }
1624 }
1625 convolve_image->type=image->type;
1626 convolve_view=DestroyCacheView(convolve_view);
1627 image_view=DestroyCacheView(image_view);
1628 normal_kernel=(double *) RelinquishMagickMemory(normal_kernel);
1629 if (status == MagickFalse)
1630 convolve_image=DestroyImage(convolve_image);
1631 return(convolve_image);
1632}
1633
1634/*
1635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1636% %
1637% %
1638% %
cristy3ed852e2009-09-05 21:47:34 +00001639% D e s p e c k l e I m a g e %
1640% %
1641% %
1642% %
1643%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1644%
1645% DespeckleImage() reduces the speckle noise in an image while perserving the
1646% edges of the original image.
1647%
1648% The format of the DespeckleImage method is:
1649%
1650% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1651%
1652% A description of each parameter follows:
1653%
1654% o image: the image.
1655%
1656% o exception: return any errors or warnings in this structure.
1657%
1658*/
1659
cristybb503372010-05-27 20:51:26 +00001660static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1661 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
cristy3ed852e2009-09-05 21:47:34 +00001662 const int polarity)
1663{
cristy3ed852e2009-09-05 21:47:34 +00001664 MagickRealType
1665 v;
1666
cristy3ed852e2009-09-05 21:47:34 +00001667 register Quantum
1668 *p,
1669 *q,
1670 *r,
1671 *s;
1672
cristy117ff172010-08-15 21:35:32 +00001673 register ssize_t
1674 x;
1675
1676 ssize_t
1677 y;
1678
cristy3ed852e2009-09-05 21:47:34 +00001679 assert(f != (Quantum *) NULL);
1680 assert(g != (Quantum *) NULL);
1681 p=f+(columns+2);
1682 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001683 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1684 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001685 {
1686 p++;
1687 q++;
1688 r++;
1689 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001690 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001691 {
1692 v=(MagickRealType) (*p);
1693 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1694 v+=ScaleCharToQuantum(1);
1695 *q=(Quantum) v;
1696 p++;
1697 q++;
1698 r++;
1699 }
1700 else
cristybb503372010-05-27 20:51:26 +00001701 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001702 {
1703 v=(MagickRealType) (*p);
1704 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
cristybb503372010-05-27 20:51:26 +00001705 v-=(ssize_t) ScaleCharToQuantum(1);
cristy3ed852e2009-09-05 21:47:34 +00001706 *q=(Quantum) v;
1707 p++;
1708 q++;
1709 r++;
1710 }
1711 p++;
1712 q++;
1713 r++;
1714 }
1715 p=f+(columns+2);
1716 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001717 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1718 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1719 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001720 {
1721 p++;
1722 q++;
1723 r++;
1724 s++;
1725 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001726 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001727 {
1728 v=(MagickRealType) (*q);
1729 if (((MagickRealType) *s >=
1730 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1731 ((MagickRealType) *r > v))
1732 v+=ScaleCharToQuantum(1);
1733 *p=(Quantum) v;
1734 p++;
1735 q++;
1736 r++;
1737 s++;
1738 }
1739 else
cristybb503372010-05-27 20:51:26 +00001740 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001741 {
1742 v=(MagickRealType) (*q);
1743 if (((MagickRealType) *s <=
1744 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1745 ((MagickRealType) *r < v))
1746 v-=(MagickRealType) ScaleCharToQuantum(1);
1747 *p=(Quantum) v;
1748 p++;
1749 q++;
1750 r++;
1751 s++;
1752 }
1753 p++;
1754 q++;
1755 r++;
1756 s++;
1757 }
1758}
1759
1760MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1761{
1762#define DespeckleImageTag "Despeckle/Image"
1763
cristy2407fc22009-09-11 00:55:25 +00001764 CacheView
1765 *despeckle_view,
1766 *image_view;
1767
cristy3ed852e2009-09-05 21:47:34 +00001768 Image
1769 *despeckle_image;
1770
cristy3ed852e2009-09-05 21:47:34 +00001771 MagickBooleanType
1772 status;
1773
cristya58c3172011-02-19 19:23:11 +00001774 register ssize_t
1775 i;
1776
cristy3ed852e2009-09-05 21:47:34 +00001777 Quantum
cristy65b9f392011-02-22 14:22:54 +00001778 *restrict buffers,
1779 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +00001780
1781 size_t
cristya58c3172011-02-19 19:23:11 +00001782 length,
1783 number_channels;
cristy117ff172010-08-15 21:35:32 +00001784
cristybb503372010-05-27 20:51:26 +00001785 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001786 X[4] = {0, 1, 1,-1},
1787 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001788
cristy3ed852e2009-09-05 21:47:34 +00001789 /*
1790 Allocate despeckled image.
1791 */
1792 assert(image != (const Image *) NULL);
1793 assert(image->signature == MagickSignature);
1794 if (image->debug != MagickFalse)
1795 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1796 assert(exception != (ExceptionInfo *) NULL);
1797 assert(exception->signature == MagickSignature);
1798 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1799 exception);
1800 if (despeckle_image == (Image *) NULL)
1801 return((Image *) NULL);
1802 if (SetImageStorageClass(despeckle_image,DirectClass) == MagickFalse)
1803 {
1804 InheritException(exception,&despeckle_image->exception);
1805 despeckle_image=DestroyImage(despeckle_image);
1806 return((Image *) NULL);
1807 }
1808 /*
1809 Allocate image buffers.
1810 */
1811 length=(size_t) ((image->columns+2)*(image->rows+2));
cristy65b9f392011-02-22 14:22:54 +00001812 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1813 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1814 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001815 {
cristy65b9f392011-02-22 14:22:54 +00001816 if (buffers != (Quantum *) NULL)
1817 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1818 if (pixels != (Quantum *) NULL)
1819 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001820 despeckle_image=DestroyImage(despeckle_image);
1821 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1822 }
1823 /*
1824 Reduce speckle in the image.
1825 */
1826 status=MagickTrue;
cristy109695a2011-02-19 19:38:14 +00001827 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
cristy3ed852e2009-09-05 21:47:34 +00001828 image_view=AcquireCacheView(image);
1829 despeckle_view=AcquireCacheView(despeckle_image);
cristy8df3d002011-02-19 19:40:59 +00001830 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001831 {
cristy3ed852e2009-09-05 21:47:34 +00001832 register Quantum
1833 *buffer,
1834 *pixel;
1835
cristyc1488b52011-02-19 18:54:15 +00001836 register ssize_t
cristya58c3172011-02-19 19:23:11 +00001837 k,
cristyc1488b52011-02-19 18:54:15 +00001838 x;
1839
cristy117ff172010-08-15 21:35:32 +00001840 ssize_t
1841 j,
1842 y;
1843
cristy3ed852e2009-09-05 21:47:34 +00001844 if (status == MagickFalse)
1845 continue;
cristy65b9f392011-02-22 14:22:54 +00001846 pixel=pixels;
cristy3ed852e2009-09-05 21:47:34 +00001847 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
cristy65b9f392011-02-22 14:22:54 +00001848 buffer=buffers;
cristybb503372010-05-27 20:51:26 +00001849 j=(ssize_t) image->columns+2;
1850 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001851 {
cristya58c3172011-02-19 19:23:11 +00001852 register const IndexPacket
1853 *restrict indexes;
1854
cristy3ed852e2009-09-05 21:47:34 +00001855 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001856 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001857
1858 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1859 if (p == (const PixelPacket *) NULL)
1860 break;
cristya58c3172011-02-19 19:23:11 +00001861 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001862 j++;
cristybb503372010-05-27 20:51:26 +00001863 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001864 {
cristya58c3172011-02-19 19:23:11 +00001865 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001866 {
cristyce70c172010-01-07 17:15:30 +00001867 case 0: pixel[j]=GetRedPixelComponent(p); break;
1868 case 1: pixel[j]=GetGreenPixelComponent(p); break;
1869 case 2: pixel[j]=GetBluePixelComponent(p); break;
1870 case 3: pixel[j]=GetOpacityPixelComponent(p); break;
cristya58c3172011-02-19 19:23:11 +00001871 case 4: pixel[j]=GetBlackPixelComponent(indexes,x); break;
cristy3ed852e2009-09-05 21:47:34 +00001872 default: break;
1873 }
1874 p++;
1875 j++;
1876 }
1877 j++;
1878 }
cristy3ed852e2009-09-05 21:47:34 +00001879 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
cristya58c3172011-02-19 19:23:11 +00001880 for (k=0; k < 4; k++)
cristy3ed852e2009-09-05 21:47:34 +00001881 {
cristya58c3172011-02-19 19:23:11 +00001882 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1883 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1884 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1885 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
cristy3ed852e2009-09-05 21:47:34 +00001886 }
cristybb503372010-05-27 20:51:26 +00001887 j=(ssize_t) image->columns+2;
1888 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001889 {
1890 MagickBooleanType
1891 sync;
1892
cristya58c3172011-02-19 19:23:11 +00001893 register IndexPacket
1894 *restrict indexes;
1895
cristy3ed852e2009-09-05 21:47:34 +00001896 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001897 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001898
1899 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1900 1,exception);
1901 if (q == (PixelPacket *) NULL)
1902 break;
cristya58c3172011-02-19 19:23:11 +00001903 indexes=GetCacheViewAuthenticIndexQueue(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001904 j++;
cristybb503372010-05-27 20:51:26 +00001905 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001906 {
cristya58c3172011-02-19 19:23:11 +00001907 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001908 {
1909 case 0: q->red=pixel[j]; break;
1910 case 1: q->green=pixel[j]; break;
1911 case 2: q->blue=pixel[j]; break;
1912 case 3: q->opacity=pixel[j]; break;
cristya58c3172011-02-19 19:23:11 +00001913 case 4: indexes[x]=pixel[j]; break;
cristy3ed852e2009-09-05 21:47:34 +00001914 default: break;
1915 }
1916 q++;
1917 j++;
1918 }
1919 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1920 if (sync == MagickFalse)
1921 {
1922 status=MagickFalse;
1923 break;
1924 }
1925 j++;
1926 }
1927 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1928 {
1929 MagickBooleanType
1930 proceed;
1931
cristya58c3172011-02-19 19:23:11 +00001932 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1933 number_channels);
cristy3ed852e2009-09-05 21:47:34 +00001934 if (proceed == MagickFalse)
1935 status=MagickFalse;
1936 }
1937 }
1938 despeckle_view=DestroyCacheView(despeckle_view);
1939 image_view=DestroyCacheView(image_view);
cristy65b9f392011-02-22 14:22:54 +00001940 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1941 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001942 despeckle_image->type=image->type;
1943 if (status == MagickFalse)
1944 despeckle_image=DestroyImage(despeckle_image);
1945 return(despeckle_image);
1946}
1947
1948/*
1949%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1950% %
1951% %
1952% %
1953% E d g e I m a g e %
1954% %
1955% %
1956% %
1957%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1958%
1959% EdgeImage() finds edges in an image. Radius defines the radius of the
1960% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1961% radius for you.
1962%
1963% The format of the EdgeImage method is:
1964%
1965% Image *EdgeImage(const Image *image,const double radius,
1966% ExceptionInfo *exception)
1967%
1968% A description of each parameter follows:
1969%
1970% o image: the image.
1971%
1972% o radius: the radius of the pixel neighborhood.
1973%
1974% o exception: return any errors or warnings in this structure.
1975%
1976*/
1977MagickExport Image *EdgeImage(const Image *image,const double radius,
1978 ExceptionInfo *exception)
1979{
1980 Image
1981 *edge_image;
1982
1983 double
1984 *kernel;
1985
cristybb503372010-05-27 20:51:26 +00001986 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001987 i;
1988
cristybb503372010-05-27 20:51:26 +00001989 size_t
cristy3ed852e2009-09-05 21:47:34 +00001990 width;
1991
1992 assert(image != (const Image *) NULL);
1993 assert(image->signature == MagickSignature);
1994 if (image->debug != MagickFalse)
1995 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1996 assert(exception != (ExceptionInfo *) NULL);
1997 assert(exception->signature == MagickSignature);
1998 width=GetOptimalKernelWidth1D(radius,0.5);
1999 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2000 if (kernel == (double *) NULL)
2001 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00002002 for (i=0; i < (ssize_t) (width*width); i++)
cristy3ed852e2009-09-05 21:47:34 +00002003 kernel[i]=(-1.0);
2004 kernel[i/2]=(double) (width*width-1.0);
2005 edge_image=ConvolveImage(image,width,kernel,exception);
2006 kernel=(double *) RelinquishMagickMemory(kernel);
2007 return(edge_image);
2008}
2009
2010/*
2011%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2012% %
2013% %
2014% %
2015% E m b o s s I m a g e %
2016% %
2017% %
2018% %
2019%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2020%
2021% EmbossImage() returns a grayscale image with a three-dimensional effect.
2022% We convolve the image with a Gaussian operator of the given radius and
2023% standard deviation (sigma). For reasonable results, radius should be
2024% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
2025% radius for you.
2026%
2027% The format of the EmbossImage method is:
2028%
2029% Image *EmbossImage(const Image *image,const double radius,
2030% const double sigma,ExceptionInfo *exception)
2031%
2032% A description of each parameter follows:
2033%
2034% o image: the image.
2035%
2036% o radius: the radius of the pixel neighborhood.
2037%
2038% o sigma: the standard deviation of the Gaussian, in pixels.
2039%
2040% o exception: return any errors or warnings in this structure.
2041%
2042*/
2043MagickExport Image *EmbossImage(const Image *image,const double radius,
2044 const double sigma,ExceptionInfo *exception)
2045{
2046 double
2047 *kernel;
2048
2049 Image
2050 *emboss_image;
2051
cristybb503372010-05-27 20:51:26 +00002052 register ssize_t
cristy47e00502009-12-17 19:19:57 +00002053 i;
2054
cristybb503372010-05-27 20:51:26 +00002055 size_t
cristy3ed852e2009-09-05 21:47:34 +00002056 width;
2057
cristy117ff172010-08-15 21:35:32 +00002058 ssize_t
2059 j,
2060 k,
2061 u,
2062 v;
2063
cristy3ed852e2009-09-05 21:47:34 +00002064 assert(image != (Image *) NULL);
2065 assert(image->signature == MagickSignature);
2066 if (image->debug != MagickFalse)
2067 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2068 assert(exception != (ExceptionInfo *) NULL);
2069 assert(exception->signature == MagickSignature);
2070 width=GetOptimalKernelWidth2D(radius,sigma);
2071 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2072 if (kernel == (double *) NULL)
2073 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00002074 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +00002075 k=j;
2076 i=0;
2077 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00002078 {
cristy47e00502009-12-17 19:19:57 +00002079 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00002080 {
cristy4205a3c2010-09-12 20:19:59 +00002081 kernel[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00002082 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00002083 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00002084 if (u != k)
cristy3ed852e2009-09-05 21:47:34 +00002085 kernel[i]=0.0;
2086 i++;
2087 }
cristy47e00502009-12-17 19:19:57 +00002088 k--;
cristy3ed852e2009-09-05 21:47:34 +00002089 }
2090 emboss_image=ConvolveImage(image,width,kernel,exception);
2091 if (emboss_image != (Image *) NULL)
2092 (void) EqualizeImage(emboss_image);
2093 kernel=(double *) RelinquishMagickMemory(kernel);
2094 return(emboss_image);
2095}
2096
2097/*
2098%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2099% %
2100% %
2101% %
cristy56a9e512010-01-06 18:18:55 +00002102% F i l t e r I m a g e %
2103% %
2104% %
2105% %
2106%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2107%
2108% FilterImage() applies a custom convolution kernel to the image.
2109%
2110% The format of the FilterImage method is:
2111%
cristy2be15382010-01-21 02:38:03 +00002112% Image *FilterImage(const Image *image,const KernelInfo *kernel,
cristy56a9e512010-01-06 18:18:55 +00002113% ExceptionInfo *exception)
2114% Image *FilterImageChannel(const Image *image,const ChannelType channel,
cristy2be15382010-01-21 02:38:03 +00002115% const KernelInfo *kernel,ExceptionInfo *exception)
cristy56a9e512010-01-06 18:18:55 +00002116%
2117% A description of each parameter follows:
2118%
2119% o image: the image.
2120%
2121% o channel: the channel type.
2122%
2123% o kernel: the filtering kernel.
2124%
2125% o exception: return any errors or warnings in this structure.
2126%
2127*/
2128
cristy2be15382010-01-21 02:38:03 +00002129MagickExport Image *FilterImage(const Image *image,const KernelInfo *kernel,
cristy56a9e512010-01-06 18:18:55 +00002130 ExceptionInfo *exception)
2131{
2132 Image
2133 *filter_image;
2134
2135 filter_image=FilterImageChannel(image,DefaultChannels,kernel,exception);
2136 return(filter_image);
2137}
2138
2139MagickExport Image *FilterImageChannel(const Image *image,
cristy2be15382010-01-21 02:38:03 +00002140 const ChannelType channel,const KernelInfo *kernel,ExceptionInfo *exception)
cristy56a9e512010-01-06 18:18:55 +00002141{
2142#define FilterImageTag "Filter/Image"
2143
2144 CacheView
2145 *filter_view,
2146 *image_view;
2147
cristy56a9e512010-01-06 18:18:55 +00002148 Image
2149 *filter_image;
2150
cristy56a9e512010-01-06 18:18:55 +00002151 MagickBooleanType
2152 status;
2153
cristybb503372010-05-27 20:51:26 +00002154 MagickOffsetType
2155 progress;
2156
cristy56a9e512010-01-06 18:18:55 +00002157 MagickPixelPacket
2158 bias;
2159
cristybb503372010-05-27 20:51:26 +00002160 ssize_t
2161 y;
2162
cristy56a9e512010-01-06 18:18:55 +00002163 /*
2164 Initialize filter image attributes.
2165 */
2166 assert(image != (Image *) NULL);
2167 assert(image->signature == MagickSignature);
2168 if (image->debug != MagickFalse)
2169 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2170 assert(exception != (ExceptionInfo *) NULL);
2171 assert(exception->signature == MagickSignature);
2172 if ((kernel->width % 2) == 0)
2173 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
2174 filter_image=CloneImage(image,0,0,MagickTrue,exception);
2175 if (filter_image == (Image *) NULL)
2176 return((Image *) NULL);
2177 if (SetImageStorageClass(filter_image,DirectClass) == MagickFalse)
2178 {
2179 InheritException(exception,&filter_image->exception);
2180 filter_image=DestroyImage(filter_image);
2181 return((Image *) NULL);
2182 }
2183 if (image->debug != MagickFalse)
2184 {
2185 char
2186 format[MaxTextExtent],
2187 *message;
2188
cristy117ff172010-08-15 21:35:32 +00002189 register const double
2190 *k;
2191
cristybb503372010-05-27 20:51:26 +00002192 ssize_t
cristy56a9e512010-01-06 18:18:55 +00002193 u,
2194 v;
2195
cristy56a9e512010-01-06 18:18:55 +00002196 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00002197 " FilterImage with %.20gx%.20g kernel:",(double) kernel->width,(double)
2198 kernel->height);
cristy56a9e512010-01-06 18:18:55 +00002199 message=AcquireString("");
2200 k=kernel->values;
cristybb503372010-05-27 20:51:26 +00002201 for (v=0; v < (ssize_t) kernel->height; v++)
cristy56a9e512010-01-06 18:18:55 +00002202 {
2203 *message='\0';
cristye8c25f92010-06-03 00:53:06 +00002204 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy56a9e512010-01-06 18:18:55 +00002205 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00002206 for (u=0; u < (ssize_t) kernel->width; u++)
cristy56a9e512010-01-06 18:18:55 +00002207 {
cristye7f51092010-01-17 00:39:37 +00002208 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
cristy56a9e512010-01-06 18:18:55 +00002209 (void) ConcatenateString(&message,format);
2210 }
2211 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2212 }
2213 message=DestroyString(message);
2214 }
cristy36826ab2010-03-06 01:29:30 +00002215 status=AccelerateConvolveImage(image,kernel,filter_image,exception);
cristyd43a46b2010-01-21 02:13:41 +00002216 if (status == MagickTrue)
2217 return(filter_image);
cristy56a9e512010-01-06 18:18:55 +00002218 /*
2219 Filter image.
2220 */
2221 status=MagickTrue;
2222 progress=0;
2223 GetMagickPixelPacket(image,&bias);
2224 SetMagickPixelPacketBias(image,&bias);
2225 image_view=AcquireCacheView(image);
2226 filter_view=AcquireCacheView(filter_image);
2227#if defined(MAGICKCORE_OPENMP_SUPPORT)
2228 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2229#endif
cristybb503372010-05-27 20:51:26 +00002230 for (y=0; y < (ssize_t) image->rows; y++)
cristy56a9e512010-01-06 18:18:55 +00002231 {
2232 MagickBooleanType
2233 sync;
2234
2235 register const IndexPacket
2236 *restrict indexes;
2237
2238 register const PixelPacket
2239 *restrict p;
2240
2241 register IndexPacket
2242 *restrict filter_indexes;
2243
cristy56a9e512010-01-06 18:18:55 +00002244 register PixelPacket
2245 *restrict q;
2246
cristy117ff172010-08-15 21:35:32 +00002247 register ssize_t
2248 x;
2249
cristy56a9e512010-01-06 18:18:55 +00002250 if (status == MagickFalse)
2251 continue;
cristybb503372010-05-27 20:51:26 +00002252 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel->width/2L),
cristy117ff172010-08-15 21:35:32 +00002253 y-(ssize_t) (kernel->height/2L),image->columns+kernel->width,
2254 kernel->height,exception);
cristy56a9e512010-01-06 18:18:55 +00002255 q=GetCacheViewAuthenticPixels(filter_view,0,y,filter_image->columns,1,
2256 exception);
2257 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2258 {
2259 status=MagickFalse;
2260 continue;
2261 }
2262 indexes=GetCacheViewVirtualIndexQueue(image_view);
2263 filter_indexes=GetCacheViewAuthenticIndexQueue(filter_view);
cristybb503372010-05-27 20:51:26 +00002264 for (x=0; x < (ssize_t) image->columns; x++)
cristy56a9e512010-01-06 18:18:55 +00002265 {
cristy56a9e512010-01-06 18:18:55 +00002266 MagickPixelPacket
2267 pixel;
2268
2269 register const double
2270 *restrict k;
2271
2272 register const PixelPacket
2273 *restrict kernel_pixels;
2274
cristybb503372010-05-27 20:51:26 +00002275 register ssize_t
cristy56a9e512010-01-06 18:18:55 +00002276 u;
2277
cristy117ff172010-08-15 21:35:32 +00002278 ssize_t
2279 v;
2280
cristy56a9e512010-01-06 18:18:55 +00002281 pixel=bias;
cristy36826ab2010-03-06 01:29:30 +00002282 k=kernel->values;
cristy56a9e512010-01-06 18:18:55 +00002283 kernel_pixels=p;
2284 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
2285 {
cristybb503372010-05-27 20:51:26 +00002286 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002287 {
cristybb503372010-05-27 20:51:26 +00002288 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002289 {
2290 pixel.red+=(*k)*kernel_pixels[u].red;
2291 pixel.green+=(*k)*kernel_pixels[u].green;
2292 pixel.blue+=(*k)*kernel_pixels[u].blue;
2293 k++;
2294 }
cristy36826ab2010-03-06 01:29:30 +00002295 kernel_pixels+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002296 }
2297 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002298 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002299 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002300 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002301 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002302 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002303 if ((channel & OpacityChannel) != 0)
2304 {
cristy36826ab2010-03-06 01:29:30 +00002305 k=kernel->values;
cristy56a9e512010-01-06 18:18:55 +00002306 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00002307 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002308 {
cristybb503372010-05-27 20:51:26 +00002309 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002310 {
2311 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2312 k++;
2313 }
cristy36826ab2010-03-06 01:29:30 +00002314 kernel_pixels+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002315 }
cristyce70c172010-01-07 17:15:30 +00002316 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002317 }
2318 if (((channel & IndexChannel) != 0) &&
2319 (image->colorspace == CMYKColorspace))
2320 {
2321 register const IndexPacket
2322 *restrict kernel_indexes;
2323
cristy36826ab2010-03-06 01:29:30 +00002324 k=kernel->values;
cristy9d314ff2011-03-09 01:30:28 +00002325 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00002326 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002327 {
cristybb503372010-05-27 20:51:26 +00002328 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002329 {
2330 pixel.index+=(*k)*kernel_indexes[u];
2331 k++;
2332 }
cristy36826ab2010-03-06 01:29:30 +00002333 kernel_indexes+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002334 }
cristyce70c172010-01-07 17:15:30 +00002335 filter_indexes[x]=ClampToQuantum(pixel.index);
cristy56a9e512010-01-06 18:18:55 +00002336 }
2337 }
2338 else
2339 {
2340 MagickRealType
2341 alpha,
2342 gamma;
2343
2344 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00002345 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002346 {
cristybb503372010-05-27 20:51:26 +00002347 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002348 {
2349 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2350 kernel_pixels[u].opacity));
2351 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
2352 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
2353 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
2354 gamma+=(*k)*alpha;
2355 k++;
2356 }
cristy36826ab2010-03-06 01:29:30 +00002357 kernel_pixels+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002358 }
2359 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2360 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002361 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002362 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002363 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002364 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002365 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002366 if ((channel & OpacityChannel) != 0)
2367 {
cristy36826ab2010-03-06 01:29:30 +00002368 k=kernel->values;
cristy56a9e512010-01-06 18:18:55 +00002369 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00002370 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002371 {
cristybb503372010-05-27 20:51:26 +00002372 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002373 {
2374 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2375 k++;
2376 }
cristy36826ab2010-03-06 01:29:30 +00002377 kernel_pixels+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002378 }
cristyce70c172010-01-07 17:15:30 +00002379 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002380 }
2381 if (((channel & IndexChannel) != 0) &&
2382 (image->colorspace == CMYKColorspace))
2383 {
2384 register const IndexPacket
2385 *restrict kernel_indexes;
2386
cristy36826ab2010-03-06 01:29:30 +00002387 k=kernel->values;
cristy56a9e512010-01-06 18:18:55 +00002388 kernel_pixels=p;
cristy9d314ff2011-03-09 01:30:28 +00002389 kernel_indexes=indexes;
cristybb503372010-05-27 20:51:26 +00002390 for (v=0; v < (ssize_t) kernel->width; v++)
cristy56a9e512010-01-06 18:18:55 +00002391 {
cristybb503372010-05-27 20:51:26 +00002392 for (u=0; u < (ssize_t) kernel->height; u++)
cristy56a9e512010-01-06 18:18:55 +00002393 {
2394 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2395 kernel_pixels[u].opacity));
2396 pixel.index+=(*k)*alpha*kernel_indexes[u];
2397 k++;
2398 }
cristy36826ab2010-03-06 01:29:30 +00002399 kernel_pixels+=image->columns+kernel->width;
2400 kernel_indexes+=image->columns+kernel->width;
cristy56a9e512010-01-06 18:18:55 +00002401 }
cristy2115aea2010-01-09 23:16:08 +00002402 filter_indexes[x]=ClampToQuantum(gamma*
2403 GetIndexPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002404 }
2405 }
cristy9d314ff2011-03-09 01:30:28 +00002406 indexes++;
cristy56a9e512010-01-06 18:18:55 +00002407 p++;
2408 q++;
2409 }
2410 sync=SyncCacheViewAuthenticPixels(filter_view,exception);
2411 if (sync == MagickFalse)
2412 status=MagickFalse;
2413 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2414 {
2415 MagickBooleanType
2416 proceed;
2417
2418#if defined(MAGICKCORE_OPENMP_SUPPORT)
2419 #pragma omp critical (MagickCore_FilterImageChannel)
2420#endif
2421 proceed=SetImageProgress(image,FilterImageTag,progress++,image->rows);
2422 if (proceed == MagickFalse)
2423 status=MagickFalse;
2424 }
2425 }
2426 filter_image->type=image->type;
2427 filter_view=DestroyCacheView(filter_view);
2428 image_view=DestroyCacheView(image_view);
cristy56a9e512010-01-06 18:18:55 +00002429 if (status == MagickFalse)
2430 filter_image=DestroyImage(filter_image);
2431 return(filter_image);
2432}
2433
2434/*
2435%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2436% %
2437% %
2438% %
cristy3ed852e2009-09-05 21:47:34 +00002439% G a u s s i a n B l u r I m a g e %
2440% %
2441% %
2442% %
2443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2444%
2445% GaussianBlurImage() blurs an image. We convolve the image with a
2446% Gaussian operator of the given radius and standard deviation (sigma).
2447% For reasonable results, the radius should be larger than sigma. Use a
2448% radius of 0 and GaussianBlurImage() selects a suitable radius for you
2449%
2450% The format of the GaussianBlurImage method is:
2451%
2452% Image *GaussianBlurImage(const Image *image,onst double radius,
2453% const double sigma,ExceptionInfo *exception)
2454% Image *GaussianBlurImageChannel(const Image *image,
2455% const ChannelType channel,const double radius,const double sigma,
2456% ExceptionInfo *exception)
2457%
2458% A description of each parameter follows:
2459%
2460% o image: the image.
2461%
2462% o channel: the channel type.
2463%
2464% o radius: the radius of the Gaussian, in pixels, not counting the center
2465% pixel.
2466%
2467% o sigma: the standard deviation of the Gaussian, in pixels.
2468%
2469% o exception: return any errors or warnings in this structure.
2470%
2471*/
2472
2473MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
2474 const double sigma,ExceptionInfo *exception)
2475{
2476 Image
2477 *blur_image;
2478
2479 blur_image=GaussianBlurImageChannel(image,DefaultChannels,radius,sigma,
2480 exception);
2481 return(blur_image);
2482}
2483
2484MagickExport Image *GaussianBlurImageChannel(const Image *image,
2485 const ChannelType channel,const double radius,const double sigma,
2486 ExceptionInfo *exception)
2487{
2488 double
2489 *kernel;
2490
2491 Image
2492 *blur_image;
2493
cristybb503372010-05-27 20:51:26 +00002494 register ssize_t
cristy47e00502009-12-17 19:19:57 +00002495 i;
2496
cristybb503372010-05-27 20:51:26 +00002497 size_t
cristy3ed852e2009-09-05 21:47:34 +00002498 width;
2499
cristy117ff172010-08-15 21:35:32 +00002500 ssize_t
2501 j,
2502 u,
2503 v;
2504
cristy3ed852e2009-09-05 21:47:34 +00002505 assert(image != (const Image *) NULL);
2506 assert(image->signature == MagickSignature);
2507 if (image->debug != MagickFalse)
2508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2509 assert(exception != (ExceptionInfo *) NULL);
2510 assert(exception->signature == MagickSignature);
2511 width=GetOptimalKernelWidth2D(radius,sigma);
2512 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2513 if (kernel == (double *) NULL)
2514 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00002515 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00002516 i=0;
cristy47e00502009-12-17 19:19:57 +00002517 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00002518 {
cristy47e00502009-12-17 19:19:57 +00002519 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00002520 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2521 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002522 }
2523 blur_image=ConvolveImageChannel(image,channel,width,kernel,exception);
2524 kernel=(double *) RelinquishMagickMemory(kernel);
2525 return(blur_image);
2526}
2527
2528/*
2529%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2530% %
2531% %
2532% %
2533% M e d i a n F i l t e r I m a g e %
2534% %
2535% %
2536% %
2537%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2538%
2539% MedianFilterImage() applies a digital filter that improves the quality
2540% of a noisy image. Each pixel is replaced by the median in a set of
2541% neighboring pixels as defined by radius.
2542%
2543% The algorithm was contributed by Mike Edmonds and implements an insertion
2544% sort for selecting median color-channel values. For more on this algorithm
2545% see "Skip Lists: A probabilistic Alternative to Balanced Trees" by William
2546% Pugh in the June 1990 of Communications of the ACM.
2547%
2548% The format of the MedianFilterImage method is:
2549%
2550% Image *MedianFilterImage(const Image *image,const double radius,
2551% ExceptionInfo *exception)
2552%
2553% A description of each parameter follows:
2554%
2555% o image: the image.
2556%
2557% o radius: the radius of the pixel neighborhood.
2558%
2559% o exception: return any errors or warnings in this structure.
2560%
2561*/
2562
cristy69ec32d2011-02-27 23:57:09 +00002563#define ListChannels 5
cristy3ed852e2009-09-05 21:47:34 +00002564
cristy69ec32d2011-02-27 23:57:09 +00002565typedef struct _ListNode
cristy3ed852e2009-09-05 21:47:34 +00002566{
cristybb503372010-05-27 20:51:26 +00002567 size_t
cristy3ed852e2009-09-05 21:47:34 +00002568 next[9],
2569 count,
2570 signature;
cristy69ec32d2011-02-27 23:57:09 +00002571} ListNode;
cristy3ed852e2009-09-05 21:47:34 +00002572
cristy69ec32d2011-02-27 23:57:09 +00002573typedef struct _SkipList
cristy3ed852e2009-09-05 21:47:34 +00002574{
cristybb503372010-05-27 20:51:26 +00002575 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002576 level;
2577
cristy69ec32d2011-02-27 23:57:09 +00002578 ListNode
cristy3ed852e2009-09-05 21:47:34 +00002579 *nodes;
cristy69ec32d2011-02-27 23:57:09 +00002580} SkipList;
cristy3ed852e2009-09-05 21:47:34 +00002581
cristy69ec32d2011-02-27 23:57:09 +00002582typedef struct _PixelList
cristy3ed852e2009-09-05 21:47:34 +00002583{
cristybb503372010-05-27 20:51:26 +00002584 size_t
cristy3ed852e2009-09-05 21:47:34 +00002585 center,
2586 seed,
2587 signature;
2588
cristy69ec32d2011-02-27 23:57:09 +00002589 SkipList
2590 lists[ListChannels];
2591} PixelList;
cristy3ed852e2009-09-05 21:47:34 +00002592
cristy69ec32d2011-02-27 23:57:09 +00002593static PixelList *DestroyPixelList(PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002594{
cristybb503372010-05-27 20:51:26 +00002595 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002596 i;
2597
cristy69ec32d2011-02-27 23:57:09 +00002598 if (pixel_list == (PixelList *) NULL)
2599 return((PixelList *) NULL);
2600 for (i=0; i < ListChannels; i++)
2601 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
2602 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
cristy3ed852e2009-09-05 21:47:34 +00002603 pixel_list->lists[i].nodes);
cristy69ec32d2011-02-27 23:57:09 +00002604 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
cristy3ed852e2009-09-05 21:47:34 +00002605 return(pixel_list);
2606}
2607
cristy69ec32d2011-02-27 23:57:09 +00002608static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002609{
cristybb503372010-05-27 20:51:26 +00002610 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002611 i;
2612
cristy69ec32d2011-02-27 23:57:09 +00002613 assert(pixel_list != (PixelList **) NULL);
cristybb503372010-05-27 20:51:26 +00002614 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy69ec32d2011-02-27 23:57:09 +00002615 if (pixel_list[i] != (PixelList *) NULL)
2616 pixel_list[i]=DestroyPixelList(pixel_list[i]);
2617 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
cristy3ed852e2009-09-05 21:47:34 +00002618 return(pixel_list);
2619}
2620
cristy69ec32d2011-02-27 23:57:09 +00002621static PixelList *AcquirePixelList(const size_t width)
cristy3ed852e2009-09-05 21:47:34 +00002622{
cristy69ec32d2011-02-27 23:57:09 +00002623 PixelList
cristy3ed852e2009-09-05 21:47:34 +00002624 *pixel_list;
2625
cristybb503372010-05-27 20:51:26 +00002626 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002627 i;
2628
cristy69ec32d2011-02-27 23:57:09 +00002629 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
2630 if (pixel_list == (PixelList *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002631 return(pixel_list);
2632 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
2633 pixel_list->center=width*width/2;
cristy69ec32d2011-02-27 23:57:09 +00002634 for (i=0; i < ListChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00002635 {
cristy69ec32d2011-02-27 23:57:09 +00002636 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
cristy3ed852e2009-09-05 21:47:34 +00002637 sizeof(*pixel_list->lists[i].nodes));
cristy69ec32d2011-02-27 23:57:09 +00002638 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
2639 return(DestroyPixelList(pixel_list));
cristy3ed852e2009-09-05 21:47:34 +00002640 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
2641 sizeof(*pixel_list->lists[i].nodes));
2642 }
2643 pixel_list->signature=MagickSignature;
2644 return(pixel_list);
2645}
2646
cristy69ec32d2011-02-27 23:57:09 +00002647static PixelList **AcquirePixelListThreadSet(const size_t width)
cristy3ed852e2009-09-05 21:47:34 +00002648{
cristy69ec32d2011-02-27 23:57:09 +00002649 PixelList
cristy3ed852e2009-09-05 21:47:34 +00002650 **pixel_list;
2651
cristy117ff172010-08-15 21:35:32 +00002652 register ssize_t
2653 i;
2654
cristybb503372010-05-27 20:51:26 +00002655 size_t
cristy3ed852e2009-09-05 21:47:34 +00002656 number_threads;
2657
2658 number_threads=GetOpenMPMaximumThreads();
cristy69ec32d2011-02-27 23:57:09 +00002659 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
cristy3ed852e2009-09-05 21:47:34 +00002660 sizeof(*pixel_list));
cristy69ec32d2011-02-27 23:57:09 +00002661 if (pixel_list == (PixelList **) NULL)
2662 return((PixelList **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002663 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
cristybb503372010-05-27 20:51:26 +00002664 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002665 {
cristy69ec32d2011-02-27 23:57:09 +00002666 pixel_list[i]=AcquirePixelList(width);
2667 if (pixel_list[i] == (PixelList *) NULL)
2668 return(DestroyPixelListThreadSet(pixel_list));
cristy3ed852e2009-09-05 21:47:34 +00002669 }
2670 return(pixel_list);
2671}
2672
cristy69ec32d2011-02-27 23:57:09 +00002673static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
2674 const size_t color)
cristy3ed852e2009-09-05 21:47:34 +00002675{
cristy69ec32d2011-02-27 23:57:09 +00002676 register SkipList
cristy3ed852e2009-09-05 21:47:34 +00002677 *list;
2678
cristy117ff172010-08-15 21:35:32 +00002679 register ssize_t
2680 level;
2681
cristybb503372010-05-27 20:51:26 +00002682 size_t
cristy3ed852e2009-09-05 21:47:34 +00002683 search,
2684 update[9];
2685
2686 /*
2687 Initialize the node.
2688 */
2689 list=pixel_list->lists+channel;
2690 list->nodes[color].signature=pixel_list->signature;
2691 list->nodes[color].count=1;
2692 /*
cristy33c53022010-06-25 12:17:27 +00002693 Determine where it belongs in the list.
cristy3ed852e2009-09-05 21:47:34 +00002694 */
2695 search=65536UL;
2696 for (level=list->level; level >= 0; level--)
2697 {
2698 while (list->nodes[search].next[level] < color)
2699 search=list->nodes[search].next[level];
2700 update[level]=search;
2701 }
2702 /*
2703 Generate a pseudo-random level for this node.
2704 */
2705 for (level=0; ; level++)
2706 {
2707 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
2708 if ((pixel_list->seed & 0x300) != 0x300)
2709 break;
2710 }
2711 if (level > 8)
2712 level=8;
2713 if (level > (list->level+2))
2714 level=list->level+2;
2715 /*
2716 If we're raising the list's level, link back to the root node.
2717 */
2718 while (level > list->level)
2719 {
2720 list->level++;
2721 update[list->level]=65536UL;
2722 }
2723 /*
2724 Link the node into the skip-list.
2725 */
2726 do
2727 {
2728 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
2729 list->nodes[update[level]].next[level]=color;
2730 }
2731 while (level-- > 0);
2732}
2733
cristyf2ad14a2011-03-18 18:57:25 +00002734static MagickPixelPacket GetMedianPixelList(PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002735{
2736 MagickPixelPacket
2737 pixel;
2738
cristy69ec32d2011-02-27 23:57:09 +00002739 register SkipList
cristy3ed852e2009-09-05 21:47:34 +00002740 *list;
2741
cristy117ff172010-08-15 21:35:32 +00002742 register ssize_t
2743 channel;
2744
cristybb503372010-05-27 20:51:26 +00002745 size_t
cristy3ed852e2009-09-05 21:47:34 +00002746 center,
2747 color,
2748 count;
2749
2750 unsigned short
cristy69ec32d2011-02-27 23:57:09 +00002751 channels[ListChannels];
cristy3ed852e2009-09-05 21:47:34 +00002752
2753 /*
2754 Find the median value for each of the color.
2755 */
2756 center=pixel_list->center;
2757 for (channel=0; channel < 5; channel++)
2758 {
2759 list=pixel_list->lists+channel;
2760 color=65536UL;
2761 count=0;
2762 do
2763 {
2764 color=list->nodes[color].next[0];
2765 count+=list->nodes[color].count;
2766 }
2767 while (count <= center);
2768 channels[channel]=(unsigned short) color;
2769 }
2770 GetMagickPixelPacket((const Image *) NULL,&pixel);
2771 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
2772 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
2773 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
2774 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
2775 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
2776 return(pixel);
2777}
2778
cristy69ec32d2011-02-27 23:57:09 +00002779static inline void InsertPixelList(const Image *image,const PixelPacket *pixel,
2780 const IndexPacket *indexes,PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002781{
cristybb503372010-05-27 20:51:26 +00002782 size_t
cristy3ed852e2009-09-05 21:47:34 +00002783 signature;
2784
2785 unsigned short
2786 index;
2787
2788 index=ScaleQuantumToShort(pixel->red);
2789 signature=pixel_list->lists[0].nodes[index].signature;
2790 if (signature == pixel_list->signature)
2791 pixel_list->lists[0].nodes[index].count++;
2792 else
cristy69ec32d2011-02-27 23:57:09 +00002793 AddNodePixelList(pixel_list,0,index);
cristy3ed852e2009-09-05 21:47:34 +00002794 index=ScaleQuantumToShort(pixel->green);
2795 signature=pixel_list->lists[1].nodes[index].signature;
2796 if (signature == pixel_list->signature)
2797 pixel_list->lists[1].nodes[index].count++;
2798 else
cristy69ec32d2011-02-27 23:57:09 +00002799 AddNodePixelList(pixel_list,1,index);
cristy3ed852e2009-09-05 21:47:34 +00002800 index=ScaleQuantumToShort(pixel->blue);
2801 signature=pixel_list->lists[2].nodes[index].signature;
2802 if (signature == pixel_list->signature)
2803 pixel_list->lists[2].nodes[index].count++;
2804 else
cristy69ec32d2011-02-27 23:57:09 +00002805 AddNodePixelList(pixel_list,2,index);
cristy3ed852e2009-09-05 21:47:34 +00002806 index=ScaleQuantumToShort(pixel->opacity);
2807 signature=pixel_list->lists[3].nodes[index].signature;
2808 if (signature == pixel_list->signature)
2809 pixel_list->lists[3].nodes[index].count++;
2810 else
cristy69ec32d2011-02-27 23:57:09 +00002811 AddNodePixelList(pixel_list,3,index);
cristy3ed852e2009-09-05 21:47:34 +00002812 if (image->colorspace == CMYKColorspace)
2813 index=ScaleQuantumToShort(*indexes);
2814 signature=pixel_list->lists[4].nodes[index].signature;
2815 if (signature == pixel_list->signature)
2816 pixel_list->lists[4].nodes[index].count++;
2817 else
cristy69ec32d2011-02-27 23:57:09 +00002818 AddNodePixelList(pixel_list,4,index);
cristy3ed852e2009-09-05 21:47:34 +00002819}
2820
cristy69ec32d2011-02-27 23:57:09 +00002821static void ResetPixelList(PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00002822{
2823 int
2824 level;
2825
cristy69ec32d2011-02-27 23:57:09 +00002826 register ListNode
cristy3ed852e2009-09-05 21:47:34 +00002827 *root;
2828
cristy69ec32d2011-02-27 23:57:09 +00002829 register SkipList
cristy3ed852e2009-09-05 21:47:34 +00002830 *list;
2831
cristy117ff172010-08-15 21:35:32 +00002832 register ssize_t
2833 channel;
2834
cristy3ed852e2009-09-05 21:47:34 +00002835 /*
2836 Reset the skip-list.
2837 */
2838 for (channel=0; channel < 5; channel++)
2839 {
2840 list=pixel_list->lists+channel;
2841 root=list->nodes+65536UL;
2842 list->level=0;
2843 for (level=0; level < 9; level++)
2844 root->next[level]=65536UL;
2845 }
2846 pixel_list->seed=pixel_list->signature++;
2847}
2848
2849MagickExport Image *MedianFilterImage(const Image *image,const double radius,
2850 ExceptionInfo *exception)
2851{
2852#define MedianFilterImageTag "MedianFilter/Image"
2853
cristyc4c8d132010-01-07 01:58:38 +00002854 CacheView
2855 *image_view,
2856 *median_view;
2857
cristy3ed852e2009-09-05 21:47:34 +00002858 Image
2859 *median_image;
2860
cristy3ed852e2009-09-05 21:47:34 +00002861 MagickBooleanType
2862 status;
2863
cristybb503372010-05-27 20:51:26 +00002864 MagickOffsetType
2865 progress;
2866
cristy69ec32d2011-02-27 23:57:09 +00002867 PixelList
cristyfa112112010-01-04 17:48:07 +00002868 **restrict pixel_list;
cristy3ed852e2009-09-05 21:47:34 +00002869
cristybb503372010-05-27 20:51:26 +00002870 size_t
cristy3ed852e2009-09-05 21:47:34 +00002871 width;
2872
cristybb503372010-05-27 20:51:26 +00002873 ssize_t
2874 y;
2875
cristy3ed852e2009-09-05 21:47:34 +00002876 /*
2877 Initialize median image attributes.
2878 */
2879 assert(image != (Image *) NULL);
2880 assert(image->signature == MagickSignature);
2881 if (image->debug != MagickFalse)
2882 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2883 assert(exception != (ExceptionInfo *) NULL);
2884 assert(exception->signature == MagickSignature);
2885 width=GetOptimalKernelWidth2D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00002886 median_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2887 exception);
2888 if (median_image == (Image *) NULL)
2889 return((Image *) NULL);
2890 if (SetImageStorageClass(median_image,DirectClass) == MagickFalse)
2891 {
2892 InheritException(exception,&median_image->exception);
2893 median_image=DestroyImage(median_image);
2894 return((Image *) NULL);
2895 }
cristy69ec32d2011-02-27 23:57:09 +00002896 pixel_list=AcquirePixelListThreadSet(width);
2897 if (pixel_list == (PixelList **) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002898 {
2899 median_image=DestroyImage(median_image);
2900 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2901 }
2902 /*
2903 Median filter each image row.
2904 */
2905 status=MagickTrue;
2906 progress=0;
2907 image_view=AcquireCacheView(image);
2908 median_view=AcquireCacheView(median_image);
cristyb5d5f722009-11-04 03:03:49 +00002909#if defined(MAGICKCORE_OPENMP_SUPPORT)
2910 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002911#endif
cristybb503372010-05-27 20:51:26 +00002912 for (y=0; y < (ssize_t) median_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002913 {
cristy5c9e6f22010-09-17 17:31:01 +00002914 const int
2915 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00002916
cristy3ed852e2009-09-05 21:47:34 +00002917 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00002918 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00002919
2920 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002921 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002922
2923 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00002924 *restrict median_indexes;
cristy3ed852e2009-09-05 21:47:34 +00002925
cristy3ed852e2009-09-05 21:47:34 +00002926 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002927 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002928
cristy117ff172010-08-15 21:35:32 +00002929 register ssize_t
2930 x;
2931
cristy3ed852e2009-09-05 21:47:34 +00002932 if (status == MagickFalse)
2933 continue;
cristy6ebe97c2010-07-03 01:17:28 +00002934 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
2935 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00002936 q=QueueCacheViewAuthenticPixels(median_view,0,y,median_image->columns,1,
2937 exception);
2938 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2939 {
2940 status=MagickFalse;
2941 continue;
2942 }
2943 indexes=GetCacheViewVirtualIndexQueue(image_view);
2944 median_indexes=GetCacheViewAuthenticIndexQueue(median_view);
cristybb503372010-05-27 20:51:26 +00002945 for (x=0; x < (ssize_t) median_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002946 {
2947 MagickPixelPacket
2948 pixel;
2949
cristy3ed852e2009-09-05 21:47:34 +00002950 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00002951 *restrict s;
cristy3ed852e2009-09-05 21:47:34 +00002952
cristy117ff172010-08-15 21:35:32 +00002953 register const PixelPacket
2954 *restrict r;
2955
cristybb503372010-05-27 20:51:26 +00002956 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002957 u,
2958 v;
2959
2960 r=p;
2961 s=indexes+x;
cristy69ec32d2011-02-27 23:57:09 +00002962 ResetPixelList(pixel_list[id]);
cristybb503372010-05-27 20:51:26 +00002963 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00002964 {
cristybb503372010-05-27 20:51:26 +00002965 for (u=0; u < (ssize_t) width; u++)
cristy69ec32d2011-02-27 23:57:09 +00002966 InsertPixelList(image,r+u,s+u,pixel_list[id]);
cristy3ed852e2009-09-05 21:47:34 +00002967 r+=image->columns+width;
2968 s+=image->columns+width;
2969 }
cristyf2ad14a2011-03-18 18:57:25 +00002970 pixel=GetMedianPixelList(pixel_list[id]);
cristy3ed852e2009-09-05 21:47:34 +00002971 SetPixelPacket(median_image,&pixel,q,median_indexes+x);
2972 p++;
2973 q++;
2974 }
2975 if (SyncCacheViewAuthenticPixels(median_view,exception) == MagickFalse)
2976 status=MagickFalse;
2977 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2978 {
2979 MagickBooleanType
2980 proceed;
2981
cristyb5d5f722009-11-04 03:03:49 +00002982#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00002983 #pragma omp critical (MagickCore_MedianFilterImage)
2984#endif
2985 proceed=SetImageProgress(image,MedianFilterImageTag,progress++,
2986 image->rows);
2987 if (proceed == MagickFalse)
2988 status=MagickFalse;
2989 }
2990 }
2991 median_view=DestroyCacheView(median_view);
2992 image_view=DestroyCacheView(image_view);
cristy69ec32d2011-02-27 23:57:09 +00002993 pixel_list=DestroyPixelListThreadSet(pixel_list);
cristy3ed852e2009-09-05 21:47:34 +00002994 return(median_image);
2995}
2996
2997/*
2998%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2999% %
3000% %
3001% %
cristy69ec32d2011-02-27 23:57:09 +00003002% M o d e I m a g e %
3003% %
3004% %
3005% %
3006%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3007%
3008% ModeImage() makes each pixel the 'predominate color' of the neighborhood
cristyfe4ba002011-02-28 14:54:12 +00003009% of the specified radius.
cristy69ec32d2011-02-27 23:57:09 +00003010%
3011% The format of the ModeImage method is:
3012%
3013% Image *ModeImage(const Image *image,const double radius,
3014% ExceptionInfo *exception)
3015%
3016% A description of each parameter follows:
3017%
3018% o image: the image.
3019%
3020% o radius: the radius of the pixel neighborhood.
3021%
3022% o exception: return any errors or warnings in this structure.
3023%
3024*/
3025
3026static MagickPixelPacket GetModePixelList(PixelList *pixel_list)
3027{
3028 MagickPixelPacket
3029 pixel;
3030
3031 register SkipList
3032 *list;
3033
3034 register ssize_t
3035 channel;
3036
3037 size_t
cristy69ec32d2011-02-27 23:57:09 +00003038 color,
3039 count,
cristya6020132011-02-28 01:06:03 +00003040 max_count,
3041 mode,
3042 width;
cristy69ec32d2011-02-27 23:57:09 +00003043
3044 unsigned short
3045 channels[5];
3046
3047 /*
cristya6020132011-02-28 01:06:03 +00003048 Make each pixel the 'predominate color' of the specified neighborhood.
cristy69ec32d2011-02-27 23:57:09 +00003049 */
cristya6020132011-02-28 01:06:03 +00003050 width=pixel_list->center << 1;
cristy69ec32d2011-02-27 23:57:09 +00003051 for (channel=0; channel < 5; channel++)
3052 {
3053 list=pixel_list->lists+channel;
3054 color=65536UL;
cristya6020132011-02-28 01:06:03 +00003055 mode=color;
3056 max_count=list->nodes[mode].count;
cristy69ec32d2011-02-27 23:57:09 +00003057 count=0;
3058 do
3059 {
cristya6020132011-02-28 01:06:03 +00003060 color=list->nodes[color].next[0];
3061 if (list->nodes[color].count > max_count)
3062 {
3063 mode=color;
3064 max_count=list->nodes[mode].count;
3065 }
cristy69ec32d2011-02-27 23:57:09 +00003066 count+=list->nodes[color].count;
3067 }
cristya6020132011-02-28 01:06:03 +00003068 while (count <= width);
3069 channels[channel]=(unsigned short) mode;
cristy69ec32d2011-02-27 23:57:09 +00003070 }
3071 GetMagickPixelPacket((const Image *) NULL,&pixel);
3072 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
3073 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
3074 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
3075 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
3076 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
3077 return(pixel);
3078}
3079
3080MagickExport Image *ModeImage(const Image *image,const double radius,
3081 ExceptionInfo *exception)
3082{
3083#define ModeImageTag "Mode/Image"
3084
3085 CacheView
3086 *image_view,
3087 *mode_view;
3088
3089 Image
3090 *mode_image;
3091
3092 MagickBooleanType
3093 status;
3094
3095 MagickOffsetType
3096 progress;
3097
3098 PixelList
3099 **restrict pixel_list;
3100
3101 size_t
3102 width;
3103
3104 ssize_t
3105 y;
3106
3107 /*
3108 Initialize mode image attributes.
3109 */
3110 assert(image != (Image *) NULL);
3111 assert(image->signature == MagickSignature);
3112 if (image->debug != MagickFalse)
3113 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3114 assert(exception != (ExceptionInfo *) NULL);
3115 assert(exception->signature == MagickSignature);
3116 width=GetOptimalKernelWidth2D(radius,0.5);
cristy69ec32d2011-02-27 23:57:09 +00003117 mode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3118 exception);
3119 if (mode_image == (Image *) NULL)
3120 return((Image *) NULL);
3121 if (SetImageStorageClass(mode_image,DirectClass) == MagickFalse)
3122 {
3123 InheritException(exception,&mode_image->exception);
3124 mode_image=DestroyImage(mode_image);
3125 return((Image *) NULL);
3126 }
3127 pixel_list=AcquirePixelListThreadSet(width);
3128 if (pixel_list == (PixelList **) NULL)
3129 {
3130 mode_image=DestroyImage(mode_image);
3131 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3132 }
3133 /*
3134 Reduce mode image.
3135 */
3136 status=MagickTrue;
3137 progress=0;
3138 image_view=AcquireCacheView(image);
3139 mode_view=AcquireCacheView(mode_image);
3140#if defined(MAGICKCORE_OPENMP_SUPPORT)
3141 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3142#endif
3143 for (y=0; y < (ssize_t) mode_image->rows; y++)
3144 {
3145 const int
3146 id = GetOpenMPThreadId();
3147
3148 register const IndexPacket
3149 *restrict indexes;
3150
3151 register const PixelPacket
3152 *restrict p;
3153
3154 register IndexPacket
3155 *restrict mode_indexes;
3156
3157 register PixelPacket
3158 *restrict q;
3159
3160 register ssize_t
3161 x;
3162
3163 if (status == MagickFalse)
3164 continue;
3165 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3166 (width/2L),image->columns+width,width,exception);
3167 q=QueueCacheViewAuthenticPixels(mode_view,0,y,mode_image->columns,1,
3168 exception);
3169 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
3170 {
3171 status=MagickFalse;
3172 continue;
3173 }
3174 indexes=GetCacheViewVirtualIndexQueue(image_view);
3175 mode_indexes=GetCacheViewAuthenticIndexQueue(mode_view);
3176 for (x=0; x < (ssize_t) mode_image->columns; x++)
3177 {
3178 MagickPixelPacket
3179 pixel;
3180
3181 register const PixelPacket
3182 *restrict r;
3183
3184 register const IndexPacket
3185 *restrict s;
3186
3187 register ssize_t
3188 u,
3189 v;
3190
3191 r=p;
3192 s=indexes+x;
3193 ResetPixelList(pixel_list[id]);
3194 for (v=0; v < (ssize_t) width; v++)
3195 {
3196 for (u=0; u < (ssize_t) width; u++)
3197 InsertPixelList(image,r+u,s+u,pixel_list[id]);
3198 r+=image->columns+width;
3199 s+=image->columns+width;
3200 }
3201 pixel=GetModePixelList(pixel_list[id]);
3202 SetPixelPacket(mode_image,&pixel,q,mode_indexes+x);
3203 p++;
3204 q++;
3205 }
3206 if (SyncCacheViewAuthenticPixels(mode_view,exception) == MagickFalse)
3207 status=MagickFalse;
3208 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3209 {
3210 MagickBooleanType
3211 proceed;
3212
3213#if defined(MAGICKCORE_OPENMP_SUPPORT)
3214 #pragma omp critical (MagickCore_ModeImage)
3215#endif
3216 proceed=SetImageProgress(image,ModeImageTag,progress++,image->rows);
3217 if (proceed == MagickFalse)
3218 status=MagickFalse;
3219 }
3220 }
3221 mode_view=DestroyCacheView(mode_view);
3222 image_view=DestroyCacheView(image_view);
3223 pixel_list=DestroyPixelListThreadSet(pixel_list);
3224 return(mode_image);
3225}
3226
3227/*
3228%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3229% %
3230% %
3231% %
cristy3ed852e2009-09-05 21:47:34 +00003232% M o t i o n B l u r I m a g e %
3233% %
3234% %
3235% %
3236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3237%
3238% MotionBlurImage() simulates motion blur. We convolve the image with a
3239% Gaussian operator of the given radius and standard deviation (sigma).
3240% For reasonable results, radius should be larger than sigma. Use a
3241% radius of 0 and MotionBlurImage() selects a suitable radius for you.
3242% Angle gives the angle of the blurring motion.
3243%
3244% Andrew Protano contributed this effect.
3245%
3246% The format of the MotionBlurImage method is:
3247%
3248% Image *MotionBlurImage(const Image *image,const double radius,
3249% const double sigma,const double angle,ExceptionInfo *exception)
3250% Image *MotionBlurImageChannel(const Image *image,const ChannelType channel,
3251% const double radius,const double sigma,const double angle,
3252% ExceptionInfo *exception)
3253%
3254% A description of each parameter follows:
3255%
3256% o image: the image.
3257%
3258% o channel: the channel type.
3259%
3260% o radius: the radius of the Gaussian, in pixels, not counting the center
3261% o radius: the radius of the Gaussian, in pixels, not counting
3262% the center pixel.
3263%
3264% o sigma: the standard deviation of the Gaussian, in pixels.
3265%
cristycee97112010-05-28 00:44:52 +00003266% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003267%
3268% o exception: return any errors or warnings in this structure.
3269%
3270*/
3271
cristybb503372010-05-27 20:51:26 +00003272static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00003273{
cristy3ed852e2009-09-05 21:47:34 +00003274 double
cristy47e00502009-12-17 19:19:57 +00003275 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00003276 normalize;
3277
cristybb503372010-05-27 20:51:26 +00003278 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003279 i;
3280
3281 /*
cristy47e00502009-12-17 19:19:57 +00003282 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00003283 */
3284 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3285 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
3286 if (kernel == (double *) NULL)
3287 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00003288 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00003289 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00003290 {
cristy4205a3c2010-09-12 20:19:59 +00003291 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
3292 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003293 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00003294 }
cristybb503372010-05-27 20:51:26 +00003295 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00003296 kernel[i]/=normalize;
3297 return(kernel);
3298}
3299
3300MagickExport Image *MotionBlurImage(const Image *image,const double radius,
3301 const double sigma,const double angle,ExceptionInfo *exception)
3302{
3303 Image
3304 *motion_blur;
3305
3306 motion_blur=MotionBlurImageChannel(image,DefaultChannels,radius,sigma,angle,
3307 exception);
3308 return(motion_blur);
3309}
3310
3311MagickExport Image *MotionBlurImageChannel(const Image *image,
3312 const ChannelType channel,const double radius,const double sigma,
3313 const double angle,ExceptionInfo *exception)
3314{
cristyc4c8d132010-01-07 01:58:38 +00003315 CacheView
3316 *blur_view,
3317 *image_view;
3318
cristy3ed852e2009-09-05 21:47:34 +00003319 double
3320 *kernel;
3321
3322 Image
3323 *blur_image;
3324
cristy3ed852e2009-09-05 21:47:34 +00003325 MagickBooleanType
3326 status;
3327
cristybb503372010-05-27 20:51:26 +00003328 MagickOffsetType
3329 progress;
3330
cristy3ed852e2009-09-05 21:47:34 +00003331 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00003332 bias;
cristy3ed852e2009-09-05 21:47:34 +00003333
3334 OffsetInfo
3335 *offset;
3336
3337 PointInfo
3338 point;
3339
cristybb503372010-05-27 20:51:26 +00003340 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003341 i;
3342
cristybb503372010-05-27 20:51:26 +00003343 size_t
cristy3ed852e2009-09-05 21:47:34 +00003344 width;
3345
cristybb503372010-05-27 20:51:26 +00003346 ssize_t
3347 y;
3348
cristy3ed852e2009-09-05 21:47:34 +00003349 assert(image != (Image *) NULL);
3350 assert(image->signature == MagickSignature);
3351 if (image->debug != MagickFalse)
3352 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3353 assert(exception != (ExceptionInfo *) NULL);
3354 width=GetOptimalKernelWidth1D(radius,sigma);
3355 kernel=GetMotionBlurKernel(width,sigma);
3356 if (kernel == (double *) NULL)
3357 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3358 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
3359 if (offset == (OffsetInfo *) NULL)
3360 {
3361 kernel=(double *) RelinquishMagickMemory(kernel);
3362 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3363 }
3364 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3365 if (blur_image == (Image *) NULL)
3366 {
3367 kernel=(double *) RelinquishMagickMemory(kernel);
3368 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3369 return((Image *) NULL);
3370 }
3371 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3372 {
3373 kernel=(double *) RelinquishMagickMemory(kernel);
3374 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3375 InheritException(exception,&blur_image->exception);
3376 blur_image=DestroyImage(blur_image);
3377 return((Image *) NULL);
3378 }
3379 point.x=(double) width*sin(DegreesToRadians(angle));
3380 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00003381 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00003382 {
cristybb503372010-05-27 20:51:26 +00003383 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
3384 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003385 }
3386 /*
3387 Motion blur image.
3388 */
3389 status=MagickTrue;
3390 progress=0;
cristyddd82202009-11-03 20:14:50 +00003391 GetMagickPixelPacket(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003392 image_view=AcquireCacheView(image);
3393 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00003394#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00003395 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00003396#endif
cristybb503372010-05-27 20:51:26 +00003397 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003398 {
3399 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003400 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00003401
cristy3ed852e2009-09-05 21:47:34 +00003402 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003403 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003404
cristy117ff172010-08-15 21:35:32 +00003405 register ssize_t
3406 x;
3407
cristy3ed852e2009-09-05 21:47:34 +00003408 if (status == MagickFalse)
3409 continue;
3410 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3411 exception);
3412 if (q == (PixelPacket *) NULL)
3413 {
3414 status=MagickFalse;
3415 continue;
3416 }
3417 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +00003418 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003419 {
3420 MagickPixelPacket
3421 qixel;
3422
3423 PixelPacket
3424 pixel;
3425
cristy117ff172010-08-15 21:35:32 +00003426 register const IndexPacket
3427 *restrict indexes;
3428
cristy3ed852e2009-09-05 21:47:34 +00003429 register double
cristyc47d1f82009-11-26 01:44:43 +00003430 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00003431
cristybb503372010-05-27 20:51:26 +00003432 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003433 i;
3434
cristy3ed852e2009-09-05 21:47:34 +00003435 k=kernel;
cristyddd82202009-11-03 20:14:50 +00003436 qixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00003437 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
3438 {
cristybb503372010-05-27 20:51:26 +00003439 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00003440 {
3441 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
3442 offset[i].y,&pixel,exception);
3443 qixel.red+=(*k)*pixel.red;
3444 qixel.green+=(*k)*pixel.green;
3445 qixel.blue+=(*k)*pixel.blue;
3446 qixel.opacity+=(*k)*pixel.opacity;
3447 if (image->colorspace == CMYKColorspace)
3448 {
3449 indexes=GetCacheViewVirtualIndexQueue(image_view);
3450 qixel.index+=(*k)*(*indexes);
3451 }
3452 k++;
3453 }
3454 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003455 q->red=ClampToQuantum(qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00003456 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003457 q->green=ClampToQuantum(qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00003458 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003459 q->blue=ClampToQuantum(qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00003460 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003461 q->opacity=ClampToQuantum(qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00003462 if (((channel & IndexChannel) != 0) &&
3463 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00003464 blur_indexes[x]=(IndexPacket) ClampToQuantum(qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00003465 }
3466 else
3467 {
3468 MagickRealType
3469 alpha,
3470 gamma;
3471
3472 alpha=0.0;
3473 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00003474 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00003475 {
3476 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
3477 offset[i].y,&pixel,exception);
cristy8a7ea362010-03-10 20:31:43 +00003478 alpha=(MagickRealType) (QuantumScale*
3479 GetAlphaPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00003480 qixel.red+=(*k)*alpha*pixel.red;
3481 qixel.green+=(*k)*alpha*pixel.green;
3482 qixel.blue+=(*k)*alpha*pixel.blue;
3483 qixel.opacity+=(*k)*pixel.opacity;
3484 if (image->colorspace == CMYKColorspace)
3485 {
3486 indexes=GetCacheViewVirtualIndexQueue(image_view);
3487 qixel.index+=(*k)*alpha*(*indexes);
3488 }
3489 gamma+=(*k)*alpha;
3490 k++;
3491 }
3492 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3493 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003494 q->red=ClampToQuantum(gamma*qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00003495 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003496 q->green=ClampToQuantum(gamma*qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00003497 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003498 q->blue=ClampToQuantum(gamma*qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00003499 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003500 q->opacity=ClampToQuantum(qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00003501 if (((channel & IndexChannel) != 0) &&
3502 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00003503 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00003504 }
3505 q++;
3506 }
3507 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3508 status=MagickFalse;
3509 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3510 {
3511 MagickBooleanType
3512 proceed;
3513
cristyb557a152011-02-22 12:14:30 +00003514#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003515 #pragma omp critical (MagickCore_MotionBlurImageChannel)
3516#endif
3517 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3518 if (proceed == MagickFalse)
3519 status=MagickFalse;
3520 }
3521 }
3522 blur_view=DestroyCacheView(blur_view);
3523 image_view=DestroyCacheView(image_view);
3524 kernel=(double *) RelinquishMagickMemory(kernel);
3525 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3526 if (status == MagickFalse)
3527 blur_image=DestroyImage(blur_image);
3528 return(blur_image);
3529}
3530
3531/*
3532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3533% %
3534% %
3535% %
3536% P r e v i e w I m a g e %
3537% %
3538% %
3539% %
3540%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3541%
3542% PreviewImage() tiles 9 thumbnails of the specified image with an image
3543% processing operation applied with varying parameters. This may be helpful
3544% pin-pointing an appropriate parameter for a particular image processing
3545% operation.
3546%
3547% The format of the PreviewImages method is:
3548%
3549% Image *PreviewImages(const Image *image,const PreviewType preview,
3550% ExceptionInfo *exception)
3551%
3552% A description of each parameter follows:
3553%
3554% o image: the image.
3555%
3556% o preview: the image processing operation.
3557%
3558% o exception: return any errors or warnings in this structure.
3559%
3560*/
3561MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
3562 ExceptionInfo *exception)
3563{
3564#define NumberTiles 9
3565#define PreviewImageTag "Preview/Image"
3566#define DefaultPreviewGeometry "204x204+10+10"
3567
3568 char
3569 factor[MaxTextExtent],
3570 label[MaxTextExtent];
3571
3572 double
3573 degrees,
3574 gamma,
3575 percentage,
3576 radius,
3577 sigma,
3578 threshold;
3579
3580 Image
3581 *images,
3582 *montage_image,
3583 *preview_image,
3584 *thumbnail;
3585
3586 ImageInfo
3587 *preview_info;
3588
cristy3ed852e2009-09-05 21:47:34 +00003589 MagickBooleanType
3590 proceed;
3591
3592 MontageInfo
3593 *montage_info;
3594
3595 QuantizeInfo
3596 quantize_info;
3597
3598 RectangleInfo
3599 geometry;
3600
cristybb503372010-05-27 20:51:26 +00003601 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003602 i,
3603 x;
3604
cristybb503372010-05-27 20:51:26 +00003605 size_t
cristy3ed852e2009-09-05 21:47:34 +00003606 colors;
3607
cristy117ff172010-08-15 21:35:32 +00003608 ssize_t
3609 y;
3610
cristy3ed852e2009-09-05 21:47:34 +00003611 /*
3612 Open output image file.
3613 */
3614 assert(image != (Image *) NULL);
3615 assert(image->signature == MagickSignature);
3616 if (image->debug != MagickFalse)
3617 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3618 colors=2;
3619 degrees=0.0;
3620 gamma=(-0.2f);
3621 preview_info=AcquireImageInfo();
3622 SetGeometry(image,&geometry);
3623 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
3624 &geometry.width,&geometry.height);
3625 images=NewImageList();
3626 percentage=12.5;
3627 GetQuantizeInfo(&quantize_info);
3628 radius=0.0;
3629 sigma=1.0;
3630 threshold=0.0;
3631 x=0;
3632 y=0;
3633 for (i=0; i < NumberTiles; i++)
3634 {
3635 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
3636 if (thumbnail == (Image *) NULL)
3637 break;
3638 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
3639 (void *) NULL);
3640 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
3641 if (i == (NumberTiles/2))
3642 {
3643 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
3644 AppendImageToList(&images,thumbnail);
3645 continue;
3646 }
3647 switch (preview)
3648 {
3649 case RotatePreview:
3650 {
3651 degrees+=45.0;
3652 preview_image=RotateImage(thumbnail,degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003653 (void) FormatMagickString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00003654 break;
3655 }
3656 case ShearPreview:
3657 {
3658 degrees+=5.0;
3659 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003660 (void) FormatMagickString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00003661 degrees,2.0*degrees);
3662 break;
3663 }
3664 case RollPreview:
3665 {
cristybb503372010-05-27 20:51:26 +00003666 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
3667 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00003668 preview_image=RollImage(thumbnail,x,y,exception);
cristye8c25f92010-06-03 00:53:06 +00003669 (void) FormatMagickString(label,MaxTextExtent,"roll %+.20gx%+.20g",
3670 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00003671 break;
3672 }
3673 case HuePreview:
3674 {
3675 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3676 if (preview_image == (Image *) NULL)
3677 break;
cristye7f51092010-01-17 00:39:37 +00003678 (void) FormatMagickString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00003679 2.0*percentage);
3680 (void) ModulateImage(preview_image,factor);
3681 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3682 break;
3683 }
3684 case SaturationPreview:
3685 {
3686 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3687 if (preview_image == (Image *) NULL)
3688 break;
cristye7f51092010-01-17 00:39:37 +00003689 (void) FormatMagickString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00003690 2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00003691 (void) ModulateImage(preview_image,factor);
3692 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3693 break;
3694 }
3695 case BrightnessPreview:
3696 {
3697 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3698 if (preview_image == (Image *) NULL)
3699 break;
cristye7f51092010-01-17 00:39:37 +00003700 (void) FormatMagickString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00003701 (void) ModulateImage(preview_image,factor);
3702 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3703 break;
3704 }
3705 case GammaPreview:
3706 default:
3707 {
3708 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3709 if (preview_image == (Image *) NULL)
3710 break;
3711 gamma+=0.4f;
3712 (void) GammaImageChannel(preview_image,DefaultChannels,gamma);
cristye7f51092010-01-17 00:39:37 +00003713 (void) FormatMagickString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00003714 break;
3715 }
3716 case SpiffPreview:
3717 {
3718 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3719 if (preview_image != (Image *) NULL)
3720 for (x=0; x < i; x++)
3721 (void) ContrastImage(preview_image,MagickTrue);
cristye8c25f92010-06-03 00:53:06 +00003722 (void) FormatMagickString(label,MaxTextExtent,"contrast (%.20g)",
3723 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00003724 break;
3725 }
3726 case DullPreview:
3727 {
3728 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3729 if (preview_image == (Image *) NULL)
3730 break;
3731 for (x=0; x < i; x++)
3732 (void) ContrastImage(preview_image,MagickFalse);
cristye8c25f92010-06-03 00:53:06 +00003733 (void) FormatMagickString(label,MaxTextExtent,"+contrast (%.20g)",
3734 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00003735 break;
3736 }
3737 case GrayscalePreview:
3738 {
3739 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3740 if (preview_image == (Image *) NULL)
3741 break;
3742 colors<<=1;
3743 quantize_info.number_colors=colors;
3744 quantize_info.colorspace=GRAYColorspace;
3745 (void) QuantizeImage(&quantize_info,preview_image);
3746 (void) FormatMagickString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00003747 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00003748 break;
3749 }
3750 case QuantizePreview:
3751 {
3752 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3753 if (preview_image == (Image *) NULL)
3754 break;
3755 colors<<=1;
3756 quantize_info.number_colors=colors;
3757 (void) QuantizeImage(&quantize_info,preview_image);
cristye8c25f92010-06-03 00:53:06 +00003758 (void) FormatMagickString(label,MaxTextExtent,"colors %.20g",(double)
3759 colors);
cristy3ed852e2009-09-05 21:47:34 +00003760 break;
3761 }
3762 case DespecklePreview:
3763 {
3764 for (x=0; x < (i-1); x++)
3765 {
3766 preview_image=DespeckleImage(thumbnail,exception);
3767 if (preview_image == (Image *) NULL)
3768 break;
3769 thumbnail=DestroyImage(thumbnail);
3770 thumbnail=preview_image;
3771 }
3772 preview_image=DespeckleImage(thumbnail,exception);
3773 if (preview_image == (Image *) NULL)
3774 break;
cristye8c25f92010-06-03 00:53:06 +00003775 (void) FormatMagickString(label,MaxTextExtent,"despeckle (%.20g)",
3776 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00003777 break;
3778 }
3779 case ReduceNoisePreview:
3780 {
3781 preview_image=ReduceNoiseImage(thumbnail,radius,exception);
cristye7f51092010-01-17 00:39:37 +00003782 (void) FormatMagickString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00003783 break;
3784 }
3785 case AddNoisePreview:
3786 {
3787 switch ((int) i)
3788 {
3789 case 0:
3790 {
3791 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
3792 break;
3793 }
3794 case 1:
3795 {
3796 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
3797 break;
3798 }
3799 case 2:
3800 {
3801 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
3802 break;
3803 }
3804 case 3:
3805 {
3806 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
3807 break;
3808 }
3809 case 4:
3810 {
3811 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
3812 break;
3813 }
3814 case 5:
3815 {
3816 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
3817 break;
3818 }
3819 default:
3820 {
3821 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
3822 break;
3823 }
3824 }
3825 preview_image=ReduceNoiseImage(thumbnail,(double) i,exception);
3826 (void) FormatMagickString(label,MaxTextExtent,"+noise %s",factor);
3827 break;
3828 }
3829 case SharpenPreview:
3830 {
3831 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
cristye7f51092010-01-17 00:39:37 +00003832 (void) FormatMagickString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00003833 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00003834 break;
3835 }
3836 case BlurPreview:
3837 {
3838 preview_image=BlurImage(thumbnail,radius,sigma,exception);
cristye7f51092010-01-17 00:39:37 +00003839 (void) FormatMagickString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00003840 sigma);
3841 break;
3842 }
3843 case ThresholdPreview:
3844 {
3845 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3846 if (preview_image == (Image *) NULL)
3847 break;
3848 (void) BilevelImage(thumbnail,
3849 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
cristye7f51092010-01-17 00:39:37 +00003850 (void) FormatMagickString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00003851 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
3852 break;
3853 }
3854 case EdgeDetectPreview:
3855 {
3856 preview_image=EdgeImage(thumbnail,radius,exception);
cristye7f51092010-01-17 00:39:37 +00003857 (void) FormatMagickString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00003858 break;
3859 }
3860 case SpreadPreview:
3861 {
3862 preview_image=SpreadImage(thumbnail,radius,exception);
cristye7f51092010-01-17 00:39:37 +00003863 (void) FormatMagickString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00003864 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00003865 break;
3866 }
3867 case SolarizePreview:
3868 {
3869 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3870 if (preview_image == (Image *) NULL)
3871 break;
3872 (void) SolarizeImage(preview_image,(double) QuantumRange*
3873 percentage/100.0);
cristye7f51092010-01-17 00:39:37 +00003874 (void) FormatMagickString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00003875 (QuantumRange*percentage)/100.0);
3876 break;
3877 }
3878 case ShadePreview:
3879 {
3880 degrees+=10.0;
3881 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
3882 exception);
cristye7f51092010-01-17 00:39:37 +00003883 (void) FormatMagickString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00003884 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00003885 break;
3886 }
3887 case RaisePreview:
3888 {
3889 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3890 if (preview_image == (Image *) NULL)
3891 break;
cristybb503372010-05-27 20:51:26 +00003892 geometry.width=(size_t) (2*i+2);
3893 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00003894 geometry.x=i/2;
3895 geometry.y=i/2;
3896 (void) RaiseImage(preview_image,&geometry,MagickTrue);
cristye8c25f92010-06-03 00:53:06 +00003897 (void) FormatMagickString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00003898 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00003899 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00003900 break;
3901 }
3902 case SegmentPreview:
3903 {
3904 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3905 if (preview_image == (Image *) NULL)
3906 break;
3907 threshold+=0.4f;
3908 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
3909 threshold);
cristye7f51092010-01-17 00:39:37 +00003910 (void) FormatMagickString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00003911 threshold,threshold);
3912 break;
3913 }
3914 case SwirlPreview:
3915 {
3916 preview_image=SwirlImage(thumbnail,degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003917 (void) FormatMagickString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00003918 degrees+=45.0;
3919 break;
3920 }
3921 case ImplodePreview:
3922 {
3923 degrees+=0.1f;
3924 preview_image=ImplodeImage(thumbnail,degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003925 (void) FormatMagickString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00003926 break;
3927 }
3928 case WavePreview:
3929 {
3930 degrees+=5.0f;
3931 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
cristye7f51092010-01-17 00:39:37 +00003932 (void) FormatMagickString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00003933 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00003934 break;
3935 }
3936 case OilPaintPreview:
3937 {
3938 preview_image=OilPaintImage(thumbnail,(double) radius,exception);
cristye7f51092010-01-17 00:39:37 +00003939 (void) FormatMagickString(label,MaxTextExtent,"paint %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00003940 break;
3941 }
3942 case CharcoalDrawingPreview:
3943 {
3944 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
3945 exception);
cristye7f51092010-01-17 00:39:37 +00003946 (void) FormatMagickString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00003947 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00003948 break;
3949 }
3950 case JPEGPreview:
3951 {
3952 char
3953 filename[MaxTextExtent];
3954
3955 int
3956 file;
3957
3958 MagickBooleanType
3959 status;
3960
3961 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3962 if (preview_image == (Image *) NULL)
3963 break;
cristybb503372010-05-27 20:51:26 +00003964 preview_info->quality=(size_t) percentage;
cristye8c25f92010-06-03 00:53:06 +00003965 (void) FormatMagickString(factor,MaxTextExtent,"%.20g",(double)
3966 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00003967 file=AcquireUniqueFileResource(filename);
3968 if (file != -1)
3969 file=close(file)-1;
3970 (void) FormatMagickString(preview_image->filename,MaxTextExtent,
3971 "jpeg:%s",filename);
3972 status=WriteImage(preview_info,preview_image);
3973 if (status != MagickFalse)
3974 {
3975 Image
3976 *quality_image;
3977
3978 (void) CopyMagickString(preview_info->filename,
3979 preview_image->filename,MaxTextExtent);
3980 quality_image=ReadImage(preview_info,exception);
3981 if (quality_image != (Image *) NULL)
3982 {
3983 preview_image=DestroyImage(preview_image);
3984 preview_image=quality_image;
3985 }
3986 }
3987 (void) RelinquishUniqueFileResource(preview_image->filename);
3988 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristye7f51092010-01-17 00:39:37 +00003989 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00003990 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
3991 1024.0/1024.0);
3992 else
3993 if (GetBlobSize(preview_image) >= 1024)
cristy8cd5b312010-01-07 01:10:24 +00003994 (void) FormatMagickString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00003995 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00003996 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00003997 else
cristye8c25f92010-06-03 00:53:06 +00003998 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%.20gb ",
3999 factor,(double) GetBlobSize(thumbnail));
cristy3ed852e2009-09-05 21:47:34 +00004000 break;
4001 }
4002 }
4003 thumbnail=DestroyImage(thumbnail);
4004 percentage+=12.5;
4005 radius+=0.5;
4006 sigma+=0.25;
4007 if (preview_image == (Image *) NULL)
4008 break;
4009 (void) DeleteImageProperty(preview_image,"label");
4010 (void) SetImageProperty(preview_image,"label",label);
4011 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00004012 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
4013 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00004014 if (proceed == MagickFalse)
4015 break;
4016 }
4017 if (images == (Image *) NULL)
4018 {
4019 preview_info=DestroyImageInfo(preview_info);
4020 return((Image *) NULL);
4021 }
4022 /*
4023 Create the montage.
4024 */
4025 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
4026 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
4027 montage_info->shadow=MagickTrue;
4028 (void) CloneString(&montage_info->tile,"3x3");
4029 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
4030 (void) CloneString(&montage_info->frame,DefaultTileFrame);
4031 montage_image=MontageImages(images,montage_info,exception);
4032 montage_info=DestroyMontageInfo(montage_info);
4033 images=DestroyImageList(images);
4034 if (montage_image == (Image *) NULL)
4035 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4036 if (montage_image->montage != (char *) NULL)
4037 {
4038 /*
4039 Free image directory.
4040 */
4041 montage_image->montage=(char *) RelinquishMagickMemory(
4042 montage_image->montage);
4043 if (image->directory != (char *) NULL)
4044 montage_image->directory=(char *) RelinquishMagickMemory(
4045 montage_image->directory);
4046 }
4047 preview_info=DestroyImageInfo(preview_info);
4048 return(montage_image);
4049}
4050
4051/*
4052%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4053% %
4054% %
4055% %
4056% R a d i a l B l u r I m a g e %
4057% %
4058% %
4059% %
4060%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4061%
4062% RadialBlurImage() applies a radial blur to the image.
4063%
4064% Andrew Protano contributed this effect.
4065%
4066% The format of the RadialBlurImage method is:
4067%
4068% Image *RadialBlurImage(const Image *image,const double angle,
4069% ExceptionInfo *exception)
4070% Image *RadialBlurImageChannel(const Image *image,const ChannelType channel,
4071% const double angle,ExceptionInfo *exception)
4072%
4073% A description of each parameter follows:
4074%
4075% o image: the image.
4076%
4077% o channel: the channel type.
4078%
4079% o angle: the angle of the radial blur.
4080%
4081% o exception: return any errors or warnings in this structure.
4082%
4083*/
4084
4085MagickExport Image *RadialBlurImage(const Image *image,const double angle,
4086 ExceptionInfo *exception)
4087{
4088 Image
4089 *blur_image;
4090
4091 blur_image=RadialBlurImageChannel(image,DefaultChannels,angle,exception);
4092 return(blur_image);
4093}
4094
4095MagickExport Image *RadialBlurImageChannel(const Image *image,
4096 const ChannelType channel,const double angle,ExceptionInfo *exception)
4097{
cristyc4c8d132010-01-07 01:58:38 +00004098 CacheView
4099 *blur_view,
4100 *image_view;
4101
cristy3ed852e2009-09-05 21:47:34 +00004102 Image
4103 *blur_image;
4104
cristy3ed852e2009-09-05 21:47:34 +00004105 MagickBooleanType
4106 status;
4107
cristybb503372010-05-27 20:51:26 +00004108 MagickOffsetType
4109 progress;
4110
cristy3ed852e2009-09-05 21:47:34 +00004111 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00004112 bias;
cristy3ed852e2009-09-05 21:47:34 +00004113
4114 MagickRealType
4115 blur_radius,
4116 *cos_theta,
4117 offset,
4118 *sin_theta,
4119 theta;
4120
4121 PointInfo
4122 blur_center;
4123
cristybb503372010-05-27 20:51:26 +00004124 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004125 i;
4126
cristybb503372010-05-27 20:51:26 +00004127 size_t
cristy3ed852e2009-09-05 21:47:34 +00004128 n;
4129
cristybb503372010-05-27 20:51:26 +00004130 ssize_t
4131 y;
4132
cristy3ed852e2009-09-05 21:47:34 +00004133 /*
4134 Allocate blur image.
4135 */
4136 assert(image != (Image *) NULL);
4137 assert(image->signature == MagickSignature);
4138 if (image->debug != MagickFalse)
4139 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4140 assert(exception != (ExceptionInfo *) NULL);
4141 assert(exception->signature == MagickSignature);
4142 blur_image=CloneImage(image,0,0,MagickTrue,exception);
4143 if (blur_image == (Image *) NULL)
4144 return((Image *) NULL);
4145 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
4146 {
4147 InheritException(exception,&blur_image->exception);
4148 blur_image=DestroyImage(blur_image);
4149 return((Image *) NULL);
4150 }
4151 blur_center.x=(double) image->columns/2.0;
4152 blur_center.y=(double) image->rows/2.0;
4153 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00004154 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00004155 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
4156 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
4157 sizeof(*cos_theta));
4158 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
4159 sizeof(*sin_theta));
4160 if ((cos_theta == (MagickRealType *) NULL) ||
4161 (sin_theta == (MagickRealType *) NULL))
4162 {
4163 blur_image=DestroyImage(blur_image);
4164 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4165 }
4166 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00004167 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00004168 {
4169 cos_theta[i]=cos((double) (theta*i-offset));
4170 sin_theta[i]=sin((double) (theta*i-offset));
4171 }
4172 /*
4173 Radial blur image.
4174 */
4175 status=MagickTrue;
4176 progress=0;
cristyddd82202009-11-03 20:14:50 +00004177 GetMagickPixelPacket(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00004178 image_view=AcquireCacheView(image);
4179 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00004180#if defined(MAGICKCORE_OPENMP_SUPPORT)
4181 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004182#endif
cristybb503372010-05-27 20:51:26 +00004183 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004184 {
4185 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004186 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00004187
4188 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004189 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00004190
cristy3ed852e2009-09-05 21:47:34 +00004191 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004192 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004193
cristy117ff172010-08-15 21:35:32 +00004194 register ssize_t
4195 x;
4196
cristy3ed852e2009-09-05 21:47:34 +00004197 if (status == MagickFalse)
4198 continue;
4199 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
4200 exception);
4201 if (q == (PixelPacket *) NULL)
4202 {
4203 status=MagickFalse;
4204 continue;
4205 }
4206 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +00004207 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004208 {
4209 MagickPixelPacket
4210 qixel;
4211
4212 MagickRealType
4213 normalize,
4214 radius;
4215
4216 PixelPacket
4217 pixel;
4218
4219 PointInfo
4220 center;
4221
cristybb503372010-05-27 20:51:26 +00004222 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004223 i;
4224
cristybb503372010-05-27 20:51:26 +00004225 size_t
cristy3ed852e2009-09-05 21:47:34 +00004226 step;
4227
4228 center.x=(double) x-blur_center.x;
4229 center.y=(double) y-blur_center.y;
4230 radius=hypot((double) center.x,center.y);
4231 if (radius == 0)
4232 step=1;
4233 else
4234 {
cristybb503372010-05-27 20:51:26 +00004235 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00004236 if (step == 0)
4237 step=1;
4238 else
4239 if (step >= n)
4240 step=n-1;
4241 }
4242 normalize=0.0;
cristyddd82202009-11-03 20:14:50 +00004243 qixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00004244 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
4245 {
cristyeaedf062010-05-29 22:36:02 +00004246 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00004247 {
cristyeaedf062010-05-29 22:36:02 +00004248 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
4249 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
4250 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
4251 cos_theta[i]+0.5),&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004252 qixel.red+=pixel.red;
4253 qixel.green+=pixel.green;
4254 qixel.blue+=pixel.blue;
4255 qixel.opacity+=pixel.opacity;
4256 if (image->colorspace == CMYKColorspace)
4257 {
4258 indexes=GetCacheViewVirtualIndexQueue(image_view);
4259 qixel.index+=(*indexes);
4260 }
4261 normalize+=1.0;
4262 }
4263 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
4264 normalize);
4265 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004266 q->red=ClampToQuantum(normalize*qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00004267 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004268 q->green=ClampToQuantum(normalize*qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00004269 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004270 q->blue=ClampToQuantum(normalize*qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00004271 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004272 q->opacity=ClampToQuantum(normalize*qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00004273 if (((channel & IndexChannel) != 0) &&
4274 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00004275 blur_indexes[x]=(IndexPacket) ClampToQuantum(normalize*qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00004276 }
4277 else
4278 {
4279 MagickRealType
4280 alpha,
4281 gamma;
4282
4283 alpha=1.0;
4284 gamma=0.0;
cristyeaedf062010-05-29 22:36:02 +00004285 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00004286 {
cristyeaedf062010-05-29 22:36:02 +00004287 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
4288 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
4289 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
4290 cos_theta[i]+0.5),&pixel,exception);
cristy46f08202010-01-10 04:04:21 +00004291 alpha=(MagickRealType) (QuantumScale*
4292 GetAlphaPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004293 qixel.red+=alpha*pixel.red;
4294 qixel.green+=alpha*pixel.green;
4295 qixel.blue+=alpha*pixel.blue;
4296 qixel.opacity+=pixel.opacity;
4297 if (image->colorspace == CMYKColorspace)
4298 {
4299 indexes=GetCacheViewVirtualIndexQueue(image_view);
4300 qixel.index+=alpha*(*indexes);
4301 }
4302 gamma+=alpha;
4303 normalize+=1.0;
4304 }
4305 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4306 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
4307 normalize);
4308 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004309 q->red=ClampToQuantum(gamma*qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00004310 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004311 q->green=ClampToQuantum(gamma*qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00004312 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004313 q->blue=ClampToQuantum(gamma*qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00004314 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004315 q->opacity=ClampToQuantum(normalize*qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00004316 if (((channel & IndexChannel) != 0) &&
4317 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00004318 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00004319 }
4320 q++;
4321 }
4322 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
4323 status=MagickFalse;
4324 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4325 {
4326 MagickBooleanType
4327 proceed;
4328
cristyb5d5f722009-11-04 03:03:49 +00004329#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004330 #pragma omp critical (MagickCore_RadialBlurImageChannel)
4331#endif
4332 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
4333 if (proceed == MagickFalse)
4334 status=MagickFalse;
4335 }
4336 }
4337 blur_view=DestroyCacheView(blur_view);
4338 image_view=DestroyCacheView(image_view);
4339 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
4340 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
4341 if (status == MagickFalse)
4342 blur_image=DestroyImage(blur_image);
4343 return(blur_image);
4344}
4345
4346/*
4347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4348% %
4349% %
4350% %
4351% R e d u c e N o i s e I m a g e %
4352% %
4353% %
4354% %
4355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4356%
4357% ReduceNoiseImage() smooths the contours of an image while still preserving
4358% edge information. The algorithm works by replacing each pixel with its
4359% neighbor closest in value. A neighbor is defined by radius. Use a radius
4360% of 0 and ReduceNoise() selects a suitable radius for you.
4361%
4362% The format of the ReduceNoiseImage method is:
4363%
4364% Image *ReduceNoiseImage(const Image *image,const double radius,
4365% ExceptionInfo *exception)
4366%
4367% A description of each parameter follows:
4368%
4369% o image: the image.
4370%
4371% o radius: the radius of the pixel neighborhood.
4372%
4373% o exception: return any errors or warnings in this structure.
4374%
4375*/
4376
cristy69ec32d2011-02-27 23:57:09 +00004377static MagickPixelPacket GetNonpeakPixelList(PixelList *pixel_list)
cristy3ed852e2009-09-05 21:47:34 +00004378{
4379 MagickPixelPacket
4380 pixel;
4381
cristy69ec32d2011-02-27 23:57:09 +00004382 register SkipList
cristy3ed852e2009-09-05 21:47:34 +00004383 *list;
4384
cristy117ff172010-08-15 21:35:32 +00004385 register ssize_t
4386 channel;
4387
cristybb503372010-05-27 20:51:26 +00004388 size_t
cristy3ed852e2009-09-05 21:47:34 +00004389 center,
4390 color,
4391 count,
cristya6020132011-02-28 01:06:03 +00004392 next,
4393 previous;
cristy3ed852e2009-09-05 21:47:34 +00004394
4395 unsigned short
4396 channels[5];
4397
4398 /*
4399 Finds the median value for each of the color.
4400 */
4401 center=pixel_list->center;
4402 for (channel=0; channel < 5; channel++)
4403 {
4404 list=pixel_list->lists+channel;
4405 color=65536UL;
4406 next=list->nodes[color].next[0];
4407 count=0;
4408 do
4409 {
4410 previous=color;
4411 color=next;
4412 next=list->nodes[color].next[0];
4413 count+=list->nodes[color].count;
4414 }
4415 while (count <= center);
4416 if ((previous == 65536UL) && (next != 65536UL))
4417 color=next;
4418 else
4419 if ((previous != 65536UL) && (next == 65536UL))
4420 color=previous;
4421 channels[channel]=(unsigned short) color;
4422 }
4423 GetMagickPixelPacket((const Image *) NULL,&pixel);
4424 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4425 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4426 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4427 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
4428 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
4429 return(pixel);
4430}
4431
4432MagickExport Image *ReduceNoiseImage(const Image *image,const double radius,
4433 ExceptionInfo *exception)
4434{
4435#define ReduceNoiseImageTag "ReduceNoise/Image"
4436
cristyfa112112010-01-04 17:48:07 +00004437 CacheView
4438 *image_view,
4439 *noise_view;
4440
cristy3ed852e2009-09-05 21:47:34 +00004441 Image
4442 *noise_image;
4443
cristy3ed852e2009-09-05 21:47:34 +00004444 MagickBooleanType
4445 status;
4446
cristybb503372010-05-27 20:51:26 +00004447 MagickOffsetType
4448 progress;
4449
cristy69ec32d2011-02-27 23:57:09 +00004450 PixelList
cristyfa112112010-01-04 17:48:07 +00004451 **restrict pixel_list;
cristy3ed852e2009-09-05 21:47:34 +00004452
cristybb503372010-05-27 20:51:26 +00004453 size_t
cristy3ed852e2009-09-05 21:47:34 +00004454 width;
4455
cristybb503372010-05-27 20:51:26 +00004456 ssize_t
4457 y;
4458
cristy3ed852e2009-09-05 21:47:34 +00004459 /*
4460 Initialize noise image attributes.
4461 */
4462 assert(image != (Image *) NULL);
4463 assert(image->signature == MagickSignature);
4464 if (image->debug != MagickFalse)
4465 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4466 assert(exception != (ExceptionInfo *) NULL);
4467 assert(exception->signature == MagickSignature);
4468 width=GetOptimalKernelWidth2D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00004469 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4470 exception);
4471 if (noise_image == (Image *) NULL)
4472 return((Image *) NULL);
4473 if (SetImageStorageClass(noise_image,DirectClass) == MagickFalse)
4474 {
4475 InheritException(exception,&noise_image->exception);
4476 noise_image=DestroyImage(noise_image);
4477 return((Image *) NULL);
4478 }
cristy69ec32d2011-02-27 23:57:09 +00004479 pixel_list=AcquirePixelListThreadSet(width);
4480 if (pixel_list == (PixelList **) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004481 {
4482 noise_image=DestroyImage(noise_image);
4483 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4484 }
4485 /*
4486 Reduce noise image.
4487 */
4488 status=MagickTrue;
4489 progress=0;
4490 image_view=AcquireCacheView(image);
4491 noise_view=AcquireCacheView(noise_image);
cristyb5d5f722009-11-04 03:03:49 +00004492#if defined(MAGICKCORE_OPENMP_SUPPORT)
4493 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004494#endif
cristybb503372010-05-27 20:51:26 +00004495 for (y=0; y < (ssize_t) noise_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004496 {
cristy5c9e6f22010-09-17 17:31:01 +00004497 const int
4498 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004499
cristy3ed852e2009-09-05 21:47:34 +00004500 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004501 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00004502
4503 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004504 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004505
4506 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004507 *restrict noise_indexes;
cristy3ed852e2009-09-05 21:47:34 +00004508
cristy3ed852e2009-09-05 21:47:34 +00004509 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004510 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004511
cristy117ff172010-08-15 21:35:32 +00004512 register ssize_t
4513 x;
4514
cristy3ed852e2009-09-05 21:47:34 +00004515 if (status == MagickFalse)
4516 continue;
cristy6ebe97c2010-07-03 01:17:28 +00004517 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
4518 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00004519 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
4520 exception);
4521 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4522 {
4523 status=MagickFalse;
4524 continue;
4525 }
4526 indexes=GetCacheViewVirtualIndexQueue(image_view);
4527 noise_indexes=GetCacheViewAuthenticIndexQueue(noise_view);
cristybb503372010-05-27 20:51:26 +00004528 for (x=0; x < (ssize_t) noise_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004529 {
4530 MagickPixelPacket
4531 pixel;
4532
4533 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004534 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004535
4536 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004537 *restrict s;
cristy3ed852e2009-09-05 21:47:34 +00004538
cristybb503372010-05-27 20:51:26 +00004539 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004540 u,
4541 v;
4542
4543 r=p;
4544 s=indexes+x;
cristy69ec32d2011-02-27 23:57:09 +00004545 ResetPixelList(pixel_list[id]);
cristybb503372010-05-27 20:51:26 +00004546 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004547 {
cristybb503372010-05-27 20:51:26 +00004548 for (u=0; u < (ssize_t) width; u++)
cristy69ec32d2011-02-27 23:57:09 +00004549 InsertPixelList(image,r+u,s+u,pixel_list[id]);
cristy3ed852e2009-09-05 21:47:34 +00004550 r+=image->columns+width;
4551 s+=image->columns+width;
4552 }
cristy69ec32d2011-02-27 23:57:09 +00004553 pixel=GetNonpeakPixelList(pixel_list[id]);
cristy3ed852e2009-09-05 21:47:34 +00004554 SetPixelPacket(noise_image,&pixel,q,noise_indexes+x);
4555 p++;
4556 q++;
4557 }
4558 if (SyncCacheViewAuthenticPixels(noise_view,exception) == MagickFalse)
4559 status=MagickFalse;
4560 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4561 {
4562 MagickBooleanType
4563 proceed;
4564
cristyb5d5f722009-11-04 03:03:49 +00004565#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004566 #pragma omp critical (MagickCore_ReduceNoiseImage)
4567#endif
4568 proceed=SetImageProgress(image,ReduceNoiseImageTag,progress++,
4569 image->rows);
4570 if (proceed == MagickFalse)
4571 status=MagickFalse;
4572 }
4573 }
4574 noise_view=DestroyCacheView(noise_view);
4575 image_view=DestroyCacheView(image_view);
cristy69ec32d2011-02-27 23:57:09 +00004576 pixel_list=DestroyPixelListThreadSet(pixel_list);
cristy3ed852e2009-09-05 21:47:34 +00004577 return(noise_image);
4578}
4579
4580/*
4581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4582% %
4583% %
4584% %
4585% S e l e c t i v e B l u r I m a g e %
4586% %
4587% %
4588% %
4589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4590%
4591% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
4592% It is similar to the unsharpen mask that sharpens everything with contrast
4593% above a certain threshold.
4594%
4595% The format of the SelectiveBlurImage method is:
4596%
4597% Image *SelectiveBlurImage(const Image *image,const double radius,
4598% const double sigma,const double threshold,ExceptionInfo *exception)
4599% Image *SelectiveBlurImageChannel(const Image *image,
4600% const ChannelType channel,const double radius,const double sigma,
4601% const double threshold,ExceptionInfo *exception)
4602%
4603% A description of each parameter follows:
4604%
4605% o image: the image.
4606%
4607% o channel: the channel type.
4608%
4609% o radius: the radius of the Gaussian, in pixels, not counting the center
4610% pixel.
4611%
4612% o sigma: the standard deviation of the Gaussian, in pixels.
4613%
4614% o threshold: only pixels within this contrast threshold are included
4615% in the blur operation.
4616%
4617% o exception: return any errors or warnings in this structure.
4618%
4619*/
4620
4621static inline MagickBooleanType SelectiveContrast(const PixelPacket *p,
4622 const PixelPacket *q,const double threshold)
4623{
4624 if (fabs(PixelIntensity(p)-PixelIntensity(q)) < threshold)
4625 return(MagickTrue);
4626 return(MagickFalse);
4627}
4628
4629MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
4630 const double sigma,const double threshold,ExceptionInfo *exception)
4631{
4632 Image
4633 *blur_image;
4634
4635 blur_image=SelectiveBlurImageChannel(image,DefaultChannels,radius,sigma,
4636 threshold,exception);
4637 return(blur_image);
4638}
4639
4640MagickExport Image *SelectiveBlurImageChannel(const Image *image,
4641 const ChannelType channel,const double radius,const double sigma,
4642 const double threshold,ExceptionInfo *exception)
4643{
4644#define SelectiveBlurImageTag "SelectiveBlur/Image"
4645
cristy47e00502009-12-17 19:19:57 +00004646 CacheView
4647 *blur_view,
4648 *image_view;
4649
cristy3ed852e2009-09-05 21:47:34 +00004650 double
cristy3ed852e2009-09-05 21:47:34 +00004651 *kernel;
4652
4653 Image
4654 *blur_image;
4655
cristy3ed852e2009-09-05 21:47:34 +00004656 MagickBooleanType
4657 status;
4658
cristybb503372010-05-27 20:51:26 +00004659 MagickOffsetType
4660 progress;
4661
cristy3ed852e2009-09-05 21:47:34 +00004662 MagickPixelPacket
cristy3ed852e2009-09-05 21:47:34 +00004663 bias;
4664
cristybb503372010-05-27 20:51:26 +00004665 register ssize_t
cristy47e00502009-12-17 19:19:57 +00004666 i;
cristy3ed852e2009-09-05 21:47:34 +00004667
cristybb503372010-05-27 20:51:26 +00004668 size_t
cristy3ed852e2009-09-05 21:47:34 +00004669 width;
4670
cristybb503372010-05-27 20:51:26 +00004671 ssize_t
4672 j,
4673 u,
4674 v,
4675 y;
4676
cristy3ed852e2009-09-05 21:47:34 +00004677 /*
4678 Initialize blur image attributes.
4679 */
4680 assert(image != (Image *) NULL);
4681 assert(image->signature == MagickSignature);
4682 if (image->debug != MagickFalse)
4683 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4684 assert(exception != (ExceptionInfo *) NULL);
4685 assert(exception->signature == MagickSignature);
4686 width=GetOptimalKernelWidth1D(radius,sigma);
4687 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
4688 if (kernel == (double *) NULL)
4689 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00004690 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00004691 i=0;
cristy47e00502009-12-17 19:19:57 +00004692 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00004693 {
cristy47e00502009-12-17 19:19:57 +00004694 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00004695 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
4696 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00004697 }
4698 if (image->debug != MagickFalse)
4699 {
4700 char
4701 format[MaxTextExtent],
4702 *message;
4703
cristy117ff172010-08-15 21:35:32 +00004704 register const double
4705 *k;
4706
cristybb503372010-05-27 20:51:26 +00004707 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004708 u,
4709 v;
4710
cristy3ed852e2009-09-05 21:47:34 +00004711 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004712 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
4713 width);
cristy3ed852e2009-09-05 21:47:34 +00004714 message=AcquireString("");
4715 k=kernel;
cristybb503372010-05-27 20:51:26 +00004716 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004717 {
4718 *message='\0';
cristye8c25f92010-06-03 00:53:06 +00004719 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00004720 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00004721 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004722 {
4723 (void) FormatMagickString(format,MaxTextExtent,"%+f ",*k++);
4724 (void) ConcatenateString(&message,format);
4725 }
4726 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
4727 }
4728 message=DestroyString(message);
4729 }
4730 blur_image=CloneImage(image,0,0,MagickTrue,exception);
4731 if (blur_image == (Image *) NULL)
4732 return((Image *) NULL);
4733 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
4734 {
4735 InheritException(exception,&blur_image->exception);
4736 blur_image=DestroyImage(blur_image);
4737 return((Image *) NULL);
4738 }
4739 /*
4740 Threshold blur image.
4741 */
4742 status=MagickTrue;
4743 progress=0;
cristyddd82202009-11-03 20:14:50 +00004744 GetMagickPixelPacket(image,&bias);
4745 SetMagickPixelPacketBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00004746 image_view=AcquireCacheView(image);
4747 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00004748#if defined(MAGICKCORE_OPENMP_SUPPORT)
4749 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004750#endif
cristybb503372010-05-27 20:51:26 +00004751 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004752 {
4753 MagickBooleanType
4754 sync;
4755
4756 MagickRealType
4757 gamma;
4758
4759 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004760 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00004761
4762 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004763 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004764
4765 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004766 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00004767
cristy3ed852e2009-09-05 21:47:34 +00004768 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004769 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004770
cristy117ff172010-08-15 21:35:32 +00004771 register ssize_t
4772 x;
4773
cristy3ed852e2009-09-05 21:47:34 +00004774 if (status == MagickFalse)
4775 continue;
cristy117ff172010-08-15 21:35:32 +00004776 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
4777 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00004778 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
4779 exception);
4780 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4781 {
4782 status=MagickFalse;
4783 continue;
4784 }
4785 indexes=GetCacheViewVirtualIndexQueue(image_view);
4786 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
cristybb503372010-05-27 20:51:26 +00004787 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004788 {
cristy3ed852e2009-09-05 21:47:34 +00004789 MagickPixelPacket
4790 pixel;
4791
4792 register const double
cristyc47d1f82009-11-26 01:44:43 +00004793 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00004794
cristybb503372010-05-27 20:51:26 +00004795 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004796 u;
4797
cristy117ff172010-08-15 21:35:32 +00004798 ssize_t
4799 j,
4800 v;
4801
cristyddd82202009-11-03 20:14:50 +00004802 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00004803 k=kernel;
4804 gamma=0.0;
4805 j=0;
4806 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
4807 {
cristybb503372010-05-27 20:51:26 +00004808 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004809 {
cristybb503372010-05-27 20:51:26 +00004810 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004811 {
4812 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4813 {
4814 pixel.red+=(*k)*(p+u+j)->red;
4815 pixel.green+=(*k)*(p+u+j)->green;
4816 pixel.blue+=(*k)*(p+u+j)->blue;
4817 gamma+=(*k);
4818 k++;
4819 }
4820 }
cristyd99b0962010-05-29 23:14:26 +00004821 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004822 }
4823 if (gamma != 0.0)
4824 {
4825 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4826 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004827 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004828 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004829 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004830 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004831 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004832 }
4833 if ((channel & OpacityChannel) != 0)
4834 {
4835 gamma=0.0;
4836 j=0;
cristybb503372010-05-27 20:51:26 +00004837 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004838 {
cristybb503372010-05-27 20:51:26 +00004839 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004840 {
4841 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4842 {
4843 pixel.opacity+=(*k)*(p+u+j)->opacity;
4844 gamma+=(*k);
4845 k++;
4846 }
4847 }
cristyeaedf062010-05-29 22:36:02 +00004848 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004849 }
4850 if (gamma != 0.0)
4851 {
4852 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4853 gamma);
cristyce70c172010-01-07 17:15:30 +00004854 SetOpacityPixelComponent(q,ClampToQuantum(gamma*
4855 GetOpacityPixelComponent(&pixel)));
cristy3ed852e2009-09-05 21:47:34 +00004856 }
4857 }
4858 if (((channel & IndexChannel) != 0) &&
4859 (image->colorspace == CMYKColorspace))
4860 {
4861 gamma=0.0;
4862 j=0;
cristybb503372010-05-27 20:51:26 +00004863 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004864 {
cristybb503372010-05-27 20:51:26 +00004865 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004866 {
4867 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4868 {
4869 pixel.index+=(*k)*indexes[x+u+j];
4870 gamma+=(*k);
4871 k++;
4872 }
4873 }
cristyeaedf062010-05-29 22:36:02 +00004874 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004875 }
4876 if (gamma != 0.0)
4877 {
4878 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4879 gamma);
cristy6db48122010-01-11 00:18:07 +00004880 blur_indexes[x]=ClampToQuantum(gamma*
4881 GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004882 }
4883 }
4884 }
4885 else
4886 {
4887 MagickRealType
4888 alpha;
4889
cristybb503372010-05-27 20:51:26 +00004890 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004891 {
cristybb503372010-05-27 20:51:26 +00004892 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004893 {
4894 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4895 {
cristy46f08202010-01-10 04:04:21 +00004896 alpha=(MagickRealType) (QuantumScale*
4897 GetAlphaPixelComponent(p+u+j));
cristy3ed852e2009-09-05 21:47:34 +00004898 pixel.red+=(*k)*alpha*(p+u+j)->red;
4899 pixel.green+=(*k)*alpha*(p+u+j)->green;
4900 pixel.blue+=(*k)*alpha*(p+u+j)->blue;
4901 pixel.opacity+=(*k)*(p+u+j)->opacity;
4902 gamma+=(*k)*alpha;
4903 k++;
4904 }
4905 }
cristyeaedf062010-05-29 22:36:02 +00004906 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004907 }
4908 if (gamma != 0.0)
4909 {
4910 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4911 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004912 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004913 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004914 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004915 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004916 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004917 }
4918 if ((channel & OpacityChannel) != 0)
4919 {
4920 gamma=0.0;
4921 j=0;
cristybb503372010-05-27 20:51:26 +00004922 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004923 {
cristybb503372010-05-27 20:51:26 +00004924 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004925 {
4926 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4927 {
4928 pixel.opacity+=(*k)*(p+u+j)->opacity;
4929 gamma+=(*k);
4930 k++;
4931 }
4932 }
cristyeaedf062010-05-29 22:36:02 +00004933 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004934 }
4935 if (gamma != 0.0)
4936 {
4937 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4938 gamma);
cristy6db48122010-01-11 00:18:07 +00004939 SetOpacityPixelComponent(q,
4940 ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004941 }
4942 }
4943 if (((channel & IndexChannel) != 0) &&
4944 (image->colorspace == CMYKColorspace))
4945 {
4946 gamma=0.0;
4947 j=0;
cristybb503372010-05-27 20:51:26 +00004948 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00004949 {
cristybb503372010-05-27 20:51:26 +00004950 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00004951 {
4952 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4953 {
cristy46f08202010-01-10 04:04:21 +00004954 alpha=(MagickRealType) (QuantumScale*
4955 GetAlphaPixelComponent(p+u+j));
cristy3ed852e2009-09-05 21:47:34 +00004956 pixel.index+=(*k)*alpha*indexes[x+u+j];
4957 gamma+=(*k);
4958 k++;
4959 }
4960 }
cristyeaedf062010-05-29 22:36:02 +00004961 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00004962 }
4963 if (gamma != 0.0)
4964 {
4965 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4966 gamma);
cristy6db48122010-01-11 00:18:07 +00004967 blur_indexes[x]=ClampToQuantum(gamma*
4968 GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004969 }
4970 }
4971 }
4972 p++;
4973 q++;
4974 }
4975 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
4976 if (sync == MagickFalse)
4977 status=MagickFalse;
4978 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4979 {
4980 MagickBooleanType
4981 proceed;
4982
cristyb5d5f722009-11-04 03:03:49 +00004983#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004984 #pragma omp critical (MagickCore_SelectiveBlurImageChannel)
4985#endif
4986 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
4987 image->rows);
4988 if (proceed == MagickFalse)
4989 status=MagickFalse;
4990 }
4991 }
4992 blur_image->type=image->type;
4993 blur_view=DestroyCacheView(blur_view);
4994 image_view=DestroyCacheView(image_view);
4995 kernel=(double *) RelinquishMagickMemory(kernel);
4996 if (status == MagickFalse)
4997 blur_image=DestroyImage(blur_image);
4998 return(blur_image);
4999}
5000
5001/*
5002%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5003% %
5004% %
5005% %
5006% S h a d e I m a g e %
5007% %
5008% %
5009% %
5010%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5011%
5012% ShadeImage() shines a distant light on an image to create a
5013% three-dimensional effect. You control the positioning of the light with
5014% azimuth and elevation; azimuth is measured in degrees off the x axis
5015% and elevation is measured in pixels above the Z axis.
5016%
5017% The format of the ShadeImage method is:
5018%
5019% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
5020% const double azimuth,const double elevation,ExceptionInfo *exception)
5021%
5022% A description of each parameter follows:
5023%
5024% o image: the image.
5025%
5026% o gray: A value other than zero shades the intensity of each pixel.
5027%
5028% o azimuth, elevation: Define the light source direction.
5029%
5030% o exception: return any errors or warnings in this structure.
5031%
5032*/
5033MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
5034 const double azimuth,const double elevation,ExceptionInfo *exception)
5035{
5036#define ShadeImageTag "Shade/Image"
5037
cristyc4c8d132010-01-07 01:58:38 +00005038 CacheView
5039 *image_view,
5040 *shade_view;
5041
cristy3ed852e2009-09-05 21:47:34 +00005042 Image
5043 *shade_image;
5044
cristy3ed852e2009-09-05 21:47:34 +00005045 MagickBooleanType
5046 status;
5047
cristybb503372010-05-27 20:51:26 +00005048 MagickOffsetType
5049 progress;
5050
cristy3ed852e2009-09-05 21:47:34 +00005051 PrimaryInfo
5052 light;
5053
cristybb503372010-05-27 20:51:26 +00005054 ssize_t
5055 y;
5056
cristy3ed852e2009-09-05 21:47:34 +00005057 /*
5058 Initialize shaded image attributes.
5059 */
5060 assert(image != (const Image *) NULL);
5061 assert(image->signature == MagickSignature);
5062 if (image->debug != MagickFalse)
5063 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5064 assert(exception != (ExceptionInfo *) NULL);
5065 assert(exception->signature == MagickSignature);
5066 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5067 if (shade_image == (Image *) NULL)
5068 return((Image *) NULL);
5069 if (SetImageStorageClass(shade_image,DirectClass) == MagickFalse)
5070 {
5071 InheritException(exception,&shade_image->exception);
5072 shade_image=DestroyImage(shade_image);
5073 return((Image *) NULL);
5074 }
5075 /*
5076 Compute the light vector.
5077 */
5078 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
5079 cos(DegreesToRadians(elevation));
5080 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
5081 cos(DegreesToRadians(elevation));
5082 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
5083 /*
5084 Shade image.
5085 */
5086 status=MagickTrue;
5087 progress=0;
5088 image_view=AcquireCacheView(image);
5089 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00005090#if defined(MAGICKCORE_OPENMP_SUPPORT)
5091 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005092#endif
cristybb503372010-05-27 20:51:26 +00005093 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005094 {
5095 MagickRealType
5096 distance,
5097 normal_distance,
5098 shade;
5099
5100 PrimaryInfo
5101 normal;
5102
5103 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005104 *restrict p,
5105 *restrict s0,
5106 *restrict s1,
5107 *restrict s2;
cristy3ed852e2009-09-05 21:47:34 +00005108
cristy3ed852e2009-09-05 21:47:34 +00005109 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005110 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005111
cristy117ff172010-08-15 21:35:32 +00005112 register ssize_t
5113 x;
5114
cristy3ed852e2009-09-05 21:47:34 +00005115 if (status == MagickFalse)
5116 continue;
5117 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
5118 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
5119 exception);
5120 if ((p == (PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
5121 {
5122 status=MagickFalse;
5123 continue;
5124 }
5125 /*
5126 Shade this row of pixels.
5127 */
5128 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
5129 s0=p+1;
5130 s1=s0+image->columns+2;
5131 s2=s1+image->columns+2;
cristybb503372010-05-27 20:51:26 +00005132 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005133 {
5134 /*
5135 Determine the surface normal and compute shading.
5136 */
5137 normal.x=(double) (PixelIntensity(s0-1)+PixelIntensity(s1-1)+
5138 PixelIntensity(s2-1)-PixelIntensity(s0+1)-PixelIntensity(s1+1)-
5139 PixelIntensity(s2+1));
5140 normal.y=(double) (PixelIntensity(s2-1)+PixelIntensity(s2)+
5141 PixelIntensity(s2+1)-PixelIntensity(s0-1)-PixelIntensity(s0)-
5142 PixelIntensity(s0+1));
5143 if ((normal.x == 0.0) && (normal.y == 0.0))
5144 shade=light.z;
5145 else
5146 {
5147 shade=0.0;
5148 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
5149 if (distance > MagickEpsilon)
5150 {
5151 normal_distance=
5152 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
5153 if (normal_distance > (MagickEpsilon*MagickEpsilon))
5154 shade=distance/sqrt((double) normal_distance);
5155 }
5156 }
5157 if (gray != MagickFalse)
5158 {
5159 q->red=(Quantum) shade;
5160 q->green=(Quantum) shade;
5161 q->blue=(Quantum) shade;
5162 }
5163 else
5164 {
cristyce70c172010-01-07 17:15:30 +00005165 q->red=ClampToQuantum(QuantumScale*shade*s1->red);
5166 q->green=ClampToQuantum(QuantumScale*shade*s1->green);
5167 q->blue=ClampToQuantum(QuantumScale*shade*s1->blue);
cristy3ed852e2009-09-05 21:47:34 +00005168 }
5169 q->opacity=s1->opacity;
5170 s0++;
5171 s1++;
5172 s2++;
5173 q++;
5174 }
5175 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
5176 status=MagickFalse;
5177 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5178 {
5179 MagickBooleanType
5180 proceed;
5181
cristyb5d5f722009-11-04 03:03:49 +00005182#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005183 #pragma omp critical (MagickCore_ShadeImage)
5184#endif
5185 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
5186 if (proceed == MagickFalse)
5187 status=MagickFalse;
5188 }
5189 }
5190 shade_view=DestroyCacheView(shade_view);
5191 image_view=DestroyCacheView(image_view);
5192 if (status == MagickFalse)
5193 shade_image=DestroyImage(shade_image);
5194 return(shade_image);
5195}
5196
5197/*
5198%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5199% %
5200% %
5201% %
5202% S h a r p e n I m a g e %
5203% %
5204% %
5205% %
5206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5207%
5208% SharpenImage() sharpens the image. We convolve the image with a Gaussian
5209% operator of the given radius and standard deviation (sigma). For
5210% reasonable results, radius should be larger than sigma. Use a radius of 0
5211% and SharpenImage() selects a suitable radius for you.
5212%
5213% Using a separable kernel would be faster, but the negative weights cancel
5214% out on the corners of the kernel producing often undesirable ringing in the
5215% filtered result; this can be avoided by using a 2D gaussian shaped image
5216% sharpening kernel instead.
5217%
5218% The format of the SharpenImage method is:
5219%
5220% Image *SharpenImage(const Image *image,const double radius,
5221% const double sigma,ExceptionInfo *exception)
5222% Image *SharpenImageChannel(const Image *image,const ChannelType channel,
5223% const double radius,const double sigma,ExceptionInfo *exception)
5224%
5225% A description of each parameter follows:
5226%
5227% o image: the image.
5228%
5229% o channel: the channel type.
5230%
5231% o radius: the radius of the Gaussian, in pixels, not counting the center
5232% pixel.
5233%
5234% o sigma: the standard deviation of the Laplacian, in pixels.
5235%
5236% o exception: return any errors or warnings in this structure.
5237%
5238*/
5239
5240MagickExport Image *SharpenImage(const Image *image,const double radius,
5241 const double sigma,ExceptionInfo *exception)
5242{
5243 Image
5244 *sharp_image;
5245
5246 sharp_image=SharpenImageChannel(image,DefaultChannels,radius,sigma,exception);
5247 return(sharp_image);
5248}
5249
5250MagickExport Image *SharpenImageChannel(const Image *image,
5251 const ChannelType channel,const double radius,const double sigma,
5252 ExceptionInfo *exception)
5253{
5254 double
cristy47e00502009-12-17 19:19:57 +00005255 *kernel,
5256 normalize;
cristy3ed852e2009-09-05 21:47:34 +00005257
5258 Image
5259 *sharp_image;
5260
cristybb503372010-05-27 20:51:26 +00005261 register ssize_t
cristy47e00502009-12-17 19:19:57 +00005262 i;
5263
cristybb503372010-05-27 20:51:26 +00005264 size_t
cristy3ed852e2009-09-05 21:47:34 +00005265 width;
5266
cristy117ff172010-08-15 21:35:32 +00005267 ssize_t
5268 j,
5269 u,
5270 v;
5271
cristy3ed852e2009-09-05 21:47:34 +00005272 assert(image != (const Image *) NULL);
5273 assert(image->signature == MagickSignature);
5274 if (image->debug != MagickFalse)
5275 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5276 assert(exception != (ExceptionInfo *) NULL);
5277 assert(exception->signature == MagickSignature);
5278 width=GetOptimalKernelWidth2D(radius,sigma);
5279 kernel=(double *) AcquireQuantumMemory((size_t) width*width,sizeof(*kernel));
5280 if (kernel == (double *) NULL)
5281 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00005282 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00005283 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +00005284 i=0;
5285 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00005286 {
cristy47e00502009-12-17 19:19:57 +00005287 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00005288 {
cristy4205a3c2010-09-12 20:19:59 +00005289 kernel[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
5290 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00005291 normalize+=kernel[i];
5292 i++;
5293 }
5294 }
5295 kernel[i/2]=(double) ((-2.0)*normalize);
5296 sharp_image=ConvolveImageChannel(image,channel,width,kernel,exception);
5297 kernel=(double *) RelinquishMagickMemory(kernel);
5298 return(sharp_image);
5299}
5300
5301/*
5302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5303% %
5304% %
5305% %
5306% S p r e a d I m a g e %
5307% %
5308% %
5309% %
5310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5311%
5312% SpreadImage() is a special effects method that randomly displaces each
5313% pixel in a block defined by the radius parameter.
5314%
5315% The format of the SpreadImage method is:
5316%
5317% Image *SpreadImage(const Image *image,const double radius,
5318% ExceptionInfo *exception)
5319%
5320% A description of each parameter follows:
5321%
5322% o image: the image.
5323%
5324% o radius: Choose a random pixel in a neighborhood of this extent.
5325%
5326% o exception: return any errors or warnings in this structure.
5327%
5328*/
5329MagickExport Image *SpreadImage(const Image *image,const double radius,
5330 ExceptionInfo *exception)
5331{
5332#define SpreadImageTag "Spread/Image"
5333
cristyfa112112010-01-04 17:48:07 +00005334 CacheView
5335 *image_view;
5336
cristy3ed852e2009-09-05 21:47:34 +00005337 Image
5338 *spread_image;
5339
cristy3ed852e2009-09-05 21:47:34 +00005340 MagickBooleanType
5341 status;
5342
cristybb503372010-05-27 20:51:26 +00005343 MagickOffsetType
5344 progress;
5345
cristy3ed852e2009-09-05 21:47:34 +00005346 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00005347 bias;
cristy3ed852e2009-09-05 21:47:34 +00005348
5349 RandomInfo
cristyfa112112010-01-04 17:48:07 +00005350 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00005351
5352 ResampleFilter
cristyfa112112010-01-04 17:48:07 +00005353 **restrict resample_filter;
cristy3ed852e2009-09-05 21:47:34 +00005354
cristybb503372010-05-27 20:51:26 +00005355 size_t
cristy3ed852e2009-09-05 21:47:34 +00005356 width;
5357
cristybb503372010-05-27 20:51:26 +00005358 ssize_t
5359 y;
5360
cristy3ed852e2009-09-05 21:47:34 +00005361 /*
5362 Initialize spread image attributes.
5363 */
5364 assert(image != (Image *) NULL);
5365 assert(image->signature == MagickSignature);
5366 if (image->debug != MagickFalse)
5367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5368 assert(exception != (ExceptionInfo *) NULL);
5369 assert(exception->signature == MagickSignature);
5370 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
5371 exception);
5372 if (spread_image == (Image *) NULL)
5373 return((Image *) NULL);
5374 if (SetImageStorageClass(spread_image,DirectClass) == MagickFalse)
5375 {
5376 InheritException(exception,&spread_image->exception);
5377 spread_image=DestroyImage(spread_image);
5378 return((Image *) NULL);
5379 }
5380 /*
5381 Spread image.
5382 */
5383 status=MagickTrue;
5384 progress=0;
cristyddd82202009-11-03 20:14:50 +00005385 GetMagickPixelPacket(spread_image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00005386 width=GetOptimalKernelWidth1D(radius,0.5);
cristyb2a11ae2010-02-22 00:53:36 +00005387 resample_filter=AcquireResampleFilterThreadSet(image,
5388 UndefinedVirtualPixelMethod,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005389 random_info=AcquireRandomInfoThreadSet();
5390 image_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00005391#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00005392 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00005393#endif
cristybb503372010-05-27 20:51:26 +00005394 for (y=0; y < (ssize_t) spread_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005395 {
cristy5c9e6f22010-09-17 17:31:01 +00005396 const int
5397 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00005398
cristy3ed852e2009-09-05 21:47:34 +00005399 MagickPixelPacket
5400 pixel;
5401
5402 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005403 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00005404
cristy3ed852e2009-09-05 21:47:34 +00005405 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005406 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005407
cristy117ff172010-08-15 21:35:32 +00005408 register ssize_t
5409 x;
5410
cristy3ed852e2009-09-05 21:47:34 +00005411 if (status == MagickFalse)
5412 continue;
5413 q=QueueCacheViewAuthenticPixels(image_view,0,y,spread_image->columns,1,
5414 exception);
5415 if (q == (PixelPacket *) NULL)
5416 {
5417 status=MagickFalse;
5418 continue;
5419 }
5420 indexes=GetCacheViewAuthenticIndexQueue(image_view);
cristyddd82202009-11-03 20:14:50 +00005421 pixel=bias;
cristybb503372010-05-27 20:51:26 +00005422 for (x=0; x < (ssize_t) spread_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005423 {
5424 (void) ResamplePixelColor(resample_filter[id],(double) x+width*
5425 (GetPseudoRandomValue(random_info[id])-0.5),(double) y+width*
5426 (GetPseudoRandomValue(random_info[id])-0.5),&pixel);
5427 SetPixelPacket(spread_image,&pixel,q,indexes+x);
5428 q++;
5429 }
5430 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5431 status=MagickFalse;
5432 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5433 {
5434 MagickBooleanType
5435 proceed;
5436
cristyb557a152011-02-22 12:14:30 +00005437#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005438 #pragma omp critical (MagickCore_SpreadImage)
5439#endif
5440 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
5441 if (proceed == MagickFalse)
5442 status=MagickFalse;
5443 }
5444 }
5445 image_view=DestroyCacheView(image_view);
5446 random_info=DestroyRandomInfoThreadSet(random_info);
5447 resample_filter=DestroyResampleFilterThreadSet(resample_filter);
5448 return(spread_image);
5449}
5450
5451/*
5452%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5453% %
5454% %
5455% %
cristy0834d642011-03-18 18:26:08 +00005456% S t a t i s t i c I m a g e %
5457% %
5458% %
5459% %
5460%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5461%
5462% StatisticImage() makes each pixel the min / max / median / mode / etc. of
5463% the neighborhood of the specified radius.
5464%
5465% The format of the StatisticImage method is:
5466%
5467% Image *StatisticImage(const Image *image,const StatisticType type,
5468% const double radius,ExceptionInfo *exception)
5469% Image *StatisticImageChannel(const Image *image,
5470% const ChannelType channel,const StatisticType type,
5471% const double radius,ExceptionInfo *exception)
5472%
5473% A description of each parameter follows:
5474%
5475% o image: the image.
5476%
5477% o channel: the image channel.
5478%
5479% o type: the statistic type (median, mode, etc.).
5480%
5481% o radius: the radius of the pixel neighborhood.
5482%
5483% o exception: return any errors or warnings in this structure.
5484%
5485*/
5486
5487MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
5488 const double radius,ExceptionInfo *exception)
5489{
5490 return(StatisticImageChannel(image,DefaultChannels,type,radius,exception));
5491}
5492
5493MagickExport Image *StatisticImageChannel(const Image *image,
5494 const ChannelType channel,const StatisticType type,const double radius,
5495 ExceptionInfo *exception)
5496{
5497#define StatisticImageTag "Statistic/Image"
5498
5499 CacheView
5500 *image_view,
5501 *statistic_view;
5502
5503 Image
5504 *statistic_image;
5505
5506 MagickBooleanType
5507 status;
5508
5509 MagickOffsetType
5510 progress;
5511
5512 PixelList
5513 **restrict pixel_list;
5514
5515 size_t
5516 width;
5517
5518 ssize_t
5519 y;
5520
5521 /*
5522 Initialize statistics image attributes.
5523 */
5524 assert(image != (Image *) NULL);
5525 assert(image->signature == MagickSignature);
5526 if (image->debug != MagickFalse)
5527 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5528 assert(exception != (ExceptionInfo *) NULL);
5529 assert(exception->signature == MagickSignature);
5530 width=GetOptimalKernelWidth2D(radius,0.5);
5531 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
5532 exception);
5533 if (statistic_image == (Image *) NULL)
5534 return((Image *) NULL);
5535 if (SetImageStorageClass(statistic_image,DirectClass) == MagickFalse)
5536 {
5537 InheritException(exception,&statistic_image->exception);
5538 statistic_image=DestroyImage(statistic_image);
5539 return((Image *) NULL);
5540 }
5541 pixel_list=AcquirePixelListThreadSet(width);
5542 if (pixel_list == (PixelList **) NULL)
5543 {
5544 statistic_image=DestroyImage(statistic_image);
5545 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5546 }
5547 /*
5548 Reduce statistics image.
5549 */
5550 status=MagickTrue;
5551 progress=0;
5552 image_view=AcquireCacheView(image);
5553 statistic_view=AcquireCacheView(statistic_image);
5554#if defined(MAGICKCORE_OPENMP_SUPPORT)
5555 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
5556#endif
5557 for (y=0; y < (ssize_t) statistic_image->rows; y++)
5558 {
5559 const int
5560 id = GetOpenMPThreadId();
5561
5562 register const IndexPacket
5563 *restrict indexes;
5564
5565 register const PixelPacket
5566 *restrict p;
5567
5568 register IndexPacket
5569 *restrict statistic_indexes;
5570
5571 register PixelPacket
5572 *restrict q;
5573
5574 register ssize_t
5575 x;
5576
5577 if (status == MagickFalse)
5578 continue;
5579 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
5580 (width/2L),image->columns+width,width,exception);
5581 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,
5582 statistic_image->columns,1,exception);
5583 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
5584 {
5585 status=MagickFalse;
5586 continue;
5587 }
5588 indexes=GetCacheViewVirtualIndexQueue(image_view);
5589 statistic_indexes=GetCacheViewAuthenticIndexQueue(statistic_view);
5590 for (x=0; x < (ssize_t) statistic_image->columns; x++)
5591 {
5592 MagickPixelPacket
5593 pixel;
5594
5595 register const PixelPacket
5596 *restrict r;
5597
5598 register const IndexPacket
5599 *restrict s;
5600
5601 register ssize_t
5602 u,
5603 v;
5604
5605 r=p;
5606 s=indexes+x;
5607 ResetPixelList(pixel_list[id]);
5608 for (v=0; v < (ssize_t) width; v++)
5609 {
5610 for (u=0; u < (ssize_t) width; u++)
5611 InsertPixelList(image,r+u,s+u,pixel_list[id]);
5612 r+=image->columns+width;
5613 s+=image->columns+width;
5614 }
5615 switch (type)
5616 {
cristyf2ad14a2011-03-18 18:57:25 +00005617 case MedianStatistic:
5618 {
5619 pixel=GetMedianPixelList(pixel_list[id]);
5620 break;
5621 }
5622 case ModeStatistic:
5623 {
5624 pixel=GetModePixelList(pixel_list[id]);
5625 break;
5626 }
5627 case NonpeakStatistic:
5628 {
5629 pixel=GetNonpeakPixelList(pixel_list[id]);
5630 break;
5631 }
5632 default:
5633 break;
cristy0834d642011-03-18 18:26:08 +00005634 }
5635 if ((channel & RedChannel) != 0)
5636 q->red=ClampToQuantum(pixel.red);
5637 if ((channel & GreenChannel) != 0)
5638 q->green=ClampToQuantum(pixel.green);
5639 if ((channel & BlueChannel) != 0)
5640 q->blue=ClampToQuantum(pixel.blue);
5641 if ((image->matte != MagickFalse) && ((channel & OpacityChannel) != 0))
5642 q->opacity=ClampToQuantum(pixel.opacity);
5643 if (((channel & IndexChannel) != 0) &&
5644 (image->colorspace == CMYKColorspace))
5645 statistic_indexes[x]=(IndexPacket) ClampToQuantum(pixel.index);
5646 p++;
5647 q++;
5648 }
5649 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
5650 status=MagickFalse;
5651 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5652 {
5653 MagickBooleanType
5654 proceed;
5655
5656#if defined(MAGICKCORE_OPENMP_SUPPORT)
5657 #pragma omp critical (MagickCore_StatisticImage)
5658#endif
5659 proceed=SetImageProgress(image,StatisticImageTag,progress++,
5660 image->rows);
5661 if (proceed == MagickFalse)
5662 status=MagickFalse;
5663 }
5664 }
5665 statistic_view=DestroyCacheView(statistic_view);
5666 image_view=DestroyCacheView(image_view);
5667 pixel_list=DestroyPixelListThreadSet(pixel_list);
5668 return(statistic_image);
5669}
5670
5671/*
5672%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5673% %
5674% %
5675% %
cristy3ed852e2009-09-05 21:47:34 +00005676% U n s h a r p M a s k I m a g e %
5677% %
5678% %
5679% %
5680%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5681%
5682% UnsharpMaskImage() sharpens one or more image channels. We convolve the
5683% image with a Gaussian operator of the given radius and standard deviation
5684% (sigma). For reasonable results, radius should be larger than sigma. Use a
5685% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
5686%
5687% The format of the UnsharpMaskImage method is:
5688%
5689% Image *UnsharpMaskImage(const Image *image,const double radius,
5690% const double sigma,const double amount,const double threshold,
5691% ExceptionInfo *exception)
5692% Image *UnsharpMaskImageChannel(const Image *image,
5693% const ChannelType channel,const double radius,const double sigma,
5694% const double amount,const double threshold,ExceptionInfo *exception)
5695%
5696% A description of each parameter follows:
5697%
5698% o image: the image.
5699%
5700% o channel: the channel type.
5701%
5702% o radius: the radius of the Gaussian, in pixels, not counting the center
5703% pixel.
5704%
5705% o sigma: the standard deviation of the Gaussian, in pixels.
5706%
5707% o amount: the percentage of the difference between the original and the
5708% blur image that is added back into the original.
5709%
5710% o threshold: the threshold in pixels needed to apply the diffence amount.
5711%
5712% o exception: return any errors or warnings in this structure.
5713%
5714*/
5715
5716MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
5717 const double sigma,const double amount,const double threshold,
5718 ExceptionInfo *exception)
5719{
5720 Image
5721 *sharp_image;
5722
5723 sharp_image=UnsharpMaskImageChannel(image,DefaultChannels,radius,sigma,amount,
5724 threshold,exception);
5725 return(sharp_image);
5726}
5727
5728MagickExport Image *UnsharpMaskImageChannel(const Image *image,
5729 const ChannelType channel,const double radius,const double sigma,
5730 const double amount,const double threshold,ExceptionInfo *exception)
5731{
5732#define SharpenImageTag "Sharpen/Image"
5733
cristyc4c8d132010-01-07 01:58:38 +00005734 CacheView
5735 *image_view,
5736 *unsharp_view;
5737
cristy3ed852e2009-09-05 21:47:34 +00005738 Image
5739 *unsharp_image;
5740
cristy3ed852e2009-09-05 21:47:34 +00005741 MagickBooleanType
5742 status;
5743
cristybb503372010-05-27 20:51:26 +00005744 MagickOffsetType
5745 progress;
5746
cristy3ed852e2009-09-05 21:47:34 +00005747 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00005748 bias;
cristy3ed852e2009-09-05 21:47:34 +00005749
5750 MagickRealType
5751 quantum_threshold;
5752
cristybb503372010-05-27 20:51:26 +00005753 ssize_t
5754 y;
5755
cristy3ed852e2009-09-05 21:47:34 +00005756 assert(image != (const Image *) NULL);
5757 assert(image->signature == MagickSignature);
5758 if (image->debug != MagickFalse)
5759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5760 assert(exception != (ExceptionInfo *) NULL);
5761 unsharp_image=BlurImageChannel(image,channel,radius,sigma,exception);
5762 if (unsharp_image == (Image *) NULL)
5763 return((Image *) NULL);
5764 quantum_threshold=(MagickRealType) QuantumRange*threshold;
5765 /*
5766 Unsharp-mask image.
5767 */
5768 status=MagickTrue;
5769 progress=0;
cristyddd82202009-11-03 20:14:50 +00005770 GetMagickPixelPacket(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00005771 image_view=AcquireCacheView(image);
5772 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00005773#if defined(MAGICKCORE_OPENMP_SUPPORT)
5774 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005775#endif
cristybb503372010-05-27 20:51:26 +00005776 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005777 {
5778 MagickPixelPacket
5779 pixel;
5780
5781 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005782 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00005783
5784 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005785 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005786
5787 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005788 *restrict unsharp_indexes;
cristy3ed852e2009-09-05 21:47:34 +00005789
cristy3ed852e2009-09-05 21:47:34 +00005790 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005791 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005792
cristy117ff172010-08-15 21:35:32 +00005793 register ssize_t
5794 x;
5795
cristy3ed852e2009-09-05 21:47:34 +00005796 if (status == MagickFalse)
5797 continue;
5798 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5799 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
5800 exception);
5801 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
5802 {
5803 status=MagickFalse;
5804 continue;
5805 }
5806 indexes=GetCacheViewVirtualIndexQueue(image_view);
5807 unsharp_indexes=GetCacheViewAuthenticIndexQueue(unsharp_view);
cristyddd82202009-11-03 20:14:50 +00005808 pixel=bias;
cristybb503372010-05-27 20:51:26 +00005809 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005810 {
5811 if ((channel & RedChannel) != 0)
5812 {
5813 pixel.red=p->red-(MagickRealType) q->red;
5814 if (fabs(2.0*pixel.red) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005815 pixel.red=(MagickRealType) GetRedPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005816 else
5817 pixel.red=(MagickRealType) p->red+(pixel.red*amount);
cristyce70c172010-01-07 17:15:30 +00005818 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005819 }
5820 if ((channel & GreenChannel) != 0)
5821 {
5822 pixel.green=p->green-(MagickRealType) q->green;
5823 if (fabs(2.0*pixel.green) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005824 pixel.green=(MagickRealType) GetGreenPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005825 else
5826 pixel.green=(MagickRealType) p->green+(pixel.green*amount);
cristyce70c172010-01-07 17:15:30 +00005827 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005828 }
5829 if ((channel & BlueChannel) != 0)
5830 {
5831 pixel.blue=p->blue-(MagickRealType) q->blue;
5832 if (fabs(2.0*pixel.blue) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005833 pixel.blue=(MagickRealType) GetBluePixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005834 else
5835 pixel.blue=(MagickRealType) p->blue+(pixel.blue*amount);
cristyce70c172010-01-07 17:15:30 +00005836 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005837 }
5838 if ((channel & OpacityChannel) != 0)
5839 {
5840 pixel.opacity=p->opacity-(MagickRealType) q->opacity;
5841 if (fabs(2.0*pixel.opacity) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005842 pixel.opacity=(MagickRealType) GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005843 else
5844 pixel.opacity=p->opacity+(pixel.opacity*amount);
cristyce70c172010-01-07 17:15:30 +00005845 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005846 }
5847 if (((channel & IndexChannel) != 0) &&
5848 (image->colorspace == CMYKColorspace))
5849 {
cristyf01182f2011-03-01 14:56:40 +00005850 pixel.index=indexes[x]-(MagickRealType) unsharp_indexes[x];
cristy3ed852e2009-09-05 21:47:34 +00005851 if (fabs(2.0*pixel.index) < quantum_threshold)
cristyb557a152011-02-22 12:14:30 +00005852 pixel.index=(MagickRealType) indexes[x];
cristy3ed852e2009-09-05 21:47:34 +00005853 else
cristyb557a152011-02-22 12:14:30 +00005854 pixel.index=(MagickRealType) indexes[x]+(pixel.index*amount);
cristyce70c172010-01-07 17:15:30 +00005855 unsharp_indexes[x]=ClampToQuantum(pixel.index);
cristy3ed852e2009-09-05 21:47:34 +00005856 }
5857 p++;
5858 q++;
5859 }
5860 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
5861 status=MagickFalse;
5862 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5863 {
5864 MagickBooleanType
5865 proceed;
5866
cristyb5d5f722009-11-04 03:03:49 +00005867#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005868 #pragma omp critical (MagickCore_UnsharpMaskImageChannel)
5869#endif
5870 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
5871 if (proceed == MagickFalse)
5872 status=MagickFalse;
5873 }
5874 }
5875 unsharp_image->type=image->type;
5876 unsharp_view=DestroyCacheView(unsharp_view);
5877 image_view=DestroyCacheView(image_view);
5878 if (status == MagickFalse)
5879 unsharp_image=DestroyImage(unsharp_image);
5880 return(unsharp_image);
5881}