blob: 0e94f53595916c3e5ea9c6dd93f5c51651314c37 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/accelerate.h"
45#include "MagickCore/blob.h"
46#include "MagickCore/cache-view.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colorspace.h"
50#include "MagickCore/constitute.h"
51#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000052#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/draw.h"
54#include "MagickCore/enhance.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/exception-private.h"
57#include "MagickCore/effect.h"
58#include "MagickCore/fx.h"
59#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000060#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000061#include "MagickCore/geometry.h"
62#include "MagickCore/image-private.h"
63#include "MagickCore/list.h"
64#include "MagickCore/log.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/monitor.h"
67#include "MagickCore/monitor-private.h"
68#include "MagickCore/montage.h"
69#include "MagickCore/morphology.h"
70#include "MagickCore/paint.h"
71#include "MagickCore/pixel-accessor.h"
72#include "MagickCore/property.h"
73#include "MagickCore/quantize.h"
74#include "MagickCore/quantum.h"
75#include "MagickCore/quantum-private.h"
76#include "MagickCore/random_.h"
77#include "MagickCore/random-private.h"
78#include "MagickCore/resample.h"
79#include "MagickCore/resample-private.h"
80#include "MagickCore/resize.h"
81#include "MagickCore/resource_.h"
82#include "MagickCore/segment.h"
cristy31bbf2f2011-11-17 13:19:37 +000083#include "MagickCore/shear.h"
cristy4c08aed2011-07-01 19:47:50 +000084#include "MagickCore/signature-private.h"
cristy99bd5232011-12-07 14:38:20 +000085#include "MagickCore/statistic.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/string_.h"
87#include "MagickCore/thread-private.h"
88#include "MagickCore/transform.h"
89#include "MagickCore/threshold.h"
cristy3ed852e2009-09-05 21:47:34 +000090
91/*
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93% %
94% %
95% %
96% A d a p t i v e B l u r I m a g e %
97% %
98% %
99% %
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101%
102% AdaptiveBlurImage() adaptively blurs the image by blurring less
103% intensely near image edges and more intensely far from edges. We blur the
104% image with a Gaussian operator of the given radius and standard deviation
105% (sigma). For reasonable results, radius should be larger than sigma. Use a
106% radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
107%
108% The format of the AdaptiveBlurImage method is:
109%
110% Image *AdaptiveBlurImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000111% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000112%
113% A description of each parameter follows:
114%
115% o image: the image.
116%
cristy3ed852e2009-09-05 21:47:34 +0000117% o radius: the radius of the Gaussian, in pixels, not counting the center
118% pixel.
119%
120% o sigma: the standard deviation of the Laplacian, in pixels.
121%
cristy4c11c2b2011-09-05 20:17:07 +0000122% o bias: the bias.
123%
cristy3ed852e2009-09-05 21:47:34 +0000124% o exception: return any errors or warnings in this structure.
125%
126*/
127
cristyf89cb1d2011-07-07 01:24:37 +0000128MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
cristy051718b2011-08-28 22:49:25 +0000129 const char *levels,ExceptionInfo *exception)
cristyf89cb1d2011-07-07 01:24:37 +0000130{
131 double
132 black_point,
133 gamma,
134 white_point;
135
136 GeometryInfo
137 geometry_info;
138
139 MagickBooleanType
140 status;
141
142 MagickStatusType
143 flags;
144
145 /*
146 Parse levels.
147 */
148 if (levels == (char *) NULL)
149 return(MagickFalse);
150 flags=ParseGeometry(levels,&geometry_info);
151 black_point=geometry_info.rho;
152 white_point=(double) QuantumRange;
153 if ((flags & SigmaValue) != 0)
154 white_point=geometry_info.sigma;
155 gamma=1.0;
156 if ((flags & XiValue) != 0)
157 gamma=geometry_info.xi;
158 if ((flags & PercentValue) != 0)
159 {
160 black_point*=(double) image->columns*image->rows/100.0;
161 white_point*=(double) image->columns*image->rows/100.0;
162 }
163 if ((flags & SigmaValue) == 0)
164 white_point=(double) QuantumRange-black_point;
165 if ((flags & AspectValue ) == 0)
cristy7c0a0a42011-08-23 17:57:25 +0000166 status=LevelImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000167 else
cristy7c0a0a42011-08-23 17:57:25 +0000168 status=LevelizeImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000169 return(status);
170}
171
cristy4282c702011-11-21 00:01:06 +0000172MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
173 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000174{
175#define AdaptiveBlurImageTag "Convolve/Image"
176#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
177
cristyc4c8d132010-01-07 01:58:38 +0000178 CacheView
179 *blur_view,
180 *edge_view,
181 *image_view;
182
cristy3ed852e2009-09-05 21:47:34 +0000183 double
cristy47e00502009-12-17 19:19:57 +0000184 **kernel,
185 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000186
187 Image
188 *blur_image,
189 *edge_image,
190 *gaussian_image;
191
cristy3ed852e2009-09-05 21:47:34 +0000192 MagickBooleanType
193 status;
194
cristybb503372010-05-27 20:51:26 +0000195 MagickOffsetType
196 progress;
197
cristybb503372010-05-27 20:51:26 +0000198 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000199 i;
cristy3ed852e2009-09-05 21:47:34 +0000200
cristybb503372010-05-27 20:51:26 +0000201 size_t
cristy3ed852e2009-09-05 21:47:34 +0000202 width;
203
cristybb503372010-05-27 20:51:26 +0000204 ssize_t
205 j,
206 k,
207 u,
208 v,
209 y;
210
cristy3ed852e2009-09-05 21:47:34 +0000211 assert(image != (const Image *) NULL);
212 assert(image->signature == MagickSignature);
213 if (image->debug != MagickFalse)
214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
215 assert(exception != (ExceptionInfo *) NULL);
216 assert(exception->signature == MagickSignature);
217 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
218 if (blur_image == (Image *) NULL)
219 return((Image *) NULL);
220 if (fabs(sigma) <= MagickEpsilon)
221 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000222 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000223 {
cristy3ed852e2009-09-05 21:47:34 +0000224 blur_image=DestroyImage(blur_image);
225 return((Image *) NULL);
226 }
227 /*
228 Edge detect the image brighness channel, level, blur, and level again.
229 */
cristy8ae632d2011-09-05 17:29:53 +0000230 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000231 if (edge_image == (Image *) NULL)
232 {
233 blur_image=DestroyImage(blur_image);
234 return((Image *) NULL);
235 }
cristy051718b2011-08-28 22:49:25 +0000236 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristy05c0c9a2011-09-05 23:16:13 +0000237 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +0000238 if (gaussian_image != (Image *) NULL)
239 {
240 edge_image=DestroyImage(edge_image);
241 edge_image=gaussian_image;
242 }
cristy051718b2011-08-28 22:49:25 +0000243 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000244 /*
245 Create a set of kernels from maximum (radius,sigma) to minimum.
246 */
247 width=GetOptimalKernelWidth2D(radius,sigma);
248 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
249 if (kernel == (double **) NULL)
250 {
251 edge_image=DestroyImage(edge_image);
252 blur_image=DestroyImage(blur_image);
253 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
254 }
255 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000256 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000257 {
258 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
259 sizeof(**kernel));
260 if (kernel[i] == (double *) NULL)
261 break;
cristy47e00502009-12-17 19:19:57 +0000262 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000263 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000264 k=0;
265 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000266 {
cristy47e00502009-12-17 19:19:57 +0000267 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000268 {
cristy4205a3c2010-09-12 20:19:59 +0000269 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
270 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000271 normalize+=kernel[i][k];
272 k++;
cristy3ed852e2009-09-05 21:47:34 +0000273 }
274 }
cristy3ed852e2009-09-05 21:47:34 +0000275 if (fabs(normalize) <= MagickEpsilon)
276 normalize=1.0;
277 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000278 for (k=0; k < (j*j); k++)
279 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000280 }
cristybb503372010-05-27 20:51:26 +0000281 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000282 {
283 for (i-=2; i >= 0; i-=2)
284 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
285 kernel=(double **) RelinquishMagickMemory(kernel);
286 edge_image=DestroyImage(edge_image);
287 blur_image=DestroyImage(blur_image);
288 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
289 }
290 /*
291 Adaptively blur image.
292 */
293 status=MagickTrue;
294 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000295 image_view=AcquireCacheView(image);
296 edge_view=AcquireCacheView(edge_image);
297 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000298#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000299 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000300#endif
cristybb503372010-05-27 20:51:26 +0000301 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
cristy4c08aed2011-07-01 19:47:50 +0000303 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000304 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000305
cristy4c08aed2011-07-01 19:47:50 +0000306 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000307 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000308
cristy117ff172010-08-15 21:35:32 +0000309 register ssize_t
310 x;
311
cristy3ed852e2009-09-05 21:47:34 +0000312 if (status == MagickFalse)
313 continue;
314 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
315 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
316 exception);
cristyacd2ed22011-08-30 01:44:23 +0000317 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000318 {
319 status=MagickFalse;
320 continue;
321 }
cristybb503372010-05-27 20:51:26 +0000322 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000323 {
cristy4c11c2b2011-09-05 20:17:07 +0000324 register const Quantum
325 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000326
cristybb503372010-05-27 20:51:26 +0000327 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000328 i;
cristy3ed852e2009-09-05 21:47:34 +0000329
cristy4c11c2b2011-09-05 20:17:07 +0000330 ssize_t
331 center,
332 j;
333
334 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000335 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000336 if (j < 0)
337 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000338 else
cristy4c11c2b2011-09-05 20:17:07 +0000339 if (j > (ssize_t) width)
340 j=(ssize_t) width;
341 if ((j & 0x01) != 0)
342 j--;
343 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
344 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000345 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000346 break;
cristy4c11c2b2011-09-05 20:17:07 +0000347 center=(ssize_t) GetPixelChannels(image)*(width-j)*
cristy075ff302011-09-07 01:51:24 +0000348 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2L);
cristy4c11c2b2011-09-05 20:17:07 +0000349 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristy4c11c2b2011-09-05 20:17:07 +0000351 MagickRealType
352 alpha,
353 gamma,
354 pixel;
355
356 PixelChannel
357 channel;
358
359 PixelTrait
360 blur_traits,
361 traits;
362
363 register const double
364 *restrict k;
365
366 register const Quantum
367 *restrict pixels;
368
369 register ssize_t
370 u;
371
372 ssize_t
373 v;
374
cristye2a912b2011-12-05 20:02:07 +0000375 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000376 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000377 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
378 if ((traits == UndefinedPixelTrait) ||
379 (blur_traits == UndefinedPixelTrait))
380 continue;
381 if ((blur_traits & CopyPixelTrait) != 0)
382 {
cristy0beccfa2011-09-25 20:47:53 +0000383 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000384 continue;
385 }
386 k=kernel[j];
387 pixels=p;
388 pixel=bias;
389 gamma=0.0;
390 if ((blur_traits & BlendPixelTrait) == 0)
391 {
392 /*
393 No alpha blending.
394 */
395 for (v=0; v < (ssize_t) (width-j); v++)
396 {
397 for (u=0; u < (ssize_t) (width-j); u++)
398 {
399 pixel+=(*k)*pixels[i];
400 gamma+=(*k);
401 k++;
402 pixels+=GetPixelChannels(image);
403 }
404 }
405 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000406 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000407 continue;
408 }
409 /*
410 Alpha blending.
411 */
412 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000413 {
cristy4c11c2b2011-09-05 20:17:07 +0000414 for (u=0; u < (ssize_t) (width-j); u++)
415 {
416 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
417 pixel+=(*k)*alpha*pixels[i];
418 gamma+=(*k)*alpha;
419 k++;
420 pixels+=GetPixelChannels(image);
421 }
cristy3ed852e2009-09-05 21:47:34 +0000422 }
cristy4c11c2b2011-09-05 20:17:07 +0000423 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000424 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000425 }
cristyed231572011-07-14 02:18:59 +0000426 q+=GetPixelChannels(blur_image);
427 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000428 }
429 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
430 status=MagickFalse;
431 if (image->progress_monitor != (MagickProgressMonitor) NULL)
432 {
433 MagickBooleanType
434 proceed;
435
cristyb5d5f722009-11-04 03:03:49 +0000436#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8ae632d2011-09-05 17:29:53 +0000437 #pragma omp critical (MagickCore_AdaptiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +0000438#endif
439 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
440 image->rows);
441 if (proceed == MagickFalse)
442 status=MagickFalse;
443 }
444 }
445 blur_image->type=image->type;
446 blur_view=DestroyCacheView(blur_view);
447 edge_view=DestroyCacheView(edge_view);
448 image_view=DestroyCacheView(image_view);
449 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000450 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000451 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
452 kernel=(double **) RelinquishMagickMemory(kernel);
453 if (status == MagickFalse)
454 blur_image=DestroyImage(blur_image);
455 return(blur_image);
456}
457
458/*
459%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460% %
461% %
462% %
463% A d a p t i v e S h a r p e n I m a g e %
464% %
465% %
466% %
467%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468%
469% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
470% intensely near image edges and less intensely far from edges. We sharpen the
471% image with a Gaussian operator of the given radius and standard deviation
472% (sigma). For reasonable results, radius should be larger than sigma. Use a
473% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
474%
475% The format of the AdaptiveSharpenImage method is:
476%
477% Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000478% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000479%
480% A description of each parameter follows:
481%
482% o image: the image.
483%
cristy3ed852e2009-09-05 21:47:34 +0000484% o radius: the radius of the Gaussian, in pixels, not counting the center
485% pixel.
486%
487% o sigma: the standard deviation of the Laplacian, in pixels.
488%
cristy4c11c2b2011-09-05 20:17:07 +0000489% o bias: the bias.
490%
cristy3ed852e2009-09-05 21:47:34 +0000491% o exception: return any errors or warnings in this structure.
492%
493*/
cristy3ed852e2009-09-05 21:47:34 +0000494MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000495 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000496{
cristy3ed852e2009-09-05 21:47:34 +0000497#define AdaptiveSharpenImageTag "Convolve/Image"
498#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
499
cristyc4c8d132010-01-07 01:58:38 +0000500 CacheView
501 *sharp_view,
502 *edge_view,
503 *image_view;
504
cristy3ed852e2009-09-05 21:47:34 +0000505 double
cristy47e00502009-12-17 19:19:57 +0000506 **kernel,
507 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000508
509 Image
510 *sharp_image,
511 *edge_image,
512 *gaussian_image;
513
cristy3ed852e2009-09-05 21:47:34 +0000514 MagickBooleanType
515 status;
516
cristybb503372010-05-27 20:51:26 +0000517 MagickOffsetType
518 progress;
519
cristybb503372010-05-27 20:51:26 +0000520 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000521 i;
cristy3ed852e2009-09-05 21:47:34 +0000522
cristybb503372010-05-27 20:51:26 +0000523 size_t
cristy3ed852e2009-09-05 21:47:34 +0000524 width;
525
cristybb503372010-05-27 20:51:26 +0000526 ssize_t
527 j,
528 k,
529 u,
530 v,
531 y;
532
cristy3ed852e2009-09-05 21:47:34 +0000533 assert(image != (const Image *) NULL);
534 assert(image->signature == MagickSignature);
535 if (image->debug != MagickFalse)
536 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
537 assert(exception != (ExceptionInfo *) NULL);
538 assert(exception->signature == MagickSignature);
539 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
540 if (sharp_image == (Image *) NULL)
541 return((Image *) NULL);
542 if (fabs(sigma) <= MagickEpsilon)
543 return(sharp_image);
cristy574cc262011-08-05 01:23:58 +0000544 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
cristy3ed852e2009-09-05 21:47:34 +0000546 sharp_image=DestroyImage(sharp_image);
547 return((Image *) NULL);
548 }
549 /*
550 Edge detect the image brighness channel, level, sharp, and level again.
551 */
cristy8ae632d2011-09-05 17:29:53 +0000552 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000553 if (edge_image == (Image *) NULL)
554 {
555 sharp_image=DestroyImage(sharp_image);
556 return((Image *) NULL);
557 }
cristy051718b2011-08-28 22:49:25 +0000558 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristy05c0c9a2011-09-05 23:16:13 +0000559 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +0000560 if (gaussian_image != (Image *) NULL)
561 {
562 edge_image=DestroyImage(edge_image);
563 edge_image=gaussian_image;
564 }
cristy051718b2011-08-28 22:49:25 +0000565 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000566 /*
567 Create a set of kernels from maximum (radius,sigma) to minimum.
568 */
569 width=GetOptimalKernelWidth2D(radius,sigma);
570 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
571 if (kernel == (double **) NULL)
572 {
573 edge_image=DestroyImage(edge_image);
574 sharp_image=DestroyImage(sharp_image);
575 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
576 }
577 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000578 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000579 {
580 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
581 sizeof(**kernel));
582 if (kernel[i] == (double *) NULL)
583 break;
cristy47e00502009-12-17 19:19:57 +0000584 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000585 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000586 k=0;
587 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000588 {
cristy47e00502009-12-17 19:19:57 +0000589 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000590 {
cristy4205a3c2010-09-12 20:19:59 +0000591 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
592 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000593 normalize+=kernel[i][k];
594 k++;
cristy3ed852e2009-09-05 21:47:34 +0000595 }
596 }
cristy3ed852e2009-09-05 21:47:34 +0000597 if (fabs(normalize) <= MagickEpsilon)
598 normalize=1.0;
599 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000600 for (k=0; k < (j*j); k++)
601 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000602 }
cristybb503372010-05-27 20:51:26 +0000603 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000604 {
605 for (i-=2; i >= 0; i-=2)
606 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
607 kernel=(double **) RelinquishMagickMemory(kernel);
608 edge_image=DestroyImage(edge_image);
609 sharp_image=DestroyImage(sharp_image);
610 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
611 }
612 /*
613 Adaptively sharpen image.
614 */
615 status=MagickTrue;
616 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000617 image_view=AcquireCacheView(image);
618 edge_view=AcquireCacheView(edge_image);
619 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000620#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000621 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000622#endif
cristybb503372010-05-27 20:51:26 +0000623 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000624 {
cristy4c08aed2011-07-01 19:47:50 +0000625 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000626 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000627
cristy4c08aed2011-07-01 19:47:50 +0000628 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000629 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000630
cristy117ff172010-08-15 21:35:32 +0000631 register ssize_t
632 x;
633
cristy3ed852e2009-09-05 21:47:34 +0000634 if (status == MagickFalse)
635 continue;
636 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
637 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
638 exception);
cristy4c08aed2011-07-01 19:47:50 +0000639 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000640 {
641 status=MagickFalse;
642 continue;
643 }
cristybb503372010-05-27 20:51:26 +0000644 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000645 {
cristy4c11c2b2011-09-05 20:17:07 +0000646 register const Quantum
647 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000648
cristybb503372010-05-27 20:51:26 +0000649 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000650 i;
cristy3ed852e2009-09-05 21:47:34 +0000651
cristy4c11c2b2011-09-05 20:17:07 +0000652 ssize_t
653 center,
654 j;
655
656 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000657 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000658 if (j < 0)
659 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000660 else
cristy4c11c2b2011-09-05 20:17:07 +0000661 if (j > (ssize_t) width)
662 j=(ssize_t) width;
663 if ((j & 0x01) != 0)
664 j--;
665 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
666 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000667 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000668 break;
cristy4c11c2b2011-09-05 20:17:07 +0000669 center=(ssize_t) GetPixelChannels(image)*(width-j)*
670 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2);
671 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000672 {
cristy4c11c2b2011-09-05 20:17:07 +0000673 MagickRealType
674 alpha,
675 gamma,
676 pixel;
677
678 PixelChannel
679 channel;
680
681 PixelTrait
682 sharp_traits,
683 traits;
684
685 register const double
686 *restrict k;
687
688 register const Quantum
689 *restrict pixels;
690
691 register ssize_t
692 u;
693
694 ssize_t
695 v;
696
cristye2a912b2011-12-05 20:02:07 +0000697 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000698 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000699 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
700 if ((traits == UndefinedPixelTrait) ||
701 (sharp_traits == UndefinedPixelTrait))
702 continue;
703 if ((sharp_traits & CopyPixelTrait) != 0)
704 {
cristy0beccfa2011-09-25 20:47:53 +0000705 SetPixelChannel(sharp_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000706 continue;
707 }
708 k=kernel[j];
709 pixels=p;
710 pixel=bias;
711 gamma=0.0;
712 if ((sharp_traits & BlendPixelTrait) == 0)
713 {
714 /*
715 No alpha blending.
716 */
717 for (v=0; v < (ssize_t) (width-j); v++)
718 {
719 for (u=0; u < (ssize_t) (width-j); u++)
720 {
721 pixel+=(*k)*pixels[i];
722 gamma+=(*k);
723 k++;
724 pixels+=GetPixelChannels(image);
725 }
726 }
727 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000728 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000729 continue;
730 }
731 /*
732 Alpha blending.
733 */
734 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000735 {
cristy4c11c2b2011-09-05 20:17:07 +0000736 for (u=0; u < (ssize_t) (width-j); u++)
737 {
738 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
739 pixel+=(*k)*alpha*pixels[i];
740 gamma+=(*k)*alpha;
741 k++;
742 pixels+=GetPixelChannels(image);
743 }
cristy3ed852e2009-09-05 21:47:34 +0000744 }
cristy4c11c2b2011-09-05 20:17:07 +0000745 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000746 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000747 }
cristyed231572011-07-14 02:18:59 +0000748 q+=GetPixelChannels(sharp_image);
749 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000750 }
751 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
752 status=MagickFalse;
753 if (image->progress_monitor != (MagickProgressMonitor) NULL)
754 {
755 MagickBooleanType
756 proceed;
757
cristyb5d5f722009-11-04 03:03:49 +0000758#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +0000759 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000760#endif
761 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
762 image->rows);
763 if (proceed == MagickFalse)
764 status=MagickFalse;
765 }
766 }
767 sharp_image->type=image->type;
768 sharp_view=DestroyCacheView(sharp_view);
769 edge_view=DestroyCacheView(edge_view);
770 image_view=DestroyCacheView(image_view);
771 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000772 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000773 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
774 kernel=(double **) RelinquishMagickMemory(kernel);
775 if (status == MagickFalse)
776 sharp_image=DestroyImage(sharp_image);
777 return(sharp_image);
778}
779
780/*
781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782% %
783% %
784% %
785% B l u r I m a g e %
786% %
787% %
788% %
789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790%
791% BlurImage() blurs an image. We convolve the image with a Gaussian operator
792% of the given radius and standard deviation (sigma). For reasonable results,
793% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
794% selects a suitable radius for you.
795%
796% BlurImage() differs from GaussianBlurImage() in that it uses a separable
797% kernel which is faster but mathematically equivalent to the non-separable
798% kernel.
799%
800% The format of the BlurImage method is:
801%
802% Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000803% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000804%
805% A description of each parameter follows:
806%
807% o image: the image.
808%
cristy3ed852e2009-09-05 21:47:34 +0000809% o radius: the radius of the Gaussian, in pixels, not counting the center
810% pixel.
811%
812% o sigma: the standard deviation of the Gaussian, in pixels.
813%
cristy05c0c9a2011-09-05 23:16:13 +0000814% o bias: the bias.
815%
cristy3ed852e2009-09-05 21:47:34 +0000816% o exception: return any errors or warnings in this structure.
817%
818*/
819
cristybb503372010-05-27 20:51:26 +0000820static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000821{
cristy3ed852e2009-09-05 21:47:34 +0000822 double
cristy47e00502009-12-17 19:19:57 +0000823 *kernel,
824 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000825
cristy117ff172010-08-15 21:35:32 +0000826 register ssize_t
827 i;
828
cristybb503372010-05-27 20:51:26 +0000829 ssize_t
cristy47e00502009-12-17 19:19:57 +0000830 j,
831 k;
cristy3ed852e2009-09-05 21:47:34 +0000832
cristy3ed852e2009-09-05 21:47:34 +0000833 /*
834 Generate a 1-D convolution kernel.
835 */
836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
837 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
838 if (kernel == (double *) NULL)
839 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000840 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000841 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000842 i=0;
843 for (k=(-j); k <= j; k++)
844 {
cristy4205a3c2010-09-12 20:19:59 +0000845 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
846 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000847 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000848 i++;
849 }
cristybb503372010-05-27 20:51:26 +0000850 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000851 kernel[i]/=normalize;
852 return(kernel);
853}
854
cristyf4ad9df2011-07-08 16:49:03 +0000855MagickExport Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000856 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000857{
858#define BlurImageTag "Blur/Image"
859
cristyc4c8d132010-01-07 01:58:38 +0000860 CacheView
861 *blur_view,
862 *image_view;
863
cristy3ed852e2009-09-05 21:47:34 +0000864 double
865 *kernel;
866
867 Image
868 *blur_image;
869
cristy3ed852e2009-09-05 21:47:34 +0000870 MagickBooleanType
871 status;
872
cristybb503372010-05-27 20:51:26 +0000873 MagickOffsetType
874 progress;
875
cristybb503372010-05-27 20:51:26 +0000876 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000877 i;
878
cristybb503372010-05-27 20:51:26 +0000879 size_t
cristy3ed852e2009-09-05 21:47:34 +0000880 width;
881
cristybb503372010-05-27 20:51:26 +0000882 ssize_t
cristyb41a1172011-09-06 00:55:14 +0000883 center,
cristybb503372010-05-27 20:51:26 +0000884 x,
885 y;
886
cristy3ed852e2009-09-05 21:47:34 +0000887 /*
888 Initialize blur image attributes.
889 */
890 assert(image != (Image *) NULL);
891 assert(image->signature == MagickSignature);
892 if (image->debug != MagickFalse)
893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
894 assert(exception != (ExceptionInfo *) NULL);
895 assert(exception->signature == MagickSignature);
cristyd25c77e2011-09-06 00:10:24 +0000896 blur_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000897 if (blur_image == (Image *) NULL)
898 return((Image *) NULL);
899 if (fabs(sigma) <= MagickEpsilon)
900 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000901 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000902 {
cristy3ed852e2009-09-05 21:47:34 +0000903 blur_image=DestroyImage(blur_image);
904 return((Image *) NULL);
905 }
906 width=GetOptimalKernelWidth1D(radius,sigma);
907 kernel=GetBlurKernel(width,sigma);
908 if (kernel == (double *) NULL)
909 {
910 blur_image=DestroyImage(blur_image);
911 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
912 }
913 if (image->debug != MagickFalse)
914 {
915 char
916 format[MaxTextExtent],
917 *message;
918
919 register const double
920 *k;
921
922 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000923 " BlurImage with %.20g kernel:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000924 message=AcquireString("");
925 k=kernel;
cristybb503372010-05-27 20:51:26 +0000926 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000927 {
928 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000929 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000930 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000931 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000932 (void) ConcatenateString(&message,format);
933 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
934 }
935 message=DestroyString(message);
936 }
937 /*
938 Blur rows.
939 */
940 status=MagickTrue;
941 progress=0;
cristyb41a1172011-09-06 00:55:14 +0000942 center=(ssize_t) GetPixelChannels(image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +0000943 image_view=AcquireCacheView(image);
944 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000945#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000946 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000947#endif
cristyb41a1172011-09-06 00:55:14 +0000948 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000949 {
cristy4c08aed2011-07-01 19:47:50 +0000950 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000951 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000952
cristy4c08aed2011-07-01 19:47:50 +0000953 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000954 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000955
cristy117ff172010-08-15 21:35:32 +0000956 register ssize_t
957 x;
958
cristy3ed852e2009-09-05 21:47:34 +0000959 if (status == MagickFalse)
960 continue;
cristy117ff172010-08-15 21:35:32 +0000961 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
962 image->columns+width,1,exception);
cristy3ed852e2009-09-05 21:47:34 +0000963 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
964 exception);
cristy4c08aed2011-07-01 19:47:50 +0000965 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000966 {
967 status=MagickFalse;
968 continue;
969 }
cristyb41a1172011-09-06 00:55:14 +0000970 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000971 {
cristybb503372010-05-27 20:51:26 +0000972 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000973 i;
974
cristyb41a1172011-09-06 00:55:14 +0000975 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
976 {
977 MagickRealType
978 alpha,
979 gamma,
980 pixel;
cristyd25c77e2011-09-06 00:10:24 +0000981
cristyb41a1172011-09-06 00:55:14 +0000982 PixelChannel
983 channel;
984
985 PixelTrait
986 blur_traits,
987 traits;
988
989 register const double
990 *restrict k;
991
992 register const Quantum
993 *restrict pixels;
994
995 register ssize_t
996 u;
997
cristye2a912b2011-12-05 20:02:07 +0000998 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000999 traits=GetPixelChannelMapTraits(image,channel);
cristyb41a1172011-09-06 00:55:14 +00001000 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1001 if ((traits == UndefinedPixelTrait) ||
1002 (blur_traits == UndefinedPixelTrait))
1003 continue;
1004 if ((blur_traits & CopyPixelTrait) != 0)
cristyd25c77e2011-09-06 00:10:24 +00001005 {
cristy0beccfa2011-09-25 20:47:53 +00001006 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001007 continue;
cristyd25c77e2011-09-06 00:10:24 +00001008 }
cristyb41a1172011-09-06 00:55:14 +00001009 k=kernel;
1010 pixels=p;
1011 pixel=0.0;
1012 if ((blur_traits & BlendPixelTrait) == 0)
1013 {
1014 /*
1015 No alpha blending.
1016 */
1017 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001018 {
cristyb41a1172011-09-06 00:55:14 +00001019 pixel+=(*k)*pixels[i];
1020 k++;
1021 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001022 }
cristy0beccfa2011-09-25 20:47:53 +00001023 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001024 continue;
1025 }
1026 /*
1027 Alpha blending.
1028 */
1029 gamma=0.0;
1030 for (u=0; u < (ssize_t) width; u++)
1031 {
1032 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1033 pixel+=(*k)*alpha*pixels[i];
1034 gamma+=(*k)*alpha;
1035 k++;
1036 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001037 }
cristyb41a1172011-09-06 00:55:14 +00001038 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001039 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001040 }
cristyed231572011-07-14 02:18:59 +00001041 p+=GetPixelChannels(image);
1042 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001043 }
1044 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1045 status=MagickFalse;
1046 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1047 {
1048 MagickBooleanType
1049 proceed;
1050
cristyb5d5f722009-11-04 03:03:49 +00001051#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001052 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001053#endif
1054 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1055 blur_image->columns);
1056 if (proceed == MagickFalse)
1057 status=MagickFalse;
1058 }
1059 }
1060 blur_view=DestroyCacheView(blur_view);
1061 image_view=DestroyCacheView(image_view);
1062 /*
1063 Blur columns.
1064 */
1065 image_view=AcquireCacheView(blur_image);
1066 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001067#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001068 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001069#endif
cristyb6dc4b72011-12-10 23:29:53 +00001070 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001071 {
cristy4c08aed2011-07-01 19:47:50 +00001072 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001073 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001074
cristy4c08aed2011-07-01 19:47:50 +00001075 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001076 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001077
cristy117ff172010-08-15 21:35:32 +00001078 register ssize_t
1079 y;
1080
cristy3ed852e2009-09-05 21:47:34 +00001081 if (status == MagickFalse)
1082 continue;
cristy117ff172010-08-15 21:35:32 +00001083 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
cristyb6dc4b72011-12-10 23:29:53 +00001084 blur_image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001085 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001086 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001087 {
1088 status=MagickFalse;
1089 continue;
1090 }
cristyb6dc4b72011-12-10 23:29:53 +00001091 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001092 {
cristybb503372010-05-27 20:51:26 +00001093 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001094 i;
1095
cristyb6dc4b72011-12-10 23:29:53 +00001096 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
cristyb41a1172011-09-06 00:55:14 +00001097 {
1098 MagickRealType
1099 alpha,
1100 gamma,
1101 pixel;
cristyd25c77e2011-09-06 00:10:24 +00001102
cristyb41a1172011-09-06 00:55:14 +00001103 PixelChannel
1104 channel;
1105
1106 PixelTrait
1107 blur_traits,
1108 traits;
1109
1110 register const double
1111 *restrict k;
1112
1113 register const Quantum
1114 *restrict pixels;
1115
1116 register ssize_t
1117 u;
1118
cristye2a912b2011-12-05 20:02:07 +00001119 channel=GetPixelChannelMapChannel(blur_image,i);
cristyabace412011-12-11 15:56:53 +00001120 traits=GetPixelChannelMapTraits(blur_image,channel);
cristyb41a1172011-09-06 00:55:14 +00001121 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1122 if ((traits == UndefinedPixelTrait) ||
1123 (blur_traits == UndefinedPixelTrait))
1124 continue;
1125 if ((blur_traits & CopyPixelTrait) != 0)
cristyd25c77e2011-09-06 00:10:24 +00001126 {
cristy0beccfa2011-09-25 20:47:53 +00001127 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001128 continue;
cristyd25c77e2011-09-06 00:10:24 +00001129 }
cristyb41a1172011-09-06 00:55:14 +00001130 k=kernel;
1131 pixels=p;
1132 pixel=0.0;
1133 if ((blur_traits & BlendPixelTrait) == 0)
1134 {
1135 /*
1136 No alpha blending.
1137 */
1138 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001139 {
cristyb41a1172011-09-06 00:55:14 +00001140 pixel+=(*k)*pixels[i];
1141 k++;
1142 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001143 }
cristy0beccfa2011-09-25 20:47:53 +00001144 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001145 continue;
1146 }
1147 /*
1148 Alpha blending.
1149 */
1150 gamma=0.0;
1151 for (u=0; u < (ssize_t) width; u++)
1152 {
cristyb6dc4b72011-12-10 23:29:53 +00001153 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1154 pixels));
cristyb41a1172011-09-06 00:55:14 +00001155 pixel+=(*k)*alpha*pixels[i];
1156 gamma+=(*k)*alpha;
1157 k++;
1158 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001159 }
cristyb41a1172011-09-06 00:55:14 +00001160 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001161 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001162 }
cristyd25c77e2011-09-06 00:10:24 +00001163 p+=GetPixelChannels(blur_image);
cristyed231572011-07-14 02:18:59 +00001164 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001165 }
1166 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1167 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001168 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001169 {
1170 MagickBooleanType
1171 proceed;
1172
cristyb5d5f722009-11-04 03:03:49 +00001173#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001174 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001175#endif
cristy4c08aed2011-07-01 19:47:50 +00001176 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1177 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001178 if (proceed == MagickFalse)
1179 status=MagickFalse;
1180 }
1181 }
1182 blur_view=DestroyCacheView(blur_view);
1183 image_view=DestroyCacheView(image_view);
1184 kernel=(double *) RelinquishMagickMemory(kernel);
1185 if (status == MagickFalse)
1186 blur_image=DestroyImage(blur_image);
1187 blur_image->type=image->type;
1188 return(blur_image);
1189}
1190
1191/*
1192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193% %
1194% %
1195% %
cristyfccdab92009-11-30 16:43:57 +00001196% C o n v o l v e I m a g e %
1197% %
1198% %
1199% %
1200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201%
1202% ConvolveImage() applies a custom convolution kernel to the image.
1203%
1204% The format of the ConvolveImage method is:
1205%
cristy5e6be1e2011-07-16 01:23:39 +00001206% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1207% ExceptionInfo *exception)
1208%
cristyfccdab92009-11-30 16:43:57 +00001209% A description of each parameter follows:
1210%
1211% o image: the image.
1212%
cristy5e6be1e2011-07-16 01:23:39 +00001213% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001214%
1215% o exception: return any errors or warnings in this structure.
1216%
1217*/
cristy5e6be1e2011-07-16 01:23:39 +00001218MagickExport Image *ConvolveImage(const Image *image,
1219 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001220{
cristyfccdab92009-11-30 16:43:57 +00001221#define ConvolveImageTag "Convolve/Image"
1222
cristyc4c8d132010-01-07 01:58:38 +00001223 CacheView
1224 *convolve_view,
cristy105ba3c2011-07-18 02:28:38 +00001225 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00001226
cristyfccdab92009-11-30 16:43:57 +00001227 Image
1228 *convolve_image;
1229
cristyfccdab92009-11-30 16:43:57 +00001230 MagickBooleanType
1231 status;
1232
cristybb503372010-05-27 20:51:26 +00001233 MagickOffsetType
1234 progress;
1235
cristybb503372010-05-27 20:51:26 +00001236 ssize_t
cristy574cc262011-08-05 01:23:58 +00001237 center,
cristybb503372010-05-27 20:51:26 +00001238 y;
1239
cristyfccdab92009-11-30 16:43:57 +00001240 /*
1241 Initialize convolve image attributes.
1242 */
1243 assert(image != (Image *) NULL);
1244 assert(image->signature == MagickSignature);
1245 if (image->debug != MagickFalse)
1246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1247 assert(exception != (ExceptionInfo *) NULL);
1248 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001249 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001250 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001251 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1252 exception);
cristyfccdab92009-11-30 16:43:57 +00001253 if (convolve_image == (Image *) NULL)
1254 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001255 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001256 {
cristyfccdab92009-11-30 16:43:57 +00001257 convolve_image=DestroyImage(convolve_image);
1258 return((Image *) NULL);
1259 }
1260 if (image->debug != MagickFalse)
1261 {
1262 char
1263 format[MaxTextExtent],
1264 *message;
1265
cristya96f2492011-12-14 18:25:41 +00001266 register const MagickRealType
cristy117ff172010-08-15 21:35:32 +00001267 *k;
1268
cristy4e154852011-07-14 13:28:53 +00001269 register ssize_t
1270 u;
1271
cristybb503372010-05-27 20:51:26 +00001272 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001273 v;
1274
cristyfccdab92009-11-30 16:43:57 +00001275 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001276 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1277 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001278 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001279 k=kernel_info->values;
1280 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001281 {
1282 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001283 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001284 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001285 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001286 {
cristyb51dff52011-05-19 16:55:47 +00001287 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001288 (void) ConcatenateString(&message,format);
1289 }
1290 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1291 }
1292 message=DestroyString(message);
1293 }
cristy7ea27962011-09-12 18:12:49 +00001294 status=AccelerateConvolveImage(image,kernel_info,convolve_image,exception);
1295 if (status == MagickTrue)
1296 return(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001297 /*
cristyfccdab92009-11-30 16:43:57 +00001298 Convolve image.
1299 */
cristy574cc262011-08-05 01:23:58 +00001300 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
cristy075ff302011-09-07 01:51:24 +00001301 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2L);
cristyfccdab92009-11-30 16:43:57 +00001302 status=MagickTrue;
1303 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001304 image_view=AcquireCacheView(image);
1305 convolve_view=AcquireCacheView(convolve_image);
1306#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001307 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001308#endif
cristybb503372010-05-27 20:51:26 +00001309 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001310 {
cristy4c08aed2011-07-01 19:47:50 +00001311 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001312 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001313
cristy4c08aed2011-07-01 19:47:50 +00001314 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001315 *restrict q;
1316
cristy117ff172010-08-15 21:35:32 +00001317 register ssize_t
1318 x;
1319
cristyfccdab92009-11-30 16:43:57 +00001320 if (status == MagickFalse)
1321 continue;
cristy105ba3c2011-07-18 02:28:38 +00001322 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1323 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1324 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001325 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001326 exception);
cristy105ba3c2011-07-18 02:28:38 +00001327 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001328 {
1329 status=MagickFalse;
1330 continue;
1331 }
cristybb503372010-05-27 20:51:26 +00001332 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001333 {
cristybb503372010-05-27 20:51:26 +00001334 register ssize_t
cristyed231572011-07-14 02:18:59 +00001335 i;
cristyfccdab92009-11-30 16:43:57 +00001336
cristya30d9ba2011-07-23 21:00:48 +00001337 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001338 {
cristyed231572011-07-14 02:18:59 +00001339 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001340 alpha,
1341 gamma,
cristyed231572011-07-14 02:18:59 +00001342 pixel;
1343
1344 PixelChannel
1345 channel;
1346
1347 PixelTrait
1348 convolve_traits,
1349 traits;
1350
cristya96f2492011-12-14 18:25:41 +00001351 register const MagickRealType
cristyed231572011-07-14 02:18:59 +00001352 *restrict k;
1353
1354 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001355 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001356
1357 register ssize_t
1358 u;
1359
1360 ssize_t
1361 v;
1362
cristye2a912b2011-12-05 20:02:07 +00001363 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001364 traits=GetPixelChannelMapTraits(image,channel);
cristy4e154852011-07-14 13:28:53 +00001365 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001366 if ((traits == UndefinedPixelTrait) ||
1367 (convolve_traits == UndefinedPixelTrait))
cristy4e154852011-07-14 13:28:53 +00001368 continue;
1369 if ((convolve_traits & CopyPixelTrait) != 0)
1370 {
cristy0beccfa2011-09-25 20:47:53 +00001371 SetPixelChannel(convolve_image,channel,p[center+i],q);
cristy4e154852011-07-14 13:28:53 +00001372 continue;
1373 }
cristy5e6be1e2011-07-16 01:23:39 +00001374 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001375 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001376 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001377 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001378 {
cristyed231572011-07-14 02:18:59 +00001379 /*
cristy4e154852011-07-14 13:28:53 +00001380 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001381 */
cristyeb52cde2011-07-17 01:52:52 +00001382 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001383 {
cristyeb52cde2011-07-17 01:52:52 +00001384 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001385 {
cristyeb52cde2011-07-17 01:52:52 +00001386 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001387 k++;
cristya30d9ba2011-07-23 21:00:48 +00001388 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001389 }
cristya30d9ba2011-07-23 21:00:48 +00001390 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001391 }
cristy0beccfa2011-09-25 20:47:53 +00001392 SetPixelChannel(convolve_image,channel,ClampToQuantum(pixel),q);
cristy4e154852011-07-14 13:28:53 +00001393 continue;
cristyed231572011-07-14 02:18:59 +00001394 }
cristy4e154852011-07-14 13:28:53 +00001395 /*
1396 Alpha blending.
1397 */
1398 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001399 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001400 {
cristyeb52cde2011-07-17 01:52:52 +00001401 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001402 {
cristyeb52cde2011-07-17 01:52:52 +00001403 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1404 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001405 gamma+=(*k)*alpha;
1406 k++;
cristya30d9ba2011-07-23 21:00:48 +00001407 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001408 }
cristya30d9ba2011-07-23 21:00:48 +00001409 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001410 }
cristy1ce96d02011-07-14 17:57:24 +00001411 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001412 SetPixelChannel(convolve_image,channel,ClampToQuantum(gamma*pixel),q);
cristyed231572011-07-14 02:18:59 +00001413 }
cristya30d9ba2011-07-23 21:00:48 +00001414 p+=GetPixelChannels(image);
1415 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001416 }
cristyed231572011-07-14 02:18:59 +00001417 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001418 status=MagickFalse;
1419 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1420 {
1421 MagickBooleanType
1422 proceed;
1423
1424#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001425 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001426#endif
1427 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1428 if (proceed == MagickFalse)
1429 status=MagickFalse;
1430 }
1431 }
1432 convolve_image->type=image->type;
1433 convolve_view=DestroyCacheView(convolve_view);
1434 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001435 if (status == MagickFalse)
1436 convolve_image=DestroyImage(convolve_image);
1437 return(convolve_image);
1438}
1439
1440/*
1441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442% %
1443% %
1444% %
cristy3ed852e2009-09-05 21:47:34 +00001445% D e s p e c k l e I m a g e %
1446% %
1447% %
1448% %
1449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450%
1451% DespeckleImage() reduces the speckle noise in an image while perserving the
cristyf0ae7762012-01-15 22:04:03 +00001452% edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1453% neighbors, then complementarily lowering pixels that are brighter than their
1454% surrounding neighbors) to reduce the speckle index of that image (reference
1455% Crimmins speckle removal).
cristy3ed852e2009-09-05 21:47:34 +00001456%
1457% The format of the DespeckleImage method is:
1458%
1459% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1460%
1461% A description of each parameter follows:
1462%
1463% o image: the image.
1464%
1465% o exception: return any errors or warnings in this structure.
1466%
1467*/
1468
cristy78f3de22012-01-16 00:43:18 +00001469static void inline Hull(const ssize_t x,const ssize_t y,const int polarity,
cristyf0ae7762012-01-15 22:04:03 +00001470 Quantum *pixels)
cristy3ed852e2009-09-05 21:47:34 +00001471{
cristyb06da092012-01-16 17:11:38 +00001472 MagickRealType
cristy78f3de22012-01-16 00:43:18 +00001473 pixel;
cristy3ed852e2009-09-05 21:47:34 +00001474
cristy78f3de22012-01-16 00:43:18 +00001475 Quantum
1476 *a,
1477 *b,
1478 *c;
1479
1480 b=pixels+4;
1481 a=b-(y*3)-x;
1482 c=b+(y*3)+x;
cristyb06da092012-01-16 17:11:38 +00001483 pixel=(MagickRealType) *b;
cristyf0ae7762012-01-15 22:04:03 +00001484 if (polarity > 0)
1485 {
cristyb06da092012-01-16 17:11:38 +00001486 if ((MagickRealType) *c >= (pixel+ScaleCharToQuantum(2)))
cristy78f3de22012-01-16 00:43:18 +00001487 pixel+=ScaleCharToQuantum(1);
cristyf0ae7762012-01-15 22:04:03 +00001488 }
1489 else
cristyb06da092012-01-16 17:11:38 +00001490 if ((MagickRealType) *c <= (pixel-ScaleCharToQuantum(2)))
cristy78f3de22012-01-16 00:43:18 +00001491 pixel-=ScaleCharToQuantum(1);
1492 if (polarity > 0)
cristyf0ae7762012-01-15 22:04:03 +00001493 {
cristyb06da092012-01-16 17:11:38 +00001494 if (((MagickRealType) *a >= (pixel+ScaleCharToQuantum(2))) &&
1495 ((MagickRealType) *c > pixel))
cristy78f3de22012-01-16 00:43:18 +00001496 pixel+=ScaleCharToQuantum(1);
cristyf0ae7762012-01-15 22:04:03 +00001497 }
cristy78f3de22012-01-16 00:43:18 +00001498 else
cristyb06da092012-01-16 17:11:38 +00001499 if (((MagickRealType) *a <= (pixel-ScaleCharToQuantum(2))) &&
1500 ((MagickRealType) *c < pixel))
cristy78f3de22012-01-16 00:43:18 +00001501 pixel-=ScaleCharToQuantum(1);
1502 pixels[4]=ClampToQuantum(pixel);
cristy3ed852e2009-09-05 21:47:34 +00001503}
1504
1505MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1506{
1507#define DespeckleImageTag "Despeckle/Image"
1508
cristy2407fc22009-09-11 00:55:25 +00001509 CacheView
1510 *despeckle_view,
1511 *image_view;
1512
cristy3ed852e2009-09-05 21:47:34 +00001513 Image
1514 *despeckle_image;
1515
cristy3ed852e2009-09-05 21:47:34 +00001516 MagickBooleanType
1517 status;
1518
cristyfc830f42012-01-15 02:45:06 +00001519 MagickOffsetType
1520 progress;
cristya63e4a92011-09-09 00:47:59 +00001521
cristyfc830f42012-01-15 02:45:06 +00001522 ssize_t
1523 y;
cristy117ff172010-08-15 21:35:32 +00001524
cristybb503372010-05-27 20:51:26 +00001525 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001526 X[4] = {0, 1, 1,-1},
1527 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001528
cristy3ed852e2009-09-05 21:47:34 +00001529 /*
1530 Allocate despeckled image.
1531 */
1532 assert(image != (const Image *) NULL);
1533 assert(image->signature == MagickSignature);
1534 if (image->debug != MagickFalse)
1535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1536 assert(exception != (ExceptionInfo *) NULL);
1537 assert(exception->signature == MagickSignature);
cristyb06da092012-01-16 17:11:38 +00001538 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1539 exception);
cristy3ed852e2009-09-05 21:47:34 +00001540 if (despeckle_image == (Image *) NULL)
1541 return((Image *) NULL);
cristya63e4a92011-09-09 00:47:59 +00001542 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1543 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001544 {
cristy3ed852e2009-09-05 21:47:34 +00001545 despeckle_image=DestroyImage(despeckle_image);
1546 return((Image *) NULL);
1547 }
1548 /*
cristyf0ae7762012-01-15 22:04:03 +00001549 Remove speckle from the image.
cristy3ed852e2009-09-05 21:47:34 +00001550 */
1551 status=MagickTrue;
cristyfc830f42012-01-15 02:45:06 +00001552 progress=0;
cristyb06da092012-01-16 17:11:38 +00001553 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00001554 despeckle_view=AcquireCacheView(despeckle_image);
cristyfc830f42012-01-15 02:45:06 +00001555#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf0ae7762012-01-15 22:04:03 +00001556 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyfc830f42012-01-15 02:45:06 +00001557#endif
cristyb06da092012-01-16 17:11:38 +00001558 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001559 {
cristy3ed852e2009-09-05 21:47:34 +00001560 register Quantum
cristyfc830f42012-01-15 02:45:06 +00001561 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001562
cristyc1488b52011-02-19 18:54:15 +00001563 register ssize_t
cristyf0ae7762012-01-15 22:04:03 +00001564 x;
cristyc1488b52011-02-19 18:54:15 +00001565
cristyfc830f42012-01-15 02:45:06 +00001566 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,1,
1567 exception);
cristyf0ae7762012-01-15 22:04:03 +00001568 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001569 {
cristya63e4a92011-09-09 00:47:59 +00001570 status=MagickFalse;
cristyfc830f42012-01-15 02:45:06 +00001571 continue;
1572 }
cristyb06da092012-01-16 17:11:38 +00001573 for (x=0; x < (ssize_t) image->columns; x++)
cristyfc830f42012-01-15 02:45:06 +00001574 {
cristyfc830f42012-01-15 02:45:06 +00001575 register ssize_t
cristyf0ae7762012-01-15 22:04:03 +00001576 i;
cristyfc830f42012-01-15 02:45:06 +00001577
cristyb06da092012-01-16 17:11:38 +00001578 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyfc830f42012-01-15 02:45:06 +00001579 {
cristyf0ae7762012-01-15 22:04:03 +00001580 PixelChannel
1581 channel;
1582
1583 PixelTrait
cristyb06da092012-01-16 17:11:38 +00001584 despeckle_traits,
cristyf0ae7762012-01-15 22:04:03 +00001585 traits;
1586
1587 Quantum
1588 pixels[9];
1589
1590 register const Quantum
1591 *restrict p;
1592
1593 register ssize_t
1594 j;
1595
cristyb06da092012-01-16 17:11:38 +00001596 channel=GetPixelChannelMapChannel(image,i);
1597 traits=GetPixelChannelMapTraits(image,channel);
1598 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1599 if ((traits == UndefinedPixelTrait) ||
1600 (despeckle_traits == UndefinedPixelTrait))
cristyf0ae7762012-01-15 22:04:03 +00001601 continue;
1602 p=GetCacheViewVirtualPixels(image_view,x-1,y-1,3,3,exception);
1603 if (p == (const Quantum *) NULL)
1604 {
1605 status=MagickFalse;
1606 continue;
1607 }
cristyf0ae7762012-01-15 22:04:03 +00001608 for (j=0; j < 9; j++)
cristyb06da092012-01-16 17:11:38 +00001609 pixels[j]=p[j*GetPixelChannels(image)+i];
1610 if ((traits & CopyPixelTrait) != 0)
1611 {
1612 SetPixelChannel(despeckle_image,channel,pixels[4],q);
1613 continue;
1614 }
cristyf0ae7762012-01-15 22:04:03 +00001615 for (j=0; j < 4; j++)
1616 {
1617 Hull(X[j],Y[j],1,pixels);
1618 Hull(-X[j],-Y[j],1,pixels);
1619 Hull(-X[j],-Y[j],-1,pixels);
1620 Hull(X[j],Y[j],-1,pixels);
1621 }
cristyb06da092012-01-16 17:11:38 +00001622 SetPixelChannel(despeckle_image,channel,pixels[4],q);
cristyfc830f42012-01-15 02:45:06 +00001623 }
cristyf0ae7762012-01-15 22:04:03 +00001624 q+=GetPixelChannels(despeckle_image);
cristy3ed852e2009-09-05 21:47:34 +00001625 }
cristyfc830f42012-01-15 02:45:06 +00001626 if (SyncCacheViewAuthenticPixels(despeckle_view,exception) == MagickFalse)
1627 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001628 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1629 {
1630 MagickBooleanType
1631 proceed;
1632
cristyfc830f42012-01-15 02:45:06 +00001633#if defined(MAGICKCORE_OPENMP_SUPPORT)
1634 #pragma omp critical (MagickCore_DespeckleImage)
1635#endif
cristyb06da092012-01-16 17:11:38 +00001636 proceed=SetImageProgress(image,DespeckleImageTag,progress,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001637 if (proceed == MagickFalse)
1638 status=MagickFalse;
1639 }
1640 }
1641 despeckle_view=DestroyCacheView(despeckle_view);
1642 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001643 despeckle_image->type=image->type;
1644 if (status == MagickFalse)
1645 despeckle_image=DestroyImage(despeckle_image);
1646 return(despeckle_image);
1647}
1648
1649/*
1650%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1651% %
1652% %
1653% %
1654% E d g e I m a g e %
1655% %
1656% %
1657% %
1658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1659%
1660% EdgeImage() finds edges in an image. Radius defines the radius of the
1661% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1662% radius for you.
1663%
1664% The format of the EdgeImage method is:
1665%
1666% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001667% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001668%
1669% A description of each parameter follows:
1670%
1671% o image: the image.
1672%
1673% o radius: the radius of the pixel neighborhood.
1674%
cristy8ae632d2011-09-05 17:29:53 +00001675% o sigma: the standard deviation of the Gaussian, in pixels.
1676%
cristy3ed852e2009-09-05 21:47:34 +00001677% o exception: return any errors or warnings in this structure.
1678%
1679*/
1680MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001681 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001682{
1683 Image
1684 *edge_image;
1685
cristy41cbe682011-07-15 19:12:37 +00001686 KernelInfo
1687 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001688
cristybb503372010-05-27 20:51:26 +00001689 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001690 i;
1691
cristybb503372010-05-27 20:51:26 +00001692 size_t
cristy3ed852e2009-09-05 21:47:34 +00001693 width;
1694
cristy41cbe682011-07-15 19:12:37 +00001695 ssize_t
1696 j,
1697 u,
1698 v;
1699
cristy3ed852e2009-09-05 21:47:34 +00001700 assert(image != (const Image *) NULL);
1701 assert(image->signature == MagickSignature);
1702 if (image->debug != MagickFalse)
1703 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1704 assert(exception != (ExceptionInfo *) NULL);
1705 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001706 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001707 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001708 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001709 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001710 kernel_info->width=width;
1711 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001712 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1713 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1714 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001715 {
1716 kernel_info=DestroyKernelInfo(kernel_info);
1717 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1718 }
1719 j=(ssize_t) kernel_info->width/2;
1720 i=0;
1721 for (v=(-j); v <= j; v++)
1722 {
1723 for (u=(-j); u <= j; u++)
1724 {
1725 kernel_info->values[i]=(-1.0);
1726 i++;
1727 }
1728 }
1729 kernel_info->values[i/2]=(double) (width*width-1.0);
anthony736a1602011-10-06 12:38:17 +00001730 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
cristy5e6be1e2011-07-16 01:23:39 +00001731 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001732 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001733 return(edge_image);
1734}
1735
1736/*
1737%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1738% %
1739% %
1740% %
1741% E m b o s s I m a g e %
1742% %
1743% %
1744% %
1745%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1746%
1747% EmbossImage() returns a grayscale image with a three-dimensional effect.
1748% We convolve the image with a Gaussian operator of the given radius and
1749% standard deviation (sigma). For reasonable results, radius should be
1750% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1751% radius for you.
1752%
1753% The format of the EmbossImage method is:
1754%
1755% Image *EmbossImage(const Image *image,const double radius,
1756% const double sigma,ExceptionInfo *exception)
1757%
1758% A description of each parameter follows:
1759%
1760% o image: the image.
1761%
1762% o radius: the radius of the pixel neighborhood.
1763%
1764% o sigma: the standard deviation of the Gaussian, in pixels.
1765%
1766% o exception: return any errors or warnings in this structure.
1767%
1768*/
1769MagickExport Image *EmbossImage(const Image *image,const double radius,
1770 const double sigma,ExceptionInfo *exception)
1771{
cristy3ed852e2009-09-05 21:47:34 +00001772 Image
1773 *emboss_image;
1774
cristy41cbe682011-07-15 19:12:37 +00001775 KernelInfo
1776 *kernel_info;
1777
cristybb503372010-05-27 20:51:26 +00001778 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001779 i;
1780
cristybb503372010-05-27 20:51:26 +00001781 size_t
cristy3ed852e2009-09-05 21:47:34 +00001782 width;
1783
cristy117ff172010-08-15 21:35:32 +00001784 ssize_t
1785 j,
1786 k,
1787 u,
1788 v;
1789
cristy41cbe682011-07-15 19:12:37 +00001790 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001791 assert(image->signature == MagickSignature);
1792 if (image->debug != MagickFalse)
1793 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1794 assert(exception != (ExceptionInfo *) NULL);
1795 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001796 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001797 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001798 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001799 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001800 kernel_info->width=width;
1801 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001802 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1803 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1804 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001805 {
1806 kernel_info=DestroyKernelInfo(kernel_info);
1807 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1808 }
1809 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001810 k=j;
1811 i=0;
1812 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001813 {
cristy47e00502009-12-17 19:19:57 +00001814 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001815 {
cristy41cbe682011-07-15 19:12:37 +00001816 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001817 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001818 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001819 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001820 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001821 i++;
1822 }
cristy47e00502009-12-17 19:19:57 +00001823 k--;
cristy3ed852e2009-09-05 21:47:34 +00001824 }
anthony736a1602011-10-06 12:38:17 +00001825 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
cristy5e6be1e2011-07-16 01:23:39 +00001826 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001827 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001828 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001829 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001830 return(emboss_image);
1831}
1832
1833/*
1834%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1835% %
1836% %
1837% %
1838% G a u s s i a n B l u r I m a g e %
1839% %
1840% %
1841% %
1842%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1843%
1844% GaussianBlurImage() blurs an image. We convolve the image with a
1845% Gaussian operator of the given radius and standard deviation (sigma).
1846% For reasonable results, the radius should be larger than sigma. Use a
1847% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1848%
1849% The format of the GaussianBlurImage method is:
1850%
1851% Image *GaussianBlurImage(const Image *image,onst double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001852% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001853%
1854% A description of each parameter follows:
1855%
1856% o image: the image.
1857%
cristy3ed852e2009-09-05 21:47:34 +00001858% o radius: the radius of the Gaussian, in pixels, not counting the center
1859% pixel.
1860%
1861% o sigma: the standard deviation of the Gaussian, in pixels.
1862%
cristy05c0c9a2011-09-05 23:16:13 +00001863% o bias: the bias.
1864%
cristy3ed852e2009-09-05 21:47:34 +00001865% o exception: return any errors or warnings in this structure.
1866%
1867*/
cristy41cbe682011-07-15 19:12:37 +00001868MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001869 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001870{
cristy3ed852e2009-09-05 21:47:34 +00001871 Image
1872 *blur_image;
1873
cristy41cbe682011-07-15 19:12:37 +00001874 KernelInfo
1875 *kernel_info;
1876
cristybb503372010-05-27 20:51:26 +00001877 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001878 i;
1879
cristybb503372010-05-27 20:51:26 +00001880 size_t
cristy3ed852e2009-09-05 21:47:34 +00001881 width;
1882
cristy117ff172010-08-15 21:35:32 +00001883 ssize_t
1884 j,
1885 u,
1886 v;
1887
cristy3ed852e2009-09-05 21:47:34 +00001888 assert(image != (const Image *) NULL);
1889 assert(image->signature == MagickSignature);
1890 if (image->debug != MagickFalse)
1891 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1892 assert(exception != (ExceptionInfo *) NULL);
1893 assert(exception->signature == MagickSignature);
1894 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001895 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001896 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001897 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001898 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1899 kernel_info->width=width;
1900 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00001901 kernel_info->bias=bias; /* FUTURE: user bias on Gaussian Blur! non-sense */
cristy41cbe682011-07-15 19:12:37 +00001902 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00001903 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1904 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1905 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001906 {
1907 kernel_info=DestroyKernelInfo(kernel_info);
1908 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1909 }
1910 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001911 i=0;
cristy47e00502009-12-17 19:19:57 +00001912 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001913 {
cristy47e00502009-12-17 19:19:57 +00001914 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001915 {
1916 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1917 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1918 i++;
1919 }
cristy3ed852e2009-09-05 21:47:34 +00001920 }
cristy5e6be1e2011-07-16 01:23:39 +00001921 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001922 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001923 return(blur_image);
1924}
1925
1926/*
1927%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1928% %
1929% %
1930% %
cristy3ed852e2009-09-05 21:47:34 +00001931% M o t i o n B l u r I m a g e %
1932% %
1933% %
1934% %
1935%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1936%
1937% MotionBlurImage() simulates motion blur. We convolve the image with a
1938% Gaussian operator of the given radius and standard deviation (sigma).
1939% For reasonable results, radius should be larger than sigma. Use a
1940% radius of 0 and MotionBlurImage() selects a suitable radius for you.
1941% Angle gives the angle of the blurring motion.
1942%
1943% Andrew Protano contributed this effect.
1944%
1945% The format of the MotionBlurImage method is:
1946%
1947% Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00001948% const double sigma,const double angle,const double bias,
1949% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001950%
1951% A description of each parameter follows:
1952%
1953% o image: the image.
1954%
cristy3ed852e2009-09-05 21:47:34 +00001955% o radius: the radius of the Gaussian, in pixels, not counting
1956% the center pixel.
1957%
1958% o sigma: the standard deviation of the Gaussian, in pixels.
1959%
cristycee97112010-05-28 00:44:52 +00001960% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00001961%
cristyf7ef0252011-09-09 14:50:06 +00001962% o bias: the bias.
1963%
cristy3ed852e2009-09-05 21:47:34 +00001964% o exception: return any errors or warnings in this structure.
1965%
1966*/
1967
cristybb503372010-05-27 20:51:26 +00001968static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00001969{
cristy3ed852e2009-09-05 21:47:34 +00001970 double
cristy47e00502009-12-17 19:19:57 +00001971 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00001972 normalize;
1973
cristybb503372010-05-27 20:51:26 +00001974 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001975 i;
1976
1977 /*
cristy47e00502009-12-17 19:19:57 +00001978 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00001979 */
1980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1981 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
1982 if (kernel == (double *) NULL)
1983 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00001984 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00001985 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00001986 {
cristy4205a3c2010-09-12 20:19:59 +00001987 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1988 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00001989 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00001990 }
cristybb503372010-05-27 20:51:26 +00001991 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001992 kernel[i]/=normalize;
1993 return(kernel);
1994}
1995
cristya63e4a92011-09-09 00:47:59 +00001996MagickExport Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00001997 const double sigma,const double angle,const double bias,
1998 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001999{
cristyc4c8d132010-01-07 01:58:38 +00002000 CacheView
2001 *blur_view,
2002 *image_view;
2003
cristy3ed852e2009-09-05 21:47:34 +00002004 double
2005 *kernel;
2006
2007 Image
2008 *blur_image;
2009
cristy3ed852e2009-09-05 21:47:34 +00002010 MagickBooleanType
2011 status;
2012
cristybb503372010-05-27 20:51:26 +00002013 MagickOffsetType
2014 progress;
2015
cristy3ed852e2009-09-05 21:47:34 +00002016 OffsetInfo
2017 *offset;
2018
2019 PointInfo
2020 point;
2021
cristybb503372010-05-27 20:51:26 +00002022 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002023 i;
2024
cristybb503372010-05-27 20:51:26 +00002025 size_t
cristy3ed852e2009-09-05 21:47:34 +00002026 width;
2027
cristybb503372010-05-27 20:51:26 +00002028 ssize_t
2029 y;
2030
cristy3ed852e2009-09-05 21:47:34 +00002031 assert(image != (Image *) NULL);
2032 assert(image->signature == MagickSignature);
2033 if (image->debug != MagickFalse)
2034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2035 assert(exception != (ExceptionInfo *) NULL);
2036 width=GetOptimalKernelWidth1D(radius,sigma);
2037 kernel=GetMotionBlurKernel(width,sigma);
2038 if (kernel == (double *) NULL)
2039 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2040 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2041 if (offset == (OffsetInfo *) NULL)
2042 {
2043 kernel=(double *) RelinquishMagickMemory(kernel);
2044 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2045 }
cristy1e7aa312011-09-10 20:01:36 +00002046 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002047 if (blur_image == (Image *) NULL)
2048 {
2049 kernel=(double *) RelinquishMagickMemory(kernel);
2050 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2051 return((Image *) NULL);
2052 }
cristy574cc262011-08-05 01:23:58 +00002053 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002054 {
2055 kernel=(double *) RelinquishMagickMemory(kernel);
2056 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002057 blur_image=DestroyImage(blur_image);
2058 return((Image *) NULL);
2059 }
2060 point.x=(double) width*sin(DegreesToRadians(angle));
2061 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002062 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002063 {
cristybb503372010-05-27 20:51:26 +00002064 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2065 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002066 }
2067 /*
2068 Motion blur image.
2069 */
2070 status=MagickTrue;
2071 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002072 image_view=AcquireCacheView(image);
2073 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002074#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00002075 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002076#endif
cristybb503372010-05-27 20:51:26 +00002077 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002078 {
cristyf7ef0252011-09-09 14:50:06 +00002079 register const Quantum
2080 *restrict p;
2081
cristy4c08aed2011-07-01 19:47:50 +00002082 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002083 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002084
cristy117ff172010-08-15 21:35:32 +00002085 register ssize_t
2086 x;
2087
cristy3ed852e2009-09-05 21:47:34 +00002088 if (status == MagickFalse)
2089 continue;
cristyf7ef0252011-09-09 14:50:06 +00002090 p=GetCacheViewVirtualPixels(blur_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00002091 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2092 exception);
cristyf7ef0252011-09-09 14:50:06 +00002093 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002094 {
2095 status=MagickFalse;
2096 continue;
2097 }
cristybb503372010-05-27 20:51:26 +00002098 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002099 {
cristybb503372010-05-27 20:51:26 +00002100 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002101 i;
2102
cristyf7ef0252011-09-09 14:50:06 +00002103 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2104 {
2105 MagickRealType
2106 alpha,
2107 gamma,
2108 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002109
cristyf7ef0252011-09-09 14:50:06 +00002110 PixelChannel
2111 channel;
2112
2113 PixelTrait
2114 blur_traits,
2115 traits;
2116
2117 register const Quantum
2118 *restrict r;
2119
2120 register double
2121 *restrict k;
2122
2123 register ssize_t
2124 j;
2125
cristye2a912b2011-12-05 20:02:07 +00002126 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002127 traits=GetPixelChannelMapTraits(image,channel);
cristyf7ef0252011-09-09 14:50:06 +00002128 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2129 if ((traits == UndefinedPixelTrait) ||
2130 (blur_traits == UndefinedPixelTrait))
2131 continue;
2132 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002133 {
cristy0beccfa2011-09-25 20:47:53 +00002134 SetPixelChannel(blur_image,channel,p[i],q);
cristyf7ef0252011-09-09 14:50:06 +00002135 continue;
cristy3ed852e2009-09-05 21:47:34 +00002136 }
cristyf7ef0252011-09-09 14:50:06 +00002137 k=kernel;
2138 pixel=bias;
2139 if ((blur_traits & BlendPixelTrait) == 0)
2140 {
2141 for (j=0; j < (ssize_t) width; j++)
2142 {
2143 r=GetCacheViewVirtualPixels(image_view,x+offset[j].x,y+
2144 offset[j].y,1,1,exception);
2145 if (r == (const Quantum *) NULL)
2146 {
2147 status=MagickFalse;
2148 continue;
2149 }
2150 pixel+=(*k)*r[i];
2151 k++;
2152 }
cristy0beccfa2011-09-25 20:47:53 +00002153 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002154 continue;
2155 }
2156 alpha=0.0;
2157 gamma=0.0;
2158 for (j=0; j < (ssize_t) width; j++)
2159 {
2160 r=GetCacheViewVirtualPixels(image_view,x+offset[j].x,y+offset[j].y,1,
2161 1,exception);
2162 if (r == (const Quantum *) NULL)
2163 {
2164 status=MagickFalse;
2165 continue;
2166 }
2167 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2168 pixel+=(*k)*alpha*r[i];
2169 gamma+=(*k)*alpha;
2170 k++;
cristy3ed852e2009-09-05 21:47:34 +00002171 }
cristyf7ef0252011-09-09 14:50:06 +00002172 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002173 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002174 }
2175 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002176 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002177 }
2178 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2179 status=MagickFalse;
2180 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2181 {
2182 MagickBooleanType
2183 proceed;
2184
cristyb557a152011-02-22 12:14:30 +00002185#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002186 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002187#endif
2188 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2189 if (proceed == MagickFalse)
2190 status=MagickFalse;
2191 }
2192 }
2193 blur_view=DestroyCacheView(blur_view);
2194 image_view=DestroyCacheView(image_view);
2195 kernel=(double *) RelinquishMagickMemory(kernel);
2196 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2197 if (status == MagickFalse)
2198 blur_image=DestroyImage(blur_image);
2199 return(blur_image);
2200}
2201
2202/*
2203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2204% %
2205% %
2206% %
2207% P r e v i e w I m a g e %
2208% %
2209% %
2210% %
2211%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2212%
2213% PreviewImage() tiles 9 thumbnails of the specified image with an image
2214% processing operation applied with varying parameters. This may be helpful
2215% pin-pointing an appropriate parameter for a particular image processing
2216% operation.
2217%
2218% The format of the PreviewImages method is:
2219%
2220% Image *PreviewImages(const Image *image,const PreviewType preview,
2221% ExceptionInfo *exception)
2222%
2223% A description of each parameter follows:
2224%
2225% o image: the image.
2226%
2227% o preview: the image processing operation.
2228%
2229% o exception: return any errors or warnings in this structure.
2230%
2231*/
2232MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2233 ExceptionInfo *exception)
2234{
2235#define NumberTiles 9
2236#define PreviewImageTag "Preview/Image"
2237#define DefaultPreviewGeometry "204x204+10+10"
2238
2239 char
2240 factor[MaxTextExtent],
2241 label[MaxTextExtent];
2242
2243 double
2244 degrees,
2245 gamma,
2246 percentage,
2247 radius,
2248 sigma,
2249 threshold;
2250
2251 Image
2252 *images,
2253 *montage_image,
2254 *preview_image,
2255 *thumbnail;
2256
2257 ImageInfo
2258 *preview_info;
2259
cristy3ed852e2009-09-05 21:47:34 +00002260 MagickBooleanType
2261 proceed;
2262
2263 MontageInfo
2264 *montage_info;
2265
2266 QuantizeInfo
2267 quantize_info;
2268
2269 RectangleInfo
2270 geometry;
2271
cristybb503372010-05-27 20:51:26 +00002272 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002273 i,
2274 x;
2275
cristybb503372010-05-27 20:51:26 +00002276 size_t
cristy3ed852e2009-09-05 21:47:34 +00002277 colors;
2278
cristy117ff172010-08-15 21:35:32 +00002279 ssize_t
2280 y;
2281
cristy3ed852e2009-09-05 21:47:34 +00002282 /*
2283 Open output image file.
2284 */
2285 assert(image != (Image *) NULL);
2286 assert(image->signature == MagickSignature);
2287 if (image->debug != MagickFalse)
2288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2289 colors=2;
2290 degrees=0.0;
2291 gamma=(-0.2f);
2292 preview_info=AcquireImageInfo();
2293 SetGeometry(image,&geometry);
2294 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2295 &geometry.width,&geometry.height);
2296 images=NewImageList();
2297 percentage=12.5;
2298 GetQuantizeInfo(&quantize_info);
2299 radius=0.0;
2300 sigma=1.0;
2301 threshold=0.0;
2302 x=0;
2303 y=0;
2304 for (i=0; i < NumberTiles; i++)
2305 {
2306 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2307 if (thumbnail == (Image *) NULL)
2308 break;
2309 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2310 (void *) NULL);
cristyd15e6592011-10-15 00:13:06 +00002311 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002312 if (i == (NumberTiles/2))
2313 {
cristy9950d572011-10-01 18:22:35 +00002314 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2315 &thumbnail->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002316 AppendImageToList(&images,thumbnail);
2317 continue;
2318 }
2319 switch (preview)
2320 {
2321 case RotatePreview:
2322 {
2323 degrees+=45.0;
2324 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002325 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002326 break;
2327 }
2328 case ShearPreview:
2329 {
2330 degrees+=5.0;
2331 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002332 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002333 degrees,2.0*degrees);
2334 break;
2335 }
2336 case RollPreview:
2337 {
cristybb503372010-05-27 20:51:26 +00002338 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2339 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002340 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002341 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002342 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002343 break;
2344 }
2345 case HuePreview:
2346 {
2347 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2348 if (preview_image == (Image *) NULL)
2349 break;
cristyb51dff52011-05-19 16:55:47 +00002350 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002351 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002352 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002353 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002354 break;
2355 }
2356 case SaturationPreview:
2357 {
2358 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2359 if (preview_image == (Image *) NULL)
2360 break;
cristyb51dff52011-05-19 16:55:47 +00002361 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002362 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002363 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002364 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002365 break;
2366 }
2367 case BrightnessPreview:
2368 {
2369 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2370 if (preview_image == (Image *) NULL)
2371 break;
cristyb51dff52011-05-19 16:55:47 +00002372 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002373 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002374 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002375 break;
2376 }
2377 case GammaPreview:
2378 default:
2379 {
2380 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2381 if (preview_image == (Image *) NULL)
2382 break;
2383 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002384 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002385 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002386 break;
2387 }
2388 case SpiffPreview:
2389 {
2390 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2391 if (preview_image != (Image *) NULL)
2392 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002393 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002394 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002395 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002396 break;
2397 }
2398 case DullPreview:
2399 {
2400 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2401 if (preview_image == (Image *) NULL)
2402 break;
2403 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002404 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002405 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002406 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002407 break;
2408 }
2409 case GrayscalePreview:
2410 {
2411 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2412 if (preview_image == (Image *) NULL)
2413 break;
2414 colors<<=1;
2415 quantize_info.number_colors=colors;
2416 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002417 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002418 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002419 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002420 break;
2421 }
2422 case QuantizePreview:
2423 {
2424 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2425 if (preview_image == (Image *) NULL)
2426 break;
2427 colors<<=1;
2428 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002429 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002430 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002431 colors);
cristy3ed852e2009-09-05 21:47:34 +00002432 break;
2433 }
2434 case DespecklePreview:
2435 {
2436 for (x=0; x < (i-1); x++)
2437 {
2438 preview_image=DespeckleImage(thumbnail,exception);
2439 if (preview_image == (Image *) NULL)
2440 break;
2441 thumbnail=DestroyImage(thumbnail);
2442 thumbnail=preview_image;
2443 }
2444 preview_image=DespeckleImage(thumbnail,exception);
2445 if (preview_image == (Image *) NULL)
2446 break;
cristyb51dff52011-05-19 16:55:47 +00002447 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002448 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002449 break;
2450 }
2451 case ReduceNoisePreview:
2452 {
cristy95c38342011-03-18 22:39:51 +00002453 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2454 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002455 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002456 break;
2457 }
2458 case AddNoisePreview:
2459 {
2460 switch ((int) i)
2461 {
2462 case 0:
2463 {
2464 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2465 break;
2466 }
2467 case 1:
2468 {
2469 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2470 break;
2471 }
2472 case 2:
2473 {
2474 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2475 break;
2476 }
2477 case 3:
2478 {
2479 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2480 break;
2481 }
2482 case 4:
2483 {
2484 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2485 break;
2486 }
2487 case 5:
2488 {
2489 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2490 break;
2491 }
2492 default:
2493 {
2494 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2495 break;
2496 }
2497 }
cristyd76c51e2011-03-26 00:21:26 +00002498 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2499 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002500 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002501 break;
2502 }
2503 case SharpenPreview:
2504 {
anthony736a1602011-10-06 12:38:17 +00002505 /* FUTURE: user bias on sharpen! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002506 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2507 exception);
cristyb51dff52011-05-19 16:55:47 +00002508 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002509 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002510 break;
2511 }
2512 case BlurPreview:
2513 {
anthony736a1602011-10-06 12:38:17 +00002514 /* FUTURE: user bias on blur! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002515 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002516 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002517 sigma);
2518 break;
2519 }
2520 case ThresholdPreview:
2521 {
2522 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2523 if (preview_image == (Image *) NULL)
2524 break;
cristye941a752011-10-15 01:52:48 +00002525 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2526 QuantumRange+1.0))/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002527 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002528 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2529 break;
2530 }
2531 case EdgeDetectPreview:
2532 {
cristy8ae632d2011-09-05 17:29:53 +00002533 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002534 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002535 break;
2536 }
2537 case SpreadPreview:
2538 {
cristy5c4e2582011-09-11 19:21:03 +00002539 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2540 exception);
cristyb51dff52011-05-19 16:55:47 +00002541 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002542 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002543 break;
2544 }
2545 case SolarizePreview:
2546 {
2547 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2548 if (preview_image == (Image *) NULL)
2549 break;
2550 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002551 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002552 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002553 (QuantumRange*percentage)/100.0);
2554 break;
2555 }
2556 case ShadePreview:
2557 {
2558 degrees+=10.0;
2559 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2560 exception);
cristyb51dff52011-05-19 16:55:47 +00002561 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002562 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002563 break;
2564 }
2565 case RaisePreview:
2566 {
2567 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2568 if (preview_image == (Image *) NULL)
2569 break;
cristybb503372010-05-27 20:51:26 +00002570 geometry.width=(size_t) (2*i+2);
2571 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002572 geometry.x=i/2;
2573 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002574 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002575 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002576 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002577 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002578 break;
2579 }
2580 case SegmentPreview:
2581 {
2582 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2583 if (preview_image == (Image *) NULL)
2584 break;
2585 threshold+=0.4f;
2586 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002587 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002588 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002589 threshold,threshold);
2590 break;
2591 }
2592 case SwirlPreview:
2593 {
cristy76f512e2011-09-12 01:26:56 +00002594 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2595 exception);
cristyb51dff52011-05-19 16:55:47 +00002596 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002597 degrees+=45.0;
2598 break;
2599 }
2600 case ImplodePreview:
2601 {
2602 degrees+=0.1f;
cristy76f512e2011-09-12 01:26:56 +00002603 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2604 exception);
cristyb51dff52011-05-19 16:55:47 +00002605 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002606 break;
2607 }
2608 case WavePreview:
2609 {
2610 degrees+=5.0f;
cristy5c4e2582011-09-11 19:21:03 +00002611 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2612 image->interpolate,exception);
cristyb51dff52011-05-19 16:55:47 +00002613 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002614 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002615 break;
2616 }
2617 case OilPaintPreview:
2618 {
cristy14973ba2011-08-27 23:48:07 +00002619 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2620 exception);
2621 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2622 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002623 break;
2624 }
2625 case CharcoalDrawingPreview:
2626 {
anthony736a1602011-10-06 12:38:17 +00002627 /* FUTURE: user bias on charcoal! This is non-sensical! */
cristy3ed852e2009-09-05 21:47:34 +00002628 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristy05c0c9a2011-09-05 23:16:13 +00002629 image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002630 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002631 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002632 break;
2633 }
2634 case JPEGPreview:
2635 {
2636 char
2637 filename[MaxTextExtent];
2638
2639 int
2640 file;
2641
2642 MagickBooleanType
2643 status;
2644
2645 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2646 if (preview_image == (Image *) NULL)
2647 break;
cristybb503372010-05-27 20:51:26 +00002648 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002649 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002650 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002651 file=AcquireUniqueFileResource(filename);
2652 if (file != -1)
2653 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002654 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002655 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002656 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002657 if (status != MagickFalse)
2658 {
2659 Image
2660 *quality_image;
2661
2662 (void) CopyMagickString(preview_info->filename,
2663 preview_image->filename,MaxTextExtent);
2664 quality_image=ReadImage(preview_info,exception);
2665 if (quality_image != (Image *) NULL)
2666 {
2667 preview_image=DestroyImage(preview_image);
2668 preview_image=quality_image;
2669 }
2670 }
2671 (void) RelinquishUniqueFileResource(preview_image->filename);
2672 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002673 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002674 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2675 1024.0/1024.0);
2676 else
2677 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002678 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002679 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002680 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002681 else
cristyb51dff52011-05-19 16:55:47 +00002682 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002683 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002684 break;
2685 }
2686 }
2687 thumbnail=DestroyImage(thumbnail);
2688 percentage+=12.5;
2689 radius+=0.5;
2690 sigma+=0.25;
2691 if (preview_image == (Image *) NULL)
2692 break;
2693 (void) DeleteImageProperty(preview_image,"label");
cristyd15e6592011-10-15 00:13:06 +00002694 (void) SetImageProperty(preview_image,"label",label,exception);
cristy3ed852e2009-09-05 21:47:34 +00002695 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002696 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2697 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002698 if (proceed == MagickFalse)
2699 break;
2700 }
2701 if (images == (Image *) NULL)
2702 {
2703 preview_info=DestroyImageInfo(preview_info);
2704 return((Image *) NULL);
2705 }
2706 /*
2707 Create the montage.
2708 */
2709 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2710 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2711 montage_info->shadow=MagickTrue;
2712 (void) CloneString(&montage_info->tile,"3x3");
2713 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2714 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2715 montage_image=MontageImages(images,montage_info,exception);
2716 montage_info=DestroyMontageInfo(montage_info);
2717 images=DestroyImageList(images);
2718 if (montage_image == (Image *) NULL)
2719 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2720 if (montage_image->montage != (char *) NULL)
2721 {
2722 /*
2723 Free image directory.
2724 */
2725 montage_image->montage=(char *) RelinquishMagickMemory(
2726 montage_image->montage);
2727 if (image->directory != (char *) NULL)
2728 montage_image->directory=(char *) RelinquishMagickMemory(
2729 montage_image->directory);
2730 }
2731 preview_info=DestroyImageInfo(preview_info);
2732 return(montage_image);
2733}
2734
2735/*
2736%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2737% %
2738% %
2739% %
2740% R a d i a l B l u r I m a g e %
2741% %
2742% %
2743% %
2744%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2745%
2746% RadialBlurImage() applies a radial blur to the image.
2747%
2748% Andrew Protano contributed this effect.
2749%
2750% The format of the RadialBlurImage method is:
2751%
2752% Image *RadialBlurImage(const Image *image,const double angle,
cristy6435bd92011-09-10 02:10:07 +00002753% const double blur,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002754%
2755% A description of each parameter follows:
2756%
2757% o image: the image.
2758%
cristy3ed852e2009-09-05 21:47:34 +00002759% o angle: the angle of the radial blur.
2760%
cristy6435bd92011-09-10 02:10:07 +00002761% o blur: the blur.
2762%
cristy3ed852e2009-09-05 21:47:34 +00002763% o exception: return any errors or warnings in this structure.
2764%
2765*/
cristy4282c702011-11-21 00:01:06 +00002766MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2767 const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002768{
cristyc4c8d132010-01-07 01:58:38 +00002769 CacheView
2770 *blur_view,
2771 *image_view;
2772
cristy3ed852e2009-09-05 21:47:34 +00002773 Image
2774 *blur_image;
2775
cristy3ed852e2009-09-05 21:47:34 +00002776 MagickBooleanType
2777 status;
2778
cristybb503372010-05-27 20:51:26 +00002779 MagickOffsetType
2780 progress;
2781
cristy3ed852e2009-09-05 21:47:34 +00002782 MagickRealType
2783 blur_radius,
2784 *cos_theta,
2785 offset,
2786 *sin_theta,
2787 theta;
2788
2789 PointInfo
2790 blur_center;
2791
cristybb503372010-05-27 20:51:26 +00002792 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002793 i;
2794
cristybb503372010-05-27 20:51:26 +00002795 size_t
cristy3ed852e2009-09-05 21:47:34 +00002796 n;
2797
cristybb503372010-05-27 20:51:26 +00002798 ssize_t
2799 y;
2800
cristy3ed852e2009-09-05 21:47:34 +00002801 /*
2802 Allocate blur image.
2803 */
2804 assert(image != (Image *) NULL);
2805 assert(image->signature == MagickSignature);
2806 if (image->debug != MagickFalse)
2807 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2808 assert(exception != (ExceptionInfo *) NULL);
2809 assert(exception->signature == MagickSignature);
cristy1e7aa312011-09-10 20:01:36 +00002810 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002811 if (blur_image == (Image *) NULL)
2812 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002813 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002814 {
cristy3ed852e2009-09-05 21:47:34 +00002815 blur_image=DestroyImage(blur_image);
2816 return((Image *) NULL);
2817 }
2818 blur_center.x=(double) image->columns/2.0;
2819 blur_center.y=(double) image->rows/2.0;
2820 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002821 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002822 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2823 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2824 sizeof(*cos_theta));
2825 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2826 sizeof(*sin_theta));
2827 if ((cos_theta == (MagickRealType *) NULL) ||
2828 (sin_theta == (MagickRealType *) NULL))
2829 {
2830 blur_image=DestroyImage(blur_image);
2831 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2832 }
2833 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002834 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002835 {
2836 cos_theta[i]=cos((double) (theta*i-offset));
2837 sin_theta[i]=sin((double) (theta*i-offset));
2838 }
2839 /*
2840 Radial blur image.
2841 */
2842 status=MagickTrue;
2843 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002844 image_view=AcquireCacheView(image);
2845 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002846#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002847 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002848#endif
cristy1e7aa312011-09-10 20:01:36 +00002849 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002850 {
cristy1e7aa312011-09-10 20:01:36 +00002851 register const Quantum
2852 *restrict p;
2853
cristy4c08aed2011-07-01 19:47:50 +00002854 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002855 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002856
cristy117ff172010-08-15 21:35:32 +00002857 register ssize_t
2858 x;
2859
cristy3ed852e2009-09-05 21:47:34 +00002860 if (status == MagickFalse)
2861 continue;
cristy1e7aa312011-09-10 20:01:36 +00002862 p=GetCacheViewVirtualPixels(blur_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00002863 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2864 exception);
cristy1e7aa312011-09-10 20:01:36 +00002865 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002866 {
2867 status=MagickFalse;
2868 continue;
2869 }
cristy1e7aa312011-09-10 20:01:36 +00002870 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002871 {
cristy3ed852e2009-09-05 21:47:34 +00002872 MagickRealType
cristy3ed852e2009-09-05 21:47:34 +00002873 radius;
2874
cristy3ed852e2009-09-05 21:47:34 +00002875 PointInfo
2876 center;
2877
cristybb503372010-05-27 20:51:26 +00002878 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002879 i;
2880
cristybb503372010-05-27 20:51:26 +00002881 size_t
cristy3ed852e2009-09-05 21:47:34 +00002882 step;
2883
2884 center.x=(double) x-blur_center.x;
2885 center.y=(double) y-blur_center.y;
2886 radius=hypot((double) center.x,center.y);
2887 if (radius == 0)
2888 step=1;
2889 else
2890 {
cristybb503372010-05-27 20:51:26 +00002891 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002892 if (step == 0)
2893 step=1;
2894 else
2895 if (step >= n)
2896 step=n-1;
2897 }
cristy1e7aa312011-09-10 20:01:36 +00002898 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2899 {
2900 MagickRealType
2901 gamma,
2902 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002903
cristy1e7aa312011-09-10 20:01:36 +00002904 PixelChannel
2905 channel;
2906
2907 PixelTrait
2908 blur_traits,
2909 traits;
2910
2911 register const Quantum
2912 *restrict r;
2913
2914 register ssize_t
2915 j;
2916
cristye2a912b2011-12-05 20:02:07 +00002917 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002918 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00002919 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2920 if ((traits == UndefinedPixelTrait) ||
2921 (blur_traits == UndefinedPixelTrait))
2922 continue;
2923 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002924 {
cristy0beccfa2011-09-25 20:47:53 +00002925 SetPixelChannel(blur_image,channel,p[i],q);
cristy1e7aa312011-09-10 20:01:36 +00002926 continue;
cristy3ed852e2009-09-05 21:47:34 +00002927 }
cristy1e7aa312011-09-10 20:01:36 +00002928 gamma=0.0;
2929 pixel=bias;
2930 if ((blur_traits & BlendPixelTrait) == 0)
2931 {
2932 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2933 {
2934 r=GetCacheViewVirtualPixels(image_view, (ssize_t) (blur_center.x+
2935 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2936 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2937 1,1,exception);
2938 if (r == (const Quantum *) NULL)
2939 {
2940 status=MagickFalse;
2941 continue;
2942 }
2943 pixel+=r[i];
2944 gamma++;
2945 }
2946 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002947 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00002948 continue;
2949 }
2950 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2951 {
2952 r=GetCacheViewVirtualPixels(image_view, (ssize_t) (blur_center.x+
2953 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2954 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2955 1,1,exception);
2956 if (r == (const Quantum *) NULL)
2957 {
2958 status=MagickFalse;
2959 continue;
2960 }
2961 pixel+=GetPixelAlpha(image,r)*r[i];
2962 gamma+=GetPixelAlpha(image,r);
cristy3ed852e2009-09-05 21:47:34 +00002963 }
cristy1e7aa312011-09-10 20:01:36 +00002964 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002965 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00002966 }
2967 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002968 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002969 }
2970 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2971 status=MagickFalse;
2972 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2973 {
2974 MagickBooleanType
2975 proceed;
2976
cristyb5d5f722009-11-04 03:03:49 +00002977#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002978 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002979#endif
2980 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2981 if (proceed == MagickFalse)
2982 status=MagickFalse;
2983 }
2984 }
2985 blur_view=DestroyCacheView(blur_view);
2986 image_view=DestroyCacheView(image_view);
2987 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
2988 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
2989 if (status == MagickFalse)
2990 blur_image=DestroyImage(blur_image);
2991 return(blur_image);
2992}
2993
2994/*
2995%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2996% %
2997% %
2998% %
cristy3ed852e2009-09-05 21:47:34 +00002999% S e l e c t i v e B l u r I m a g e %
3000% %
3001% %
3002% %
3003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3004%
3005% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3006% It is similar to the unsharpen mask that sharpens everything with contrast
3007% above a certain threshold.
3008%
3009% The format of the SelectiveBlurImage method is:
3010%
3011% Image *SelectiveBlurImage(const Image *image,const double radius,
cristy1e7aa312011-09-10 20:01:36 +00003012% const double sigma,const double threshold,const double bias,
3013% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003014%
3015% A description of each parameter follows:
3016%
3017% o image: the image.
3018%
cristy3ed852e2009-09-05 21:47:34 +00003019% o radius: the radius of the Gaussian, in pixels, not counting the center
3020% pixel.
3021%
3022% o sigma: the standard deviation of the Gaussian, in pixels.
3023%
3024% o threshold: only pixels within this contrast threshold are included
3025% in the blur operation.
3026%
cristy1e7aa312011-09-10 20:01:36 +00003027% o bias: the bias.
3028%
cristy3ed852e2009-09-05 21:47:34 +00003029% o exception: return any errors or warnings in this structure.
3030%
3031*/
cristy4282c702011-11-21 00:01:06 +00003032MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3033 const double sigma,const double threshold,const double bias,
3034 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003035{
3036#define SelectiveBlurImageTag "SelectiveBlur/Image"
3037
cristy47e00502009-12-17 19:19:57 +00003038 CacheView
3039 *blur_view,
3040 *image_view;
3041
cristy3ed852e2009-09-05 21:47:34 +00003042 double
cristy3ed852e2009-09-05 21:47:34 +00003043 *kernel;
3044
3045 Image
3046 *blur_image;
3047
cristy3ed852e2009-09-05 21:47:34 +00003048 MagickBooleanType
3049 status;
3050
cristybb503372010-05-27 20:51:26 +00003051 MagickOffsetType
3052 progress;
3053
cristybb503372010-05-27 20:51:26 +00003054 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003055 i;
cristy3ed852e2009-09-05 21:47:34 +00003056
cristybb503372010-05-27 20:51:26 +00003057 size_t
cristy3ed852e2009-09-05 21:47:34 +00003058 width;
3059
cristybb503372010-05-27 20:51:26 +00003060 ssize_t
cristyc8523c12011-09-13 00:02:53 +00003061 center,
cristybb503372010-05-27 20:51:26 +00003062 j,
3063 u,
3064 v,
3065 y;
3066
cristy3ed852e2009-09-05 21:47:34 +00003067 /*
3068 Initialize blur image attributes.
3069 */
3070 assert(image != (Image *) NULL);
3071 assert(image->signature == MagickSignature);
3072 if (image->debug != MagickFalse)
3073 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3074 assert(exception != (ExceptionInfo *) NULL);
3075 assert(exception->signature == MagickSignature);
3076 width=GetOptimalKernelWidth1D(radius,sigma);
3077 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3078 if (kernel == (double *) NULL)
3079 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003080 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003081 i=0;
cristy47e00502009-12-17 19:19:57 +00003082 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003083 {
cristy47e00502009-12-17 19:19:57 +00003084 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003085 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3086 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003087 }
3088 if (image->debug != MagickFalse)
3089 {
3090 char
3091 format[MaxTextExtent],
3092 *message;
3093
cristy117ff172010-08-15 21:35:32 +00003094 register const double
3095 *k;
3096
cristybb503372010-05-27 20:51:26 +00003097 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003098 u,
3099 v;
3100
cristy3ed852e2009-09-05 21:47:34 +00003101 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003102 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3103 width);
cristy3ed852e2009-09-05 21:47:34 +00003104 message=AcquireString("");
3105 k=kernel;
cristybb503372010-05-27 20:51:26 +00003106 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003107 {
3108 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003109 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003110 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003111 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003112 {
cristyb51dff52011-05-19 16:55:47 +00003113 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003114 (void) ConcatenateString(&message,format);
3115 }
3116 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3117 }
3118 message=DestroyString(message);
3119 }
cristy1e7aa312011-09-10 20:01:36 +00003120 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003121 if (blur_image == (Image *) NULL)
3122 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003123 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003124 {
cristy3ed852e2009-09-05 21:47:34 +00003125 blur_image=DestroyImage(blur_image);
3126 return((Image *) NULL);
3127 }
3128 /*
3129 Threshold blur image.
3130 */
3131 status=MagickTrue;
3132 progress=0;
cristyc8523c12011-09-13 00:02:53 +00003133 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3134 GetPixelChannels(image)*(width/2L));
cristy3ed852e2009-09-05 21:47:34 +00003135 image_view=AcquireCacheView(image);
3136 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003137#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003138 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003139#endif
cristybb503372010-05-27 20:51:26 +00003140 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003141 {
cristy4c08aed2011-07-01 19:47:50 +00003142 double
3143 contrast;
3144
cristy3ed852e2009-09-05 21:47:34 +00003145 MagickBooleanType
3146 sync;
3147
cristy4c08aed2011-07-01 19:47:50 +00003148 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003149 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003150
cristy4c08aed2011-07-01 19:47:50 +00003151 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003152 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003153
cristy117ff172010-08-15 21:35:32 +00003154 register ssize_t
3155 x;
3156
cristy3ed852e2009-09-05 21:47:34 +00003157 if (status == MagickFalse)
3158 continue;
cristy117ff172010-08-15 21:35:32 +00003159 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3160 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00003161 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3162 exception);
cristy4c08aed2011-07-01 19:47:50 +00003163 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003164 {
3165 status=MagickFalse;
3166 continue;
3167 }
cristybb503372010-05-27 20:51:26 +00003168 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003169 {
cristybb503372010-05-27 20:51:26 +00003170 register ssize_t
cristy1e7aa312011-09-10 20:01:36 +00003171 i;
cristy3ed852e2009-09-05 21:47:34 +00003172
cristy1e7aa312011-09-10 20:01:36 +00003173 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3174 {
3175 MagickRealType
3176 alpha,
3177 gamma,
3178 intensity,
3179 pixel;
cristy117ff172010-08-15 21:35:32 +00003180
cristy1e7aa312011-09-10 20:01:36 +00003181 PixelChannel
3182 channel;
3183
3184 PixelTrait
3185 blur_traits,
3186 traits;
3187
3188 register const double
3189 *restrict k;
3190
3191 register const Quantum
3192 *restrict pixels;
3193
3194 register ssize_t
3195 u;
3196
3197 ssize_t
3198 v;
3199
cristye2a912b2011-12-05 20:02:07 +00003200 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003201 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003202 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3203 if ((traits == UndefinedPixelTrait) ||
3204 (blur_traits == UndefinedPixelTrait))
3205 continue;
3206 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003207 {
cristy0beccfa2011-09-25 20:47:53 +00003208 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003209 continue;
cristy3ed852e2009-09-05 21:47:34 +00003210 }
cristy1e7aa312011-09-10 20:01:36 +00003211 k=kernel;
3212 pixel=bias;
3213 pixels=p;
cristyc8523c12011-09-13 00:02:53 +00003214 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
cristy1e7aa312011-09-10 20:01:36 +00003215 gamma=0.0;
3216 if ((blur_traits & BlendPixelTrait) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003217 {
cristy1e7aa312011-09-10 20:01:36 +00003218 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003219 {
cristy1e7aa312011-09-10 20:01:36 +00003220 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003221 {
cristy1e7aa312011-09-10 20:01:36 +00003222 contrast=GetPixelIntensity(image,pixels)-intensity;
3223 if (fabs(contrast) < threshold)
3224 {
3225 pixel+=(*k)*pixels[i];
3226 gamma+=(*k);
3227 }
3228 k++;
3229 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003230 }
cristy1e7aa312011-09-10 20:01:36 +00003231 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003232 }
cristy1e7aa312011-09-10 20:01:36 +00003233 if (fabs((double) gamma) < MagickEpsilon)
3234 {
cristy0beccfa2011-09-25 20:47:53 +00003235 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003236 continue;
3237 }
3238 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003239 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003240 continue;
3241 }
3242 for (v=0; v < (ssize_t) width; v++)
3243 {
3244 for (u=0; u < (ssize_t) width; u++)
3245 {
3246 contrast=GetPixelIntensity(image,pixels)-intensity;
3247 if (fabs(contrast) < threshold)
3248 {
3249 alpha=(MagickRealType) (QuantumScale*
3250 GetPixelAlpha(image,pixels));
3251 pixel+=(*k)*alpha*pixels[i];
3252 gamma+=(*k)*alpha;
3253 }
3254 k++;
3255 pixels+=GetPixelChannels(image);
3256 }
3257 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003258 }
cristy1e7aa312011-09-10 20:01:36 +00003259 if (fabs((double) gamma) < MagickEpsilon)
3260 {
cristy0beccfa2011-09-25 20:47:53 +00003261 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003262 continue;
3263 }
3264 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003265 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003266 }
cristyed231572011-07-14 02:18:59 +00003267 p+=GetPixelChannels(image);
3268 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003269 }
3270 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3271 if (sync == MagickFalse)
3272 status=MagickFalse;
3273 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3274 {
3275 MagickBooleanType
3276 proceed;
3277
cristyb5d5f722009-11-04 03:03:49 +00003278#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003279 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003280#endif
3281 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3282 image->rows);
3283 if (proceed == MagickFalse)
3284 status=MagickFalse;
3285 }
3286 }
3287 blur_image->type=image->type;
3288 blur_view=DestroyCacheView(blur_view);
3289 image_view=DestroyCacheView(image_view);
3290 kernel=(double *) RelinquishMagickMemory(kernel);
3291 if (status == MagickFalse)
3292 blur_image=DestroyImage(blur_image);
3293 return(blur_image);
3294}
3295
3296/*
3297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3298% %
3299% %
3300% %
3301% S h a d e I m a g e %
3302% %
3303% %
3304% %
3305%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3306%
3307% ShadeImage() shines a distant light on an image to create a
3308% three-dimensional effect. You control the positioning of the light with
3309% azimuth and elevation; azimuth is measured in degrees off the x axis
3310% and elevation is measured in pixels above the Z axis.
3311%
3312% The format of the ShadeImage method is:
3313%
3314% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3315% const double azimuth,const double elevation,ExceptionInfo *exception)
3316%
3317% A description of each parameter follows:
3318%
3319% o image: the image.
3320%
3321% o gray: A value other than zero shades the intensity of each pixel.
3322%
3323% o azimuth, elevation: Define the light source direction.
3324%
3325% o exception: return any errors or warnings in this structure.
3326%
3327*/
3328MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3329 const double azimuth,const double elevation,ExceptionInfo *exception)
3330{
3331#define ShadeImageTag "Shade/Image"
3332
cristyc4c8d132010-01-07 01:58:38 +00003333 CacheView
3334 *image_view,
3335 *shade_view;
3336
cristy3ed852e2009-09-05 21:47:34 +00003337 Image
3338 *shade_image;
3339
cristy3ed852e2009-09-05 21:47:34 +00003340 MagickBooleanType
3341 status;
3342
cristybb503372010-05-27 20:51:26 +00003343 MagickOffsetType
3344 progress;
3345
cristy3ed852e2009-09-05 21:47:34 +00003346 PrimaryInfo
3347 light;
3348
cristybb503372010-05-27 20:51:26 +00003349 ssize_t
3350 y;
3351
cristy3ed852e2009-09-05 21:47:34 +00003352 /*
3353 Initialize shaded image attributes.
3354 */
3355 assert(image != (const Image *) NULL);
3356 assert(image->signature == MagickSignature);
3357 if (image->debug != MagickFalse)
3358 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3359 assert(exception != (ExceptionInfo *) NULL);
3360 assert(exception->signature == MagickSignature);
3361 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3362 if (shade_image == (Image *) NULL)
3363 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003364 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003365 {
cristy3ed852e2009-09-05 21:47:34 +00003366 shade_image=DestroyImage(shade_image);
3367 return((Image *) NULL);
3368 }
3369 /*
3370 Compute the light vector.
3371 */
3372 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3373 cos(DegreesToRadians(elevation));
3374 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3375 cos(DegreesToRadians(elevation));
3376 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3377 /*
3378 Shade image.
3379 */
3380 status=MagickTrue;
3381 progress=0;
3382 image_view=AcquireCacheView(image);
3383 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003384#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003385 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003386#endif
cristybb503372010-05-27 20:51:26 +00003387 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003388 {
3389 MagickRealType
3390 distance,
3391 normal_distance,
3392 shade;
3393
3394 PrimaryInfo
3395 normal;
3396
cristy4c08aed2011-07-01 19:47:50 +00003397 register const Quantum
cristy1e7aa312011-09-10 20:01:36 +00003398 *restrict center,
cristyc47d1f82009-11-26 01:44:43 +00003399 *restrict p,
cristy1e7aa312011-09-10 20:01:36 +00003400 *restrict post,
3401 *restrict pre;
cristy3ed852e2009-09-05 21:47:34 +00003402
cristy4c08aed2011-07-01 19:47:50 +00003403 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003404 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003405
cristy117ff172010-08-15 21:35:32 +00003406 register ssize_t
3407 x;
3408
cristy3ed852e2009-09-05 21:47:34 +00003409 if (status == MagickFalse)
3410 continue;
3411 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3412 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3413 exception);
cristy4c08aed2011-07-01 19:47:50 +00003414 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003415 {
3416 status=MagickFalse;
3417 continue;
3418 }
3419 /*
3420 Shade this row of pixels.
3421 */
3422 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristy1e7aa312011-09-10 20:01:36 +00003423 pre=p+GetPixelChannels(image);
3424 center=pre+(image->columns+2)*GetPixelChannels(image);
3425 post=center+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003426 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003427 {
cristy1e7aa312011-09-10 20:01:36 +00003428 register ssize_t
3429 i;
3430
cristy3ed852e2009-09-05 21:47:34 +00003431 /*
3432 Determine the surface normal and compute shading.
3433 */
cristy1e7aa312011-09-10 20:01:36 +00003434 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3435 GetPixelIntensity(image,center-GetPixelChannels(image))+
3436 GetPixelIntensity(image,post-GetPixelChannels(image))-
3437 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3438 GetPixelIntensity(image,center+GetPixelChannels(image))-
3439 GetPixelIntensity(image,post+GetPixelChannels(image)));
3440 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3441 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3442 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3443 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3444 GetPixelIntensity(image,pre+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003445 if ((normal.x == 0.0) && (normal.y == 0.0))
3446 shade=light.z;
3447 else
3448 {
3449 shade=0.0;
3450 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3451 if (distance > MagickEpsilon)
3452 {
3453 normal_distance=
3454 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3455 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3456 shade=distance/sqrt((double) normal_distance);
3457 }
3458 }
cristy1e7aa312011-09-10 20:01:36 +00003459 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3460 {
3461 PixelChannel
3462 channel;
3463
3464 PixelTrait
3465 shade_traits,
3466 traits;
3467
cristye2a912b2011-12-05 20:02:07 +00003468 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003469 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003470 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3471 if ((traits == UndefinedPixelTrait) ||
3472 (shade_traits == UndefinedPixelTrait))
3473 continue;
3474 if ((shade_traits & CopyPixelTrait) != 0)
3475 {
cristy0beccfa2011-09-25 20:47:53 +00003476 SetPixelChannel(shade_image,channel,center[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003477 continue;
3478 }
3479 if (gray != MagickFalse)
3480 {
cristy0beccfa2011-09-25 20:47:53 +00003481 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
cristy1e7aa312011-09-10 20:01:36 +00003482 continue;
3483 }
cristy0beccfa2011-09-25 20:47:53 +00003484 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3485 center[i]),q);
cristy1e7aa312011-09-10 20:01:36 +00003486 }
3487 pre+=GetPixelChannels(image);
3488 center+=GetPixelChannels(image);
3489 post+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003490 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003491 }
3492 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3493 status=MagickFalse;
3494 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3495 {
3496 MagickBooleanType
3497 proceed;
3498
cristyb5d5f722009-11-04 03:03:49 +00003499#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003500 #pragma omp critical (MagickCore_ShadeImage)
3501#endif
3502 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3503 if (proceed == MagickFalse)
3504 status=MagickFalse;
3505 }
3506 }
3507 shade_view=DestroyCacheView(shade_view);
3508 image_view=DestroyCacheView(image_view);
3509 if (status == MagickFalse)
3510 shade_image=DestroyImage(shade_image);
3511 return(shade_image);
3512}
3513
3514/*
3515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3516% %
3517% %
3518% %
3519% S h a r p e n I m a g e %
3520% %
3521% %
3522% %
3523%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3524%
3525% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3526% operator of the given radius and standard deviation (sigma). For
3527% reasonable results, radius should be larger than sigma. Use a radius of 0
3528% and SharpenImage() selects a suitable radius for you.
3529%
3530% Using a separable kernel would be faster, but the negative weights cancel
3531% out on the corners of the kernel producing often undesirable ringing in the
3532% filtered result; this can be avoided by using a 2D gaussian shaped image
3533% sharpening kernel instead.
3534%
3535% The format of the SharpenImage method is:
3536%
3537% Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003538% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003539%
3540% A description of each parameter follows:
3541%
3542% o image: the image.
3543%
cristy3ed852e2009-09-05 21:47:34 +00003544% o radius: the radius of the Gaussian, in pixels, not counting the center
3545% pixel.
3546%
3547% o sigma: the standard deviation of the Laplacian, in pixels.
3548%
cristy05c0c9a2011-09-05 23:16:13 +00003549% o bias: bias.
3550%
cristy3ed852e2009-09-05 21:47:34 +00003551% o exception: return any errors or warnings in this structure.
3552%
3553*/
cristy3ed852e2009-09-05 21:47:34 +00003554MagickExport Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003555 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003556{
cristy3ed852e2009-09-05 21:47:34 +00003557 double
cristy47e00502009-12-17 19:19:57 +00003558 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003559
3560 Image
3561 *sharp_image;
3562
cristy41cbe682011-07-15 19:12:37 +00003563 KernelInfo
3564 *kernel_info;
3565
cristybb503372010-05-27 20:51:26 +00003566 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003567 i;
3568
cristybb503372010-05-27 20:51:26 +00003569 size_t
cristy3ed852e2009-09-05 21:47:34 +00003570 width;
3571
cristy117ff172010-08-15 21:35:32 +00003572 ssize_t
3573 j,
3574 u,
3575 v;
3576
cristy3ed852e2009-09-05 21:47:34 +00003577 assert(image != (const Image *) NULL);
3578 assert(image->signature == MagickSignature);
3579 if (image->debug != MagickFalse)
3580 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3581 assert(exception != (ExceptionInfo *) NULL);
3582 assert(exception->signature == MagickSignature);
3583 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003584 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003585 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003586 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003587 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3588 kernel_info->width=width;
3589 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00003590 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
cristy41cbe682011-07-15 19:12:37 +00003591 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00003592 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3593 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3594 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00003595 {
3596 kernel_info=DestroyKernelInfo(kernel_info);
3597 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3598 }
cristy3ed852e2009-09-05 21:47:34 +00003599 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003600 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003601 i=0;
3602 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003603 {
cristy47e00502009-12-17 19:19:57 +00003604 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003605 {
cristy41cbe682011-07-15 19:12:37 +00003606 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3607 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3608 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003609 i++;
3610 }
3611 }
cristy41cbe682011-07-15 19:12:37 +00003612 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003613 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003614 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003615 return(sharp_image);
3616}
3617
3618/*
3619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3620% %
3621% %
3622% %
3623% S p r e a d I m a g e %
3624% %
3625% %
3626% %
3627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3628%
3629% SpreadImage() is a special effects method that randomly displaces each
3630% pixel in a block defined by the radius parameter.
3631%
3632% The format of the SpreadImage method is:
3633%
3634% Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003635% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003636%
3637% A description of each parameter follows:
3638%
3639% o image: the image.
3640%
cristy5c4e2582011-09-11 19:21:03 +00003641% o radius: choose a random pixel in a neighborhood of this extent.
3642%
3643% o method: the pixel interpolation method.
cristy3ed852e2009-09-05 21:47:34 +00003644%
3645% o exception: return any errors or warnings in this structure.
3646%
3647*/
3648MagickExport Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003649 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003650{
3651#define SpreadImageTag "Spread/Image"
3652
cristyfa112112010-01-04 17:48:07 +00003653 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003654 *image_view,
3655 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003656
cristy3ed852e2009-09-05 21:47:34 +00003657 Image
3658 *spread_image;
3659
cristy3ed852e2009-09-05 21:47:34 +00003660 MagickBooleanType
3661 status;
3662
cristybb503372010-05-27 20:51:26 +00003663 MagickOffsetType
3664 progress;
3665
cristy3ed852e2009-09-05 21:47:34 +00003666 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003667 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003668
cristybb503372010-05-27 20:51:26 +00003669 size_t
cristy3ed852e2009-09-05 21:47:34 +00003670 width;
3671
cristybb503372010-05-27 20:51:26 +00003672 ssize_t
3673 y;
3674
cristy3ed852e2009-09-05 21:47:34 +00003675 /*
3676 Initialize spread image attributes.
3677 */
3678 assert(image != (Image *) NULL);
3679 assert(image->signature == MagickSignature);
3680 if (image->debug != MagickFalse)
3681 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3682 assert(exception != (ExceptionInfo *) NULL);
3683 assert(exception->signature == MagickSignature);
3684 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3685 exception);
3686 if (spread_image == (Image *) NULL)
3687 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003688 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003689 {
cristy3ed852e2009-09-05 21:47:34 +00003690 spread_image=DestroyImage(spread_image);
3691 return((Image *) NULL);
3692 }
3693 /*
3694 Spread image.
3695 */
3696 status=MagickTrue;
3697 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003698 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003699 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003700 image_view=AcquireCacheView(image);
3701 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003702#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00003703 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003704#endif
cristybe82ad52011-09-06 01:17:20 +00003705 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003706 {
cristy5c9e6f22010-09-17 17:31:01 +00003707 const int
3708 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003709
cristybe82ad52011-09-06 01:17:20 +00003710 register const Quantum
3711 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003712
cristy4c08aed2011-07-01 19:47:50 +00003713 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003714 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003715
cristy117ff172010-08-15 21:35:32 +00003716 register ssize_t
3717 x;
3718
cristy3ed852e2009-09-05 21:47:34 +00003719 if (status == MagickFalse)
3720 continue;
cristybe82ad52011-09-06 01:17:20 +00003721 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy9f7e7cb2011-03-26 00:49:57 +00003722 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003723 exception);
cristybe82ad52011-09-06 01:17:20 +00003724 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003725 {
3726 status=MagickFalse;
3727 continue;
3728 }
cristybe82ad52011-09-06 01:17:20 +00003729 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003730 {
cristybe82ad52011-09-06 01:17:20 +00003731 PointInfo
3732 point;
3733
cristybe82ad52011-09-06 01:17:20 +00003734 point.x=GetPseudoRandomValue(random_info[id]);
3735 point.y=GetPseudoRandomValue(random_info[id]);
cristy5c4e2582011-09-11 19:21:03 +00003736 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3737 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
cristyed231572011-07-14 02:18:59 +00003738 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003739 }
cristy9f7e7cb2011-03-26 00:49:57 +00003740 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003741 status=MagickFalse;
3742 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3743 {
3744 MagickBooleanType
3745 proceed;
3746
cristyb557a152011-02-22 12:14:30 +00003747#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003748 #pragma omp critical (MagickCore_SpreadImage)
3749#endif
3750 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3751 if (proceed == MagickFalse)
3752 status=MagickFalse;
3753 }
3754 }
cristy9f7e7cb2011-03-26 00:49:57 +00003755 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003756 image_view=DestroyCacheView(image_view);
3757 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003758 return(spread_image);
3759}
3760
3761/*
3762%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3763% %
3764% %
3765% %
3766% U n s h a r p M a s k I m a g e %
3767% %
3768% %
3769% %
3770%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3771%
3772% UnsharpMaskImage() sharpens one or more image channels. We convolve the
3773% image with a Gaussian operator of the given radius and standard deviation
3774% (sigma). For reasonable results, radius should be larger than sigma. Use a
3775% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3776%
3777% The format of the UnsharpMaskImage method is:
3778%
3779% Image *UnsharpMaskImage(const Image *image,const double radius,
3780% const double sigma,const double amount,const double threshold,
3781% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003782%
3783% A description of each parameter follows:
3784%
3785% o image: the image.
3786%
cristy3ed852e2009-09-05 21:47:34 +00003787% o radius: the radius of the Gaussian, in pixels, not counting the center
3788% pixel.
3789%
3790% o sigma: the standard deviation of the Gaussian, in pixels.
3791%
3792% o amount: the percentage of the difference between the original and the
3793% blur image that is added back into the original.
3794%
3795% o threshold: the threshold in pixels needed to apply the diffence amount.
3796%
3797% o exception: return any errors or warnings in this structure.
3798%
3799*/
cristy31bb6272011-11-20 23:57:25 +00003800MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3801 const double sigma,const double amount,const double threshold,
3802 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003803{
3804#define SharpenImageTag "Sharpen/Image"
3805
cristyc4c8d132010-01-07 01:58:38 +00003806 CacheView
3807 *image_view,
3808 *unsharp_view;
3809
cristy3ed852e2009-09-05 21:47:34 +00003810 Image
3811 *unsharp_image;
3812
cristy3ed852e2009-09-05 21:47:34 +00003813 MagickBooleanType
3814 status;
3815
cristybb503372010-05-27 20:51:26 +00003816 MagickOffsetType
3817 progress;
3818
cristy3ed852e2009-09-05 21:47:34 +00003819 MagickRealType
3820 quantum_threshold;
3821
cristybb503372010-05-27 20:51:26 +00003822 ssize_t
3823 y;
3824
cristy3ed852e2009-09-05 21:47:34 +00003825 assert(image != (const Image *) NULL);
3826 assert(image->signature == MagickSignature);
3827 if (image->debug != MagickFalse)
3828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3829 assert(exception != (ExceptionInfo *) NULL);
anthony736a1602011-10-06 12:38:17 +00003830
3831
3832 /* FUTURE: use of bias on sharpen is non-sensical */
cristy05c0c9a2011-09-05 23:16:13 +00003833 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
anthony736a1602011-10-06 12:38:17 +00003834
cristy3ed852e2009-09-05 21:47:34 +00003835 if (unsharp_image == (Image *) NULL)
3836 return((Image *) NULL);
3837 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3838 /*
3839 Unsharp-mask image.
3840 */
3841 status=MagickTrue;
3842 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003843 image_view=AcquireCacheView(image);
3844 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00003845#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003846 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003847#endif
cristybb503372010-05-27 20:51:26 +00003848 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003849 {
cristy4c08aed2011-07-01 19:47:50 +00003850 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003851 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003852
cristy4c08aed2011-07-01 19:47:50 +00003853 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003854 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003855
cristy117ff172010-08-15 21:35:32 +00003856 register ssize_t
3857 x;
3858
cristy3ed852e2009-09-05 21:47:34 +00003859 if (status == MagickFalse)
3860 continue;
3861 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3862 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3863 exception);
cristy4c08aed2011-07-01 19:47:50 +00003864 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003865 {
3866 status=MagickFalse;
3867 continue;
3868 }
cristybb503372010-05-27 20:51:26 +00003869 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003870 {
cristy7f3a0d12011-09-05 23:27:59 +00003871 register ssize_t
3872 i;
3873
3874 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3875 {
3876 MagickRealType
3877 pixel;
3878
3879 PixelChannel
3880 channel;
3881
3882 PixelTrait
3883 traits,
3884 unsharp_traits;
3885
cristye2a912b2011-12-05 20:02:07 +00003886 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003887 traits=GetPixelChannelMapTraits(image,channel);
cristy7f3a0d12011-09-05 23:27:59 +00003888 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3889 if ((traits == UndefinedPixelTrait) ||
3890 (unsharp_traits == UndefinedPixelTrait))
3891 continue;
3892 if ((unsharp_traits & CopyPixelTrait) != 0)
3893 {
cristy0beccfa2011-09-25 20:47:53 +00003894 SetPixelChannel(unsharp_image,channel,p[i],q);
cristy7f3a0d12011-09-05 23:27:59 +00003895 continue;
3896 }
cristy0beccfa2011-09-25 20:47:53 +00003897 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
cristy7f3a0d12011-09-05 23:27:59 +00003898 if (fabs(2.0*pixel) < quantum_threshold)
3899 pixel=(MagickRealType) p[i];
3900 else
3901 pixel=(MagickRealType) p[i]+amount*pixel;
cristy0beccfa2011-09-25 20:47:53 +00003902 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
cristy7f3a0d12011-09-05 23:27:59 +00003903 }
cristyed231572011-07-14 02:18:59 +00003904 p+=GetPixelChannels(image);
3905 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00003906 }
3907 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3908 status=MagickFalse;
3909 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3910 {
3911 MagickBooleanType
3912 proceed;
3913
cristyb5d5f722009-11-04 03:03:49 +00003914#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003915 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00003916#endif
3917 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3918 if (proceed == MagickFalse)
3919 status=MagickFalse;
3920 }
3921 }
3922 unsharp_image->type=image->type;
3923 unsharp_view=DestroyCacheView(unsharp_view);
3924 image_view=DestroyCacheView(image_view);
3925 if (status == MagickFalse)
3926 unsharp_image=DestroyImage(unsharp_image);
3927 return(unsharp_image);
3928}