blob: d4ca0194162318690b61c9cf67be98cebdf7cb8b [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,
cristyaa2c16c2012-03-25 22:21:35 +0000111% const double sigma,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%
122% o exception: return any errors or warnings in this structure.
123%
124*/
125
cristyf89cb1d2011-07-07 01:24:37 +0000126MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
cristy051718b2011-08-28 22:49:25 +0000127 const char *levels,ExceptionInfo *exception)
cristyf89cb1d2011-07-07 01:24:37 +0000128{
129 double
130 black_point,
131 gamma,
132 white_point;
133
134 GeometryInfo
135 geometry_info;
136
137 MagickBooleanType
138 status;
139
140 MagickStatusType
141 flags;
142
143 /*
144 Parse levels.
145 */
146 if (levels == (char *) NULL)
147 return(MagickFalse);
148 flags=ParseGeometry(levels,&geometry_info);
149 black_point=geometry_info.rho;
150 white_point=(double) QuantumRange;
151 if ((flags & SigmaValue) != 0)
152 white_point=geometry_info.sigma;
153 gamma=1.0;
154 if ((flags & XiValue) != 0)
155 gamma=geometry_info.xi;
156 if ((flags & PercentValue) != 0)
157 {
158 black_point*=(double) image->columns*image->rows/100.0;
159 white_point*=(double) image->columns*image->rows/100.0;
160 }
161 if ((flags & SigmaValue) == 0)
162 white_point=(double) QuantumRange-black_point;
163 if ((flags & AspectValue ) == 0)
cristy7c0a0a42011-08-23 17:57:25 +0000164 status=LevelImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000165 else
cristy7c0a0a42011-08-23 17:57:25 +0000166 status=LevelizeImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000167 return(status);
168}
169
cristy4282c702011-11-21 00:01:06 +0000170MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000171 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000172{
173#define AdaptiveBlurImageTag "Convolve/Image"
174#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
175
cristyc4c8d132010-01-07 01:58:38 +0000176 CacheView
177 *blur_view,
178 *edge_view,
179 *image_view;
180
cristy3ed852e2009-09-05 21:47:34 +0000181 double
cristy47e00502009-12-17 19:19:57 +0000182 **kernel,
183 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000184
185 Image
186 *blur_image,
187 *edge_image,
188 *gaussian_image;
189
cristy3ed852e2009-09-05 21:47:34 +0000190 MagickBooleanType
191 status;
192
cristybb503372010-05-27 20:51:26 +0000193 MagickOffsetType
194 progress;
195
cristybb503372010-05-27 20:51:26 +0000196 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000197 i;
cristy3ed852e2009-09-05 21:47:34 +0000198
cristybb503372010-05-27 20:51:26 +0000199 size_t
cristy3ed852e2009-09-05 21:47:34 +0000200 width;
201
cristybb503372010-05-27 20:51:26 +0000202 ssize_t
203 j,
204 k,
205 u,
206 v,
207 y;
208
cristy3ed852e2009-09-05 21:47:34 +0000209 assert(image != (const Image *) NULL);
210 assert(image->signature == MagickSignature);
211 if (image->debug != MagickFalse)
212 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
213 assert(exception != (ExceptionInfo *) NULL);
214 assert(exception->signature == MagickSignature);
215 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
216 if (blur_image == (Image *) NULL)
217 return((Image *) NULL);
218 if (fabs(sigma) <= MagickEpsilon)
219 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000220 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000221 {
cristy3ed852e2009-09-05 21:47:34 +0000222 blur_image=DestroyImage(blur_image);
223 return((Image *) NULL);
224 }
225 /*
226 Edge detect the image brighness channel, level, blur, and level again.
227 */
cristy8ae632d2011-09-05 17:29:53 +0000228 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000229 if (edge_image == (Image *) NULL)
230 {
231 blur_image=DestroyImage(blur_image);
232 return((Image *) NULL);
233 }
cristy051718b2011-08-28 22:49:25 +0000234 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristyd89705a2012-01-20 02:52:24 +0000235 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000236 if (gaussian_image != (Image *) NULL)
237 {
238 edge_image=DestroyImage(edge_image);
239 edge_image=gaussian_image;
240 }
cristy051718b2011-08-28 22:49:25 +0000241 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000242 /*
243 Create a set of kernels from maximum (radius,sigma) to minimum.
244 */
245 width=GetOptimalKernelWidth2D(radius,sigma);
cristy219a63a2012-01-17 12:30:33 +0000246 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000247 if (kernel == (double **) NULL)
248 {
249 edge_image=DestroyImage(edge_image);
250 blur_image=DestroyImage(blur_image);
251 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
252 }
253 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000254 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000255 {
cristyf5d0f3e2012-01-17 03:20:33 +0000256 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
cristy3ed852e2009-09-05 21:47:34 +0000257 sizeof(**kernel));
258 if (kernel[i] == (double *) NULL)
259 break;
cristy47e00502009-12-17 19:19:57 +0000260 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000261 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000262 k=0;
263 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000264 {
cristy47e00502009-12-17 19:19:57 +0000265 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000266 {
cristy4205a3c2010-09-12 20:19:59 +0000267 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
268 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000269 normalize+=kernel[i][k];
270 k++;
cristy3ed852e2009-09-05 21:47:34 +0000271 }
272 }
cristy3ed852e2009-09-05 21:47:34 +0000273 if (fabs(normalize) <= MagickEpsilon)
274 normalize=1.0;
275 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000276 for (k=0; k < (j*j); k++)
277 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000278 }
cristybb503372010-05-27 20:51:26 +0000279 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000280 {
281 for (i-=2; i >= 0; i-=2)
cristyf5d0f3e2012-01-17 03:20:33 +0000282 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000283 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000284 edge_image=DestroyImage(edge_image);
285 blur_image=DestroyImage(blur_image);
286 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
287 }
288 /*
289 Adaptively blur image.
290 */
291 status=MagickTrue;
292 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000293 image_view=AcquireCacheView(image);
294 edge_view=AcquireCacheView(edge_image);
295 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000296#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000297 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000298#endif
cristybb503372010-05-27 20:51:26 +0000299 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000300 {
cristy4c08aed2011-07-01 19:47:50 +0000301 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000302 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000303
cristy4c08aed2011-07-01 19:47:50 +0000304 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000305 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000306
cristy117ff172010-08-15 21:35:32 +0000307 register ssize_t
308 x;
309
cristy3ed852e2009-09-05 21:47:34 +0000310 if (status == MagickFalse)
311 continue;
312 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
313 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
314 exception);
cristyacd2ed22011-08-30 01:44:23 +0000315 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000316 {
317 status=MagickFalse;
318 continue;
319 }
cristybb503372010-05-27 20:51:26 +0000320 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000321 {
cristy4c11c2b2011-09-05 20:17:07 +0000322 register const Quantum
323 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000324
cristybb503372010-05-27 20:51:26 +0000325 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000326 i;
cristy3ed852e2009-09-05 21:47:34 +0000327
cristy4c11c2b2011-09-05 20:17:07 +0000328 ssize_t
329 center,
330 j;
331
332 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000333 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000334 if (j < 0)
335 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000336 else
cristy4c11c2b2011-09-05 20:17:07 +0000337 if (j > (ssize_t) width)
338 j=(ssize_t) width;
339 if ((j & 0x01) != 0)
340 j--;
341 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
342 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000343 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000344 break;
cristyd09f8802012-02-04 16:44:10 +0000345 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
346 GetPixelChannels(image)*((width-j)/2L);
cristy4c11c2b2011-09-05 20:17:07 +0000347 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000348 {
cristy4c11c2b2011-09-05 20:17:07 +0000349 MagickRealType
350 alpha,
351 gamma,
352 pixel;
353
354 PixelChannel
355 channel;
356
357 PixelTrait
358 blur_traits,
359 traits;
360
361 register const double
362 *restrict k;
363
364 register const Quantum
365 *restrict pixels;
366
367 register ssize_t
368 u;
369
370 ssize_t
371 v;
372
cristye2a912b2011-12-05 20:02:07 +0000373 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000374 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000375 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
376 if ((traits == UndefinedPixelTrait) ||
377 (blur_traits == UndefinedPixelTrait))
378 continue;
cristyd09f8802012-02-04 16:44:10 +0000379 if (((blur_traits & CopyPixelTrait) != 0) ||
380 (GetPixelMask(image,q) != 0))
cristy4c11c2b2011-09-05 20:17:07 +0000381 {
cristy0beccfa2011-09-25 20:47:53 +0000382 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000383 continue;
384 }
385 k=kernel[j];
386 pixels=p;
cristyaa2c16c2012-03-25 22:21:35 +0000387 pixel=0.0;
cristy4c11c2b2011-09-05 20:17:07 +0000388 gamma=0.0;
389 if ((blur_traits & BlendPixelTrait) == 0)
390 {
391 /*
392 No alpha blending.
393 */
394 for (v=0; v < (ssize_t) (width-j); v++)
395 {
396 for (u=0; u < (ssize_t) (width-j); u++)
397 {
398 pixel+=(*k)*pixels[i];
399 gamma+=(*k);
400 k++;
401 pixels+=GetPixelChannels(image);
402 }
403 }
404 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000405 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000406 continue;
407 }
408 /*
409 Alpha blending.
410 */
411 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000412 {
cristy4c11c2b2011-09-05 20:17:07 +0000413 for (u=0; u < (ssize_t) (width-j); u++)
414 {
415 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
416 pixel+=(*k)*alpha*pixels[i];
417 gamma+=(*k)*alpha;
418 k++;
419 pixels+=GetPixelChannels(image);
420 }
cristy3ed852e2009-09-05 21:47:34 +0000421 }
cristy4c11c2b2011-09-05 20:17:07 +0000422 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000423 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000424 }
cristyed231572011-07-14 02:18:59 +0000425 q+=GetPixelChannels(blur_image);
426 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000427 }
428 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
429 status=MagickFalse;
430 if (image->progress_monitor != (MagickProgressMonitor) NULL)
431 {
432 MagickBooleanType
433 proceed;
434
cristyb5d5f722009-11-04 03:03:49 +0000435#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +0000436 #pragma omp critical (MagickCore_AdaptiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +0000437#endif
438 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
439 image->rows);
440 if (proceed == MagickFalse)
441 status=MagickFalse;
442 }
443 }
444 blur_image->type=image->type;
445 blur_view=DestroyCacheView(blur_view);
446 edge_view=DestroyCacheView(edge_view);
447 image_view=DestroyCacheView(image_view);
448 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000449 for (i=0; i < (ssize_t) width; i+=2)
cristyf5d0f3e2012-01-17 03:20:33 +0000450 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000451 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000452 if (status == MagickFalse)
453 blur_image=DestroyImage(blur_image);
454 return(blur_image);
455}
456
457/*
458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459% %
460% %
461% %
462% A d a p t i v e S h a r p e n I m a g e %
463% %
464% %
465% %
466%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467%
468% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
469% intensely near image edges and less intensely far from edges. We sharpen the
470% image with a Gaussian operator of the given radius and standard deviation
471% (sigma). For reasonable results, radius should be larger than sigma. Use a
472% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
473%
474% The format of the AdaptiveSharpenImage method is:
475%
476% Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000477% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000478%
479% A description of each parameter follows:
480%
481% o image: the image.
482%
cristy3ed852e2009-09-05 21:47:34 +0000483% o radius: the radius of the Gaussian, in pixels, not counting the center
484% pixel.
485%
486% o sigma: the standard deviation of the Laplacian, in pixels.
487%
488% o exception: return any errors or warnings in this structure.
489%
490*/
cristy3ed852e2009-09-05 21:47:34 +0000491MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000492 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000493{
cristy3ed852e2009-09-05 21:47:34 +0000494#define AdaptiveSharpenImageTag "Convolve/Image"
495#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
496
cristyc4c8d132010-01-07 01:58:38 +0000497 CacheView
498 *sharp_view,
499 *edge_view,
500 *image_view;
501
cristy3ed852e2009-09-05 21:47:34 +0000502 double
cristy47e00502009-12-17 19:19:57 +0000503 **kernel,
504 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000505
506 Image
507 *sharp_image,
508 *edge_image,
509 *gaussian_image;
510
cristy3ed852e2009-09-05 21:47:34 +0000511 MagickBooleanType
512 status;
513
cristybb503372010-05-27 20:51:26 +0000514 MagickOffsetType
515 progress;
516
cristybb503372010-05-27 20:51:26 +0000517 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000518 i;
cristy3ed852e2009-09-05 21:47:34 +0000519
cristybb503372010-05-27 20:51:26 +0000520 size_t
cristy3ed852e2009-09-05 21:47:34 +0000521 width;
522
cristybb503372010-05-27 20:51:26 +0000523 ssize_t
524 j,
525 k,
526 u,
527 v,
528 y;
529
cristy3ed852e2009-09-05 21:47:34 +0000530 assert(image != (const Image *) NULL);
531 assert(image->signature == MagickSignature);
532 if (image->debug != MagickFalse)
533 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
534 assert(exception != (ExceptionInfo *) NULL);
535 assert(exception->signature == MagickSignature);
536 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
537 if (sharp_image == (Image *) NULL)
538 return((Image *) NULL);
539 if (fabs(sigma) <= MagickEpsilon)
540 return(sharp_image);
cristy574cc262011-08-05 01:23:58 +0000541 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000542 {
cristy3ed852e2009-09-05 21:47:34 +0000543 sharp_image=DestroyImage(sharp_image);
544 return((Image *) NULL);
545 }
546 /*
547 Edge detect the image brighness channel, level, sharp, and level again.
548 */
cristy8ae632d2011-09-05 17:29:53 +0000549 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000550 if (edge_image == (Image *) NULL)
551 {
552 sharp_image=DestroyImage(sharp_image);
553 return((Image *) NULL);
554 }
cristy051718b2011-08-28 22:49:25 +0000555 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristyd89705a2012-01-20 02:52:24 +0000556 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000557 if (gaussian_image != (Image *) NULL)
558 {
559 edge_image=DestroyImage(edge_image);
560 edge_image=gaussian_image;
561 }
cristy051718b2011-08-28 22:49:25 +0000562 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000563 /*
564 Create a set of kernels from maximum (radius,sigma) to minimum.
565 */
566 width=GetOptimalKernelWidth2D(radius,sigma);
cristy219a63a2012-01-17 12:30:33 +0000567 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000568 if (kernel == (double **) NULL)
569 {
570 edge_image=DestroyImage(edge_image);
571 sharp_image=DestroyImage(sharp_image);
572 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
573 }
574 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000575 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000576 {
cristy59d1f6d2012-01-17 03:17:40 +0000577 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
cristy3ed852e2009-09-05 21:47:34 +0000578 sizeof(**kernel));
579 if (kernel[i] == (double *) NULL)
580 break;
cristy47e00502009-12-17 19:19:57 +0000581 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000582 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000583 k=0;
584 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000585 {
cristy47e00502009-12-17 19:19:57 +0000586 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000587 {
cristy4205a3c2010-09-12 20:19:59 +0000588 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
589 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000590 normalize+=kernel[i][k];
591 k++;
cristy3ed852e2009-09-05 21:47:34 +0000592 }
593 }
cristy3ed852e2009-09-05 21:47:34 +0000594 if (fabs(normalize) <= MagickEpsilon)
595 normalize=1.0;
596 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000597 for (k=0; k < (j*j); k++)
598 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000599 }
cristybb503372010-05-27 20:51:26 +0000600 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000601 {
602 for (i-=2; i >= 0; i-=2)
cristy59d1f6d2012-01-17 03:17:40 +0000603 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000604 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000605 edge_image=DestroyImage(edge_image);
606 sharp_image=DestroyImage(sharp_image);
607 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
608 }
609 /*
610 Adaptively sharpen image.
611 */
612 status=MagickTrue;
613 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000614 image_view=AcquireCacheView(image);
615 edge_view=AcquireCacheView(edge_image);
616 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000617#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000618 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000619#endif
cristybb503372010-05-27 20:51:26 +0000620 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000621 {
cristy4c08aed2011-07-01 19:47:50 +0000622 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000623 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000624
cristy4c08aed2011-07-01 19:47:50 +0000625 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000626 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000627
cristy117ff172010-08-15 21:35:32 +0000628 register ssize_t
629 x;
630
cristy3ed852e2009-09-05 21:47:34 +0000631 if (status == MagickFalse)
632 continue;
633 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
634 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
635 exception);
cristy4c08aed2011-07-01 19:47:50 +0000636 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000637 {
638 status=MagickFalse;
639 continue;
640 }
cristybb503372010-05-27 20:51:26 +0000641 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000642 {
cristy4c11c2b2011-09-05 20:17:07 +0000643 register const Quantum
644 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000645
cristybb503372010-05-27 20:51:26 +0000646 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000647 i;
cristy3ed852e2009-09-05 21:47:34 +0000648
cristy4c11c2b2011-09-05 20:17:07 +0000649 ssize_t
650 center,
651 j;
652
653 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000654 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000655 if (j < 0)
656 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000657 else
cristy4c11c2b2011-09-05 20:17:07 +0000658 if (j > (ssize_t) width)
659 j=(ssize_t) width;
660 if ((j & 0x01) != 0)
661 j--;
662 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
663 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000664 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000665 break;
cristyd09f8802012-02-04 16:44:10 +0000666 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
667 GetPixelChannels(image)*((width-j)/2);
cristyc94ba6f2012-01-29 23:19:58 +0000668 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000669 {
cristy4c11c2b2011-09-05 20:17:07 +0000670 MagickRealType
671 alpha,
672 gamma,
673 pixel;
674
675 PixelChannel
676 channel;
677
678 PixelTrait
679 sharp_traits,
680 traits;
681
682 register const double
683 *restrict k;
684
685 register const Quantum
686 *restrict pixels;
687
688 register ssize_t
689 u;
690
691 ssize_t
692 v;
693
cristye2a912b2011-12-05 20:02:07 +0000694 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000695 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000696 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
697 if ((traits == UndefinedPixelTrait) ||
698 (sharp_traits == UndefinedPixelTrait))
699 continue;
cristyd09f8802012-02-04 16:44:10 +0000700 if (((sharp_traits & CopyPixelTrait) != 0) ||
701 (GetPixelMask(image,q) != 0))
cristy4c11c2b2011-09-05 20:17:07 +0000702 {
cristy0beccfa2011-09-25 20:47:53 +0000703 SetPixelChannel(sharp_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000704 continue;
705 }
706 k=kernel[j];
707 pixels=p;
cristyaa2c16c2012-03-25 22:21:35 +0000708 pixel=0.0;
cristy4c11c2b2011-09-05 20:17:07 +0000709 gamma=0.0;
710 if ((sharp_traits & BlendPixelTrait) == 0)
711 {
712 /*
713 No alpha blending.
714 */
715 for (v=0; v < (ssize_t) (width-j); v++)
716 {
717 for (u=0; u < (ssize_t) (width-j); u++)
718 {
719 pixel+=(*k)*pixels[i];
720 gamma+=(*k);
721 k++;
722 pixels+=GetPixelChannels(image);
723 }
724 }
725 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000726 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000727 continue;
728 }
729 /*
730 Alpha blending.
731 */
732 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000733 {
cristy4c11c2b2011-09-05 20:17:07 +0000734 for (u=0; u < (ssize_t) (width-j); u++)
735 {
736 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
737 pixel+=(*k)*alpha*pixels[i];
738 gamma+=(*k)*alpha;
739 k++;
740 pixels+=GetPixelChannels(image);
741 }
cristy3ed852e2009-09-05 21:47:34 +0000742 }
cristy4c11c2b2011-09-05 20:17:07 +0000743 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000744 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000745 }
cristyed231572011-07-14 02:18:59 +0000746 q+=GetPixelChannels(sharp_image);
747 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000748 }
749 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
750 status=MagickFalse;
751 if (image->progress_monitor != (MagickProgressMonitor) NULL)
752 {
753 MagickBooleanType
754 proceed;
755
cristyb5d5f722009-11-04 03:03:49 +0000756#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +0000757 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000758#endif
759 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
760 image->rows);
761 if (proceed == MagickFalse)
762 status=MagickFalse;
763 }
764 }
765 sharp_image->type=image->type;
766 sharp_view=DestroyCacheView(sharp_view);
767 edge_view=DestroyCacheView(edge_view);
768 image_view=DestroyCacheView(image_view);
769 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000770 for (i=0; i < (ssize_t) width; i+=2)
cristy59d1f6d2012-01-17 03:17:40 +0000771 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000772 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000773 if (status == MagickFalse)
774 sharp_image=DestroyImage(sharp_image);
775 return(sharp_image);
776}
777
778/*
779%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780% %
781% %
782% %
783% B l u r I m a g e %
784% %
785% %
786% %
787%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788%
789% BlurImage() blurs an image. We convolve the image with a Gaussian operator
790% of the given radius and standard deviation (sigma). For reasonable results,
791% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
792% selects a suitable radius for you.
793%
794% BlurImage() differs from GaussianBlurImage() in that it uses a separable
795% kernel which is faster but mathematically equivalent to the non-separable
796% kernel.
797%
798% The format of the BlurImage method is:
799%
800% Image *BlurImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000801% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000802%
803% A description of each parameter follows:
804%
805% o image: the image.
806%
cristy3ed852e2009-09-05 21:47:34 +0000807% o radius: the radius of the Gaussian, in pixels, not counting the center
808% pixel.
809%
810% o sigma: the standard deviation of the Gaussian, in pixels.
811%
812% o exception: return any errors or warnings in this structure.
813%
814*/
815
cristybb503372010-05-27 20:51:26 +0000816static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000817{
cristy3ed852e2009-09-05 21:47:34 +0000818 double
cristy47e00502009-12-17 19:19:57 +0000819 *kernel,
820 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000821
cristy117ff172010-08-15 21:35:32 +0000822 register ssize_t
823 i;
824
cristybb503372010-05-27 20:51:26 +0000825 ssize_t
cristy47e00502009-12-17 19:19:57 +0000826 j,
827 k;
cristy3ed852e2009-09-05 21:47:34 +0000828
cristy3ed852e2009-09-05 21:47:34 +0000829 /*
830 Generate a 1-D convolution kernel.
831 */
832 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +0000833 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000834 if (kernel == (double *) NULL)
835 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000836 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000837 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000838 i=0;
839 for (k=(-j); k <= j; k++)
840 {
cristy4205a3c2010-09-12 20:19:59 +0000841 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
842 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000843 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000844 i++;
845 }
cristybb503372010-05-27 20:51:26 +0000846 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000847 kernel[i]/=normalize;
848 return(kernel);
849}
850
cristyf4ad9df2011-07-08 16:49:03 +0000851MagickExport Image *BlurImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000852 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000853{
854#define BlurImageTag "Blur/Image"
855
cristyc4c8d132010-01-07 01:58:38 +0000856 CacheView
857 *blur_view,
858 *image_view;
859
cristy3ed852e2009-09-05 21:47:34 +0000860 double
861 *kernel;
862
863 Image
864 *blur_image;
865
cristy3ed852e2009-09-05 21:47:34 +0000866 MagickBooleanType
867 status;
868
cristybb503372010-05-27 20:51:26 +0000869 MagickOffsetType
870 progress;
871
cristybb503372010-05-27 20:51:26 +0000872 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000873 i;
874
cristybb503372010-05-27 20:51:26 +0000875 size_t
cristy3ed852e2009-09-05 21:47:34 +0000876 width;
877
cristybb503372010-05-27 20:51:26 +0000878 ssize_t
cristyb41a1172011-09-06 00:55:14 +0000879 center,
cristybb503372010-05-27 20:51:26 +0000880 x,
881 y;
882
cristy3ed852e2009-09-05 21:47:34 +0000883 /*
884 Initialize blur image attributes.
885 */
886 assert(image != (Image *) NULL);
887 assert(image->signature == MagickSignature);
888 if (image->debug != MagickFalse)
889 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
890 assert(exception != (ExceptionInfo *) NULL);
891 assert(exception->signature == MagickSignature);
cristyd25c77e2011-09-06 00:10:24 +0000892 blur_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000893 if (blur_image == (Image *) NULL)
894 return((Image *) NULL);
895 if (fabs(sigma) <= MagickEpsilon)
896 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000897 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000898 {
cristy3ed852e2009-09-05 21:47:34 +0000899 blur_image=DestroyImage(blur_image);
900 return((Image *) NULL);
901 }
902 width=GetOptimalKernelWidth1D(radius,sigma);
903 kernel=GetBlurKernel(width,sigma);
904 if (kernel == (double *) NULL)
905 {
906 blur_image=DestroyImage(blur_image);
907 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
908 }
909 if (image->debug != MagickFalse)
910 {
911 char
912 format[MaxTextExtent],
913 *message;
914
915 register const double
916 *k;
917
918 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristyef1f9072012-01-20 14:43:06 +0000919 " blur image with kernel width %.20g:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000920 message=AcquireString("");
921 k=kernel;
cristybb503372010-05-27 20:51:26 +0000922 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000923 {
924 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000925 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000926 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000927 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000928 (void) ConcatenateString(&message,format);
929 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
930 }
931 message=DestroyString(message);
932 }
933 /*
934 Blur rows.
935 */
936 status=MagickTrue;
937 progress=0;
cristyb41a1172011-09-06 00:55:14 +0000938 center=(ssize_t) GetPixelChannels(image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +0000939 image_view=AcquireCacheView(image);
940 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000941#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000942 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000943#endif
cristyb41a1172011-09-06 00:55:14 +0000944 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000945 {
cristy4c08aed2011-07-01 19:47:50 +0000946 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000947 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000948
cristy4c08aed2011-07-01 19:47:50 +0000949 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000950 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000951
cristy117ff172010-08-15 21:35:32 +0000952 register ssize_t
953 x;
954
cristy3ed852e2009-09-05 21:47:34 +0000955 if (status == MagickFalse)
956 continue;
cristy117ff172010-08-15 21:35:32 +0000957 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
958 image->columns+width,1,exception);
cristy8b49f382012-01-17 03:11:03 +0000959 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000960 exception);
cristy4c08aed2011-07-01 19:47:50 +0000961 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000962 {
963 status=MagickFalse;
964 continue;
965 }
cristyb41a1172011-09-06 00:55:14 +0000966 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000967 {
cristybb503372010-05-27 20:51:26 +0000968 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000969 i;
970
cristyb41a1172011-09-06 00:55:14 +0000971 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
972 {
973 MagickRealType
974 alpha,
975 gamma,
976 pixel;
cristyd25c77e2011-09-06 00:10:24 +0000977
cristyb41a1172011-09-06 00:55:14 +0000978 PixelChannel
979 channel;
980
981 PixelTrait
982 blur_traits,
983 traits;
984
985 register const double
986 *restrict k;
987
988 register const Quantum
989 *restrict pixels;
990
991 register ssize_t
992 u;
993
cristye2a912b2011-12-05 20:02:07 +0000994 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000995 traits=GetPixelChannelMapTraits(image,channel);
cristyb41a1172011-09-06 00:55:14 +0000996 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
997 if ((traits == UndefinedPixelTrait) ||
998 (blur_traits == UndefinedPixelTrait))
999 continue;
cristyd09f8802012-02-04 16:44:10 +00001000 if (((blur_traits & CopyPixelTrait) != 0) ||
1001 (GetPixelMask(image,p) != 0))
cristyd25c77e2011-09-06 00:10:24 +00001002 {
cristy0beccfa2011-09-25 20:47:53 +00001003 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001004 continue;
cristyd25c77e2011-09-06 00:10:24 +00001005 }
cristyb41a1172011-09-06 00:55:14 +00001006 k=kernel;
1007 pixels=p;
1008 pixel=0.0;
1009 if ((blur_traits & BlendPixelTrait) == 0)
1010 {
1011 /*
1012 No alpha blending.
1013 */
1014 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001015 {
cristyb41a1172011-09-06 00:55:14 +00001016 pixel+=(*k)*pixels[i];
1017 k++;
1018 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001019 }
cristy0beccfa2011-09-25 20:47:53 +00001020 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001021 continue;
1022 }
1023 /*
1024 Alpha blending.
1025 */
1026 gamma=0.0;
1027 for (u=0; u < (ssize_t) width; u++)
1028 {
1029 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1030 pixel+=(*k)*alpha*pixels[i];
1031 gamma+=(*k)*alpha;
1032 k++;
1033 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001034 }
cristyb41a1172011-09-06 00:55:14 +00001035 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001036 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001037 }
cristyed231572011-07-14 02:18:59 +00001038 p+=GetPixelChannels(image);
1039 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001040 }
1041 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1042 status=MagickFalse;
1043 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1044 {
1045 MagickBooleanType
1046 proceed;
1047
cristyb5d5f722009-11-04 03:03:49 +00001048#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyef1f9072012-01-20 14:43:06 +00001049 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001050#endif
1051 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1052 blur_image->columns);
1053 if (proceed == MagickFalse)
1054 status=MagickFalse;
1055 }
1056 }
1057 blur_view=DestroyCacheView(blur_view);
1058 image_view=DestroyCacheView(image_view);
1059 /*
1060 Blur columns.
1061 */
cristyef1f9072012-01-20 14:43:06 +00001062 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +00001063 image_view=AcquireCacheView(blur_image);
1064 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001065#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001066 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001067#endif
cristyb6dc4b72011-12-10 23:29:53 +00001068 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001069 {
cristy4c08aed2011-07-01 19:47:50 +00001070 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001071 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001072
cristy4c08aed2011-07-01 19:47:50 +00001073 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001074 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001075
cristy117ff172010-08-15 21:35:32 +00001076 register ssize_t
1077 y;
1078
cristy3ed852e2009-09-05 21:47:34 +00001079 if (status == MagickFalse)
1080 continue;
cristy117ff172010-08-15 21:35:32 +00001081 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
cristyb6dc4b72011-12-10 23:29:53 +00001082 blur_image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001083 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001084 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001085 {
1086 status=MagickFalse;
1087 continue;
1088 }
cristyb6dc4b72011-12-10 23:29:53 +00001089 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001090 {
cristybb503372010-05-27 20:51:26 +00001091 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001092 i;
1093
cristyb6dc4b72011-12-10 23:29:53 +00001094 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
cristyb41a1172011-09-06 00:55:14 +00001095 {
1096 MagickRealType
1097 alpha,
1098 gamma,
1099 pixel;
cristyd25c77e2011-09-06 00:10:24 +00001100
cristyb41a1172011-09-06 00:55:14 +00001101 PixelChannel
1102 channel;
1103
1104 PixelTrait
1105 blur_traits,
1106 traits;
1107
1108 register const double
1109 *restrict k;
1110
1111 register const Quantum
1112 *restrict pixels;
1113
1114 register ssize_t
1115 u;
1116
cristye2a912b2011-12-05 20:02:07 +00001117 channel=GetPixelChannelMapChannel(blur_image,i);
cristyabace412011-12-11 15:56:53 +00001118 traits=GetPixelChannelMapTraits(blur_image,channel);
cristyb41a1172011-09-06 00:55:14 +00001119 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1120 if ((traits == UndefinedPixelTrait) ||
1121 (blur_traits == UndefinedPixelTrait))
1122 continue;
cristyd09f8802012-02-04 16:44:10 +00001123 if (((blur_traits & CopyPixelTrait) != 0) ||
1124 (GetPixelMask(blur_image,p) != 0))
cristyd25c77e2011-09-06 00:10:24 +00001125 {
cristy0beccfa2011-09-25 20:47:53 +00001126 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001127 continue;
cristyd25c77e2011-09-06 00:10:24 +00001128 }
cristyb41a1172011-09-06 00:55:14 +00001129 k=kernel;
1130 pixels=p;
1131 pixel=0.0;
1132 if ((blur_traits & BlendPixelTrait) == 0)
1133 {
1134 /*
1135 No alpha blending.
1136 */
1137 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001138 {
cristyb41a1172011-09-06 00:55:14 +00001139 pixel+=(*k)*pixels[i];
1140 k++;
1141 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001142 }
cristy0beccfa2011-09-25 20:47:53 +00001143 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001144 continue;
1145 }
1146 /*
1147 Alpha blending.
1148 */
1149 gamma=0.0;
1150 for (u=0; u < (ssize_t) width; u++)
1151 {
cristyb6dc4b72011-12-10 23:29:53 +00001152 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1153 pixels));
cristyb41a1172011-09-06 00:55:14 +00001154 pixel+=(*k)*alpha*pixels[i];
1155 gamma+=(*k)*alpha;
1156 k++;
1157 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001158 }
cristyb41a1172011-09-06 00:55:14 +00001159 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001160 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001161 }
cristyd25c77e2011-09-06 00:10:24 +00001162 p+=GetPixelChannels(blur_image);
cristyed231572011-07-14 02:18:59 +00001163 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001164 }
1165 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1166 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001167 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001168 {
1169 MagickBooleanType
1170 proceed;
1171
cristyb5d5f722009-11-04 03:03:49 +00001172#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyef1f9072012-01-20 14:43:06 +00001173 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001174#endif
cristy4c08aed2011-07-01 19:47:50 +00001175 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1176 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001177 if (proceed == MagickFalse)
1178 status=MagickFalse;
1179 }
1180 }
1181 blur_view=DestroyCacheView(blur_view);
1182 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00001183 kernel=(double *) RelinquishAlignedMemory(kernel);
1184 blur_image->type=image->type;
cristy3ed852e2009-09-05 21:47:34 +00001185 if (status == MagickFalse)
1186 blur_image=DestroyImage(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001187 return(blur_image);
1188}
1189
1190/*
1191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1192% %
1193% %
1194% %
cristyfccdab92009-11-30 16:43:57 +00001195% C o n v o l v e I m a g e %
1196% %
1197% %
1198% %
1199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200%
1201% ConvolveImage() applies a custom convolution kernel to the image.
1202%
1203% The format of the ConvolveImage method is:
1204%
cristy5e6be1e2011-07-16 01:23:39 +00001205% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1206% ExceptionInfo *exception)
1207%
cristyfccdab92009-11-30 16:43:57 +00001208% A description of each parameter follows:
1209%
1210% o image: the image.
1211%
cristy5e6be1e2011-07-16 01:23:39 +00001212% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001213%
1214% o exception: return any errors or warnings in this structure.
1215%
1216*/
cristy5e6be1e2011-07-16 01:23:39 +00001217MagickExport Image *ConvolveImage(const Image *image,
1218 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001219{
cristy1d326872012-03-07 22:21:31 +00001220 return(MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception));
cristyfccdab92009-11-30 16:43:57 +00001221}
1222
1223/*
1224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1225% %
1226% %
1227% %
cristy3ed852e2009-09-05 21:47:34 +00001228% D e s p e c k l e I m a g e %
1229% %
1230% %
1231% %
1232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233%
1234% DespeckleImage() reduces the speckle noise in an image while perserving the
cristyf0ae7762012-01-15 22:04:03 +00001235% edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1236% neighbors, then complementarily lowering pixels that are brighter than their
1237% surrounding neighbors) to reduce the speckle index of that image (reference
1238% Crimmins speckle removal).
cristy3ed852e2009-09-05 21:47:34 +00001239%
1240% The format of the DespeckleImage method is:
1241%
1242% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1243%
1244% A description of each parameter follows:
1245%
1246% o image: the image.
1247%
1248% o exception: return any errors or warnings in this structure.
1249%
1250*/
1251
cristy5473bf02012-01-16 19:15:08 +00001252static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1253 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1254 Quantum *restrict g)
cristy3ed852e2009-09-05 21:47:34 +00001255{
cristy7b4b8862012-01-16 19:52:19 +00001256 register Quantum
cristy5473bf02012-01-16 19:15:08 +00001257 *p,
1258 *q,
1259 *r,
1260 *s;
cristy78f3de22012-01-16 00:43:18 +00001261
cristy5473bf02012-01-16 19:15:08 +00001262 ssize_t
1263 y;
1264
1265 assert(f != (Quantum *) NULL);
1266 assert(g != (Quantum *) NULL);
1267 p=f+(columns+2);
1268 q=g+(columns+2);
1269 r=p+(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001270#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8de86be2012-01-20 02:05:06 +00001271 #pragma omp parallel for schedule(static)
cristy5473bf02012-01-16 19:15:08 +00001272#endif
1273 for (y=0; y < (ssize_t) rows; y++)
1274 {
cristy7b4b8862012-01-16 19:52:19 +00001275 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001276 i,
1277 x;
1278
cristy7b4b8862012-01-16 19:52:19 +00001279 SignedQuantum
1280 v;
1281
cristy5473bf02012-01-16 19:15:08 +00001282 i=(2*y+1)+y*columns;
1283 if (polarity > 0)
1284 for (x=0; x < (ssize_t) columns; x++)
1285 {
1286 v=(SignedQuantum) p[i];
1287 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1288 v+=ScaleCharToQuantum(1);
1289 q[i]=(Quantum) v;
1290 i++;
1291 }
1292 else
1293 for (x=0; x < (ssize_t) columns; x++)
1294 {
1295 v=(SignedQuantum) p[i];
1296 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1297 v-=ScaleCharToQuantum(1);
1298 q[i]=(Quantum) v;
1299 i++;
1300 }
1301 }
1302 p=f+(columns+2);
1303 q=g+(columns+2);
1304 r=q+(y_offset*(columns+2)+x_offset);
1305 s=q-(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001306#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8de86be2012-01-20 02:05:06 +00001307 #pragma omp parallel for schedule(static)
cristy5473bf02012-01-16 19:15:08 +00001308#endif
1309 for (y=0; y < (ssize_t) rows; y++)
1310 {
cristy7b4b8862012-01-16 19:52:19 +00001311 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001312 i,
1313 x;
1314
cristy7b4b8862012-01-16 19:52:19 +00001315 SignedQuantum
1316 v;
1317
cristy5473bf02012-01-16 19:15:08 +00001318 i=(2*y+1)+y*columns;
1319 if (polarity > 0)
1320 for (x=0; x < (ssize_t) columns; x++)
1321 {
1322 v=(SignedQuantum) q[i];
1323 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1324 ((SignedQuantum) r[i] > v))
1325 v+=ScaleCharToQuantum(1);
1326 p[i]=(Quantum) v;
1327 i++;
1328 }
1329 else
1330 for (x=0; x < (ssize_t) columns; x++)
1331 {
1332 v=(SignedQuantum) q[i];
1333 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1334 ((SignedQuantum) r[i] < v))
1335 v-=ScaleCharToQuantum(1);
1336 p[i]=(Quantum) v;
1337 i++;
1338 }
1339 }
cristy3ed852e2009-09-05 21:47:34 +00001340}
1341
1342MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1343{
1344#define DespeckleImageTag "Despeckle/Image"
1345
cristy2407fc22009-09-11 00:55:25 +00001346 CacheView
1347 *despeckle_view,
1348 *image_view;
1349
cristy3ed852e2009-09-05 21:47:34 +00001350 Image
1351 *despeckle_image;
1352
cristy3ed852e2009-09-05 21:47:34 +00001353 MagickBooleanType
1354 status;
1355
cristy5473bf02012-01-16 19:15:08 +00001356 Quantum
1357 *restrict buffer,
1358 *restrict pixels;
cristya63e4a92011-09-09 00:47:59 +00001359
cristy5473bf02012-01-16 19:15:08 +00001360 register ssize_t
1361 i;
1362
1363 size_t
1364 length;
cristy117ff172010-08-15 21:35:32 +00001365
cristybb503372010-05-27 20:51:26 +00001366 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001367 X[4] = {0, 1, 1,-1},
1368 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001369
cristy3ed852e2009-09-05 21:47:34 +00001370 /*
1371 Allocate despeckled image.
1372 */
1373 assert(image != (const Image *) NULL);
1374 assert(image->signature == MagickSignature);
1375 if (image->debug != MagickFalse)
1376 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1377 assert(exception != (ExceptionInfo *) NULL);
1378 assert(exception->signature == MagickSignature);
cristy5473bf02012-01-16 19:15:08 +00001379 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001380 if (despeckle_image == (Image *) NULL)
1381 return((Image *) NULL);
cristya63e4a92011-09-09 00:47:59 +00001382 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1383 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001384 {
cristy3ed852e2009-09-05 21:47:34 +00001385 despeckle_image=DestroyImage(despeckle_image);
1386 return((Image *) NULL);
1387 }
1388 /*
cristy5473bf02012-01-16 19:15:08 +00001389 Allocate image buffer.
1390 */
1391 length=(size_t) ((image->columns+2)*(image->rows+2));
1392 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1393 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1394 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1395 {
1396 if (buffer != (Quantum *) NULL)
1397 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1398 if (pixels != (Quantum *) NULL)
1399 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1400 despeckle_image=DestroyImage(despeckle_image);
1401 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1402 }
1403 /*
1404 Reduce speckle in the image.
cristy3ed852e2009-09-05 21:47:34 +00001405 */
1406 status=MagickTrue;
cristyb06da092012-01-16 17:11:38 +00001407 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00001408 despeckle_view=AcquireCacheView(despeckle_image);
cristy5473bf02012-01-16 19:15:08 +00001409 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +00001410 {
cristy5473bf02012-01-16 19:15:08 +00001411 PixelChannel
1412 channel;
1413
1414 PixelTrait
1415 despeckle_traits,
1416 traits;
cristy3ed852e2009-09-05 21:47:34 +00001417
cristyc1488b52011-02-19 18:54:15 +00001418 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001419 k,
cristyf0ae7762012-01-15 22:04:03 +00001420 x;
cristyc1488b52011-02-19 18:54:15 +00001421
cristy5473bf02012-01-16 19:15:08 +00001422 ssize_t
1423 j,
1424 y;
1425
1426 if (status == MagickFalse)
1427 continue;
1428 channel=GetPixelChannelMapChannel(image,i);
1429 traits=GetPixelChannelMapTraits(image,channel);
1430 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1431 if ((traits == UndefinedPixelTrait) ||
1432 (despeckle_traits == UndefinedPixelTrait))
1433 continue;
1434 if ((despeckle_traits & CopyPixelTrait) != 0)
1435 continue;
1436 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1437 j=(ssize_t) image->columns+2;
1438 for (y=0; y < (ssize_t) image->rows; y++)
cristyfc830f42012-01-15 02:45:06 +00001439 {
cristy5473bf02012-01-16 19:15:08 +00001440 register const Quantum
1441 *restrict p;
cristyfc830f42012-01-15 02:45:06 +00001442
cristy5473bf02012-01-16 19:15:08 +00001443 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1444 if (p == (const Quantum *) NULL)
cristyf0ae7762012-01-15 22:04:03 +00001445 {
cristy5473bf02012-01-16 19:15:08 +00001446 status=MagickFalse;
1447 continue;
cristyf0ae7762012-01-15 22:04:03 +00001448 }
cristy5473bf02012-01-16 19:15:08 +00001449 j++;
1450 for (x=0; x < (ssize_t) image->columns; x++)
1451 {
1452 pixels[j++]=p[i];
1453 p+=GetPixelChannels(image);
cristyfc830f42012-01-15 02:45:06 +00001454 }
cristy5473bf02012-01-16 19:15:08 +00001455 j++;
cristy3ed852e2009-09-05 21:47:34 +00001456 }
cristy5473bf02012-01-16 19:15:08 +00001457 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1458 for (k=0; k < 4; k++)
1459 {
1460 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1461 Hull(-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1462 Hull(-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1463 Hull(X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1464 }
1465 j=(ssize_t) image->columns+2;
1466 for (y=0; y < (ssize_t) image->rows; y++)
1467 {
1468 MagickBooleanType
1469 sync;
1470
1471 register Quantum
1472 *restrict q;
1473
cristyd13e8eb2012-01-17 02:36:42 +00001474 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1475 despeckle_image->columns,1,exception);
cristy5473bf02012-01-16 19:15:08 +00001476 if (q == (Quantum *) NULL)
1477 {
1478 status=MagickFalse;
1479 continue;
1480 }
1481 j++;
1482 for (x=0; x < (ssize_t) image->columns; x++)
1483 {
1484 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1485 q+=GetPixelChannels(despeckle_image);
1486 }
1487 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1488 if (sync == MagickFalse)
1489 status=MagickFalse;
1490 j++;
1491 }
cristy3ed852e2009-09-05 21:47:34 +00001492 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1493 {
1494 MagickBooleanType
1495 proceed;
1496
cristy5473bf02012-01-16 19:15:08 +00001497 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1498 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00001499 if (proceed == MagickFalse)
1500 status=MagickFalse;
1501 }
1502 }
1503 despeckle_view=DestroyCacheView(despeckle_view);
1504 image_view=DestroyCacheView(image_view);
cristy5473bf02012-01-16 19:15:08 +00001505 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1506 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001507 despeckle_image->type=image->type;
1508 if (status == MagickFalse)
1509 despeckle_image=DestroyImage(despeckle_image);
1510 return(despeckle_image);
1511}
1512
1513/*
1514%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1515% %
1516% %
1517% %
1518% E d g e I m a g e %
1519% %
1520% %
1521% %
1522%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1523%
1524% EdgeImage() finds edges in an image. Radius defines the radius of the
1525% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1526% radius for you.
1527%
1528% The format of the EdgeImage method is:
1529%
1530% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001531% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001532%
1533% A description of each parameter follows:
1534%
1535% o image: the image.
1536%
1537% o radius: the radius of the pixel neighborhood.
1538%
cristy8ae632d2011-09-05 17:29:53 +00001539% o sigma: the standard deviation of the Gaussian, in pixels.
1540%
cristy3ed852e2009-09-05 21:47:34 +00001541% o exception: return any errors or warnings in this structure.
1542%
1543*/
1544MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001545 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001546{
1547 Image
1548 *edge_image;
1549
cristy41cbe682011-07-15 19:12:37 +00001550 KernelInfo
1551 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001552
cristybb503372010-05-27 20:51:26 +00001553 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001554 i;
1555
cristybb503372010-05-27 20:51:26 +00001556 size_t
cristy3ed852e2009-09-05 21:47:34 +00001557 width;
1558
cristy41cbe682011-07-15 19:12:37 +00001559 ssize_t
1560 j,
1561 u,
1562 v;
1563
cristy3ed852e2009-09-05 21:47:34 +00001564 assert(image != (const Image *) NULL);
1565 assert(image->signature == MagickSignature);
1566 if (image->debug != MagickFalse)
1567 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1568 assert(exception != (ExceptionInfo *) NULL);
1569 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001570 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001571 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001572 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001573 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001574 kernel_info->width=width;
1575 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001576 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1577 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1578 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001579 {
1580 kernel_info=DestroyKernelInfo(kernel_info);
1581 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1582 }
1583 j=(ssize_t) kernel_info->width/2;
1584 i=0;
1585 for (v=(-j); v <= j; v++)
1586 {
1587 for (u=(-j); u <= j; u++)
1588 {
1589 kernel_info->values[i]=(-1.0);
1590 i++;
1591 }
1592 }
1593 kernel_info->values[i/2]=(double) (width*width-1.0);
cristy5e6be1e2011-07-16 01:23:39 +00001594 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001595 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001596 return(edge_image);
1597}
1598
1599/*
1600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1601% %
1602% %
1603% %
1604% E m b o s s I m a g e %
1605% %
1606% %
1607% %
1608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1609%
1610% EmbossImage() returns a grayscale image with a three-dimensional effect.
1611% We convolve the image with a Gaussian operator of the given radius and
1612% standard deviation (sigma). For reasonable results, radius should be
1613% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1614% radius for you.
1615%
1616% The format of the EmbossImage method is:
1617%
1618% Image *EmbossImage(const Image *image,const double radius,
1619% const double sigma,ExceptionInfo *exception)
1620%
1621% A description of each parameter follows:
1622%
1623% o image: the image.
1624%
1625% o radius: the radius of the pixel neighborhood.
1626%
1627% o sigma: the standard deviation of the Gaussian, in pixels.
1628%
1629% o exception: return any errors or warnings in this structure.
1630%
1631*/
1632MagickExport Image *EmbossImage(const Image *image,const double radius,
1633 const double sigma,ExceptionInfo *exception)
1634{
cristy3ed852e2009-09-05 21:47:34 +00001635 Image
1636 *emboss_image;
1637
cristy41cbe682011-07-15 19:12:37 +00001638 KernelInfo
1639 *kernel_info;
1640
cristybb503372010-05-27 20:51:26 +00001641 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001642 i;
1643
cristybb503372010-05-27 20:51:26 +00001644 size_t
cristy3ed852e2009-09-05 21:47:34 +00001645 width;
1646
cristy117ff172010-08-15 21:35:32 +00001647 ssize_t
1648 j,
1649 k,
1650 u,
1651 v;
1652
cristy41cbe682011-07-15 19:12:37 +00001653 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001654 assert(image->signature == MagickSignature);
1655 if (image->debug != MagickFalse)
1656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1657 assert(exception != (ExceptionInfo *) NULL);
1658 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001659 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001660 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001661 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001662 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001663 kernel_info->width=width;
1664 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001665 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1666 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1667 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001668 {
1669 kernel_info=DestroyKernelInfo(kernel_info);
1670 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1671 }
1672 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001673 k=j;
1674 i=0;
1675 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001676 {
cristy47e00502009-12-17 19:19:57 +00001677 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001678 {
cristy41cbe682011-07-15 19:12:37 +00001679 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001680 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001681 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001682 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001683 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001684 i++;
1685 }
cristy47e00502009-12-17 19:19:57 +00001686 k--;
cristy3ed852e2009-09-05 21:47:34 +00001687 }
cristy5e6be1e2011-07-16 01:23:39 +00001688 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001689 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001690 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001691 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001692 return(emboss_image);
1693}
1694
1695/*
1696%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1697% %
1698% %
1699% %
1700% G a u s s i a n B l u r I m a g e %
1701% %
1702% %
1703% %
1704%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1705%
1706% GaussianBlurImage() blurs an image. We convolve the image with a
1707% Gaussian operator of the given radius and standard deviation (sigma).
1708% For reasonable results, the radius should be larger than sigma. Use a
1709% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1710%
1711% The format of the GaussianBlurImage method is:
1712%
1713% Image *GaussianBlurImage(const Image *image,onst double radius,
cristyd89705a2012-01-20 02:52:24 +00001714% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001715%
1716% A description of each parameter follows:
1717%
1718% o image: the image.
1719%
cristy3ed852e2009-09-05 21:47:34 +00001720% o radius: the radius of the Gaussian, in pixels, not counting the center
1721% pixel.
1722%
1723% o sigma: the standard deviation of the Gaussian, in pixels.
1724%
1725% o exception: return any errors or warnings in this structure.
1726%
1727*/
cristy41cbe682011-07-15 19:12:37 +00001728MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristyd89705a2012-01-20 02:52:24 +00001729 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001730{
cristy3ed852e2009-09-05 21:47:34 +00001731 Image
1732 *blur_image;
1733
cristy41cbe682011-07-15 19:12:37 +00001734 KernelInfo
1735 *kernel_info;
1736
cristybb503372010-05-27 20:51:26 +00001737 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001738 i;
1739
cristybb503372010-05-27 20:51:26 +00001740 size_t
cristy3ed852e2009-09-05 21:47:34 +00001741 width;
1742
cristy117ff172010-08-15 21:35:32 +00001743 ssize_t
1744 j,
1745 u,
1746 v;
1747
cristy3ed852e2009-09-05 21:47:34 +00001748 assert(image != (const Image *) NULL);
1749 assert(image->signature == MagickSignature);
1750 if (image->debug != MagickFalse)
1751 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1752 assert(exception != (ExceptionInfo *) NULL);
1753 assert(exception->signature == MagickSignature);
1754 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001755 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001756 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001757 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001758 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1759 kernel_info->width=width;
1760 kernel_info->height=width;
1761 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00001762 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1763 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1764 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001765 {
1766 kernel_info=DestroyKernelInfo(kernel_info);
1767 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1768 }
1769 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001770 i=0;
cristy47e00502009-12-17 19:19:57 +00001771 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001772 {
cristy47e00502009-12-17 19:19:57 +00001773 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001774 {
1775 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1776 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1777 i++;
1778 }
cristy3ed852e2009-09-05 21:47:34 +00001779 }
cristy5e6be1e2011-07-16 01:23:39 +00001780 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001781 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001782 return(blur_image);
1783}
1784
1785/*
1786%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1787% %
1788% %
1789% %
cristy3ed852e2009-09-05 21:47:34 +00001790% M o t i o n B l u r I m a g e %
1791% %
1792% %
1793% %
1794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1795%
1796% MotionBlurImage() simulates motion blur. We convolve the image with a
1797% Gaussian operator of the given radius and standard deviation (sigma).
1798% For reasonable results, radius should be larger than sigma. Use a
1799% radius of 0 and MotionBlurImage() selects a suitable radius for you.
1800% Angle gives the angle of the blurring motion.
1801%
1802% Andrew Protano contributed this effect.
1803%
1804% The format of the MotionBlurImage method is:
1805%
1806% Image *MotionBlurImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00001807% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001808%
1809% A description of each parameter follows:
1810%
1811% o image: the image.
1812%
cristy3ed852e2009-09-05 21:47:34 +00001813% o radius: the radius of the Gaussian, in pixels, not counting
1814% the center pixel.
1815%
1816% o sigma: the standard deviation of the Gaussian, in pixels.
1817%
cristycee97112010-05-28 00:44:52 +00001818% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00001819%
1820% o exception: return any errors or warnings in this structure.
1821%
1822*/
1823
cristybb503372010-05-27 20:51:26 +00001824static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00001825{
cristy3ed852e2009-09-05 21:47:34 +00001826 double
cristy47e00502009-12-17 19:19:57 +00001827 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00001828 normalize;
1829
cristybb503372010-05-27 20:51:26 +00001830 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001831 i;
1832
1833 /*
cristy47e00502009-12-17 19:19:57 +00001834 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00001835 */
1836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +00001837 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00001838 if (kernel == (double *) NULL)
1839 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00001840 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00001841 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00001842 {
cristy4205a3c2010-09-12 20:19:59 +00001843 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1844 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00001845 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00001846 }
cristybb503372010-05-27 20:51:26 +00001847 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001848 kernel[i]/=normalize;
1849 return(kernel);
1850}
1851
cristya63e4a92011-09-09 00:47:59 +00001852MagickExport Image *MotionBlurImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00001853 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001854{
cristyc4c8d132010-01-07 01:58:38 +00001855 CacheView
1856 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00001857 *image_view,
1858 *motion_view;
cristyc4c8d132010-01-07 01:58:38 +00001859
cristy3ed852e2009-09-05 21:47:34 +00001860 double
1861 *kernel;
1862
1863 Image
1864 *blur_image;
1865
cristy3ed852e2009-09-05 21:47:34 +00001866 MagickBooleanType
1867 status;
1868
cristybb503372010-05-27 20:51:26 +00001869 MagickOffsetType
1870 progress;
1871
cristy3ed852e2009-09-05 21:47:34 +00001872 OffsetInfo
1873 *offset;
1874
1875 PointInfo
1876 point;
1877
cristybb503372010-05-27 20:51:26 +00001878 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001879 i;
1880
cristybb503372010-05-27 20:51:26 +00001881 size_t
cristy3ed852e2009-09-05 21:47:34 +00001882 width;
1883
cristybb503372010-05-27 20:51:26 +00001884 ssize_t
1885 y;
1886
cristy3ed852e2009-09-05 21:47:34 +00001887 assert(image != (Image *) NULL);
1888 assert(image->signature == MagickSignature);
1889 if (image->debug != MagickFalse)
1890 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1891 assert(exception != (ExceptionInfo *) NULL);
1892 width=GetOptimalKernelWidth1D(radius,sigma);
1893 kernel=GetMotionBlurKernel(width,sigma);
1894 if (kernel == (double *) NULL)
1895 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1896 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1897 if (offset == (OffsetInfo *) NULL)
1898 {
cristy8b49f382012-01-17 03:11:03 +00001899 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00001900 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1901 }
cristy1e7aa312011-09-10 20:01:36 +00001902 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001903 if (blur_image == (Image *) NULL)
1904 {
cristy8b49f382012-01-17 03:11:03 +00001905 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00001906 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1907 return((Image *) NULL);
1908 }
cristy574cc262011-08-05 01:23:58 +00001909 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001910 {
cristy8b49f382012-01-17 03:11:03 +00001911 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00001912 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00001913 blur_image=DestroyImage(blur_image);
1914 return((Image *) NULL);
1915 }
1916 point.x=(double) width*sin(DegreesToRadians(angle));
1917 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00001918 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001919 {
cristybb503372010-05-27 20:51:26 +00001920 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1921 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001922 }
1923 /*
1924 Motion blur image.
1925 */
1926 status=MagickTrue;
1927 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001928 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00001929 motion_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00001930 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00001931#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00001932 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001933#endif
cristybb503372010-05-27 20:51:26 +00001934 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001935 {
cristyf7ef0252011-09-09 14:50:06 +00001936 register const Quantum
1937 *restrict p;
1938
cristy4c08aed2011-07-01 19:47:50 +00001939 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001940 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001941
cristy117ff172010-08-15 21:35:32 +00001942 register ssize_t
1943 x;
1944
cristy3ed852e2009-09-05 21:47:34 +00001945 if (status == MagickFalse)
1946 continue;
cristy8b49f382012-01-17 03:11:03 +00001947 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1948 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00001949 exception);
cristyf7ef0252011-09-09 14:50:06 +00001950 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001951 {
1952 status=MagickFalse;
1953 continue;
1954 }
cristybb503372010-05-27 20:51:26 +00001955 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001956 {
cristybb503372010-05-27 20:51:26 +00001957 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001958 i;
1959
cristyf7ef0252011-09-09 14:50:06 +00001960 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1961 {
1962 MagickRealType
1963 alpha,
1964 gamma,
1965 pixel;
cristy3ed852e2009-09-05 21:47:34 +00001966
cristyf7ef0252011-09-09 14:50:06 +00001967 PixelChannel
1968 channel;
1969
1970 PixelTrait
1971 blur_traits,
1972 traits;
1973
1974 register const Quantum
1975 *restrict r;
1976
1977 register double
1978 *restrict k;
1979
1980 register ssize_t
1981 j;
1982
cristye2a912b2011-12-05 20:02:07 +00001983 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001984 traits=GetPixelChannelMapTraits(image,channel);
cristyf7ef0252011-09-09 14:50:06 +00001985 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1986 if ((traits == UndefinedPixelTrait) ||
1987 (blur_traits == UndefinedPixelTrait))
1988 continue;
cristyd09f8802012-02-04 16:44:10 +00001989 if (((blur_traits & CopyPixelTrait) != 0) ||
1990 (GetPixelMask(image,p) != 0))
cristy3ed852e2009-09-05 21:47:34 +00001991 {
cristy0beccfa2011-09-25 20:47:53 +00001992 SetPixelChannel(blur_image,channel,p[i],q);
cristyf7ef0252011-09-09 14:50:06 +00001993 continue;
cristy3ed852e2009-09-05 21:47:34 +00001994 }
cristyf7ef0252011-09-09 14:50:06 +00001995 k=kernel;
cristyaa2c16c2012-03-25 22:21:35 +00001996 pixel=0.0;
cristyf7ef0252011-09-09 14:50:06 +00001997 if ((blur_traits & BlendPixelTrait) == 0)
1998 {
1999 for (j=0; j < (ssize_t) width; j++)
2000 {
cristy8b49f382012-01-17 03:11:03 +00002001 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
cristyf7ef0252011-09-09 14:50:06 +00002002 offset[j].y,1,1,exception);
2003 if (r == (const Quantum *) NULL)
2004 {
2005 status=MagickFalse;
2006 continue;
2007 }
2008 pixel+=(*k)*r[i];
2009 k++;
2010 }
cristy0beccfa2011-09-25 20:47:53 +00002011 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002012 continue;
2013 }
2014 alpha=0.0;
2015 gamma=0.0;
2016 for (j=0; j < (ssize_t) width; j++)
2017 {
cristy8b49f382012-01-17 03:11:03 +00002018 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
cristyf7ef0252011-09-09 14:50:06 +00002019 1,exception);
2020 if (r == (const Quantum *) NULL)
2021 {
2022 status=MagickFalse;
2023 continue;
2024 }
2025 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2026 pixel+=(*k)*alpha*r[i];
2027 gamma+=(*k)*alpha;
2028 k++;
cristy3ed852e2009-09-05 21:47:34 +00002029 }
cristyf7ef0252011-09-09 14:50:06 +00002030 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002031 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002032 }
2033 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002034 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002035 }
2036 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2037 status=MagickFalse;
2038 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2039 {
2040 MagickBooleanType
2041 proceed;
2042
cristyb557a152011-02-22 12:14:30 +00002043#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00002044 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002045#endif
2046 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2047 if (proceed == MagickFalse)
2048 status=MagickFalse;
2049 }
2050 }
2051 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00002052 motion_view=DestroyCacheView(motion_view);
cristy3ed852e2009-09-05 21:47:34 +00002053 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00002054 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002055 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2056 if (status == MagickFalse)
2057 blur_image=DestroyImage(blur_image);
2058 return(blur_image);
2059}
2060
2061/*
2062%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2063% %
2064% %
2065% %
2066% P r e v i e w I m a g e %
2067% %
2068% %
2069% %
2070%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2071%
2072% PreviewImage() tiles 9 thumbnails of the specified image with an image
2073% processing operation applied with varying parameters. This may be helpful
2074% pin-pointing an appropriate parameter for a particular image processing
2075% operation.
2076%
2077% The format of the PreviewImages method is:
2078%
2079% Image *PreviewImages(const Image *image,const PreviewType preview,
2080% ExceptionInfo *exception)
2081%
2082% A description of each parameter follows:
2083%
2084% o image: the image.
2085%
2086% o preview: the image processing operation.
2087%
2088% o exception: return any errors or warnings in this structure.
2089%
2090*/
2091MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2092 ExceptionInfo *exception)
2093{
2094#define NumberTiles 9
2095#define PreviewImageTag "Preview/Image"
2096#define DefaultPreviewGeometry "204x204+10+10"
2097
2098 char
2099 factor[MaxTextExtent],
2100 label[MaxTextExtent];
2101
2102 double
2103 degrees,
2104 gamma,
2105 percentage,
2106 radius,
2107 sigma,
2108 threshold;
2109
2110 Image
2111 *images,
2112 *montage_image,
2113 *preview_image,
2114 *thumbnail;
2115
2116 ImageInfo
2117 *preview_info;
2118
cristy3ed852e2009-09-05 21:47:34 +00002119 MagickBooleanType
2120 proceed;
2121
2122 MontageInfo
2123 *montage_info;
2124
2125 QuantizeInfo
2126 quantize_info;
2127
2128 RectangleInfo
2129 geometry;
2130
cristybb503372010-05-27 20:51:26 +00002131 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002132 i,
2133 x;
2134
cristybb503372010-05-27 20:51:26 +00002135 size_t
cristy3ed852e2009-09-05 21:47:34 +00002136 colors;
2137
cristy117ff172010-08-15 21:35:32 +00002138 ssize_t
2139 y;
2140
cristy3ed852e2009-09-05 21:47:34 +00002141 /*
2142 Open output image file.
2143 */
2144 assert(image != (Image *) NULL);
2145 assert(image->signature == MagickSignature);
2146 if (image->debug != MagickFalse)
2147 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2148 colors=2;
2149 degrees=0.0;
2150 gamma=(-0.2f);
2151 preview_info=AcquireImageInfo();
2152 SetGeometry(image,&geometry);
2153 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2154 &geometry.width,&geometry.height);
2155 images=NewImageList();
2156 percentage=12.5;
2157 GetQuantizeInfo(&quantize_info);
2158 radius=0.0;
2159 sigma=1.0;
2160 threshold=0.0;
2161 x=0;
2162 y=0;
2163 for (i=0; i < NumberTiles; i++)
2164 {
2165 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2166 if (thumbnail == (Image *) NULL)
2167 break;
2168 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2169 (void *) NULL);
cristyd15e6592011-10-15 00:13:06 +00002170 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002171 if (i == (NumberTiles/2))
2172 {
cristy9950d572011-10-01 18:22:35 +00002173 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2174 &thumbnail->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002175 AppendImageToList(&images,thumbnail);
2176 continue;
2177 }
2178 switch (preview)
2179 {
2180 case RotatePreview:
2181 {
2182 degrees+=45.0;
2183 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002184 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002185 break;
2186 }
2187 case ShearPreview:
2188 {
2189 degrees+=5.0;
2190 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002191 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002192 degrees,2.0*degrees);
2193 break;
2194 }
2195 case RollPreview:
2196 {
cristybb503372010-05-27 20:51:26 +00002197 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2198 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002199 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002200 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002201 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002202 break;
2203 }
2204 case HuePreview:
2205 {
2206 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2207 if (preview_image == (Image *) NULL)
2208 break;
cristyb51dff52011-05-19 16:55:47 +00002209 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002210 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002211 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002212 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002213 break;
2214 }
2215 case SaturationPreview:
2216 {
2217 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2218 if (preview_image == (Image *) NULL)
2219 break;
cristyb51dff52011-05-19 16:55:47 +00002220 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002221 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002222 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002223 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002224 break;
2225 }
2226 case BrightnessPreview:
2227 {
2228 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2229 if (preview_image == (Image *) NULL)
2230 break;
cristyb51dff52011-05-19 16:55:47 +00002231 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002232 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002233 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002234 break;
2235 }
2236 case GammaPreview:
2237 default:
2238 {
2239 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2240 if (preview_image == (Image *) NULL)
2241 break;
2242 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002243 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002244 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002245 break;
2246 }
2247 case SpiffPreview:
2248 {
2249 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2250 if (preview_image != (Image *) NULL)
2251 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002252 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002253 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002254 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002255 break;
2256 }
2257 case DullPreview:
2258 {
2259 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2260 if (preview_image == (Image *) NULL)
2261 break;
2262 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002263 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002264 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002265 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002266 break;
2267 }
2268 case GrayscalePreview:
2269 {
2270 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2271 if (preview_image == (Image *) NULL)
2272 break;
2273 colors<<=1;
2274 quantize_info.number_colors=colors;
2275 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002276 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002277 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002278 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002279 break;
2280 }
2281 case QuantizePreview:
2282 {
2283 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2284 if (preview_image == (Image *) NULL)
2285 break;
2286 colors<<=1;
2287 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002288 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002289 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002290 colors);
cristy3ed852e2009-09-05 21:47:34 +00002291 break;
2292 }
2293 case DespecklePreview:
2294 {
2295 for (x=0; x < (i-1); x++)
2296 {
2297 preview_image=DespeckleImage(thumbnail,exception);
2298 if (preview_image == (Image *) NULL)
2299 break;
2300 thumbnail=DestroyImage(thumbnail);
2301 thumbnail=preview_image;
2302 }
2303 preview_image=DespeckleImage(thumbnail,exception);
2304 if (preview_image == (Image *) NULL)
2305 break;
cristyb51dff52011-05-19 16:55:47 +00002306 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002307 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002308 break;
2309 }
2310 case ReduceNoisePreview:
2311 {
cristy95c38342011-03-18 22:39:51 +00002312 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2313 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002314 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002315 break;
2316 }
2317 case AddNoisePreview:
2318 {
2319 switch ((int) i)
2320 {
2321 case 0:
2322 {
2323 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2324 break;
2325 }
2326 case 1:
2327 {
2328 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2329 break;
2330 }
2331 case 2:
2332 {
2333 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2334 break;
2335 }
2336 case 3:
2337 {
2338 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2339 break;
2340 }
2341 case 4:
2342 {
2343 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2344 break;
2345 }
2346 case 5:
2347 {
2348 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2349 break;
2350 }
2351 default:
2352 {
2353 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2354 break;
2355 }
2356 }
cristyd76c51e2011-03-26 00:21:26 +00002357 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2358 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002359 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002360 break;
2361 }
2362 case SharpenPreview:
2363 {
cristyaa2c16c2012-03-25 22:21:35 +00002364 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002365 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002366 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002367 break;
2368 }
2369 case BlurPreview:
2370 {
cristyaa2c16c2012-03-25 22:21:35 +00002371 preview_image=BlurImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002372 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002373 sigma);
2374 break;
2375 }
2376 case ThresholdPreview:
2377 {
2378 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2379 if (preview_image == (Image *) NULL)
2380 break;
cristye941a752011-10-15 01:52:48 +00002381 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2382 QuantumRange+1.0))/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002383 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002384 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2385 break;
2386 }
2387 case EdgeDetectPreview:
2388 {
cristy8ae632d2011-09-05 17:29:53 +00002389 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002390 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002391 break;
2392 }
2393 case SpreadPreview:
2394 {
cristy5c4e2582011-09-11 19:21:03 +00002395 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2396 exception);
cristyb51dff52011-05-19 16:55:47 +00002397 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002398 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002399 break;
2400 }
2401 case SolarizePreview:
2402 {
2403 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2404 if (preview_image == (Image *) NULL)
2405 break;
2406 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002407 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002408 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002409 (QuantumRange*percentage)/100.0);
2410 break;
2411 }
2412 case ShadePreview:
2413 {
2414 degrees+=10.0;
2415 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2416 exception);
cristyb51dff52011-05-19 16:55:47 +00002417 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002418 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002419 break;
2420 }
2421 case RaisePreview:
2422 {
2423 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2424 if (preview_image == (Image *) NULL)
2425 break;
cristybb503372010-05-27 20:51:26 +00002426 geometry.width=(size_t) (2*i+2);
2427 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002428 geometry.x=i/2;
2429 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002430 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002431 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002432 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002433 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002434 break;
2435 }
2436 case SegmentPreview:
2437 {
2438 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2439 if (preview_image == (Image *) NULL)
2440 break;
2441 threshold+=0.4f;
2442 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002443 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002444 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002445 threshold,threshold);
2446 break;
2447 }
2448 case SwirlPreview:
2449 {
cristy76f512e2011-09-12 01:26:56 +00002450 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2451 exception);
cristyb51dff52011-05-19 16:55:47 +00002452 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002453 degrees+=45.0;
2454 break;
2455 }
2456 case ImplodePreview:
2457 {
2458 degrees+=0.1f;
cristy76f512e2011-09-12 01:26:56 +00002459 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2460 exception);
cristyb51dff52011-05-19 16:55:47 +00002461 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002462 break;
2463 }
2464 case WavePreview:
2465 {
2466 degrees+=5.0f;
cristy5c4e2582011-09-11 19:21:03 +00002467 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2468 image->interpolate,exception);
cristyb51dff52011-05-19 16:55:47 +00002469 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002470 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002471 break;
2472 }
2473 case OilPaintPreview:
2474 {
cristy14973ba2011-08-27 23:48:07 +00002475 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2476 exception);
2477 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2478 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002479 break;
2480 }
2481 case CharcoalDrawingPreview:
2482 {
2483 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristyaa2c16c2012-03-25 22:21:35 +00002484 exception);
cristyb51dff52011-05-19 16:55:47 +00002485 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002486 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002487 break;
2488 }
2489 case JPEGPreview:
2490 {
2491 char
2492 filename[MaxTextExtent];
2493
2494 int
2495 file;
2496
2497 MagickBooleanType
2498 status;
2499
2500 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2501 if (preview_image == (Image *) NULL)
2502 break;
cristybb503372010-05-27 20:51:26 +00002503 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002504 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002505 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002506 file=AcquireUniqueFileResource(filename);
2507 if (file != -1)
2508 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002509 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002510 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002511 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002512 if (status != MagickFalse)
2513 {
2514 Image
2515 *quality_image;
2516
2517 (void) CopyMagickString(preview_info->filename,
2518 preview_image->filename,MaxTextExtent);
2519 quality_image=ReadImage(preview_info,exception);
2520 if (quality_image != (Image *) NULL)
2521 {
2522 preview_image=DestroyImage(preview_image);
2523 preview_image=quality_image;
2524 }
2525 }
2526 (void) RelinquishUniqueFileResource(preview_image->filename);
2527 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002528 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002529 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2530 1024.0/1024.0);
2531 else
2532 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002533 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002534 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002535 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002536 else
cristyb51dff52011-05-19 16:55:47 +00002537 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002538 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002539 break;
2540 }
2541 }
2542 thumbnail=DestroyImage(thumbnail);
2543 percentage+=12.5;
2544 radius+=0.5;
2545 sigma+=0.25;
2546 if (preview_image == (Image *) NULL)
2547 break;
2548 (void) DeleteImageProperty(preview_image,"label");
cristyd15e6592011-10-15 00:13:06 +00002549 (void) SetImageProperty(preview_image,"label",label,exception);
cristy3ed852e2009-09-05 21:47:34 +00002550 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002551 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2552 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002553 if (proceed == MagickFalse)
2554 break;
2555 }
2556 if (images == (Image *) NULL)
2557 {
2558 preview_info=DestroyImageInfo(preview_info);
2559 return((Image *) NULL);
2560 }
2561 /*
2562 Create the montage.
2563 */
2564 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2565 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2566 montage_info->shadow=MagickTrue;
2567 (void) CloneString(&montage_info->tile,"3x3");
2568 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2569 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2570 montage_image=MontageImages(images,montage_info,exception);
2571 montage_info=DestroyMontageInfo(montage_info);
2572 images=DestroyImageList(images);
2573 if (montage_image == (Image *) NULL)
2574 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2575 if (montage_image->montage != (char *) NULL)
2576 {
2577 /*
2578 Free image directory.
2579 */
2580 montage_image->montage=(char *) RelinquishMagickMemory(
2581 montage_image->montage);
2582 if (image->directory != (char *) NULL)
2583 montage_image->directory=(char *) RelinquishMagickMemory(
2584 montage_image->directory);
2585 }
2586 preview_info=DestroyImageInfo(preview_info);
2587 return(montage_image);
2588}
2589
2590/*
2591%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2592% %
2593% %
2594% %
2595% R a d i a l B l u r I m a g e %
2596% %
2597% %
2598% %
2599%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2600%
2601% RadialBlurImage() applies a radial blur to the image.
2602%
2603% Andrew Protano contributed this effect.
2604%
2605% The format of the RadialBlurImage method is:
2606%
2607% Image *RadialBlurImage(const Image *image,const double angle,
cristyaa2c16c2012-03-25 22:21:35 +00002608% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002609%
2610% A description of each parameter follows:
2611%
2612% o image: the image.
2613%
cristy3ed852e2009-09-05 21:47:34 +00002614% o angle: the angle of the radial blur.
2615%
cristy6435bd92011-09-10 02:10:07 +00002616% o blur: the blur.
2617%
cristy3ed852e2009-09-05 21:47:34 +00002618% o exception: return any errors or warnings in this structure.
2619%
2620*/
cristy4282c702011-11-21 00:01:06 +00002621MagickExport Image *RadialBlurImage(const Image *image,const double angle,
cristyaa2c16c2012-03-25 22:21:35 +00002622 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002623{
cristyc4c8d132010-01-07 01:58:38 +00002624 CacheView
2625 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002626 *image_view,
2627 *radial_view;
cristyc4c8d132010-01-07 01:58:38 +00002628
cristy3ed852e2009-09-05 21:47:34 +00002629 Image
2630 *blur_image;
2631
cristy3ed852e2009-09-05 21:47:34 +00002632 MagickBooleanType
2633 status;
2634
cristybb503372010-05-27 20:51:26 +00002635 MagickOffsetType
2636 progress;
2637
cristy3ed852e2009-09-05 21:47:34 +00002638 MagickRealType
2639 blur_radius,
2640 *cos_theta,
2641 offset,
2642 *sin_theta,
2643 theta;
2644
2645 PointInfo
2646 blur_center;
2647
cristybb503372010-05-27 20:51:26 +00002648 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002649 i;
2650
cristybb503372010-05-27 20:51:26 +00002651 size_t
cristy3ed852e2009-09-05 21:47:34 +00002652 n;
2653
cristybb503372010-05-27 20:51:26 +00002654 ssize_t
2655 y;
2656
cristy3ed852e2009-09-05 21:47:34 +00002657 /*
2658 Allocate blur image.
2659 */
2660 assert(image != (Image *) NULL);
2661 assert(image->signature == MagickSignature);
2662 if (image->debug != MagickFalse)
2663 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2664 assert(exception != (ExceptionInfo *) NULL);
2665 assert(exception->signature == MagickSignature);
cristy1e7aa312011-09-10 20:01:36 +00002666 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002667 if (blur_image == (Image *) NULL)
2668 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002669 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002670 {
cristy3ed852e2009-09-05 21:47:34 +00002671 blur_image=DestroyImage(blur_image);
2672 return((Image *) NULL);
2673 }
2674 blur_center.x=(double) image->columns/2.0;
2675 blur_center.y=(double) image->rows/2.0;
2676 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002677 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002678 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2679 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2680 sizeof(*cos_theta));
2681 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2682 sizeof(*sin_theta));
2683 if ((cos_theta == (MagickRealType *) NULL) ||
2684 (sin_theta == (MagickRealType *) NULL))
2685 {
2686 blur_image=DestroyImage(blur_image);
2687 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2688 }
2689 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002690 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002691 {
2692 cos_theta[i]=cos((double) (theta*i-offset));
2693 sin_theta[i]=sin((double) (theta*i-offset));
2694 }
2695 /*
2696 Radial blur image.
2697 */
2698 status=MagickTrue;
2699 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002700 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002701 radial_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002702 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002703#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002704 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002705#endif
cristy1e7aa312011-09-10 20:01:36 +00002706 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002707 {
cristy1e7aa312011-09-10 20:01:36 +00002708 register const Quantum
2709 *restrict p;
2710
cristy4c08aed2011-07-01 19:47:50 +00002711 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002712 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002713
cristy117ff172010-08-15 21:35:32 +00002714 register ssize_t
2715 x;
2716
cristy3ed852e2009-09-05 21:47:34 +00002717 if (status == MagickFalse)
2718 continue;
cristy8b49f382012-01-17 03:11:03 +00002719 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2720 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002721 exception);
cristy1e7aa312011-09-10 20:01:36 +00002722 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002723 {
2724 status=MagickFalse;
2725 continue;
2726 }
cristy1e7aa312011-09-10 20:01:36 +00002727 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002728 {
cristy3ed852e2009-09-05 21:47:34 +00002729 MagickRealType
cristy3ed852e2009-09-05 21:47:34 +00002730 radius;
2731
cristy3ed852e2009-09-05 21:47:34 +00002732 PointInfo
2733 center;
2734
cristybb503372010-05-27 20:51:26 +00002735 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002736 i;
2737
cristybb503372010-05-27 20:51:26 +00002738 size_t
cristy3ed852e2009-09-05 21:47:34 +00002739 step;
2740
2741 center.x=(double) x-blur_center.x;
2742 center.y=(double) y-blur_center.y;
2743 radius=hypot((double) center.x,center.y);
2744 if (radius == 0)
2745 step=1;
2746 else
2747 {
cristybb503372010-05-27 20:51:26 +00002748 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002749 if (step == 0)
2750 step=1;
2751 else
2752 if (step >= n)
2753 step=n-1;
2754 }
cristy1e7aa312011-09-10 20:01:36 +00002755 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2756 {
2757 MagickRealType
2758 gamma,
2759 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002760
cristy1e7aa312011-09-10 20:01:36 +00002761 PixelChannel
2762 channel;
2763
2764 PixelTrait
2765 blur_traits,
2766 traits;
2767
2768 register const Quantum
2769 *restrict r;
2770
2771 register ssize_t
2772 j;
2773
cristye2a912b2011-12-05 20:02:07 +00002774 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002775 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00002776 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2777 if ((traits == UndefinedPixelTrait) ||
2778 (blur_traits == UndefinedPixelTrait))
2779 continue;
cristyd09f8802012-02-04 16:44:10 +00002780 if (((blur_traits & CopyPixelTrait) != 0) ||
2781 (GetPixelMask(image,p) != 0))
cristy3ed852e2009-09-05 21:47:34 +00002782 {
cristy0beccfa2011-09-25 20:47:53 +00002783 SetPixelChannel(blur_image,channel,p[i],q);
cristy1e7aa312011-09-10 20:01:36 +00002784 continue;
cristy3ed852e2009-09-05 21:47:34 +00002785 }
cristy1e7aa312011-09-10 20:01:36 +00002786 gamma=0.0;
cristyaa2c16c2012-03-25 22:21:35 +00002787 pixel=0.0;
cristy1e7aa312011-09-10 20:01:36 +00002788 if ((blur_traits & BlendPixelTrait) == 0)
2789 {
2790 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2791 {
cristy8b49f382012-01-17 03:11:03 +00002792 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00002793 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2794 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2795 1,1,exception);
2796 if (r == (const Quantum *) NULL)
2797 {
2798 status=MagickFalse;
2799 continue;
2800 }
2801 pixel+=r[i];
2802 gamma++;
2803 }
2804 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002805 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00002806 continue;
2807 }
2808 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2809 {
cristy8b49f382012-01-17 03:11:03 +00002810 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00002811 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2812 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2813 1,1,exception);
2814 if (r == (const Quantum *) NULL)
2815 {
2816 status=MagickFalse;
2817 continue;
2818 }
2819 pixel+=GetPixelAlpha(image,r)*r[i];
2820 gamma+=GetPixelAlpha(image,r);
cristy3ed852e2009-09-05 21:47:34 +00002821 }
cristy1e7aa312011-09-10 20:01:36 +00002822 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002823 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00002824 }
2825 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002826 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002827 }
2828 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2829 status=MagickFalse;
2830 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2831 {
2832 MagickBooleanType
2833 proceed;
2834
cristyb5d5f722009-11-04 03:03:49 +00002835#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00002836 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002837#endif
2838 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2839 if (proceed == MagickFalse)
2840 status=MagickFalse;
2841 }
2842 }
2843 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00002844 radial_view=DestroyCacheView(radial_view);
cristy3ed852e2009-09-05 21:47:34 +00002845 image_view=DestroyCacheView(image_view);
2846 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
2847 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
2848 if (status == MagickFalse)
2849 blur_image=DestroyImage(blur_image);
2850 return(blur_image);
2851}
2852
2853/*
2854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2855% %
2856% %
2857% %
cristy3ed852e2009-09-05 21:47:34 +00002858% S e l e c t i v e B l u r I m a g e %
2859% %
2860% %
2861% %
2862%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2863%
2864% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2865% It is similar to the unsharpen mask that sharpens everything with contrast
2866% above a certain threshold.
2867%
2868% The format of the SelectiveBlurImage method is:
2869%
2870% Image *SelectiveBlurImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00002871% const double sigma,const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002872%
2873% A description of each parameter follows:
2874%
2875% o image: the image.
2876%
cristy3ed852e2009-09-05 21:47:34 +00002877% o radius: the radius of the Gaussian, in pixels, not counting the center
2878% pixel.
2879%
2880% o sigma: the standard deviation of the Gaussian, in pixels.
2881%
2882% o threshold: only pixels within this contrast threshold are included
2883% in the blur operation.
2884%
2885% o exception: return any errors or warnings in this structure.
2886%
2887*/
cristy4282c702011-11-21 00:01:06 +00002888MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00002889 const double sigma,const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002890{
2891#define SelectiveBlurImageTag "SelectiveBlur/Image"
2892
cristy47e00502009-12-17 19:19:57 +00002893 CacheView
2894 *blur_view,
2895 *image_view;
2896
cristy3ed852e2009-09-05 21:47:34 +00002897 double
cristy3ed852e2009-09-05 21:47:34 +00002898 *kernel;
2899
2900 Image
2901 *blur_image;
2902
cristy3ed852e2009-09-05 21:47:34 +00002903 MagickBooleanType
2904 status;
2905
cristybb503372010-05-27 20:51:26 +00002906 MagickOffsetType
2907 progress;
2908
cristybb503372010-05-27 20:51:26 +00002909 register ssize_t
cristy47e00502009-12-17 19:19:57 +00002910 i;
cristy3ed852e2009-09-05 21:47:34 +00002911
cristybb503372010-05-27 20:51:26 +00002912 size_t
cristy3ed852e2009-09-05 21:47:34 +00002913 width;
2914
cristybb503372010-05-27 20:51:26 +00002915 ssize_t
cristyc8523c12011-09-13 00:02:53 +00002916 center,
cristybb503372010-05-27 20:51:26 +00002917 j,
2918 u,
2919 v,
2920 y;
2921
cristy3ed852e2009-09-05 21:47:34 +00002922 /*
2923 Initialize blur image attributes.
2924 */
2925 assert(image != (Image *) NULL);
2926 assert(image->signature == MagickSignature);
2927 if (image->debug != MagickFalse)
2928 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2929 assert(exception != (ExceptionInfo *) NULL);
2930 assert(exception->signature == MagickSignature);
2931 width=GetOptimalKernelWidth1D(radius,sigma);
cristy8b49f382012-01-17 03:11:03 +00002932 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00002933 if (kernel == (double *) NULL)
2934 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00002935 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00002936 i=0;
cristy47e00502009-12-17 19:19:57 +00002937 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00002938 {
cristy47e00502009-12-17 19:19:57 +00002939 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00002940 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2941 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002942 }
2943 if (image->debug != MagickFalse)
2944 {
2945 char
2946 format[MaxTextExtent],
2947 *message;
2948
cristy117ff172010-08-15 21:35:32 +00002949 register const double
2950 *k;
2951
cristybb503372010-05-27 20:51:26 +00002952 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002953 u,
2954 v;
2955
cristy3ed852e2009-09-05 21:47:34 +00002956 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00002957 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2958 width);
cristy3ed852e2009-09-05 21:47:34 +00002959 message=AcquireString("");
2960 k=kernel;
cristybb503372010-05-27 20:51:26 +00002961 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00002962 {
2963 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00002964 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00002965 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00002966 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00002967 {
cristyb51dff52011-05-19 16:55:47 +00002968 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00002969 (void) ConcatenateString(&message,format);
2970 }
2971 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2972 }
2973 message=DestroyString(message);
2974 }
cristy1e7aa312011-09-10 20:01:36 +00002975 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002976 if (blur_image == (Image *) NULL)
2977 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002978 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002979 {
cristy3ed852e2009-09-05 21:47:34 +00002980 blur_image=DestroyImage(blur_image);
2981 return((Image *) NULL);
2982 }
2983 /*
2984 Threshold blur image.
2985 */
2986 status=MagickTrue;
2987 progress=0;
cristyc8523c12011-09-13 00:02:53 +00002988 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
2989 GetPixelChannels(image)*(width/2L));
cristy3ed852e2009-09-05 21:47:34 +00002990 image_view=AcquireCacheView(image);
2991 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002992#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002993 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002994#endif
cristybb503372010-05-27 20:51:26 +00002995 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002996 {
cristy4c08aed2011-07-01 19:47:50 +00002997 double
2998 contrast;
2999
cristy3ed852e2009-09-05 21:47:34 +00003000 MagickBooleanType
3001 sync;
3002
cristy4c08aed2011-07-01 19:47:50 +00003003 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003004 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003005
cristy4c08aed2011-07-01 19:47:50 +00003006 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003007 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003008
cristy117ff172010-08-15 21:35:32 +00003009 register ssize_t
3010 x;
3011
cristy3ed852e2009-09-05 21:47:34 +00003012 if (status == MagickFalse)
3013 continue;
cristy117ff172010-08-15 21:35:32 +00003014 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3015 (width/2L),image->columns+width,width,exception);
cristy8b49f382012-01-17 03:11:03 +00003016 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003017 exception);
cristy4c08aed2011-07-01 19:47:50 +00003018 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003019 {
3020 status=MagickFalse;
3021 continue;
3022 }
cristybb503372010-05-27 20:51:26 +00003023 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003024 {
cristybb503372010-05-27 20:51:26 +00003025 register ssize_t
cristy1e7aa312011-09-10 20:01:36 +00003026 i;
cristy3ed852e2009-09-05 21:47:34 +00003027
cristy1e7aa312011-09-10 20:01:36 +00003028 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3029 {
3030 MagickRealType
3031 alpha,
3032 gamma,
3033 intensity,
3034 pixel;
cristy117ff172010-08-15 21:35:32 +00003035
cristy1e7aa312011-09-10 20:01:36 +00003036 PixelChannel
3037 channel;
3038
3039 PixelTrait
3040 blur_traits,
3041 traits;
3042
3043 register const double
3044 *restrict k;
3045
3046 register const Quantum
3047 *restrict pixels;
3048
3049 register ssize_t
3050 u;
3051
3052 ssize_t
3053 v;
3054
cristye2a912b2011-12-05 20:02:07 +00003055 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003056 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003057 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3058 if ((traits == UndefinedPixelTrait) ||
3059 (blur_traits == UndefinedPixelTrait))
3060 continue;
cristyd09f8802012-02-04 16:44:10 +00003061 if (((blur_traits & CopyPixelTrait) != 0) ||
3062 (GetPixelMask(image,p) != 0))
cristy3ed852e2009-09-05 21:47:34 +00003063 {
cristy0beccfa2011-09-25 20:47:53 +00003064 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003065 continue;
cristy3ed852e2009-09-05 21:47:34 +00003066 }
cristy1e7aa312011-09-10 20:01:36 +00003067 k=kernel;
cristyaa2c16c2012-03-25 22:21:35 +00003068 pixel=0.0;
cristy1e7aa312011-09-10 20:01:36 +00003069 pixels=p;
cristyc8523c12011-09-13 00:02:53 +00003070 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
cristy1e7aa312011-09-10 20:01:36 +00003071 gamma=0.0;
3072 if ((blur_traits & BlendPixelTrait) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003073 {
cristy1e7aa312011-09-10 20:01:36 +00003074 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003075 {
cristy1e7aa312011-09-10 20:01:36 +00003076 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003077 {
cristy1e7aa312011-09-10 20:01:36 +00003078 contrast=GetPixelIntensity(image,pixels)-intensity;
3079 if (fabs(contrast) < threshold)
3080 {
3081 pixel+=(*k)*pixels[i];
3082 gamma+=(*k);
3083 }
3084 k++;
3085 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003086 }
cristy1e7aa312011-09-10 20:01:36 +00003087 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003088 }
cristy1e7aa312011-09-10 20:01:36 +00003089 if (fabs((double) gamma) < MagickEpsilon)
3090 {
cristy0beccfa2011-09-25 20:47:53 +00003091 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003092 continue;
3093 }
3094 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003095 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003096 continue;
3097 }
3098 for (v=0; v < (ssize_t) width; v++)
3099 {
3100 for (u=0; u < (ssize_t) width; u++)
3101 {
3102 contrast=GetPixelIntensity(image,pixels)-intensity;
3103 if (fabs(contrast) < threshold)
3104 {
3105 alpha=(MagickRealType) (QuantumScale*
3106 GetPixelAlpha(image,pixels));
3107 pixel+=(*k)*alpha*pixels[i];
3108 gamma+=(*k)*alpha;
3109 }
3110 k++;
3111 pixels+=GetPixelChannels(image);
3112 }
3113 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003114 }
cristy1e7aa312011-09-10 20:01:36 +00003115 if (fabs((double) gamma) < MagickEpsilon)
3116 {
cristy0beccfa2011-09-25 20:47:53 +00003117 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003118 continue;
3119 }
3120 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003121 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003122 }
cristyed231572011-07-14 02:18:59 +00003123 p+=GetPixelChannels(image);
3124 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003125 }
3126 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3127 if (sync == MagickFalse)
3128 status=MagickFalse;
3129 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3130 {
3131 MagickBooleanType
3132 proceed;
3133
cristyb5d5f722009-11-04 03:03:49 +00003134#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00003135 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003136#endif
3137 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3138 image->rows);
3139 if (proceed == MagickFalse)
3140 status=MagickFalse;
3141 }
3142 }
3143 blur_image->type=image->type;
3144 blur_view=DestroyCacheView(blur_view);
3145 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00003146 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00003147 if (status == MagickFalse)
3148 blur_image=DestroyImage(blur_image);
3149 return(blur_image);
3150}
3151
3152/*
3153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3154% %
3155% %
3156% %
3157% S h a d e I m a g e %
3158% %
3159% %
3160% %
3161%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3162%
3163% ShadeImage() shines a distant light on an image to create a
3164% three-dimensional effect. You control the positioning of the light with
3165% azimuth and elevation; azimuth is measured in degrees off the x axis
3166% and elevation is measured in pixels above the Z axis.
3167%
3168% The format of the ShadeImage method is:
3169%
3170% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3171% const double azimuth,const double elevation,ExceptionInfo *exception)
3172%
3173% A description of each parameter follows:
3174%
3175% o image: the image.
3176%
3177% o gray: A value other than zero shades the intensity of each pixel.
3178%
3179% o azimuth, elevation: Define the light source direction.
3180%
3181% o exception: return any errors or warnings in this structure.
3182%
3183*/
3184MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3185 const double azimuth,const double elevation,ExceptionInfo *exception)
3186{
3187#define ShadeImageTag "Shade/Image"
3188
cristyc4c8d132010-01-07 01:58:38 +00003189 CacheView
3190 *image_view,
3191 *shade_view;
3192
cristy3ed852e2009-09-05 21:47:34 +00003193 Image
3194 *shade_image;
3195
cristy3ed852e2009-09-05 21:47:34 +00003196 MagickBooleanType
3197 status;
3198
cristybb503372010-05-27 20:51:26 +00003199 MagickOffsetType
3200 progress;
3201
cristy3ed852e2009-09-05 21:47:34 +00003202 PrimaryInfo
3203 light;
3204
cristybb503372010-05-27 20:51:26 +00003205 ssize_t
3206 y;
3207
cristy3ed852e2009-09-05 21:47:34 +00003208 /*
3209 Initialize shaded image attributes.
3210 */
3211 assert(image != (const Image *) NULL);
3212 assert(image->signature == MagickSignature);
3213 if (image->debug != MagickFalse)
3214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3215 assert(exception != (ExceptionInfo *) NULL);
3216 assert(exception->signature == MagickSignature);
3217 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3218 if (shade_image == (Image *) NULL)
3219 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003220 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003221 {
cristy3ed852e2009-09-05 21:47:34 +00003222 shade_image=DestroyImage(shade_image);
3223 return((Image *) NULL);
3224 }
3225 /*
3226 Compute the light vector.
3227 */
3228 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3229 cos(DegreesToRadians(elevation));
3230 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3231 cos(DegreesToRadians(elevation));
3232 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3233 /*
3234 Shade image.
3235 */
3236 status=MagickTrue;
3237 progress=0;
3238 image_view=AcquireCacheView(image);
3239 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003240#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003241 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003242#endif
cristybb503372010-05-27 20:51:26 +00003243 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003244 {
3245 MagickRealType
3246 distance,
3247 normal_distance,
3248 shade;
3249
3250 PrimaryInfo
3251 normal;
3252
cristy4c08aed2011-07-01 19:47:50 +00003253 register const Quantum
cristy1e7aa312011-09-10 20:01:36 +00003254 *restrict center,
cristyc47d1f82009-11-26 01:44:43 +00003255 *restrict p,
cristy1e7aa312011-09-10 20:01:36 +00003256 *restrict post,
3257 *restrict pre;
cristy3ed852e2009-09-05 21:47:34 +00003258
cristy4c08aed2011-07-01 19:47:50 +00003259 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003260 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003261
cristy117ff172010-08-15 21:35:32 +00003262 register ssize_t
3263 x;
3264
cristy3ed852e2009-09-05 21:47:34 +00003265 if (status == MagickFalse)
3266 continue;
3267 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3268 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3269 exception);
cristy4c08aed2011-07-01 19:47:50 +00003270 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003271 {
3272 status=MagickFalse;
3273 continue;
3274 }
3275 /*
3276 Shade this row of pixels.
3277 */
3278 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristy1e7aa312011-09-10 20:01:36 +00003279 pre=p+GetPixelChannels(image);
3280 center=pre+(image->columns+2)*GetPixelChannels(image);
3281 post=center+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003282 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003283 {
cristy1e7aa312011-09-10 20:01:36 +00003284 register ssize_t
3285 i;
3286
cristy3ed852e2009-09-05 21:47:34 +00003287 /*
3288 Determine the surface normal and compute shading.
3289 */
cristy1e7aa312011-09-10 20:01:36 +00003290 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3291 GetPixelIntensity(image,center-GetPixelChannels(image))+
3292 GetPixelIntensity(image,post-GetPixelChannels(image))-
3293 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3294 GetPixelIntensity(image,center+GetPixelChannels(image))-
3295 GetPixelIntensity(image,post+GetPixelChannels(image)));
3296 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3297 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3298 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3299 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3300 GetPixelIntensity(image,pre+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003301 if ((normal.x == 0.0) && (normal.y == 0.0))
3302 shade=light.z;
3303 else
3304 {
3305 shade=0.0;
3306 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3307 if (distance > MagickEpsilon)
3308 {
3309 normal_distance=
3310 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3311 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3312 shade=distance/sqrt((double) normal_distance);
3313 }
3314 }
cristy1e7aa312011-09-10 20:01:36 +00003315 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3316 {
3317 PixelChannel
3318 channel;
3319
3320 PixelTrait
3321 shade_traits,
3322 traits;
3323
cristye2a912b2011-12-05 20:02:07 +00003324 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003325 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003326 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3327 if ((traits == UndefinedPixelTrait) ||
3328 (shade_traits == UndefinedPixelTrait))
3329 continue;
cristyd09f8802012-02-04 16:44:10 +00003330 if (((shade_traits & CopyPixelTrait) != 0) ||
3331 (GetPixelMask(image,pre) != 0))
cristy1e7aa312011-09-10 20:01:36 +00003332 {
cristy0beccfa2011-09-25 20:47:53 +00003333 SetPixelChannel(shade_image,channel,center[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003334 continue;
3335 }
3336 if (gray != MagickFalse)
3337 {
cristy0beccfa2011-09-25 20:47:53 +00003338 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
cristy1e7aa312011-09-10 20:01:36 +00003339 continue;
3340 }
cristy0beccfa2011-09-25 20:47:53 +00003341 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3342 center[i]),q);
cristy1e7aa312011-09-10 20:01:36 +00003343 }
3344 pre+=GetPixelChannels(image);
3345 center+=GetPixelChannels(image);
3346 post+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003347 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003348 }
3349 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3350 status=MagickFalse;
3351 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3352 {
3353 MagickBooleanType
3354 proceed;
3355
cristyb5d5f722009-11-04 03:03:49 +00003356#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00003357 #pragma omp critical (MagickCore_ShadeImage)
cristy3ed852e2009-09-05 21:47:34 +00003358#endif
3359 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3360 if (proceed == MagickFalse)
3361 status=MagickFalse;
3362 }
3363 }
3364 shade_view=DestroyCacheView(shade_view);
3365 image_view=DestroyCacheView(image_view);
3366 if (status == MagickFalse)
3367 shade_image=DestroyImage(shade_image);
3368 return(shade_image);
3369}
3370
3371/*
3372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3373% %
3374% %
3375% %
3376% S h a r p e n I m a g e %
3377% %
3378% %
3379% %
3380%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3381%
3382% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3383% operator of the given radius and standard deviation (sigma). For
3384% reasonable results, radius should be larger than sigma. Use a radius of 0
3385% and SharpenImage() selects a suitable radius for you.
3386%
3387% Using a separable kernel would be faster, but the negative weights cancel
3388% out on the corners of the kernel producing often undesirable ringing in the
3389% filtered result; this can be avoided by using a 2D gaussian shaped image
3390% sharpening kernel instead.
3391%
3392% The format of the SharpenImage method is:
3393%
3394% Image *SharpenImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00003395% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003396%
3397% A description of each parameter follows:
3398%
3399% o image: the image.
3400%
cristy3ed852e2009-09-05 21:47:34 +00003401% o radius: the radius of the Gaussian, in pixels, not counting the center
3402% pixel.
3403%
3404% o sigma: the standard deviation of the Laplacian, in pixels.
3405%
3406% o exception: return any errors or warnings in this structure.
3407%
3408*/
cristy3ed852e2009-09-05 21:47:34 +00003409MagickExport Image *SharpenImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00003410 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003411{
cristy3ed852e2009-09-05 21:47:34 +00003412 double
cristy47e00502009-12-17 19:19:57 +00003413 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003414
3415 Image
3416 *sharp_image;
3417
cristy41cbe682011-07-15 19:12:37 +00003418 KernelInfo
3419 *kernel_info;
3420
cristybb503372010-05-27 20:51:26 +00003421 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003422 i;
3423
cristybb503372010-05-27 20:51:26 +00003424 size_t
cristy3ed852e2009-09-05 21:47:34 +00003425 width;
3426
cristy117ff172010-08-15 21:35:32 +00003427 ssize_t
3428 j,
3429 u,
3430 v;
3431
cristy3ed852e2009-09-05 21:47:34 +00003432 assert(image != (const Image *) NULL);
3433 assert(image->signature == MagickSignature);
3434 if (image->debug != MagickFalse)
3435 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3436 assert(exception != (ExceptionInfo *) NULL);
3437 assert(exception->signature == MagickSignature);
3438 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003439 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003440 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003441 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003442 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3443 kernel_info->width=width;
3444 kernel_info->height=width;
3445 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00003446 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3447 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3448 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00003449 {
3450 kernel_info=DestroyKernelInfo(kernel_info);
3451 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3452 }
cristy3ed852e2009-09-05 21:47:34 +00003453 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003454 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003455 i=0;
3456 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003457 {
cristy47e00502009-12-17 19:19:57 +00003458 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003459 {
cristy41cbe682011-07-15 19:12:37 +00003460 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3461 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3462 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003463 i++;
3464 }
3465 }
cristy41cbe682011-07-15 19:12:37 +00003466 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003467 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003468 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003469 return(sharp_image);
3470}
3471
3472/*
3473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3474% %
3475% %
3476% %
3477% S p r e a d I m a g e %
3478% %
3479% %
3480% %
3481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3482%
3483% SpreadImage() is a special effects method that randomly displaces each
3484% pixel in a block defined by the radius parameter.
3485%
3486% The format of the SpreadImage method is:
3487%
3488% Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003489% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003490%
3491% A description of each parameter follows:
3492%
3493% o image: the image.
3494%
cristy5c4e2582011-09-11 19:21:03 +00003495% o radius: choose a random pixel in a neighborhood of this extent.
3496%
3497% o method: the pixel interpolation method.
cristy3ed852e2009-09-05 21:47:34 +00003498%
3499% o exception: return any errors or warnings in this structure.
3500%
3501*/
3502MagickExport Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003503 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003504{
3505#define SpreadImageTag "Spread/Image"
3506
cristyfa112112010-01-04 17:48:07 +00003507 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003508 *image_view,
3509 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003510
cristy3ed852e2009-09-05 21:47:34 +00003511 Image
3512 *spread_image;
3513
cristy3ed852e2009-09-05 21:47:34 +00003514 MagickBooleanType
cristye7452652012-04-14 01:34:21 +00003515 concurrent,
cristy3ed852e2009-09-05 21:47:34 +00003516 status;
3517
cristybb503372010-05-27 20:51:26 +00003518 MagickOffsetType
3519 progress;
3520
cristy3ed852e2009-09-05 21:47:34 +00003521 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003522 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003523
cristybb503372010-05-27 20:51:26 +00003524 size_t
cristy3ed852e2009-09-05 21:47:34 +00003525 width;
3526
cristybb503372010-05-27 20:51:26 +00003527 ssize_t
3528 y;
3529
cristy3ed852e2009-09-05 21:47:34 +00003530 /*
3531 Initialize spread image attributes.
3532 */
3533 assert(image != (Image *) NULL);
3534 assert(image->signature == MagickSignature);
3535 if (image->debug != MagickFalse)
3536 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3537 assert(exception != (ExceptionInfo *) NULL);
3538 assert(exception->signature == MagickSignature);
3539 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3540 exception);
3541 if (spread_image == (Image *) NULL)
3542 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003543 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003544 {
cristy3ed852e2009-09-05 21:47:34 +00003545 spread_image=DestroyImage(spread_image);
3546 return((Image *) NULL);
3547 }
3548 /*
3549 Spread image.
3550 */
3551 status=MagickTrue;
3552 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003553 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003554 random_info=AcquireRandomInfoThreadSet();
cristye7452652012-04-14 01:34:21 +00003555 concurrent=GetRandomSecretKey(random_info[0]) == ~0UL ? MagickTrue :
3556 MagickFalse;
cristy9f7e7cb2011-03-26 00:49:57 +00003557 image_view=AcquireCacheView(image);
3558 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003559#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye7452652012-04-14 01:34:21 +00003560 #pragma omp parallel for schedule(static,8) shared(progress,status) omp_concurrent(concurrent)
cristy3ed852e2009-09-05 21:47:34 +00003561#endif
cristybe82ad52011-09-06 01:17:20 +00003562 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003563 {
cristy5c9e6f22010-09-17 17:31:01 +00003564 const int
3565 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003566
cristybe82ad52011-09-06 01:17:20 +00003567 register const Quantum
3568 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003569
cristy4c08aed2011-07-01 19:47:50 +00003570 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003571 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003572
cristy117ff172010-08-15 21:35:32 +00003573 register ssize_t
3574 x;
3575
cristy3ed852e2009-09-05 21:47:34 +00003576 if (status == MagickFalse)
3577 continue;
cristybe82ad52011-09-06 01:17:20 +00003578 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy9f7e7cb2011-03-26 00:49:57 +00003579 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003580 exception);
cristybe82ad52011-09-06 01:17:20 +00003581 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003582 {
3583 status=MagickFalse;
3584 continue;
3585 }
cristybe82ad52011-09-06 01:17:20 +00003586 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003587 {
cristybe82ad52011-09-06 01:17:20 +00003588 PointInfo
3589 point;
3590
cristybe82ad52011-09-06 01:17:20 +00003591 point.x=GetPseudoRandomValue(random_info[id]);
3592 point.y=GetPseudoRandomValue(random_info[id]);
cristy5c4e2582011-09-11 19:21:03 +00003593 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3594 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
cristyed231572011-07-14 02:18:59 +00003595 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003596 }
cristy9f7e7cb2011-03-26 00:49:57 +00003597 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003598 status=MagickFalse;
3599 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3600 {
3601 MagickBooleanType
3602 proceed;
3603
cristyb557a152011-02-22 12:14:30 +00003604#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00003605 #pragma omp critical (MagickCore_SpreadImage)
cristy3ed852e2009-09-05 21:47:34 +00003606#endif
3607 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3608 if (proceed == MagickFalse)
3609 status=MagickFalse;
3610 }
3611 }
cristy9f7e7cb2011-03-26 00:49:57 +00003612 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003613 image_view=DestroyCacheView(image_view);
3614 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003615 return(spread_image);
3616}
3617
3618/*
3619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3620% %
3621% %
3622% %
3623% U n s h a r p M a s k I m a g e %
3624% %
3625% %
3626% %
3627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3628%
3629% UnsharpMaskImage() sharpens one or more image channels. We convolve the
3630% image with a Gaussian operator of the given radius and standard deviation
3631% (sigma). For reasonable results, radius should be larger than sigma. Use a
3632% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3633%
3634% The format of the UnsharpMaskImage method is:
3635%
3636% Image *UnsharpMaskImage(const Image *image,const double radius,
3637% const double sigma,const double amount,const double threshold,
3638% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003639%
3640% A description of each parameter follows:
3641%
3642% o image: the image.
3643%
cristy3ed852e2009-09-05 21:47:34 +00003644% o radius: the radius of the Gaussian, in pixels, not counting the center
3645% pixel.
3646%
3647% o sigma: the standard deviation of the Gaussian, in pixels.
3648%
3649% o amount: the percentage of the difference between the original and the
3650% blur image that is added back into the original.
3651%
3652% o threshold: the threshold in pixels needed to apply the diffence amount.
3653%
3654% o exception: return any errors or warnings in this structure.
3655%
3656*/
cristy31bb6272011-11-20 23:57:25 +00003657MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3658 const double sigma,const double amount,const double threshold,
3659 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003660{
3661#define SharpenImageTag "Sharpen/Image"
3662
cristyc4c8d132010-01-07 01:58:38 +00003663 CacheView
3664 *image_view,
3665 *unsharp_view;
3666
cristy3ed852e2009-09-05 21:47:34 +00003667 Image
3668 *unsharp_image;
3669
cristy3ed852e2009-09-05 21:47:34 +00003670 MagickBooleanType
3671 status;
3672
cristybb503372010-05-27 20:51:26 +00003673 MagickOffsetType
3674 progress;
3675
cristy3ed852e2009-09-05 21:47:34 +00003676 MagickRealType
3677 quantum_threshold;
3678
cristybb503372010-05-27 20:51:26 +00003679 ssize_t
3680 y;
3681
cristy3ed852e2009-09-05 21:47:34 +00003682 assert(image != (const Image *) NULL);
3683 assert(image->signature == MagickSignature);
3684 if (image->debug != MagickFalse)
3685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3686 assert(exception != (ExceptionInfo *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +00003687 unsharp_image=BlurImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00003688 if (unsharp_image == (Image *) NULL)
3689 return((Image *) NULL);
3690 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3691 /*
3692 Unsharp-mask image.
3693 */
3694 status=MagickTrue;
3695 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003696 image_view=AcquireCacheView(image);
3697 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00003698#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003699 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003700#endif
cristybb503372010-05-27 20:51:26 +00003701 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003702 {
cristy4c08aed2011-07-01 19:47:50 +00003703 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003704 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003705
cristy4c08aed2011-07-01 19:47:50 +00003706 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003707 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003708
cristy117ff172010-08-15 21:35:32 +00003709 register ssize_t
3710 x;
3711
cristy3ed852e2009-09-05 21:47:34 +00003712 if (status == MagickFalse)
3713 continue;
3714 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy8b49f382012-01-17 03:11:03 +00003715 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003716 exception);
cristy4c08aed2011-07-01 19:47:50 +00003717 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003718 {
3719 status=MagickFalse;
3720 continue;
3721 }
cristybb503372010-05-27 20:51:26 +00003722 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003723 {
cristy7f3a0d12011-09-05 23:27:59 +00003724 register ssize_t
3725 i;
3726
3727 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3728 {
3729 MagickRealType
3730 pixel;
3731
3732 PixelChannel
3733 channel;
3734
3735 PixelTrait
3736 traits,
3737 unsharp_traits;
3738
cristye2a912b2011-12-05 20:02:07 +00003739 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003740 traits=GetPixelChannelMapTraits(image,channel);
cristy7f3a0d12011-09-05 23:27:59 +00003741 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3742 if ((traits == UndefinedPixelTrait) ||
3743 (unsharp_traits == UndefinedPixelTrait))
3744 continue;
cristyd09f8802012-02-04 16:44:10 +00003745 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3746 (GetPixelMask(image,p) != 0))
cristy7f3a0d12011-09-05 23:27:59 +00003747 {
cristy0beccfa2011-09-25 20:47:53 +00003748 SetPixelChannel(unsharp_image,channel,p[i],q);
cristy7f3a0d12011-09-05 23:27:59 +00003749 continue;
3750 }
cristy0beccfa2011-09-25 20:47:53 +00003751 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
cristy7f3a0d12011-09-05 23:27:59 +00003752 if (fabs(2.0*pixel) < quantum_threshold)
3753 pixel=(MagickRealType) p[i];
3754 else
3755 pixel=(MagickRealType) p[i]+amount*pixel;
cristy0beccfa2011-09-25 20:47:53 +00003756 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
cristy7f3a0d12011-09-05 23:27:59 +00003757 }
cristyed231572011-07-14 02:18:59 +00003758 p+=GetPixelChannels(image);
3759 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00003760 }
3761 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3762 status=MagickFalse;
3763 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3764 {
3765 MagickBooleanType
3766 proceed;
3767
cristyb5d5f722009-11-04 03:03:49 +00003768#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00003769 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00003770#endif
3771 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3772 if (proceed == MagickFalse)
3773 status=MagickFalse;
3774 }
3775 }
3776 unsharp_image->type=image->type;
3777 unsharp_view=DestroyCacheView(unsharp_view);
3778 image_view=DestroyCacheView(image_view);
3779 if (status == MagickFalse)
3780 unsharp_image=DestroyImage(unsharp_image);
3781 return(unsharp_image);
3782}