blob: d6631d5f0f097392d92ff86e28f415e453c4b376 [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
cristy676251b2012-01-16 17:15:13 +00001636 proceed=SetImageProgress(image,DespeckleImageTag,progress++,
1637 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001638 if (proceed == MagickFalse)
1639 status=MagickFalse;
1640 }
1641 }
1642 despeckle_view=DestroyCacheView(despeckle_view);
1643 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001644 despeckle_image->type=image->type;
1645 if (status == MagickFalse)
1646 despeckle_image=DestroyImage(despeckle_image);
1647 return(despeckle_image);
1648}
1649
1650/*
1651%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1652% %
1653% %
1654% %
1655% E d g e I m a g e %
1656% %
1657% %
1658% %
1659%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1660%
1661% EdgeImage() finds edges in an image. Radius defines the radius of the
1662% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1663% radius for you.
1664%
1665% The format of the EdgeImage method is:
1666%
1667% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001668% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001669%
1670% A description of each parameter follows:
1671%
1672% o image: the image.
1673%
1674% o radius: the radius of the pixel neighborhood.
1675%
cristy8ae632d2011-09-05 17:29:53 +00001676% o sigma: the standard deviation of the Gaussian, in pixels.
1677%
cristy3ed852e2009-09-05 21:47:34 +00001678% o exception: return any errors or warnings in this structure.
1679%
1680*/
1681MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001682 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001683{
1684 Image
1685 *edge_image;
1686
cristy41cbe682011-07-15 19:12:37 +00001687 KernelInfo
1688 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001689
cristybb503372010-05-27 20:51:26 +00001690 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001691 i;
1692
cristybb503372010-05-27 20:51:26 +00001693 size_t
cristy3ed852e2009-09-05 21:47:34 +00001694 width;
1695
cristy41cbe682011-07-15 19:12:37 +00001696 ssize_t
1697 j,
1698 u,
1699 v;
1700
cristy3ed852e2009-09-05 21:47:34 +00001701 assert(image != (const Image *) NULL);
1702 assert(image->signature == MagickSignature);
1703 if (image->debug != MagickFalse)
1704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1705 assert(exception != (ExceptionInfo *) NULL);
1706 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001707 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001708 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001709 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001710 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001711 kernel_info->width=width;
1712 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001713 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1714 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1715 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001716 {
1717 kernel_info=DestroyKernelInfo(kernel_info);
1718 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1719 }
1720 j=(ssize_t) kernel_info->width/2;
1721 i=0;
1722 for (v=(-j); v <= j; v++)
1723 {
1724 for (u=(-j); u <= j; u++)
1725 {
1726 kernel_info->values[i]=(-1.0);
1727 i++;
1728 }
1729 }
1730 kernel_info->values[i/2]=(double) (width*width-1.0);
anthony736a1602011-10-06 12:38:17 +00001731 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
cristy5e6be1e2011-07-16 01:23:39 +00001732 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001733 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001734 return(edge_image);
1735}
1736
1737/*
1738%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1739% %
1740% %
1741% %
1742% E m b o s s I m a g e %
1743% %
1744% %
1745% %
1746%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1747%
1748% EmbossImage() returns a grayscale image with a three-dimensional effect.
1749% We convolve the image with a Gaussian operator of the given radius and
1750% standard deviation (sigma). For reasonable results, radius should be
1751% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1752% radius for you.
1753%
1754% The format of the EmbossImage method is:
1755%
1756% Image *EmbossImage(const Image *image,const double radius,
1757% const double sigma,ExceptionInfo *exception)
1758%
1759% A description of each parameter follows:
1760%
1761% o image: the image.
1762%
1763% o radius: the radius of the pixel neighborhood.
1764%
1765% o sigma: the standard deviation of the Gaussian, in pixels.
1766%
1767% o exception: return any errors or warnings in this structure.
1768%
1769*/
1770MagickExport Image *EmbossImage(const Image *image,const double radius,
1771 const double sigma,ExceptionInfo *exception)
1772{
cristy3ed852e2009-09-05 21:47:34 +00001773 Image
1774 *emboss_image;
1775
cristy41cbe682011-07-15 19:12:37 +00001776 KernelInfo
1777 *kernel_info;
1778
cristybb503372010-05-27 20:51:26 +00001779 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001780 i;
1781
cristybb503372010-05-27 20:51:26 +00001782 size_t
cristy3ed852e2009-09-05 21:47:34 +00001783 width;
1784
cristy117ff172010-08-15 21:35:32 +00001785 ssize_t
1786 j,
1787 k,
1788 u,
1789 v;
1790
cristy41cbe682011-07-15 19:12:37 +00001791 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001792 assert(image->signature == MagickSignature);
1793 if (image->debug != MagickFalse)
1794 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1795 assert(exception != (ExceptionInfo *) NULL);
1796 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001797 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001798 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001799 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001800 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001801 kernel_info->width=width;
1802 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001803 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1804 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1805 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001806 {
1807 kernel_info=DestroyKernelInfo(kernel_info);
1808 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1809 }
1810 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001811 k=j;
1812 i=0;
1813 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001814 {
cristy47e00502009-12-17 19:19:57 +00001815 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001816 {
cristy41cbe682011-07-15 19:12:37 +00001817 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001818 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001819 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001820 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001821 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001822 i++;
1823 }
cristy47e00502009-12-17 19:19:57 +00001824 k--;
cristy3ed852e2009-09-05 21:47:34 +00001825 }
anthony736a1602011-10-06 12:38:17 +00001826 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
cristy5e6be1e2011-07-16 01:23:39 +00001827 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001828 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001829 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001830 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001831 return(emboss_image);
1832}
1833
1834/*
1835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1836% %
1837% %
1838% %
1839% G a u s s i a n B l u r I m a g e %
1840% %
1841% %
1842% %
1843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1844%
1845% GaussianBlurImage() blurs an image. We convolve the image with a
1846% Gaussian operator of the given radius and standard deviation (sigma).
1847% For reasonable results, the radius should be larger than sigma. Use a
1848% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1849%
1850% The format of the GaussianBlurImage method is:
1851%
1852% Image *GaussianBlurImage(const Image *image,onst double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001853% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001854%
1855% A description of each parameter follows:
1856%
1857% o image: the image.
1858%
cristy3ed852e2009-09-05 21:47:34 +00001859% o radius: the radius of the Gaussian, in pixels, not counting the center
1860% pixel.
1861%
1862% o sigma: the standard deviation of the Gaussian, in pixels.
1863%
cristy05c0c9a2011-09-05 23:16:13 +00001864% o bias: the bias.
1865%
cristy3ed852e2009-09-05 21:47:34 +00001866% o exception: return any errors or warnings in this structure.
1867%
1868*/
cristy41cbe682011-07-15 19:12:37 +00001869MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001870 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001871{
cristy3ed852e2009-09-05 21:47:34 +00001872 Image
1873 *blur_image;
1874
cristy41cbe682011-07-15 19:12:37 +00001875 KernelInfo
1876 *kernel_info;
1877
cristybb503372010-05-27 20:51:26 +00001878 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001879 i;
1880
cristybb503372010-05-27 20:51:26 +00001881 size_t
cristy3ed852e2009-09-05 21:47:34 +00001882 width;
1883
cristy117ff172010-08-15 21:35:32 +00001884 ssize_t
1885 j,
1886 u,
1887 v;
1888
cristy3ed852e2009-09-05 21:47:34 +00001889 assert(image != (const Image *) NULL);
1890 assert(image->signature == MagickSignature);
1891 if (image->debug != MagickFalse)
1892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1893 assert(exception != (ExceptionInfo *) NULL);
1894 assert(exception->signature == MagickSignature);
1895 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001896 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001897 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001898 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001899 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1900 kernel_info->width=width;
1901 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00001902 kernel_info->bias=bias; /* FUTURE: user bias on Gaussian Blur! non-sense */
cristy41cbe682011-07-15 19:12:37 +00001903 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00001904 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1905 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1906 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001907 {
1908 kernel_info=DestroyKernelInfo(kernel_info);
1909 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1910 }
1911 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001912 i=0;
cristy47e00502009-12-17 19:19:57 +00001913 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001914 {
cristy47e00502009-12-17 19:19:57 +00001915 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001916 {
1917 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1918 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1919 i++;
1920 }
cristy3ed852e2009-09-05 21:47:34 +00001921 }
cristy5e6be1e2011-07-16 01:23:39 +00001922 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001923 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001924 return(blur_image);
1925}
1926
1927/*
1928%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1929% %
1930% %
1931% %
cristy3ed852e2009-09-05 21:47:34 +00001932% M o t i o n B l u r I m a g e %
1933% %
1934% %
1935% %
1936%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1937%
1938% MotionBlurImage() simulates motion blur. We convolve the image with a
1939% Gaussian operator of the given radius and standard deviation (sigma).
1940% For reasonable results, radius should be larger than sigma. Use a
1941% radius of 0 and MotionBlurImage() selects a suitable radius for you.
1942% Angle gives the angle of the blurring motion.
1943%
1944% Andrew Protano contributed this effect.
1945%
1946% The format of the MotionBlurImage method is:
1947%
1948% Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00001949% const double sigma,const double angle,const double bias,
1950% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001951%
1952% A description of each parameter follows:
1953%
1954% o image: the image.
1955%
cristy3ed852e2009-09-05 21:47:34 +00001956% o radius: the radius of the Gaussian, in pixels, not counting
1957% the center pixel.
1958%
1959% o sigma: the standard deviation of the Gaussian, in pixels.
1960%
cristycee97112010-05-28 00:44:52 +00001961% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00001962%
cristyf7ef0252011-09-09 14:50:06 +00001963% o bias: the bias.
1964%
cristy3ed852e2009-09-05 21:47:34 +00001965% o exception: return any errors or warnings in this structure.
1966%
1967*/
1968
cristybb503372010-05-27 20:51:26 +00001969static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00001970{
cristy3ed852e2009-09-05 21:47:34 +00001971 double
cristy47e00502009-12-17 19:19:57 +00001972 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00001973 normalize;
1974
cristybb503372010-05-27 20:51:26 +00001975 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001976 i;
1977
1978 /*
cristy47e00502009-12-17 19:19:57 +00001979 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00001980 */
1981 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1982 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
1983 if (kernel == (double *) NULL)
1984 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00001985 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00001986 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00001987 {
cristy4205a3c2010-09-12 20:19:59 +00001988 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1989 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00001990 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00001991 }
cristybb503372010-05-27 20:51:26 +00001992 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001993 kernel[i]/=normalize;
1994 return(kernel);
1995}
1996
cristya63e4a92011-09-09 00:47:59 +00001997MagickExport Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00001998 const double sigma,const double angle,const double bias,
1999 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002000{
cristyc4c8d132010-01-07 01:58:38 +00002001 CacheView
2002 *blur_view,
2003 *image_view;
2004
cristy3ed852e2009-09-05 21:47:34 +00002005 double
2006 *kernel;
2007
2008 Image
2009 *blur_image;
2010
cristy3ed852e2009-09-05 21:47:34 +00002011 MagickBooleanType
2012 status;
2013
cristybb503372010-05-27 20:51:26 +00002014 MagickOffsetType
2015 progress;
2016
cristy3ed852e2009-09-05 21:47:34 +00002017 OffsetInfo
2018 *offset;
2019
2020 PointInfo
2021 point;
2022
cristybb503372010-05-27 20:51:26 +00002023 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002024 i;
2025
cristybb503372010-05-27 20:51:26 +00002026 size_t
cristy3ed852e2009-09-05 21:47:34 +00002027 width;
2028
cristybb503372010-05-27 20:51:26 +00002029 ssize_t
2030 y;
2031
cristy3ed852e2009-09-05 21:47:34 +00002032 assert(image != (Image *) NULL);
2033 assert(image->signature == MagickSignature);
2034 if (image->debug != MagickFalse)
2035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2036 assert(exception != (ExceptionInfo *) NULL);
2037 width=GetOptimalKernelWidth1D(radius,sigma);
2038 kernel=GetMotionBlurKernel(width,sigma);
2039 if (kernel == (double *) NULL)
2040 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2041 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2042 if (offset == (OffsetInfo *) NULL)
2043 {
2044 kernel=(double *) RelinquishMagickMemory(kernel);
2045 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2046 }
cristy1e7aa312011-09-10 20:01:36 +00002047 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002048 if (blur_image == (Image *) NULL)
2049 {
2050 kernel=(double *) RelinquishMagickMemory(kernel);
2051 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2052 return((Image *) NULL);
2053 }
cristy574cc262011-08-05 01:23:58 +00002054 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002055 {
2056 kernel=(double *) RelinquishMagickMemory(kernel);
2057 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002058 blur_image=DestroyImage(blur_image);
2059 return((Image *) NULL);
2060 }
2061 point.x=(double) width*sin(DegreesToRadians(angle));
2062 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002063 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002064 {
cristybb503372010-05-27 20:51:26 +00002065 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2066 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002067 }
2068 /*
2069 Motion blur image.
2070 */
2071 status=MagickTrue;
2072 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002073 image_view=AcquireCacheView(image);
2074 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002075#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00002076 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002077#endif
cristybb503372010-05-27 20:51:26 +00002078 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002079 {
cristyf7ef0252011-09-09 14:50:06 +00002080 register const Quantum
2081 *restrict p;
2082
cristy4c08aed2011-07-01 19:47:50 +00002083 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002084 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002085
cristy117ff172010-08-15 21:35:32 +00002086 register ssize_t
2087 x;
2088
cristy3ed852e2009-09-05 21:47:34 +00002089 if (status == MagickFalse)
2090 continue;
cristyf7ef0252011-09-09 14:50:06 +00002091 p=GetCacheViewVirtualPixels(blur_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00002092 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2093 exception);
cristyf7ef0252011-09-09 14:50:06 +00002094 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002095 {
2096 status=MagickFalse;
2097 continue;
2098 }
cristybb503372010-05-27 20:51:26 +00002099 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002100 {
cristybb503372010-05-27 20:51:26 +00002101 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002102 i;
2103
cristyf7ef0252011-09-09 14:50:06 +00002104 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2105 {
2106 MagickRealType
2107 alpha,
2108 gamma,
2109 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002110
cristyf7ef0252011-09-09 14:50:06 +00002111 PixelChannel
2112 channel;
2113
2114 PixelTrait
2115 blur_traits,
2116 traits;
2117
2118 register const Quantum
2119 *restrict r;
2120
2121 register double
2122 *restrict k;
2123
2124 register ssize_t
2125 j;
2126
cristye2a912b2011-12-05 20:02:07 +00002127 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002128 traits=GetPixelChannelMapTraits(image,channel);
cristyf7ef0252011-09-09 14:50:06 +00002129 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2130 if ((traits == UndefinedPixelTrait) ||
2131 (blur_traits == UndefinedPixelTrait))
2132 continue;
2133 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002134 {
cristy0beccfa2011-09-25 20:47:53 +00002135 SetPixelChannel(blur_image,channel,p[i],q);
cristyf7ef0252011-09-09 14:50:06 +00002136 continue;
cristy3ed852e2009-09-05 21:47:34 +00002137 }
cristyf7ef0252011-09-09 14:50:06 +00002138 k=kernel;
2139 pixel=bias;
2140 if ((blur_traits & BlendPixelTrait) == 0)
2141 {
2142 for (j=0; j < (ssize_t) width; j++)
2143 {
2144 r=GetCacheViewVirtualPixels(image_view,x+offset[j].x,y+
2145 offset[j].y,1,1,exception);
2146 if (r == (const Quantum *) NULL)
2147 {
2148 status=MagickFalse;
2149 continue;
2150 }
2151 pixel+=(*k)*r[i];
2152 k++;
2153 }
cristy0beccfa2011-09-25 20:47:53 +00002154 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002155 continue;
2156 }
2157 alpha=0.0;
2158 gamma=0.0;
2159 for (j=0; j < (ssize_t) width; j++)
2160 {
2161 r=GetCacheViewVirtualPixels(image_view,x+offset[j].x,y+offset[j].y,1,
2162 1,exception);
2163 if (r == (const Quantum *) NULL)
2164 {
2165 status=MagickFalse;
2166 continue;
2167 }
2168 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2169 pixel+=(*k)*alpha*r[i];
2170 gamma+=(*k)*alpha;
2171 k++;
cristy3ed852e2009-09-05 21:47:34 +00002172 }
cristyf7ef0252011-09-09 14:50:06 +00002173 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002174 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002175 }
2176 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002177 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002178 }
2179 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2180 status=MagickFalse;
2181 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2182 {
2183 MagickBooleanType
2184 proceed;
2185
cristyb557a152011-02-22 12:14:30 +00002186#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002187 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002188#endif
2189 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2190 if (proceed == MagickFalse)
2191 status=MagickFalse;
2192 }
2193 }
2194 blur_view=DestroyCacheView(blur_view);
2195 image_view=DestroyCacheView(image_view);
2196 kernel=(double *) RelinquishMagickMemory(kernel);
2197 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2198 if (status == MagickFalse)
2199 blur_image=DestroyImage(blur_image);
2200 return(blur_image);
2201}
2202
2203/*
2204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2205% %
2206% %
2207% %
2208% P r e v i e w I m a g e %
2209% %
2210% %
2211% %
2212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2213%
2214% PreviewImage() tiles 9 thumbnails of the specified image with an image
2215% processing operation applied with varying parameters. This may be helpful
2216% pin-pointing an appropriate parameter for a particular image processing
2217% operation.
2218%
2219% The format of the PreviewImages method is:
2220%
2221% Image *PreviewImages(const Image *image,const PreviewType preview,
2222% ExceptionInfo *exception)
2223%
2224% A description of each parameter follows:
2225%
2226% o image: the image.
2227%
2228% o preview: the image processing operation.
2229%
2230% o exception: return any errors or warnings in this structure.
2231%
2232*/
2233MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2234 ExceptionInfo *exception)
2235{
2236#define NumberTiles 9
2237#define PreviewImageTag "Preview/Image"
2238#define DefaultPreviewGeometry "204x204+10+10"
2239
2240 char
2241 factor[MaxTextExtent],
2242 label[MaxTextExtent];
2243
2244 double
2245 degrees,
2246 gamma,
2247 percentage,
2248 radius,
2249 sigma,
2250 threshold;
2251
2252 Image
2253 *images,
2254 *montage_image,
2255 *preview_image,
2256 *thumbnail;
2257
2258 ImageInfo
2259 *preview_info;
2260
cristy3ed852e2009-09-05 21:47:34 +00002261 MagickBooleanType
2262 proceed;
2263
2264 MontageInfo
2265 *montage_info;
2266
2267 QuantizeInfo
2268 quantize_info;
2269
2270 RectangleInfo
2271 geometry;
2272
cristybb503372010-05-27 20:51:26 +00002273 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002274 i,
2275 x;
2276
cristybb503372010-05-27 20:51:26 +00002277 size_t
cristy3ed852e2009-09-05 21:47:34 +00002278 colors;
2279
cristy117ff172010-08-15 21:35:32 +00002280 ssize_t
2281 y;
2282
cristy3ed852e2009-09-05 21:47:34 +00002283 /*
2284 Open output image file.
2285 */
2286 assert(image != (Image *) NULL);
2287 assert(image->signature == MagickSignature);
2288 if (image->debug != MagickFalse)
2289 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2290 colors=2;
2291 degrees=0.0;
2292 gamma=(-0.2f);
2293 preview_info=AcquireImageInfo();
2294 SetGeometry(image,&geometry);
2295 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2296 &geometry.width,&geometry.height);
2297 images=NewImageList();
2298 percentage=12.5;
2299 GetQuantizeInfo(&quantize_info);
2300 radius=0.0;
2301 sigma=1.0;
2302 threshold=0.0;
2303 x=0;
2304 y=0;
2305 for (i=0; i < NumberTiles; i++)
2306 {
2307 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2308 if (thumbnail == (Image *) NULL)
2309 break;
2310 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2311 (void *) NULL);
cristyd15e6592011-10-15 00:13:06 +00002312 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002313 if (i == (NumberTiles/2))
2314 {
cristy9950d572011-10-01 18:22:35 +00002315 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2316 &thumbnail->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002317 AppendImageToList(&images,thumbnail);
2318 continue;
2319 }
2320 switch (preview)
2321 {
2322 case RotatePreview:
2323 {
2324 degrees+=45.0;
2325 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002326 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002327 break;
2328 }
2329 case ShearPreview:
2330 {
2331 degrees+=5.0;
2332 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002333 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002334 degrees,2.0*degrees);
2335 break;
2336 }
2337 case RollPreview:
2338 {
cristybb503372010-05-27 20:51:26 +00002339 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2340 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002341 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002342 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002343 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002344 break;
2345 }
2346 case HuePreview:
2347 {
2348 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2349 if (preview_image == (Image *) NULL)
2350 break;
cristyb51dff52011-05-19 16:55:47 +00002351 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002352 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002353 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002354 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002355 break;
2356 }
2357 case SaturationPreview:
2358 {
2359 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2360 if (preview_image == (Image *) NULL)
2361 break;
cristyb51dff52011-05-19 16:55:47 +00002362 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002363 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002364 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002365 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002366 break;
2367 }
2368 case BrightnessPreview:
2369 {
2370 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2371 if (preview_image == (Image *) NULL)
2372 break;
cristyb51dff52011-05-19 16:55:47 +00002373 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002374 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002375 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002376 break;
2377 }
2378 case GammaPreview:
2379 default:
2380 {
2381 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2382 if (preview_image == (Image *) NULL)
2383 break;
2384 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002385 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002386 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002387 break;
2388 }
2389 case SpiffPreview:
2390 {
2391 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2392 if (preview_image != (Image *) NULL)
2393 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002394 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002395 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002396 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002397 break;
2398 }
2399 case DullPreview:
2400 {
2401 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2402 if (preview_image == (Image *) NULL)
2403 break;
2404 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002405 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002406 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002407 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002408 break;
2409 }
2410 case GrayscalePreview:
2411 {
2412 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2413 if (preview_image == (Image *) NULL)
2414 break;
2415 colors<<=1;
2416 quantize_info.number_colors=colors;
2417 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002418 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002419 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002420 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002421 break;
2422 }
2423 case QuantizePreview:
2424 {
2425 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2426 if (preview_image == (Image *) NULL)
2427 break;
2428 colors<<=1;
2429 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002430 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002431 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002432 colors);
cristy3ed852e2009-09-05 21:47:34 +00002433 break;
2434 }
2435 case DespecklePreview:
2436 {
2437 for (x=0; x < (i-1); x++)
2438 {
2439 preview_image=DespeckleImage(thumbnail,exception);
2440 if (preview_image == (Image *) NULL)
2441 break;
2442 thumbnail=DestroyImage(thumbnail);
2443 thumbnail=preview_image;
2444 }
2445 preview_image=DespeckleImage(thumbnail,exception);
2446 if (preview_image == (Image *) NULL)
2447 break;
cristyb51dff52011-05-19 16:55:47 +00002448 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002449 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002450 break;
2451 }
2452 case ReduceNoisePreview:
2453 {
cristy95c38342011-03-18 22:39:51 +00002454 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2455 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002456 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002457 break;
2458 }
2459 case AddNoisePreview:
2460 {
2461 switch ((int) i)
2462 {
2463 case 0:
2464 {
2465 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2466 break;
2467 }
2468 case 1:
2469 {
2470 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2471 break;
2472 }
2473 case 2:
2474 {
2475 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2476 break;
2477 }
2478 case 3:
2479 {
2480 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2481 break;
2482 }
2483 case 4:
2484 {
2485 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2486 break;
2487 }
2488 case 5:
2489 {
2490 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2491 break;
2492 }
2493 default:
2494 {
2495 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2496 break;
2497 }
2498 }
cristyd76c51e2011-03-26 00:21:26 +00002499 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2500 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002501 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002502 break;
2503 }
2504 case SharpenPreview:
2505 {
anthony736a1602011-10-06 12:38:17 +00002506 /* FUTURE: user bias on sharpen! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002507 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2508 exception);
cristyb51dff52011-05-19 16:55:47 +00002509 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002510 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002511 break;
2512 }
2513 case BlurPreview:
2514 {
anthony736a1602011-10-06 12:38:17 +00002515 /* FUTURE: user bias on blur! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002516 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002517 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002518 sigma);
2519 break;
2520 }
2521 case ThresholdPreview:
2522 {
2523 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2524 if (preview_image == (Image *) NULL)
2525 break;
cristye941a752011-10-15 01:52:48 +00002526 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2527 QuantumRange+1.0))/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002528 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002529 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2530 break;
2531 }
2532 case EdgeDetectPreview:
2533 {
cristy8ae632d2011-09-05 17:29:53 +00002534 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002535 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002536 break;
2537 }
2538 case SpreadPreview:
2539 {
cristy5c4e2582011-09-11 19:21:03 +00002540 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2541 exception);
cristyb51dff52011-05-19 16:55:47 +00002542 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002543 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002544 break;
2545 }
2546 case SolarizePreview:
2547 {
2548 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2549 if (preview_image == (Image *) NULL)
2550 break;
2551 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002552 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002553 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002554 (QuantumRange*percentage)/100.0);
2555 break;
2556 }
2557 case ShadePreview:
2558 {
2559 degrees+=10.0;
2560 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2561 exception);
cristyb51dff52011-05-19 16:55:47 +00002562 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002563 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002564 break;
2565 }
2566 case RaisePreview:
2567 {
2568 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2569 if (preview_image == (Image *) NULL)
2570 break;
cristybb503372010-05-27 20:51:26 +00002571 geometry.width=(size_t) (2*i+2);
2572 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002573 geometry.x=i/2;
2574 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002575 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002576 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002577 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002578 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002579 break;
2580 }
2581 case SegmentPreview:
2582 {
2583 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2584 if (preview_image == (Image *) NULL)
2585 break;
2586 threshold+=0.4f;
2587 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002588 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002589 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002590 threshold,threshold);
2591 break;
2592 }
2593 case SwirlPreview:
2594 {
cristy76f512e2011-09-12 01:26:56 +00002595 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2596 exception);
cristyb51dff52011-05-19 16:55:47 +00002597 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002598 degrees+=45.0;
2599 break;
2600 }
2601 case ImplodePreview:
2602 {
2603 degrees+=0.1f;
cristy76f512e2011-09-12 01:26:56 +00002604 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2605 exception);
cristyb51dff52011-05-19 16:55:47 +00002606 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002607 break;
2608 }
2609 case WavePreview:
2610 {
2611 degrees+=5.0f;
cristy5c4e2582011-09-11 19:21:03 +00002612 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2613 image->interpolate,exception);
cristyb51dff52011-05-19 16:55:47 +00002614 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002615 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002616 break;
2617 }
2618 case OilPaintPreview:
2619 {
cristy14973ba2011-08-27 23:48:07 +00002620 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2621 exception);
2622 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2623 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002624 break;
2625 }
2626 case CharcoalDrawingPreview:
2627 {
anthony736a1602011-10-06 12:38:17 +00002628 /* FUTURE: user bias on charcoal! This is non-sensical! */
cristy3ed852e2009-09-05 21:47:34 +00002629 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristy05c0c9a2011-09-05 23:16:13 +00002630 image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002631 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002632 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002633 break;
2634 }
2635 case JPEGPreview:
2636 {
2637 char
2638 filename[MaxTextExtent];
2639
2640 int
2641 file;
2642
2643 MagickBooleanType
2644 status;
2645
2646 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2647 if (preview_image == (Image *) NULL)
2648 break;
cristybb503372010-05-27 20:51:26 +00002649 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002650 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002651 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002652 file=AcquireUniqueFileResource(filename);
2653 if (file != -1)
2654 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002655 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002656 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002657 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002658 if (status != MagickFalse)
2659 {
2660 Image
2661 *quality_image;
2662
2663 (void) CopyMagickString(preview_info->filename,
2664 preview_image->filename,MaxTextExtent);
2665 quality_image=ReadImage(preview_info,exception);
2666 if (quality_image != (Image *) NULL)
2667 {
2668 preview_image=DestroyImage(preview_image);
2669 preview_image=quality_image;
2670 }
2671 }
2672 (void) RelinquishUniqueFileResource(preview_image->filename);
2673 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002674 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002675 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2676 1024.0/1024.0);
2677 else
2678 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002679 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002680 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002681 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002682 else
cristyb51dff52011-05-19 16:55:47 +00002683 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002684 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002685 break;
2686 }
2687 }
2688 thumbnail=DestroyImage(thumbnail);
2689 percentage+=12.5;
2690 radius+=0.5;
2691 sigma+=0.25;
2692 if (preview_image == (Image *) NULL)
2693 break;
2694 (void) DeleteImageProperty(preview_image,"label");
cristyd15e6592011-10-15 00:13:06 +00002695 (void) SetImageProperty(preview_image,"label",label,exception);
cristy3ed852e2009-09-05 21:47:34 +00002696 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002697 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2698 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002699 if (proceed == MagickFalse)
2700 break;
2701 }
2702 if (images == (Image *) NULL)
2703 {
2704 preview_info=DestroyImageInfo(preview_info);
2705 return((Image *) NULL);
2706 }
2707 /*
2708 Create the montage.
2709 */
2710 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2711 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2712 montage_info->shadow=MagickTrue;
2713 (void) CloneString(&montage_info->tile,"3x3");
2714 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2715 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2716 montage_image=MontageImages(images,montage_info,exception);
2717 montage_info=DestroyMontageInfo(montage_info);
2718 images=DestroyImageList(images);
2719 if (montage_image == (Image *) NULL)
2720 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2721 if (montage_image->montage != (char *) NULL)
2722 {
2723 /*
2724 Free image directory.
2725 */
2726 montage_image->montage=(char *) RelinquishMagickMemory(
2727 montage_image->montage);
2728 if (image->directory != (char *) NULL)
2729 montage_image->directory=(char *) RelinquishMagickMemory(
2730 montage_image->directory);
2731 }
2732 preview_info=DestroyImageInfo(preview_info);
2733 return(montage_image);
2734}
2735
2736/*
2737%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2738% %
2739% %
2740% %
2741% R a d i a l B l u r I m a g e %
2742% %
2743% %
2744% %
2745%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2746%
2747% RadialBlurImage() applies a radial blur to the image.
2748%
2749% Andrew Protano contributed this effect.
2750%
2751% The format of the RadialBlurImage method is:
2752%
2753% Image *RadialBlurImage(const Image *image,const double angle,
cristy6435bd92011-09-10 02:10:07 +00002754% const double blur,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002755%
2756% A description of each parameter follows:
2757%
2758% o image: the image.
2759%
cristy3ed852e2009-09-05 21:47:34 +00002760% o angle: the angle of the radial blur.
2761%
cristy6435bd92011-09-10 02:10:07 +00002762% o blur: the blur.
2763%
cristy3ed852e2009-09-05 21:47:34 +00002764% o exception: return any errors or warnings in this structure.
2765%
2766*/
cristy4282c702011-11-21 00:01:06 +00002767MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2768 const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002769{
cristyc4c8d132010-01-07 01:58:38 +00002770 CacheView
2771 *blur_view,
2772 *image_view;
2773
cristy3ed852e2009-09-05 21:47:34 +00002774 Image
2775 *blur_image;
2776
cristy3ed852e2009-09-05 21:47:34 +00002777 MagickBooleanType
2778 status;
2779
cristybb503372010-05-27 20:51:26 +00002780 MagickOffsetType
2781 progress;
2782
cristy3ed852e2009-09-05 21:47:34 +00002783 MagickRealType
2784 blur_radius,
2785 *cos_theta,
2786 offset,
2787 *sin_theta,
2788 theta;
2789
2790 PointInfo
2791 blur_center;
2792
cristybb503372010-05-27 20:51:26 +00002793 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002794 i;
2795
cristybb503372010-05-27 20:51:26 +00002796 size_t
cristy3ed852e2009-09-05 21:47:34 +00002797 n;
2798
cristybb503372010-05-27 20:51:26 +00002799 ssize_t
2800 y;
2801
cristy3ed852e2009-09-05 21:47:34 +00002802 /*
2803 Allocate blur image.
2804 */
2805 assert(image != (Image *) NULL);
2806 assert(image->signature == MagickSignature);
2807 if (image->debug != MagickFalse)
2808 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2809 assert(exception != (ExceptionInfo *) NULL);
2810 assert(exception->signature == MagickSignature);
cristy1e7aa312011-09-10 20:01:36 +00002811 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002812 if (blur_image == (Image *) NULL)
2813 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002814 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002815 {
cristy3ed852e2009-09-05 21:47:34 +00002816 blur_image=DestroyImage(blur_image);
2817 return((Image *) NULL);
2818 }
2819 blur_center.x=(double) image->columns/2.0;
2820 blur_center.y=(double) image->rows/2.0;
2821 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002822 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002823 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2824 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2825 sizeof(*cos_theta));
2826 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2827 sizeof(*sin_theta));
2828 if ((cos_theta == (MagickRealType *) NULL) ||
2829 (sin_theta == (MagickRealType *) NULL))
2830 {
2831 blur_image=DestroyImage(blur_image);
2832 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2833 }
2834 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002835 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002836 {
2837 cos_theta[i]=cos((double) (theta*i-offset));
2838 sin_theta[i]=sin((double) (theta*i-offset));
2839 }
2840 /*
2841 Radial blur image.
2842 */
2843 status=MagickTrue;
2844 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002845 image_view=AcquireCacheView(image);
2846 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002847#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002848 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002849#endif
cristy1e7aa312011-09-10 20:01:36 +00002850 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002851 {
cristy1e7aa312011-09-10 20:01:36 +00002852 register const Quantum
2853 *restrict p;
2854
cristy4c08aed2011-07-01 19:47:50 +00002855 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002856 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002857
cristy117ff172010-08-15 21:35:32 +00002858 register ssize_t
2859 x;
2860
cristy3ed852e2009-09-05 21:47:34 +00002861 if (status == MagickFalse)
2862 continue;
cristy1e7aa312011-09-10 20:01:36 +00002863 p=GetCacheViewVirtualPixels(blur_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00002864 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2865 exception);
cristy1e7aa312011-09-10 20:01:36 +00002866 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002867 {
2868 status=MagickFalse;
2869 continue;
2870 }
cristy1e7aa312011-09-10 20:01:36 +00002871 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002872 {
cristy3ed852e2009-09-05 21:47:34 +00002873 MagickRealType
cristy3ed852e2009-09-05 21:47:34 +00002874 radius;
2875
cristy3ed852e2009-09-05 21:47:34 +00002876 PointInfo
2877 center;
2878
cristybb503372010-05-27 20:51:26 +00002879 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002880 i;
2881
cristybb503372010-05-27 20:51:26 +00002882 size_t
cristy3ed852e2009-09-05 21:47:34 +00002883 step;
2884
2885 center.x=(double) x-blur_center.x;
2886 center.y=(double) y-blur_center.y;
2887 radius=hypot((double) center.x,center.y);
2888 if (radius == 0)
2889 step=1;
2890 else
2891 {
cristybb503372010-05-27 20:51:26 +00002892 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002893 if (step == 0)
2894 step=1;
2895 else
2896 if (step >= n)
2897 step=n-1;
2898 }
cristy1e7aa312011-09-10 20:01:36 +00002899 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2900 {
2901 MagickRealType
2902 gamma,
2903 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002904
cristy1e7aa312011-09-10 20:01:36 +00002905 PixelChannel
2906 channel;
2907
2908 PixelTrait
2909 blur_traits,
2910 traits;
2911
2912 register const Quantum
2913 *restrict r;
2914
2915 register ssize_t
2916 j;
2917
cristye2a912b2011-12-05 20:02:07 +00002918 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002919 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00002920 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2921 if ((traits == UndefinedPixelTrait) ||
2922 (blur_traits == UndefinedPixelTrait))
2923 continue;
2924 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002925 {
cristy0beccfa2011-09-25 20:47:53 +00002926 SetPixelChannel(blur_image,channel,p[i],q);
cristy1e7aa312011-09-10 20:01:36 +00002927 continue;
cristy3ed852e2009-09-05 21:47:34 +00002928 }
cristy1e7aa312011-09-10 20:01:36 +00002929 gamma=0.0;
2930 pixel=bias;
2931 if ((blur_traits & BlendPixelTrait) == 0)
2932 {
2933 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2934 {
2935 r=GetCacheViewVirtualPixels(image_view, (ssize_t) (blur_center.x+
2936 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2937 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2938 1,1,exception);
2939 if (r == (const Quantum *) NULL)
2940 {
2941 status=MagickFalse;
2942 continue;
2943 }
2944 pixel+=r[i];
2945 gamma++;
2946 }
2947 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002948 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00002949 continue;
2950 }
2951 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2952 {
2953 r=GetCacheViewVirtualPixels(image_view, (ssize_t) (blur_center.x+
2954 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2955 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2956 1,1,exception);
2957 if (r == (const Quantum *) NULL)
2958 {
2959 status=MagickFalse;
2960 continue;
2961 }
2962 pixel+=GetPixelAlpha(image,r)*r[i];
2963 gamma+=GetPixelAlpha(image,r);
cristy3ed852e2009-09-05 21:47:34 +00002964 }
cristy1e7aa312011-09-10 20:01:36 +00002965 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002966 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00002967 }
2968 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002969 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002970 }
2971 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2972 status=MagickFalse;
2973 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2974 {
2975 MagickBooleanType
2976 proceed;
2977
cristyb5d5f722009-11-04 03:03:49 +00002978#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002979 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002980#endif
2981 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2982 if (proceed == MagickFalse)
2983 status=MagickFalse;
2984 }
2985 }
2986 blur_view=DestroyCacheView(blur_view);
2987 image_view=DestroyCacheView(image_view);
2988 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
2989 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
2990 if (status == MagickFalse)
2991 blur_image=DestroyImage(blur_image);
2992 return(blur_image);
2993}
2994
2995/*
2996%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2997% %
2998% %
2999% %
cristy3ed852e2009-09-05 21:47:34 +00003000% S e l e c t i v e B l u r I m a g e %
3001% %
3002% %
3003% %
3004%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3005%
3006% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3007% It is similar to the unsharpen mask that sharpens everything with contrast
3008% above a certain threshold.
3009%
3010% The format of the SelectiveBlurImage method is:
3011%
3012% Image *SelectiveBlurImage(const Image *image,const double radius,
cristy1e7aa312011-09-10 20:01:36 +00003013% const double sigma,const double threshold,const double bias,
3014% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003015%
3016% A description of each parameter follows:
3017%
3018% o image: the image.
3019%
cristy3ed852e2009-09-05 21:47:34 +00003020% o radius: the radius of the Gaussian, in pixels, not counting the center
3021% pixel.
3022%
3023% o sigma: the standard deviation of the Gaussian, in pixels.
3024%
3025% o threshold: only pixels within this contrast threshold are included
3026% in the blur operation.
3027%
cristy1e7aa312011-09-10 20:01:36 +00003028% o bias: the bias.
3029%
cristy3ed852e2009-09-05 21:47:34 +00003030% o exception: return any errors or warnings in this structure.
3031%
3032*/
cristy4282c702011-11-21 00:01:06 +00003033MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3034 const double sigma,const double threshold,const double bias,
3035 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003036{
3037#define SelectiveBlurImageTag "SelectiveBlur/Image"
3038
cristy47e00502009-12-17 19:19:57 +00003039 CacheView
3040 *blur_view,
3041 *image_view;
3042
cristy3ed852e2009-09-05 21:47:34 +00003043 double
cristy3ed852e2009-09-05 21:47:34 +00003044 *kernel;
3045
3046 Image
3047 *blur_image;
3048
cristy3ed852e2009-09-05 21:47:34 +00003049 MagickBooleanType
3050 status;
3051
cristybb503372010-05-27 20:51:26 +00003052 MagickOffsetType
3053 progress;
3054
cristybb503372010-05-27 20:51:26 +00003055 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003056 i;
cristy3ed852e2009-09-05 21:47:34 +00003057
cristybb503372010-05-27 20:51:26 +00003058 size_t
cristy3ed852e2009-09-05 21:47:34 +00003059 width;
3060
cristybb503372010-05-27 20:51:26 +00003061 ssize_t
cristyc8523c12011-09-13 00:02:53 +00003062 center,
cristybb503372010-05-27 20:51:26 +00003063 j,
3064 u,
3065 v,
3066 y;
3067
cristy3ed852e2009-09-05 21:47:34 +00003068 /*
3069 Initialize blur image attributes.
3070 */
3071 assert(image != (Image *) NULL);
3072 assert(image->signature == MagickSignature);
3073 if (image->debug != MagickFalse)
3074 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3075 assert(exception != (ExceptionInfo *) NULL);
3076 assert(exception->signature == MagickSignature);
3077 width=GetOptimalKernelWidth1D(radius,sigma);
3078 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3079 if (kernel == (double *) NULL)
3080 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003081 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003082 i=0;
cristy47e00502009-12-17 19:19:57 +00003083 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003084 {
cristy47e00502009-12-17 19:19:57 +00003085 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003086 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3087 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003088 }
3089 if (image->debug != MagickFalse)
3090 {
3091 char
3092 format[MaxTextExtent],
3093 *message;
3094
cristy117ff172010-08-15 21:35:32 +00003095 register const double
3096 *k;
3097
cristybb503372010-05-27 20:51:26 +00003098 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003099 u,
3100 v;
3101
cristy3ed852e2009-09-05 21:47:34 +00003102 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003103 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3104 width);
cristy3ed852e2009-09-05 21:47:34 +00003105 message=AcquireString("");
3106 k=kernel;
cristybb503372010-05-27 20:51:26 +00003107 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003108 {
3109 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003110 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003111 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003112 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003113 {
cristyb51dff52011-05-19 16:55:47 +00003114 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003115 (void) ConcatenateString(&message,format);
3116 }
3117 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3118 }
3119 message=DestroyString(message);
3120 }
cristy1e7aa312011-09-10 20:01:36 +00003121 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003122 if (blur_image == (Image *) NULL)
3123 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003124 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003125 {
cristy3ed852e2009-09-05 21:47:34 +00003126 blur_image=DestroyImage(blur_image);
3127 return((Image *) NULL);
3128 }
3129 /*
3130 Threshold blur image.
3131 */
3132 status=MagickTrue;
3133 progress=0;
cristyc8523c12011-09-13 00:02:53 +00003134 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3135 GetPixelChannels(image)*(width/2L));
cristy3ed852e2009-09-05 21:47:34 +00003136 image_view=AcquireCacheView(image);
3137 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003138#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003139 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003140#endif
cristybb503372010-05-27 20:51:26 +00003141 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003142 {
cristy4c08aed2011-07-01 19:47:50 +00003143 double
3144 contrast;
3145
cristy3ed852e2009-09-05 21:47:34 +00003146 MagickBooleanType
3147 sync;
3148
cristy4c08aed2011-07-01 19:47:50 +00003149 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003150 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003151
cristy4c08aed2011-07-01 19:47:50 +00003152 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003153 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003154
cristy117ff172010-08-15 21:35:32 +00003155 register ssize_t
3156 x;
3157
cristy3ed852e2009-09-05 21:47:34 +00003158 if (status == MagickFalse)
3159 continue;
cristy117ff172010-08-15 21:35:32 +00003160 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3161 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00003162 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3163 exception);
cristy4c08aed2011-07-01 19:47:50 +00003164 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003165 {
3166 status=MagickFalse;
3167 continue;
3168 }
cristybb503372010-05-27 20:51:26 +00003169 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003170 {
cristybb503372010-05-27 20:51:26 +00003171 register ssize_t
cristy1e7aa312011-09-10 20:01:36 +00003172 i;
cristy3ed852e2009-09-05 21:47:34 +00003173
cristy1e7aa312011-09-10 20:01:36 +00003174 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3175 {
3176 MagickRealType
3177 alpha,
3178 gamma,
3179 intensity,
3180 pixel;
cristy117ff172010-08-15 21:35:32 +00003181
cristy1e7aa312011-09-10 20:01:36 +00003182 PixelChannel
3183 channel;
3184
3185 PixelTrait
3186 blur_traits,
3187 traits;
3188
3189 register const double
3190 *restrict k;
3191
3192 register const Quantum
3193 *restrict pixels;
3194
3195 register ssize_t
3196 u;
3197
3198 ssize_t
3199 v;
3200
cristye2a912b2011-12-05 20:02:07 +00003201 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003202 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003203 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3204 if ((traits == UndefinedPixelTrait) ||
3205 (blur_traits == UndefinedPixelTrait))
3206 continue;
3207 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003208 {
cristy0beccfa2011-09-25 20:47:53 +00003209 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003210 continue;
cristy3ed852e2009-09-05 21:47:34 +00003211 }
cristy1e7aa312011-09-10 20:01:36 +00003212 k=kernel;
3213 pixel=bias;
3214 pixels=p;
cristyc8523c12011-09-13 00:02:53 +00003215 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
cristy1e7aa312011-09-10 20:01:36 +00003216 gamma=0.0;
3217 if ((blur_traits & BlendPixelTrait) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003218 {
cristy1e7aa312011-09-10 20:01:36 +00003219 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003220 {
cristy1e7aa312011-09-10 20:01:36 +00003221 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003222 {
cristy1e7aa312011-09-10 20:01:36 +00003223 contrast=GetPixelIntensity(image,pixels)-intensity;
3224 if (fabs(contrast) < threshold)
3225 {
3226 pixel+=(*k)*pixels[i];
3227 gamma+=(*k);
3228 }
3229 k++;
3230 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003231 }
cristy1e7aa312011-09-10 20:01:36 +00003232 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003233 }
cristy1e7aa312011-09-10 20:01:36 +00003234 if (fabs((double) gamma) < MagickEpsilon)
3235 {
cristy0beccfa2011-09-25 20:47:53 +00003236 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003237 continue;
3238 }
3239 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003240 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003241 continue;
3242 }
3243 for (v=0; v < (ssize_t) width; v++)
3244 {
3245 for (u=0; u < (ssize_t) width; u++)
3246 {
3247 contrast=GetPixelIntensity(image,pixels)-intensity;
3248 if (fabs(contrast) < threshold)
3249 {
3250 alpha=(MagickRealType) (QuantumScale*
3251 GetPixelAlpha(image,pixels));
3252 pixel+=(*k)*alpha*pixels[i];
3253 gamma+=(*k)*alpha;
3254 }
3255 k++;
3256 pixels+=GetPixelChannels(image);
3257 }
3258 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003259 }
cristy1e7aa312011-09-10 20:01:36 +00003260 if (fabs((double) gamma) < MagickEpsilon)
3261 {
cristy0beccfa2011-09-25 20:47:53 +00003262 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003263 continue;
3264 }
3265 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003266 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003267 }
cristyed231572011-07-14 02:18:59 +00003268 p+=GetPixelChannels(image);
3269 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003270 }
3271 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3272 if (sync == MagickFalse)
3273 status=MagickFalse;
3274 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3275 {
3276 MagickBooleanType
3277 proceed;
3278
cristyb5d5f722009-11-04 03:03:49 +00003279#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003280 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003281#endif
3282 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3283 image->rows);
3284 if (proceed == MagickFalse)
3285 status=MagickFalse;
3286 }
3287 }
3288 blur_image->type=image->type;
3289 blur_view=DestroyCacheView(blur_view);
3290 image_view=DestroyCacheView(image_view);
3291 kernel=(double *) RelinquishMagickMemory(kernel);
3292 if (status == MagickFalse)
3293 blur_image=DestroyImage(blur_image);
3294 return(blur_image);
3295}
3296
3297/*
3298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3299% %
3300% %
3301% %
3302% S h a d e I m a g e %
3303% %
3304% %
3305% %
3306%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3307%
3308% ShadeImage() shines a distant light on an image to create a
3309% three-dimensional effect. You control the positioning of the light with
3310% azimuth and elevation; azimuth is measured in degrees off the x axis
3311% and elevation is measured in pixels above the Z axis.
3312%
3313% The format of the ShadeImage method is:
3314%
3315% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3316% const double azimuth,const double elevation,ExceptionInfo *exception)
3317%
3318% A description of each parameter follows:
3319%
3320% o image: the image.
3321%
3322% o gray: A value other than zero shades the intensity of each pixel.
3323%
3324% o azimuth, elevation: Define the light source direction.
3325%
3326% o exception: return any errors or warnings in this structure.
3327%
3328*/
3329MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3330 const double azimuth,const double elevation,ExceptionInfo *exception)
3331{
3332#define ShadeImageTag "Shade/Image"
3333
cristyc4c8d132010-01-07 01:58:38 +00003334 CacheView
3335 *image_view,
3336 *shade_view;
3337
cristy3ed852e2009-09-05 21:47:34 +00003338 Image
3339 *shade_image;
3340
cristy3ed852e2009-09-05 21:47:34 +00003341 MagickBooleanType
3342 status;
3343
cristybb503372010-05-27 20:51:26 +00003344 MagickOffsetType
3345 progress;
3346
cristy3ed852e2009-09-05 21:47:34 +00003347 PrimaryInfo
3348 light;
3349
cristybb503372010-05-27 20:51:26 +00003350 ssize_t
3351 y;
3352
cristy3ed852e2009-09-05 21:47:34 +00003353 /*
3354 Initialize shaded image attributes.
3355 */
3356 assert(image != (const Image *) NULL);
3357 assert(image->signature == MagickSignature);
3358 if (image->debug != MagickFalse)
3359 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3360 assert(exception != (ExceptionInfo *) NULL);
3361 assert(exception->signature == MagickSignature);
3362 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3363 if (shade_image == (Image *) NULL)
3364 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003365 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003366 {
cristy3ed852e2009-09-05 21:47:34 +00003367 shade_image=DestroyImage(shade_image);
3368 return((Image *) NULL);
3369 }
3370 /*
3371 Compute the light vector.
3372 */
3373 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3374 cos(DegreesToRadians(elevation));
3375 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3376 cos(DegreesToRadians(elevation));
3377 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3378 /*
3379 Shade image.
3380 */
3381 status=MagickTrue;
3382 progress=0;
3383 image_view=AcquireCacheView(image);
3384 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003385#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003386 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003387#endif
cristybb503372010-05-27 20:51:26 +00003388 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003389 {
3390 MagickRealType
3391 distance,
3392 normal_distance,
3393 shade;
3394
3395 PrimaryInfo
3396 normal;
3397
cristy4c08aed2011-07-01 19:47:50 +00003398 register const Quantum
cristy1e7aa312011-09-10 20:01:36 +00003399 *restrict center,
cristyc47d1f82009-11-26 01:44:43 +00003400 *restrict p,
cristy1e7aa312011-09-10 20:01:36 +00003401 *restrict post,
3402 *restrict pre;
cristy3ed852e2009-09-05 21:47:34 +00003403
cristy4c08aed2011-07-01 19:47:50 +00003404 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003405 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003406
cristy117ff172010-08-15 21:35:32 +00003407 register ssize_t
3408 x;
3409
cristy3ed852e2009-09-05 21:47:34 +00003410 if (status == MagickFalse)
3411 continue;
3412 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3413 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3414 exception);
cristy4c08aed2011-07-01 19:47:50 +00003415 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003416 {
3417 status=MagickFalse;
3418 continue;
3419 }
3420 /*
3421 Shade this row of pixels.
3422 */
3423 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristy1e7aa312011-09-10 20:01:36 +00003424 pre=p+GetPixelChannels(image);
3425 center=pre+(image->columns+2)*GetPixelChannels(image);
3426 post=center+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003427 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003428 {
cristy1e7aa312011-09-10 20:01:36 +00003429 register ssize_t
3430 i;
3431
cristy3ed852e2009-09-05 21:47:34 +00003432 /*
3433 Determine the surface normal and compute shading.
3434 */
cristy1e7aa312011-09-10 20:01:36 +00003435 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3436 GetPixelIntensity(image,center-GetPixelChannels(image))+
3437 GetPixelIntensity(image,post-GetPixelChannels(image))-
3438 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3439 GetPixelIntensity(image,center+GetPixelChannels(image))-
3440 GetPixelIntensity(image,post+GetPixelChannels(image)));
3441 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3442 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3443 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3444 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3445 GetPixelIntensity(image,pre+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003446 if ((normal.x == 0.0) && (normal.y == 0.0))
3447 shade=light.z;
3448 else
3449 {
3450 shade=0.0;
3451 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3452 if (distance > MagickEpsilon)
3453 {
3454 normal_distance=
3455 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3456 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3457 shade=distance/sqrt((double) normal_distance);
3458 }
3459 }
cristy1e7aa312011-09-10 20:01:36 +00003460 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3461 {
3462 PixelChannel
3463 channel;
3464
3465 PixelTrait
3466 shade_traits,
3467 traits;
3468
cristye2a912b2011-12-05 20:02:07 +00003469 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003470 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003471 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3472 if ((traits == UndefinedPixelTrait) ||
3473 (shade_traits == UndefinedPixelTrait))
3474 continue;
3475 if ((shade_traits & CopyPixelTrait) != 0)
3476 {
cristy0beccfa2011-09-25 20:47:53 +00003477 SetPixelChannel(shade_image,channel,center[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003478 continue;
3479 }
3480 if (gray != MagickFalse)
3481 {
cristy0beccfa2011-09-25 20:47:53 +00003482 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
cristy1e7aa312011-09-10 20:01:36 +00003483 continue;
3484 }
cristy0beccfa2011-09-25 20:47:53 +00003485 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3486 center[i]),q);
cristy1e7aa312011-09-10 20:01:36 +00003487 }
3488 pre+=GetPixelChannels(image);
3489 center+=GetPixelChannels(image);
3490 post+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003491 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003492 }
3493 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3494 status=MagickFalse;
3495 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3496 {
3497 MagickBooleanType
3498 proceed;
3499
cristyb5d5f722009-11-04 03:03:49 +00003500#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003501 #pragma omp critical (MagickCore_ShadeImage)
3502#endif
3503 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3504 if (proceed == MagickFalse)
3505 status=MagickFalse;
3506 }
3507 }
3508 shade_view=DestroyCacheView(shade_view);
3509 image_view=DestroyCacheView(image_view);
3510 if (status == MagickFalse)
3511 shade_image=DestroyImage(shade_image);
3512 return(shade_image);
3513}
3514
3515/*
3516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3517% %
3518% %
3519% %
3520% S h a r p e n I m a g e %
3521% %
3522% %
3523% %
3524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3525%
3526% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3527% operator of the given radius and standard deviation (sigma). For
3528% reasonable results, radius should be larger than sigma. Use a radius of 0
3529% and SharpenImage() selects a suitable radius for you.
3530%
3531% Using a separable kernel would be faster, but the negative weights cancel
3532% out on the corners of the kernel producing often undesirable ringing in the
3533% filtered result; this can be avoided by using a 2D gaussian shaped image
3534% sharpening kernel instead.
3535%
3536% The format of the SharpenImage method is:
3537%
3538% Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003539% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003540%
3541% A description of each parameter follows:
3542%
3543% o image: the image.
3544%
cristy3ed852e2009-09-05 21:47:34 +00003545% o radius: the radius of the Gaussian, in pixels, not counting the center
3546% pixel.
3547%
3548% o sigma: the standard deviation of the Laplacian, in pixels.
3549%
cristy05c0c9a2011-09-05 23:16:13 +00003550% o bias: bias.
3551%
cristy3ed852e2009-09-05 21:47:34 +00003552% o exception: return any errors or warnings in this structure.
3553%
3554*/
cristy3ed852e2009-09-05 21:47:34 +00003555MagickExport Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003556 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003557{
cristy3ed852e2009-09-05 21:47:34 +00003558 double
cristy47e00502009-12-17 19:19:57 +00003559 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003560
3561 Image
3562 *sharp_image;
3563
cristy41cbe682011-07-15 19:12:37 +00003564 KernelInfo
3565 *kernel_info;
3566
cristybb503372010-05-27 20:51:26 +00003567 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003568 i;
3569
cristybb503372010-05-27 20:51:26 +00003570 size_t
cristy3ed852e2009-09-05 21:47:34 +00003571 width;
3572
cristy117ff172010-08-15 21:35:32 +00003573 ssize_t
3574 j,
3575 u,
3576 v;
3577
cristy3ed852e2009-09-05 21:47:34 +00003578 assert(image != (const Image *) NULL);
3579 assert(image->signature == MagickSignature);
3580 if (image->debug != MagickFalse)
3581 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3582 assert(exception != (ExceptionInfo *) NULL);
3583 assert(exception->signature == MagickSignature);
3584 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003585 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003586 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003587 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003588 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3589 kernel_info->width=width;
3590 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00003591 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
cristy41cbe682011-07-15 19:12:37 +00003592 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00003593 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3594 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3595 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00003596 {
3597 kernel_info=DestroyKernelInfo(kernel_info);
3598 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3599 }
cristy3ed852e2009-09-05 21:47:34 +00003600 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003601 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003602 i=0;
3603 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003604 {
cristy47e00502009-12-17 19:19:57 +00003605 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003606 {
cristy41cbe682011-07-15 19:12:37 +00003607 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3608 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3609 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003610 i++;
3611 }
3612 }
cristy41cbe682011-07-15 19:12:37 +00003613 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003614 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003615 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003616 return(sharp_image);
3617}
3618
3619/*
3620%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3621% %
3622% %
3623% %
3624% S p r e a d I m a g e %
3625% %
3626% %
3627% %
3628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3629%
3630% SpreadImage() is a special effects method that randomly displaces each
3631% pixel in a block defined by the radius parameter.
3632%
3633% The format of the SpreadImage method is:
3634%
3635% Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003636% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003637%
3638% A description of each parameter follows:
3639%
3640% o image: the image.
3641%
cristy5c4e2582011-09-11 19:21:03 +00003642% o radius: choose a random pixel in a neighborhood of this extent.
3643%
3644% o method: the pixel interpolation method.
cristy3ed852e2009-09-05 21:47:34 +00003645%
3646% o exception: return any errors or warnings in this structure.
3647%
3648*/
3649MagickExport Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003650 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003651{
3652#define SpreadImageTag "Spread/Image"
3653
cristyfa112112010-01-04 17:48:07 +00003654 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003655 *image_view,
3656 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003657
cristy3ed852e2009-09-05 21:47:34 +00003658 Image
3659 *spread_image;
3660
cristy3ed852e2009-09-05 21:47:34 +00003661 MagickBooleanType
3662 status;
3663
cristybb503372010-05-27 20:51:26 +00003664 MagickOffsetType
3665 progress;
3666
cristy3ed852e2009-09-05 21:47:34 +00003667 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003668 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003669
cristybb503372010-05-27 20:51:26 +00003670 size_t
cristy3ed852e2009-09-05 21:47:34 +00003671 width;
3672
cristybb503372010-05-27 20:51:26 +00003673 ssize_t
3674 y;
3675
cristy3ed852e2009-09-05 21:47:34 +00003676 /*
3677 Initialize spread image attributes.
3678 */
3679 assert(image != (Image *) NULL);
3680 assert(image->signature == MagickSignature);
3681 if (image->debug != MagickFalse)
3682 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3683 assert(exception != (ExceptionInfo *) NULL);
3684 assert(exception->signature == MagickSignature);
3685 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3686 exception);
3687 if (spread_image == (Image *) NULL)
3688 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003689 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003690 {
cristy3ed852e2009-09-05 21:47:34 +00003691 spread_image=DestroyImage(spread_image);
3692 return((Image *) NULL);
3693 }
3694 /*
3695 Spread image.
3696 */
3697 status=MagickTrue;
3698 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003699 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003700 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003701 image_view=AcquireCacheView(image);
3702 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003703#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00003704 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003705#endif
cristybe82ad52011-09-06 01:17:20 +00003706 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003707 {
cristy5c9e6f22010-09-17 17:31:01 +00003708 const int
3709 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003710
cristybe82ad52011-09-06 01:17:20 +00003711 register const Quantum
3712 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003713
cristy4c08aed2011-07-01 19:47:50 +00003714 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003715 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003716
cristy117ff172010-08-15 21:35:32 +00003717 register ssize_t
3718 x;
3719
cristy3ed852e2009-09-05 21:47:34 +00003720 if (status == MagickFalse)
3721 continue;
cristybe82ad52011-09-06 01:17:20 +00003722 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy9f7e7cb2011-03-26 00:49:57 +00003723 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003724 exception);
cristybe82ad52011-09-06 01:17:20 +00003725 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003726 {
3727 status=MagickFalse;
3728 continue;
3729 }
cristybe82ad52011-09-06 01:17:20 +00003730 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003731 {
cristybe82ad52011-09-06 01:17:20 +00003732 PointInfo
3733 point;
3734
cristybe82ad52011-09-06 01:17:20 +00003735 point.x=GetPseudoRandomValue(random_info[id]);
3736 point.y=GetPseudoRandomValue(random_info[id]);
cristy5c4e2582011-09-11 19:21:03 +00003737 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3738 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
cristyed231572011-07-14 02:18:59 +00003739 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003740 }
cristy9f7e7cb2011-03-26 00:49:57 +00003741 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003742 status=MagickFalse;
3743 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3744 {
3745 MagickBooleanType
3746 proceed;
3747
cristyb557a152011-02-22 12:14:30 +00003748#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003749 #pragma omp critical (MagickCore_SpreadImage)
3750#endif
3751 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3752 if (proceed == MagickFalse)
3753 status=MagickFalse;
3754 }
3755 }
cristy9f7e7cb2011-03-26 00:49:57 +00003756 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003757 image_view=DestroyCacheView(image_view);
3758 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003759 return(spread_image);
3760}
3761
3762/*
3763%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3764% %
3765% %
3766% %
3767% U n s h a r p M a s k I m a g e %
3768% %
3769% %
3770% %
3771%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3772%
3773% UnsharpMaskImage() sharpens one or more image channels. We convolve the
3774% image with a Gaussian operator of the given radius and standard deviation
3775% (sigma). For reasonable results, radius should be larger than sigma. Use a
3776% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3777%
3778% The format of the UnsharpMaskImage method is:
3779%
3780% Image *UnsharpMaskImage(const Image *image,const double radius,
3781% const double sigma,const double amount,const double threshold,
3782% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003783%
3784% A description of each parameter follows:
3785%
3786% o image: the image.
3787%
cristy3ed852e2009-09-05 21:47:34 +00003788% o radius: the radius of the Gaussian, in pixels, not counting the center
3789% pixel.
3790%
3791% o sigma: the standard deviation of the Gaussian, in pixels.
3792%
3793% o amount: the percentage of the difference between the original and the
3794% blur image that is added back into the original.
3795%
3796% o threshold: the threshold in pixels needed to apply the diffence amount.
3797%
3798% o exception: return any errors or warnings in this structure.
3799%
3800*/
cristy31bb6272011-11-20 23:57:25 +00003801MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3802 const double sigma,const double amount,const double threshold,
3803 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003804{
3805#define SharpenImageTag "Sharpen/Image"
3806
cristyc4c8d132010-01-07 01:58:38 +00003807 CacheView
3808 *image_view,
3809 *unsharp_view;
3810
cristy3ed852e2009-09-05 21:47:34 +00003811 Image
3812 *unsharp_image;
3813
cristy3ed852e2009-09-05 21:47:34 +00003814 MagickBooleanType
3815 status;
3816
cristybb503372010-05-27 20:51:26 +00003817 MagickOffsetType
3818 progress;
3819
cristy3ed852e2009-09-05 21:47:34 +00003820 MagickRealType
3821 quantum_threshold;
3822
cristybb503372010-05-27 20:51:26 +00003823 ssize_t
3824 y;
3825
cristy3ed852e2009-09-05 21:47:34 +00003826 assert(image != (const Image *) NULL);
3827 assert(image->signature == MagickSignature);
3828 if (image->debug != MagickFalse)
3829 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3830 assert(exception != (ExceptionInfo *) NULL);
anthony736a1602011-10-06 12:38:17 +00003831
3832
3833 /* FUTURE: use of bias on sharpen is non-sensical */
cristy05c0c9a2011-09-05 23:16:13 +00003834 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
anthony736a1602011-10-06 12:38:17 +00003835
cristy3ed852e2009-09-05 21:47:34 +00003836 if (unsharp_image == (Image *) NULL)
3837 return((Image *) NULL);
3838 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3839 /*
3840 Unsharp-mask image.
3841 */
3842 status=MagickTrue;
3843 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003844 image_view=AcquireCacheView(image);
3845 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00003846#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003847 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003848#endif
cristybb503372010-05-27 20:51:26 +00003849 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003850 {
cristy4c08aed2011-07-01 19:47:50 +00003851 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003852 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003853
cristy4c08aed2011-07-01 19:47:50 +00003854 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003855 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003856
cristy117ff172010-08-15 21:35:32 +00003857 register ssize_t
3858 x;
3859
cristy3ed852e2009-09-05 21:47:34 +00003860 if (status == MagickFalse)
3861 continue;
3862 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3863 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3864 exception);
cristy4c08aed2011-07-01 19:47:50 +00003865 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003866 {
3867 status=MagickFalse;
3868 continue;
3869 }
cristybb503372010-05-27 20:51:26 +00003870 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003871 {
cristy7f3a0d12011-09-05 23:27:59 +00003872 register ssize_t
3873 i;
3874
3875 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3876 {
3877 MagickRealType
3878 pixel;
3879
3880 PixelChannel
3881 channel;
3882
3883 PixelTrait
3884 traits,
3885 unsharp_traits;
3886
cristye2a912b2011-12-05 20:02:07 +00003887 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003888 traits=GetPixelChannelMapTraits(image,channel);
cristy7f3a0d12011-09-05 23:27:59 +00003889 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3890 if ((traits == UndefinedPixelTrait) ||
3891 (unsharp_traits == UndefinedPixelTrait))
3892 continue;
3893 if ((unsharp_traits & CopyPixelTrait) != 0)
3894 {
cristy0beccfa2011-09-25 20:47:53 +00003895 SetPixelChannel(unsharp_image,channel,p[i],q);
cristy7f3a0d12011-09-05 23:27:59 +00003896 continue;
3897 }
cristy0beccfa2011-09-25 20:47:53 +00003898 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
cristy7f3a0d12011-09-05 23:27:59 +00003899 if (fabs(2.0*pixel) < quantum_threshold)
3900 pixel=(MagickRealType) p[i];
3901 else
3902 pixel=(MagickRealType) p[i]+amount*pixel;
cristy0beccfa2011-09-25 20:47:53 +00003903 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
cristy7f3a0d12011-09-05 23:27:59 +00003904 }
cristyed231572011-07-14 02:18:59 +00003905 p+=GetPixelChannels(image);
3906 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00003907 }
3908 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3909 status=MagickFalse;
3910 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3911 {
3912 MagickBooleanType
3913 proceed;
3914
cristyb5d5f722009-11-04 03:03:49 +00003915#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003916 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00003917#endif
3918 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3919 if (proceed == MagickFalse)
3920 status=MagickFalse;
3921 }
3922 }
3923 unsharp_image->type=image->type;
3924 unsharp_view=DestroyCacheView(unsharp_view);
3925 image_view=DestroyCacheView(image_view);
3926 if (status == MagickFalse)
3927 unsharp_image=DestroyImage(unsharp_image);
3928 return(unsharp_image);
3929}