blob: a61be98e35c1f6055ac76f7566eef761836b69c7 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% EEEEE FFFFF FFFFF EEEEE CCCC TTTTT %
7% E F F E C T %
8% EEE FFF FFF EEE C T %
9% E F F E C T %
10% EEEEE F F EEEEE CCCC T %
11% %
12% %
13% MagickCore Image Effects Methods %
14% %
15% Software Design %
16% John Cristy %
17% October 1996 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
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"
52#include "MagickCore/draw.h"
53#include "MagickCore/enhance.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/effect.h"
57#include "MagickCore/fx.h"
58#include "MagickCore/gem.h"
59#include "MagickCore/geometry.h"
60#include "MagickCore/image-private.h"
61#include "MagickCore/list.h"
62#include "MagickCore/log.h"
63#include "MagickCore/memory_.h"
64#include "MagickCore/monitor.h"
65#include "MagickCore/monitor-private.h"
66#include "MagickCore/montage.h"
67#include "MagickCore/morphology.h"
68#include "MagickCore/paint.h"
69#include "MagickCore/pixel-accessor.h"
70#include "MagickCore/property.h"
71#include "MagickCore/quantize.h"
72#include "MagickCore/quantum.h"
73#include "MagickCore/quantum-private.h"
74#include "MagickCore/random_.h"
75#include "MagickCore/random-private.h"
76#include "MagickCore/resample.h"
77#include "MagickCore/resample-private.h"
78#include "MagickCore/resize.h"
79#include "MagickCore/resource_.h"
80#include "MagickCore/segment.h"
81#include "MagickCore/shear.h"
82#include "MagickCore/signature-private.h"
83#include "MagickCore/string_.h"
84#include "MagickCore/thread-private.h"
85#include "MagickCore/transform.h"
86#include "MagickCore/threshold.h"
cristy3ed852e2009-09-05 21:47:34 +000087
88/*
89%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90% %
91% %
92% %
93% A d a p t i v e B l u r I m a g e %
94% %
95% %
96% %
97%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98%
99% AdaptiveBlurImage() adaptively blurs the image by blurring less
100% intensely near image edges and more intensely far from edges. We blur the
101% image with a Gaussian operator of the given radius and standard deviation
102% (sigma). For reasonable results, radius should be larger than sigma. Use a
103% radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
104%
105% The format of the AdaptiveBlurImage method is:
106%
107% Image *AdaptiveBlurImage(const Image *image,const double radius,
108% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000109%
110% A description of each parameter follows:
111%
112% o image: the image.
113%
cristy3ed852e2009-09-05 21:47:34 +0000114% o radius: the radius of the Gaussian, in pixels, not counting the center
115% pixel.
116%
117% o sigma: the standard deviation of the Laplacian, in pixels.
118%
119% o exception: return any errors or warnings in this structure.
120%
121*/
122
cristyf89cb1d2011-07-07 01:24:37 +0000123MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
cristy051718b2011-08-28 22:49:25 +0000124 const char *levels,ExceptionInfo *exception)
cristyf89cb1d2011-07-07 01:24:37 +0000125{
126 double
127 black_point,
128 gamma,
129 white_point;
130
131 GeometryInfo
132 geometry_info;
133
134 MagickBooleanType
135 status;
136
137 MagickStatusType
138 flags;
139
140 /*
141 Parse levels.
142 */
143 if (levels == (char *) NULL)
144 return(MagickFalse);
145 flags=ParseGeometry(levels,&geometry_info);
146 black_point=geometry_info.rho;
147 white_point=(double) QuantumRange;
148 if ((flags & SigmaValue) != 0)
149 white_point=geometry_info.sigma;
150 gamma=1.0;
151 if ((flags & XiValue) != 0)
152 gamma=geometry_info.xi;
153 if ((flags & PercentValue) != 0)
154 {
155 black_point*=(double) image->columns*image->rows/100.0;
156 white_point*=(double) image->columns*image->rows/100.0;
157 }
158 if ((flags & SigmaValue) == 0)
159 white_point=(double) QuantumRange-black_point;
160 if ((flags & AspectValue ) == 0)
cristy7c0a0a42011-08-23 17:57:25 +0000161 status=LevelImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000162 else
cristy7c0a0a42011-08-23 17:57:25 +0000163 status=LevelizeImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000164 return(status);
165}
166
cristyf4ad9df2011-07-08 16:49:03 +0000167MagickExport Image *AdaptiveBlurImage(const Image *image,
168 const double radius,const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000169{
170#define AdaptiveBlurImageTag "Convolve/Image"
171#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
172
cristyc4c8d132010-01-07 01:58:38 +0000173 CacheView
174 *blur_view,
175 *edge_view,
176 *image_view;
177
cristy3ed852e2009-09-05 21:47:34 +0000178 double
cristy47e00502009-12-17 19:19:57 +0000179 **kernel,
180 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000181
182 Image
183 *blur_image,
184 *edge_image,
185 *gaussian_image;
186
cristy3ed852e2009-09-05 21:47:34 +0000187 MagickBooleanType
188 status;
189
cristybb503372010-05-27 20:51:26 +0000190 MagickOffsetType
191 progress;
192
cristy4c08aed2011-07-01 19:47:50 +0000193 PixelInfo
cristyddd82202009-11-03 20:14:50 +0000194 bias;
cristy3ed852e2009-09-05 21:47:34 +0000195
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 */
228 edge_image=EdgeImage(image,radius,exception);
229 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);
cristy3ed852e2009-09-05 21:47:34 +0000235 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
236 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);
246 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
247 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 {
256 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
257 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)
282 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
283 kernel=(double **) RelinquishMagickMemory(kernel);
284 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;
cristy4c08aed2011-07-01 19:47:50 +0000293 GetPixelInfo(image,&bias);
294 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000295 image_view=AcquireCacheView(image);
296 edge_view=AcquireCacheView(edge_image);
297 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000298#if defined(MAGICKCORE_OPENMP_SUPPORT)
299 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000300#endif
cristybb503372010-05-27 20:51:26 +0000301 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
cristy4c08aed2011-07-01 19:47:50 +0000303 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000304 *restrict p,
305 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000306
cristy4c08aed2011-07-01 19:47:50 +0000307 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000308 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000309
cristy117ff172010-08-15 21:35:32 +0000310 register ssize_t
311 x;
312
cristy3ed852e2009-09-05 21:47:34 +0000313 if (status == MagickFalse)
314 continue;
315 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
316 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
317 exception);
cristyacd2ed22011-08-30 01:44:23 +0000318 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000319 {
320 status=MagickFalse;
321 continue;
322 }
cristybb503372010-05-27 20:51:26 +0000323 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000324 {
cristy4c08aed2011-07-01 19:47:50 +0000325 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000326 pixel;
327
328 MagickRealType
329 alpha,
330 gamma;
331
332 register const double
cristyc47d1f82009-11-26 01:44:43 +0000333 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000334
cristybb503372010-05-27 20:51:26 +0000335 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000336 i,
337 u,
338 v;
339
340 gamma=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000341 i=(ssize_t) ceil((double) width*QuantumScale*
342 GetPixelIntensity(edge_image,r)-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000343 if (i < 0)
344 i=0;
345 else
cristybb503372010-05-27 20:51:26 +0000346 if (i > (ssize_t) width)
347 i=(ssize_t) width;
cristy3ed852e2009-09-05 21:47:34 +0000348 if ((i & 0x01) != 0)
349 i--;
cristya21afde2010-07-02 00:45:40 +0000350 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
351 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
cristy4c08aed2011-07-01 19:47:50 +0000352 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000353 break;
cristyddd82202009-11-03 20:14:50 +0000354 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000355 k=kernel[i];
cristybb503372010-05-27 20:51:26 +0000356 for (v=0; v < (ssize_t) (width-i); v++)
cristy3ed852e2009-09-05 21:47:34 +0000357 {
cristybb503372010-05-27 20:51:26 +0000358 for (u=0; u < (ssize_t) (width-i); u++)
cristy3ed852e2009-09-05 21:47:34 +0000359 {
360 alpha=1.0;
cristyed231572011-07-14 02:18:59 +0000361 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000362 (image->matte != MagickFalse))
cristy4c08aed2011-07-01 19:47:50 +0000363 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +0000364 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000365 pixel.red+=(*k)*alpha*GetPixelRed(image,p);
cristyed231572011-07-14 02:18:59 +0000366 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000367 pixel.green+=(*k)*alpha*GetPixelGreen(image,p);
cristyed231572011-07-14 02:18:59 +0000368 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000369 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p);
cristyed231572011-07-14 02:18:59 +0000370 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000371 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000372 pixel.black+=(*k)*alpha*GetPixelBlack(image,p);
cristyed231572011-07-14 02:18:59 +0000373 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000374 pixel.alpha+=(*k)*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000375 gamma+=(*k)*alpha;
376 k++;
cristyed231572011-07-14 02:18:59 +0000377 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000378 }
379 }
380 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +0000381 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000382 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000383 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000384 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000385 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000386 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000387 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000388 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000389 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000390 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000391 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +0000392 q+=GetPixelChannels(blur_image);
393 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000394 }
395 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
396 status=MagickFalse;
397 if (image->progress_monitor != (MagickProgressMonitor) NULL)
398 {
399 MagickBooleanType
400 proceed;
401
cristyb5d5f722009-11-04 03:03:49 +0000402#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9aa95be2011-07-20 21:56:45 +0000403 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000404#endif
405 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
406 image->rows);
407 if (proceed == MagickFalse)
408 status=MagickFalse;
409 }
410 }
411 blur_image->type=image->type;
412 blur_view=DestroyCacheView(blur_view);
413 edge_view=DestroyCacheView(edge_view);
414 image_view=DestroyCacheView(image_view);
415 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000416 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000417 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
418 kernel=(double **) RelinquishMagickMemory(kernel);
419 if (status == MagickFalse)
420 blur_image=DestroyImage(blur_image);
421 return(blur_image);
422}
423
424/*
425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426% %
427% %
428% %
429% A d a p t i v e S h a r p e n I m a g e %
430% %
431% %
432% %
433%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434%
435% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
436% intensely near image edges and less intensely far from edges. We sharpen the
437% image with a Gaussian operator of the given radius and standard deviation
438% (sigma). For reasonable results, radius should be larger than sigma. Use a
439% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
440%
441% The format of the AdaptiveSharpenImage method is:
442%
443% Image *AdaptiveSharpenImage(const Image *image,const double radius,
444% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000445%
446% A description of each parameter follows:
447%
448% o image: the image.
449%
cristy3ed852e2009-09-05 21:47:34 +0000450% o radius: the radius of the Gaussian, in pixels, not counting the center
451% pixel.
452%
453% o sigma: the standard deviation of the Laplacian, in pixels.
454%
455% o exception: return any errors or warnings in this structure.
456%
457*/
cristy3ed852e2009-09-05 21:47:34 +0000458MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
459 const double sigma,ExceptionInfo *exception)
460{
cristy3ed852e2009-09-05 21:47:34 +0000461#define AdaptiveSharpenImageTag "Convolve/Image"
462#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
463
cristyc4c8d132010-01-07 01:58:38 +0000464 CacheView
465 *sharp_view,
466 *edge_view,
467 *image_view;
468
cristy3ed852e2009-09-05 21:47:34 +0000469 double
cristy47e00502009-12-17 19:19:57 +0000470 **kernel,
471 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000472
473 Image
474 *sharp_image,
475 *edge_image,
476 *gaussian_image;
477
cristy3ed852e2009-09-05 21:47:34 +0000478 MagickBooleanType
479 status;
480
cristybb503372010-05-27 20:51:26 +0000481 MagickOffsetType
482 progress;
483
cristy4c08aed2011-07-01 19:47:50 +0000484 PixelInfo
cristyddd82202009-11-03 20:14:50 +0000485 bias;
cristy3ed852e2009-09-05 21:47:34 +0000486
cristybb503372010-05-27 20:51:26 +0000487 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000488 i;
cristy3ed852e2009-09-05 21:47:34 +0000489
cristybb503372010-05-27 20:51:26 +0000490 size_t
cristy3ed852e2009-09-05 21:47:34 +0000491 width;
492
cristybb503372010-05-27 20:51:26 +0000493 ssize_t
494 j,
495 k,
496 u,
497 v,
498 y;
499
cristy3ed852e2009-09-05 21:47:34 +0000500 assert(image != (const Image *) NULL);
501 assert(image->signature == MagickSignature);
502 if (image->debug != MagickFalse)
503 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
504 assert(exception != (ExceptionInfo *) NULL);
505 assert(exception->signature == MagickSignature);
506 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
507 if (sharp_image == (Image *) NULL)
508 return((Image *) NULL);
509 if (fabs(sigma) <= MagickEpsilon)
510 return(sharp_image);
cristy574cc262011-08-05 01:23:58 +0000511 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000512 {
cristy3ed852e2009-09-05 21:47:34 +0000513 sharp_image=DestroyImage(sharp_image);
514 return((Image *) NULL);
515 }
516 /*
517 Edge detect the image brighness channel, level, sharp, and level again.
518 */
519 edge_image=EdgeImage(image,radius,exception);
520 if (edge_image == (Image *) NULL)
521 {
522 sharp_image=DestroyImage(sharp_image);
523 return((Image *) NULL);
524 }
cristy051718b2011-08-28 22:49:25 +0000525 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000526 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
527 if (gaussian_image != (Image *) NULL)
528 {
529 edge_image=DestroyImage(edge_image);
530 edge_image=gaussian_image;
531 }
cristy051718b2011-08-28 22:49:25 +0000532 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000533 /*
534 Create a set of kernels from maximum (radius,sigma) to minimum.
535 */
536 width=GetOptimalKernelWidth2D(radius,sigma);
537 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
538 if (kernel == (double **) NULL)
539 {
540 edge_image=DestroyImage(edge_image);
541 sharp_image=DestroyImage(sharp_image);
542 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
543 }
544 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000545 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000546 {
547 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
548 sizeof(**kernel));
549 if (kernel[i] == (double *) NULL)
550 break;
cristy47e00502009-12-17 19:19:57 +0000551 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000552 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000553 k=0;
554 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000555 {
cristy47e00502009-12-17 19:19:57 +0000556 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000557 {
cristy4205a3c2010-09-12 20:19:59 +0000558 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
559 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000560 normalize+=kernel[i][k];
561 k++;
cristy3ed852e2009-09-05 21:47:34 +0000562 }
563 }
cristy3ed852e2009-09-05 21:47:34 +0000564 if (fabs(normalize) <= MagickEpsilon)
565 normalize=1.0;
566 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000567 for (k=0; k < (j*j); k++)
568 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000569 }
cristybb503372010-05-27 20:51:26 +0000570 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000571 {
572 for (i-=2; i >= 0; i-=2)
573 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
574 kernel=(double **) RelinquishMagickMemory(kernel);
575 edge_image=DestroyImage(edge_image);
576 sharp_image=DestroyImage(sharp_image);
577 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
578 }
579 /*
580 Adaptively sharpen image.
581 */
582 status=MagickTrue;
583 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000584 GetPixelInfo(image,&bias);
585 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000586 image_view=AcquireCacheView(image);
587 edge_view=AcquireCacheView(edge_image);
588 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000589#if defined(MAGICKCORE_OPENMP_SUPPORT)
590 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000591#endif
cristybb503372010-05-27 20:51:26 +0000592 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000593 {
cristy4c08aed2011-07-01 19:47:50 +0000594 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000595 *restrict p,
596 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000597
cristy4c08aed2011-07-01 19:47:50 +0000598 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000599 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000600
cristy117ff172010-08-15 21:35:32 +0000601 register ssize_t
602 x;
603
cristy3ed852e2009-09-05 21:47:34 +0000604 if (status == MagickFalse)
605 continue;
606 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
607 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
608 exception);
cristy4c08aed2011-07-01 19:47:50 +0000609 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000610 {
611 status=MagickFalse;
612 continue;
613 }
cristybb503372010-05-27 20:51:26 +0000614 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000615 {
cristy4c08aed2011-07-01 19:47:50 +0000616 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000617 pixel;
618
619 MagickRealType
620 alpha,
621 gamma;
622
623 register const double
cristyc47d1f82009-11-26 01:44:43 +0000624 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000625
cristybb503372010-05-27 20:51:26 +0000626 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000627 i,
628 u,
629 v;
630
631 gamma=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000632 i=(ssize_t) ceil((double) width*QuantumScale*
633 GetPixelIntensity(edge_image,r)-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000634 if (i < 0)
635 i=0;
636 else
cristybb503372010-05-27 20:51:26 +0000637 if (i > (ssize_t) width)
638 i=(ssize_t) width;
cristy3ed852e2009-09-05 21:47:34 +0000639 if ((i & 0x01) != 0)
640 i--;
cristy117ff172010-08-15 21:35:32 +0000641 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
642 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
cristy4c08aed2011-07-01 19:47:50 +0000643 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000644 break;
cristy3ed852e2009-09-05 21:47:34 +0000645 k=kernel[i];
cristyddd82202009-11-03 20:14:50 +0000646 pixel=bias;
cristybb503372010-05-27 20:51:26 +0000647 for (v=0; v < (ssize_t) (width-i); v++)
cristy3ed852e2009-09-05 21:47:34 +0000648 {
cristybb503372010-05-27 20:51:26 +0000649 for (u=0; u < (ssize_t) (width-i); u++)
cristy3ed852e2009-09-05 21:47:34 +0000650 {
651 alpha=1.0;
cristyed231572011-07-14 02:18:59 +0000652 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000653 (image->matte != MagickFalse))
cristy4c08aed2011-07-01 19:47:50 +0000654 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +0000655 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000656 pixel.red+=(*k)*alpha*GetPixelRed(image,p);
cristyed231572011-07-14 02:18:59 +0000657 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000658 pixel.green+=(*k)*alpha*GetPixelGreen(image,p);
cristyed231572011-07-14 02:18:59 +0000659 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000660 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p);
cristyed231572011-07-14 02:18:59 +0000661 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000662 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000663 pixel.black+=(*k)*alpha*GetPixelBlack(image,p);
cristyed231572011-07-14 02:18:59 +0000664 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000665 pixel.alpha+=(*k)*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000666 gamma+=(*k)*alpha;
667 k++;
cristyed231572011-07-14 02:18:59 +0000668 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000669 }
670 }
671 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +0000672 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000673 SetPixelRed(sharp_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000674 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000675 SetPixelGreen(sharp_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000676 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000677 SetPixelBlue(sharp_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000678 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000679 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000680 SetPixelBlack(sharp_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000681 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000682 SetPixelAlpha(sharp_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +0000683 q+=GetPixelChannels(sharp_image);
684 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000685 }
686 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
687 status=MagickFalse;
688 if (image->progress_monitor != (MagickProgressMonitor) NULL)
689 {
690 MagickBooleanType
691 proceed;
692
cristyb5d5f722009-11-04 03:03:49 +0000693#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +0000694 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000695#endif
696 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
697 image->rows);
698 if (proceed == MagickFalse)
699 status=MagickFalse;
700 }
701 }
702 sharp_image->type=image->type;
703 sharp_view=DestroyCacheView(sharp_view);
704 edge_view=DestroyCacheView(edge_view);
705 image_view=DestroyCacheView(image_view);
706 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000707 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000708 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
709 kernel=(double **) RelinquishMagickMemory(kernel);
710 if (status == MagickFalse)
711 sharp_image=DestroyImage(sharp_image);
712 return(sharp_image);
713}
714
715/*
716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717% %
718% %
719% %
720% B l u r I m a g e %
721% %
722% %
723% %
724%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725%
726% BlurImage() blurs an image. We convolve the image with a Gaussian operator
727% of the given radius and standard deviation (sigma). For reasonable results,
728% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
729% selects a suitable radius for you.
730%
731% BlurImage() differs from GaussianBlurImage() in that it uses a separable
732% kernel which is faster but mathematically equivalent to the non-separable
733% kernel.
734%
735% The format of the BlurImage method is:
736%
737% Image *BlurImage(const Image *image,const double radius,
738% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000739%
740% A description of each parameter follows:
741%
742% o image: the image.
743%
cristy3ed852e2009-09-05 21:47:34 +0000744% o radius: the radius of the Gaussian, in pixels, not counting the center
745% pixel.
746%
747% o sigma: the standard deviation of the Gaussian, in pixels.
748%
749% o exception: return any errors or warnings in this structure.
750%
751*/
752
cristybb503372010-05-27 20:51:26 +0000753static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000754{
cristy3ed852e2009-09-05 21:47:34 +0000755 double
cristy47e00502009-12-17 19:19:57 +0000756 *kernel,
757 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000758
cristy117ff172010-08-15 21:35:32 +0000759 register ssize_t
760 i;
761
cristybb503372010-05-27 20:51:26 +0000762 ssize_t
cristy47e00502009-12-17 19:19:57 +0000763 j,
764 k;
cristy3ed852e2009-09-05 21:47:34 +0000765
cristy3ed852e2009-09-05 21:47:34 +0000766 /*
767 Generate a 1-D convolution kernel.
768 */
769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
770 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
771 if (kernel == (double *) NULL)
772 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000773 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000774 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000775 i=0;
776 for (k=(-j); k <= j; k++)
777 {
cristy4205a3c2010-09-12 20:19:59 +0000778 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
779 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000780 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000781 i++;
782 }
cristybb503372010-05-27 20:51:26 +0000783 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000784 kernel[i]/=normalize;
785 return(kernel);
786}
787
cristyf4ad9df2011-07-08 16:49:03 +0000788MagickExport Image *BlurImage(const Image *image,const double radius,
789 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000790{
791#define BlurImageTag "Blur/Image"
792
cristyc4c8d132010-01-07 01:58:38 +0000793 CacheView
794 *blur_view,
795 *image_view;
796
cristy3ed852e2009-09-05 21:47:34 +0000797 double
798 *kernel;
799
800 Image
801 *blur_image;
802
cristy3ed852e2009-09-05 21:47:34 +0000803 MagickBooleanType
804 status;
805
cristybb503372010-05-27 20:51:26 +0000806 MagickOffsetType
807 progress;
808
cristy4c08aed2011-07-01 19:47:50 +0000809 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000810 bias;
811
cristybb503372010-05-27 20:51:26 +0000812 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000813 i;
814
cristybb503372010-05-27 20:51:26 +0000815 size_t
cristy3ed852e2009-09-05 21:47:34 +0000816 width;
817
cristybb503372010-05-27 20:51:26 +0000818 ssize_t
819 x,
820 y;
821
cristy3ed852e2009-09-05 21:47:34 +0000822 /*
823 Initialize blur image attributes.
824 */
825 assert(image != (Image *) NULL);
826 assert(image->signature == MagickSignature);
827 if (image->debug != MagickFalse)
828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
829 assert(exception != (ExceptionInfo *) NULL);
830 assert(exception->signature == MagickSignature);
831 blur_image=CloneImage(image,0,0,MagickTrue,exception);
832 if (blur_image == (Image *) NULL)
833 return((Image *) NULL);
834 if (fabs(sigma) <= MagickEpsilon)
835 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000836 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000837 {
cristy3ed852e2009-09-05 21:47:34 +0000838 blur_image=DestroyImage(blur_image);
839 return((Image *) NULL);
840 }
841 width=GetOptimalKernelWidth1D(radius,sigma);
842 kernel=GetBlurKernel(width,sigma);
843 if (kernel == (double *) NULL)
844 {
845 blur_image=DestroyImage(blur_image);
846 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
847 }
848 if (image->debug != MagickFalse)
849 {
850 char
851 format[MaxTextExtent],
852 *message;
853
854 register const double
855 *k;
856
857 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000858 " BlurImage with %.20g kernel:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000859 message=AcquireString("");
860 k=kernel;
cristybb503372010-05-27 20:51:26 +0000861 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000862 {
863 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000864 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000865 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000866 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000867 (void) ConcatenateString(&message,format);
868 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
869 }
870 message=DestroyString(message);
871 }
872 /*
873 Blur rows.
874 */
875 status=MagickTrue;
876 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000877 GetPixelInfo(image,&bias);
878 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000879 image_view=AcquireCacheView(image);
880 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000881#if defined(MAGICKCORE_OPENMP_SUPPORT)
882 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000883#endif
cristybb503372010-05-27 20:51:26 +0000884 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000885 {
cristy4c08aed2011-07-01 19:47:50 +0000886 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000887 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000888
cristy4c08aed2011-07-01 19:47:50 +0000889 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000890 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000891
cristy117ff172010-08-15 21:35:32 +0000892 register ssize_t
893 x;
894
cristy3ed852e2009-09-05 21:47:34 +0000895 if (status == MagickFalse)
896 continue;
cristy117ff172010-08-15 21:35:32 +0000897 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
898 image->columns+width,1,exception);
cristy3ed852e2009-09-05 21:47:34 +0000899 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
900 exception);
cristy4c08aed2011-07-01 19:47:50 +0000901 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000902 {
903 status=MagickFalse;
904 continue;
905 }
cristybb503372010-05-27 20:51:26 +0000906 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000907 {
cristy4c08aed2011-07-01 19:47:50 +0000908 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000909 pixel;
910
911 register const double
cristyc47d1f82009-11-26 01:44:43 +0000912 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000913
cristy4c08aed2011-07-01 19:47:50 +0000914 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000915 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000916
cristybb503372010-05-27 20:51:26 +0000917 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000918 i;
919
cristyddd82202009-11-03 20:14:50 +0000920 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000921 k=kernel;
922 kernel_pixels=p;
cristyed231572011-07-14 02:18:59 +0000923 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
cristyf4ad9df2011-07-08 16:49:03 +0000924 (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000925 {
cristybb503372010-05-27 20:51:26 +0000926 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000927 {
cristy4c08aed2011-07-01 19:47:50 +0000928 pixel.red+=(*k)*GetPixelRed(image,kernel_pixels);
929 pixel.green+=(*k)*GetPixelGreen(image,kernel_pixels);
930 pixel.blue+=(*k)*GetPixelBlue(image,kernel_pixels);
931 if (image->colorspace == CMYKColorspace)
932 pixel.black+=(*k)*GetPixelBlack(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000933 k++;
cristyed231572011-07-14 02:18:59 +0000934 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000935 }
cristyed231572011-07-14 02:18:59 +0000936 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000937 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000938 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000939 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000940 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000941 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000942 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +0000943 (blur_image->colorspace == CMYKColorspace))
944 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000945 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000946 {
947 k=kernel;
948 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +0000949 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000950 {
cristy4c08aed2011-07-01 19:47:50 +0000951 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000952 k++;
cristyed231572011-07-14 02:18:59 +0000953 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000954 }
cristy4c08aed2011-07-01 19:47:50 +0000955 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000956 }
957 }
958 else
959 {
960 MagickRealType
961 alpha,
962 gamma;
963
964 gamma=0.0;
cristybb503372010-05-27 20:51:26 +0000965 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000966 {
cristy4c08aed2011-07-01 19:47:50 +0000967 alpha=(MagickRealType) (QuantumScale*
968 GetPixelAlpha(image,kernel_pixels));
969 pixel.red+=(*k)*alpha*GetPixelRed(image,kernel_pixels);
970 pixel.green+=(*k)*alpha*GetPixelGreen(image,kernel_pixels);
971 pixel.blue+=(*k)*alpha*GetPixelBlue(image,kernel_pixels);
972 if (image->colorspace == CMYKColorspace)
973 pixel.black+=(*k)*alpha*GetPixelBlack(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000974 gamma+=(*k)*alpha;
975 k++;
cristyed231572011-07-14 02:18:59 +0000976 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000977 }
978 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +0000979 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000980 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000981 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000982 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000983 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000984 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000985 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +0000986 (blur_image->colorspace == CMYKColorspace))
987 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000988 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000989 {
990 k=kernel;
991 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +0000992 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000993 {
cristy4c08aed2011-07-01 19:47:50 +0000994 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000995 k++;
cristyed231572011-07-14 02:18:59 +0000996 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000997 }
cristy4c08aed2011-07-01 19:47:50 +0000998 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000999 }
1000 }
cristyed231572011-07-14 02:18:59 +00001001 p+=GetPixelChannels(image);
1002 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001003 }
1004 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1005 status=MagickFalse;
1006 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1007 {
1008 MagickBooleanType
1009 proceed;
1010
cristyb5d5f722009-11-04 03:03:49 +00001011#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001012 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001013#endif
1014 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1015 blur_image->columns);
1016 if (proceed == MagickFalse)
1017 status=MagickFalse;
1018 }
1019 }
1020 blur_view=DestroyCacheView(blur_view);
1021 image_view=DestroyCacheView(image_view);
1022 /*
1023 Blur columns.
1024 */
1025 image_view=AcquireCacheView(blur_image);
1026 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001027#if defined(MAGICKCORE_OPENMP_SUPPORT)
1028 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001029#endif
cristybb503372010-05-27 20:51:26 +00001030 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001031 {
cristy4c08aed2011-07-01 19:47:50 +00001032 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001033 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001034
cristy4c08aed2011-07-01 19:47:50 +00001035 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001036 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001037
cristy117ff172010-08-15 21:35:32 +00001038 register ssize_t
1039 y;
1040
cristy3ed852e2009-09-05 21:47:34 +00001041 if (status == MagickFalse)
1042 continue;
cristy117ff172010-08-15 21:35:32 +00001043 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1044 image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001045 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001046 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001047 {
1048 status=MagickFalse;
1049 continue;
1050 }
cristybb503372010-05-27 20:51:26 +00001051 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001052 {
cristy4c08aed2011-07-01 19:47:50 +00001053 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001054 pixel;
1055
1056 register const double
cristyc47d1f82009-11-26 01:44:43 +00001057 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00001058
cristy4c08aed2011-07-01 19:47:50 +00001059 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001060 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001061
cristybb503372010-05-27 20:51:26 +00001062 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001063 i;
1064
cristyddd82202009-11-03 20:14:50 +00001065 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00001066 k=kernel;
1067 kernel_pixels=p;
cristyed231572011-07-14 02:18:59 +00001068 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
cristyf4ad9df2011-07-08 16:49:03 +00001069 (blur_image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001070 {
cristybb503372010-05-27 20:51:26 +00001071 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001072 {
cristy4c08aed2011-07-01 19:47:50 +00001073 pixel.red+=(*k)*GetPixelRed(blur_image,kernel_pixels);
1074 pixel.green+=(*k)*GetPixelGreen(blur_image,kernel_pixels);
1075 pixel.blue+=(*k)*GetPixelBlue(blur_image,kernel_pixels);
1076 if (blur_image->colorspace == CMYKColorspace)
1077 pixel.black+=(*k)*GetPixelBlack(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001078 k++;
cristyed231572011-07-14 02:18:59 +00001079 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001080 }
cristyed231572011-07-14 02:18:59 +00001081 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001082 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00001083 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001084 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00001085 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001086 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00001087 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00001088 (blur_image->colorspace == CMYKColorspace))
1089 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +00001090 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001091 {
1092 k=kernel;
1093 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001094 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001095 {
cristy4c08aed2011-07-01 19:47:50 +00001096 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001097 k++;
cristyed231572011-07-14 02:18:59 +00001098 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001099 }
cristy4c08aed2011-07-01 19:47:50 +00001100 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001101 }
1102 }
1103 else
1104 {
1105 MagickRealType
1106 alpha,
1107 gamma;
1108
1109 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001110 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001111 {
cristy46f08202010-01-10 04:04:21 +00001112 alpha=(MagickRealType) (QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +00001113 GetPixelAlpha(blur_image,kernel_pixels));
1114 pixel.red+=(*k)*alpha*GetPixelRed(blur_image,kernel_pixels);
1115 pixel.green+=(*k)*alpha*GetPixelGreen(blur_image,kernel_pixels);
1116 pixel.blue+=(*k)*alpha*GetPixelBlue(blur_image,kernel_pixels);
1117 if (blur_image->colorspace == CMYKColorspace)
1118 pixel.black+=(*k)*alpha*GetPixelBlack(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001119 gamma+=(*k)*alpha;
1120 k++;
cristyed231572011-07-14 02:18:59 +00001121 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001122 }
1123 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00001124 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001125 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00001126 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001127 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00001128 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001129 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00001130 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00001131 (blur_image->colorspace == CMYKColorspace))
1132 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +00001133 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001134 {
1135 k=kernel;
1136 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001137 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001138 {
cristy4c08aed2011-07-01 19:47:50 +00001139 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001140 k++;
cristyed231572011-07-14 02:18:59 +00001141 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001142 }
cristy4c08aed2011-07-01 19:47:50 +00001143 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001144 }
1145 }
cristyed231572011-07-14 02:18:59 +00001146 p+=GetPixelChannels(blur_image);
1147 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001148 }
1149 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1150 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001151 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001152 {
1153 MagickBooleanType
1154 proceed;
1155
cristyb5d5f722009-11-04 03:03:49 +00001156#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001157 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001158#endif
cristy4c08aed2011-07-01 19:47:50 +00001159 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1160 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001161 if (proceed == MagickFalse)
1162 status=MagickFalse;
1163 }
1164 }
1165 blur_view=DestroyCacheView(blur_view);
1166 image_view=DestroyCacheView(image_view);
1167 kernel=(double *) RelinquishMagickMemory(kernel);
1168 if (status == MagickFalse)
1169 blur_image=DestroyImage(blur_image);
1170 blur_image->type=image->type;
1171 return(blur_image);
1172}
1173
1174/*
1175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1176% %
1177% %
1178% %
cristyfccdab92009-11-30 16:43:57 +00001179% C o n v o l v e I m a g e %
1180% %
1181% %
1182% %
1183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1184%
1185% ConvolveImage() applies a custom convolution kernel to the image.
1186%
1187% The format of the ConvolveImage method is:
1188%
cristy5e6be1e2011-07-16 01:23:39 +00001189% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1190% ExceptionInfo *exception)
1191%
cristyfccdab92009-11-30 16:43:57 +00001192% A description of each parameter follows:
1193%
1194% o image: the image.
1195%
cristy5e6be1e2011-07-16 01:23:39 +00001196% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001197%
1198% o exception: return any errors or warnings in this structure.
1199%
1200*/
cristy5e6be1e2011-07-16 01:23:39 +00001201MagickExport Image *ConvolveImage(const Image *image,
1202 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001203{
cristyfccdab92009-11-30 16:43:57 +00001204#define ConvolveImageTag "Convolve/Image"
1205
cristyc4c8d132010-01-07 01:58:38 +00001206 CacheView
1207 *convolve_view,
cristy105ba3c2011-07-18 02:28:38 +00001208 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00001209
cristyfccdab92009-11-30 16:43:57 +00001210 Image
1211 *convolve_image;
1212
cristyfccdab92009-11-30 16:43:57 +00001213 MagickBooleanType
1214 status;
1215
cristybb503372010-05-27 20:51:26 +00001216 MagickOffsetType
1217 progress;
1218
cristybb503372010-05-27 20:51:26 +00001219 ssize_t
cristy574cc262011-08-05 01:23:58 +00001220 center,
cristybb503372010-05-27 20:51:26 +00001221 y;
1222
cristyfccdab92009-11-30 16:43:57 +00001223 /*
1224 Initialize convolve image attributes.
1225 */
1226 assert(image != (Image *) NULL);
1227 assert(image->signature == MagickSignature);
1228 if (image->debug != MagickFalse)
1229 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1230 assert(exception != (ExceptionInfo *) NULL);
1231 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001232 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001233 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001234 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1235 exception);
cristyfccdab92009-11-30 16:43:57 +00001236 if (convolve_image == (Image *) NULL)
1237 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001238 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001239 {
cristyfccdab92009-11-30 16:43:57 +00001240 convolve_image=DestroyImage(convolve_image);
1241 return((Image *) NULL);
1242 }
1243 if (image->debug != MagickFalse)
1244 {
1245 char
1246 format[MaxTextExtent],
1247 *message;
1248
cristy117ff172010-08-15 21:35:32 +00001249 register const double
1250 *k;
1251
cristy4e154852011-07-14 13:28:53 +00001252 register ssize_t
1253 u;
1254
cristybb503372010-05-27 20:51:26 +00001255 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001256 v;
1257
cristyfccdab92009-11-30 16:43:57 +00001258 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001259 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1260 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001261 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001262 k=kernel_info->values;
1263 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001264 {
1265 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001266 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001267 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001268 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001269 {
cristyb51dff52011-05-19 16:55:47 +00001270 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001271 (void) ConcatenateString(&message,format);
1272 }
1273 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1274 }
1275 message=DestroyString(message);
1276 }
1277 /*
cristyfccdab92009-11-30 16:43:57 +00001278 Convolve image.
1279 */
cristy574cc262011-08-05 01:23:58 +00001280 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
1281 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2);
cristyfccdab92009-11-30 16:43:57 +00001282 status=MagickTrue;
1283 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001284 image_view=AcquireCacheView(image);
1285 convolve_view=AcquireCacheView(convolve_image);
1286#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy175653e2011-07-10 23:13:34 +00001287 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001288#endif
cristybb503372010-05-27 20:51:26 +00001289 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001290 {
cristy4c08aed2011-07-01 19:47:50 +00001291 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001292 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001293
cristy4c08aed2011-07-01 19:47:50 +00001294 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001295 *restrict q;
1296
cristy117ff172010-08-15 21:35:32 +00001297 register ssize_t
1298 x;
1299
cristyfccdab92009-11-30 16:43:57 +00001300 if (status == MagickFalse)
1301 continue;
cristy105ba3c2011-07-18 02:28:38 +00001302 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1303 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1304 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001305 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001306 exception);
cristy105ba3c2011-07-18 02:28:38 +00001307 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001308 {
1309 status=MagickFalse;
1310 continue;
1311 }
cristybb503372010-05-27 20:51:26 +00001312 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001313 {
cristybb503372010-05-27 20:51:26 +00001314 register ssize_t
cristyed231572011-07-14 02:18:59 +00001315 i;
cristyfccdab92009-11-30 16:43:57 +00001316
cristya30d9ba2011-07-23 21:00:48 +00001317 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001318 {
cristyed231572011-07-14 02:18:59 +00001319 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001320 alpha,
1321 gamma,
cristyed231572011-07-14 02:18:59 +00001322 pixel;
1323
1324 PixelChannel
1325 channel;
1326
1327 PixelTrait
1328 convolve_traits,
1329 traits;
1330
1331 register const double
1332 *restrict k;
1333
1334 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001335 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001336
1337 register ssize_t
1338 u;
1339
1340 ssize_t
1341 v;
1342
cristy30301712011-07-18 15:06:51 +00001343 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristy4e154852011-07-14 13:28:53 +00001344 if (traits == UndefinedPixelTrait)
cristyed231572011-07-14 02:18:59 +00001345 continue;
cristy30301712011-07-18 15:06:51 +00001346 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
cristy4e154852011-07-14 13:28:53 +00001347 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
1348 if (convolve_traits == UndefinedPixelTrait)
1349 continue;
1350 if ((convolve_traits & CopyPixelTrait) != 0)
1351 {
cristyf7dc44c2011-07-20 14:41:15 +00001352 q[channel]=p[center+i];
cristy4e154852011-07-14 13:28:53 +00001353 continue;
1354 }
cristy5e6be1e2011-07-16 01:23:39 +00001355 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001356 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001357 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001358 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001359 {
cristyed231572011-07-14 02:18:59 +00001360 /*
cristy4e154852011-07-14 13:28:53 +00001361 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001362 */
cristyeb52cde2011-07-17 01:52:52 +00001363 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001364 {
cristyeb52cde2011-07-17 01:52:52 +00001365 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001366 {
cristyeb52cde2011-07-17 01:52:52 +00001367 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001368 k++;
cristya30d9ba2011-07-23 21:00:48 +00001369 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001370 }
cristya30d9ba2011-07-23 21:00:48 +00001371 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001372 }
cristyf7dc44c2011-07-20 14:41:15 +00001373 q[channel]=ClampToQuantum(pixel);
cristy4e154852011-07-14 13:28:53 +00001374 continue;
cristyed231572011-07-14 02:18:59 +00001375 }
cristy4e154852011-07-14 13:28:53 +00001376 /*
1377 Alpha blending.
1378 */
1379 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001380 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001381 {
cristyeb52cde2011-07-17 01:52:52 +00001382 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001383 {
cristyeb52cde2011-07-17 01:52:52 +00001384 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1385 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001386 gamma+=(*k)*alpha;
1387 k++;
cristya30d9ba2011-07-23 21:00:48 +00001388 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001389 }
cristya30d9ba2011-07-23 21:00:48 +00001390 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001391 }
cristy1ce96d02011-07-14 17:57:24 +00001392 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristye7a41c92011-07-20 21:31:01 +00001393 q[channel]=ClampToQuantum(gamma*pixel);
cristyed231572011-07-14 02:18:59 +00001394 }
cristya30d9ba2011-07-23 21:00:48 +00001395 p+=GetPixelChannels(image);
1396 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001397 }
cristyed231572011-07-14 02:18:59 +00001398 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001399 status=MagickFalse;
1400 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1401 {
1402 MagickBooleanType
1403 proceed;
1404
1405#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001406 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001407#endif
1408 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1409 if (proceed == MagickFalse)
1410 status=MagickFalse;
1411 }
1412 }
1413 convolve_image->type=image->type;
1414 convolve_view=DestroyCacheView(convolve_view);
1415 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001416 if (status == MagickFalse)
1417 convolve_image=DestroyImage(convolve_image);
1418 return(convolve_image);
1419}
1420
1421/*
1422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1423% %
1424% %
1425% %
cristy3ed852e2009-09-05 21:47:34 +00001426% D e s p e c k l e I m a g e %
1427% %
1428% %
1429% %
1430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431%
1432% DespeckleImage() reduces the speckle noise in an image while perserving the
1433% edges of the original image.
1434%
1435% The format of the DespeckleImage method is:
1436%
1437% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1438%
1439% A description of each parameter follows:
1440%
1441% o image: the image.
1442%
1443% o exception: return any errors or warnings in this structure.
1444%
1445*/
1446
cristybb503372010-05-27 20:51:26 +00001447static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1448 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
cristy3ed852e2009-09-05 21:47:34 +00001449 const int polarity)
1450{
cristy3ed852e2009-09-05 21:47:34 +00001451 MagickRealType
1452 v;
1453
cristy3ed852e2009-09-05 21:47:34 +00001454 register Quantum
1455 *p,
1456 *q,
1457 *r,
1458 *s;
1459
cristy117ff172010-08-15 21:35:32 +00001460 register ssize_t
1461 x;
1462
1463 ssize_t
1464 y;
1465
cristy3ed852e2009-09-05 21:47:34 +00001466 assert(f != (Quantum *) NULL);
1467 assert(g != (Quantum *) NULL);
1468 p=f+(columns+2);
1469 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001470 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1471 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001472 {
1473 p++;
1474 q++;
1475 r++;
1476 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001477 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001478 {
1479 v=(MagickRealType) (*p);
1480 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1481 v+=ScaleCharToQuantum(1);
1482 *q=(Quantum) v;
1483 p++;
1484 q++;
1485 r++;
1486 }
1487 else
cristybb503372010-05-27 20:51:26 +00001488 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001489 {
1490 v=(MagickRealType) (*p);
1491 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
cristybb503372010-05-27 20:51:26 +00001492 v-=(ssize_t) ScaleCharToQuantum(1);
cristy3ed852e2009-09-05 21:47:34 +00001493 *q=(Quantum) v;
1494 p++;
1495 q++;
1496 r++;
1497 }
1498 p++;
1499 q++;
1500 r++;
1501 }
1502 p=f+(columns+2);
1503 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001504 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1505 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1506 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001507 {
1508 p++;
1509 q++;
1510 r++;
1511 s++;
1512 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001513 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001514 {
1515 v=(MagickRealType) (*q);
1516 if (((MagickRealType) *s >=
1517 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1518 ((MagickRealType) *r > v))
1519 v+=ScaleCharToQuantum(1);
1520 *p=(Quantum) v;
1521 p++;
1522 q++;
1523 r++;
1524 s++;
1525 }
1526 else
cristybb503372010-05-27 20:51:26 +00001527 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001528 {
1529 v=(MagickRealType) (*q);
1530 if (((MagickRealType) *s <=
1531 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1532 ((MagickRealType) *r < v))
1533 v-=(MagickRealType) ScaleCharToQuantum(1);
1534 *p=(Quantum) v;
1535 p++;
1536 q++;
1537 r++;
1538 s++;
1539 }
1540 p++;
1541 q++;
1542 r++;
1543 s++;
1544 }
1545}
1546
1547MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1548{
1549#define DespeckleImageTag "Despeckle/Image"
1550
cristy2407fc22009-09-11 00:55:25 +00001551 CacheView
1552 *despeckle_view,
1553 *image_view;
1554
cristy3ed852e2009-09-05 21:47:34 +00001555 Image
1556 *despeckle_image;
1557
cristy3ed852e2009-09-05 21:47:34 +00001558 MagickBooleanType
1559 status;
1560
cristya58c3172011-02-19 19:23:11 +00001561 register ssize_t
1562 i;
1563
cristy3ed852e2009-09-05 21:47:34 +00001564 Quantum
cristy65b9f392011-02-22 14:22:54 +00001565 *restrict buffers,
1566 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +00001567
1568 size_t
cristya58c3172011-02-19 19:23:11 +00001569 length,
1570 number_channels;
cristy117ff172010-08-15 21:35:32 +00001571
cristybb503372010-05-27 20:51:26 +00001572 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001573 X[4] = {0, 1, 1,-1},
1574 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001575
cristy3ed852e2009-09-05 21:47:34 +00001576 /*
1577 Allocate despeckled image.
1578 */
1579 assert(image != (const Image *) NULL);
1580 assert(image->signature == MagickSignature);
1581 if (image->debug != MagickFalse)
1582 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1583 assert(exception != (ExceptionInfo *) NULL);
1584 assert(exception->signature == MagickSignature);
1585 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1586 exception);
1587 if (despeckle_image == (Image *) NULL)
1588 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001589 if (SetImageStorageClass(despeckle_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001590 {
cristy3ed852e2009-09-05 21:47:34 +00001591 despeckle_image=DestroyImage(despeckle_image);
1592 return((Image *) NULL);
1593 }
1594 /*
1595 Allocate image buffers.
1596 */
1597 length=(size_t) ((image->columns+2)*(image->rows+2));
cristy65b9f392011-02-22 14:22:54 +00001598 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1599 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1600 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001601 {
cristy65b9f392011-02-22 14:22:54 +00001602 if (buffers != (Quantum *) NULL)
1603 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1604 if (pixels != (Quantum *) NULL)
1605 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001606 despeckle_image=DestroyImage(despeckle_image);
1607 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1608 }
1609 /*
1610 Reduce speckle in the image.
1611 */
1612 status=MagickTrue;
cristy109695a2011-02-19 19:38:14 +00001613 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
cristy3ed852e2009-09-05 21:47:34 +00001614 image_view=AcquireCacheView(image);
1615 despeckle_view=AcquireCacheView(despeckle_image);
cristy8df3d002011-02-19 19:40:59 +00001616 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001617 {
cristy3ed852e2009-09-05 21:47:34 +00001618 register Quantum
1619 *buffer,
1620 *pixel;
1621
cristyc1488b52011-02-19 18:54:15 +00001622 register ssize_t
cristya58c3172011-02-19 19:23:11 +00001623 k,
cristyc1488b52011-02-19 18:54:15 +00001624 x;
1625
cristy117ff172010-08-15 21:35:32 +00001626 ssize_t
1627 j,
1628 y;
1629
cristy3ed852e2009-09-05 21:47:34 +00001630 if (status == MagickFalse)
1631 continue;
cristy65b9f392011-02-22 14:22:54 +00001632 pixel=pixels;
cristy3ed852e2009-09-05 21:47:34 +00001633 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
cristy65b9f392011-02-22 14:22:54 +00001634 buffer=buffers;
cristybb503372010-05-27 20:51:26 +00001635 j=(ssize_t) image->columns+2;
1636 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001637 {
cristy4c08aed2011-07-01 19:47:50 +00001638 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001639 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001640
1641 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001642 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001643 break;
1644 j++;
cristybb503372010-05-27 20:51:26 +00001645 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001646 {
cristya58c3172011-02-19 19:23:11 +00001647 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001648 {
cristy4c08aed2011-07-01 19:47:50 +00001649 case 0: pixel[j]=GetPixelRed(image,p); break;
1650 case 1: pixel[j]=GetPixelGreen(image,p); break;
1651 case 2: pixel[j]=GetPixelBlue(image,p); break;
1652 case 3: pixel[j]=GetPixelAlpha(image,p); break;
1653 case 4: pixel[j]=GetPixelBlack(image,p); break;
cristy3ed852e2009-09-05 21:47:34 +00001654 default: break;
1655 }
cristyed231572011-07-14 02:18:59 +00001656 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001657 j++;
1658 }
1659 j++;
1660 }
cristy3ed852e2009-09-05 21:47:34 +00001661 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
cristya58c3172011-02-19 19:23:11 +00001662 for (k=0; k < 4; k++)
cristy3ed852e2009-09-05 21:47:34 +00001663 {
cristya58c3172011-02-19 19:23:11 +00001664 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1665 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1666 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1667 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
cristy3ed852e2009-09-05 21:47:34 +00001668 }
cristybb503372010-05-27 20:51:26 +00001669 j=(ssize_t) image->columns+2;
1670 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001671 {
1672 MagickBooleanType
1673 sync;
1674
cristy4c08aed2011-07-01 19:47:50 +00001675 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001676 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001677
1678 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1679 1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001680 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001681 break;
1682 j++;
cristybb503372010-05-27 20:51:26 +00001683 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001684 {
cristya58c3172011-02-19 19:23:11 +00001685 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001686 {
cristy4c08aed2011-07-01 19:47:50 +00001687 case 0: SetPixelRed(despeckle_image,pixel[j],q); break;
1688 case 1: SetPixelGreen(despeckle_image,pixel[j],q); break;
1689 case 2: SetPixelBlue(despeckle_image,pixel[j],q); break;
1690 case 3: SetPixelAlpha(despeckle_image,pixel[j],q); break;
1691 case 4: SetPixelBlack(despeckle_image,pixel[j],q); break;
cristy3ed852e2009-09-05 21:47:34 +00001692 default: break;
1693 }
cristyed231572011-07-14 02:18:59 +00001694 q+=GetPixelChannels(despeckle_image);
cristy3ed852e2009-09-05 21:47:34 +00001695 j++;
1696 }
1697 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1698 if (sync == MagickFalse)
1699 {
1700 status=MagickFalse;
1701 break;
1702 }
1703 j++;
1704 }
1705 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1706 {
1707 MagickBooleanType
1708 proceed;
1709
cristya58c3172011-02-19 19:23:11 +00001710 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1711 number_channels);
cristy3ed852e2009-09-05 21:47:34 +00001712 if (proceed == MagickFalse)
1713 status=MagickFalse;
1714 }
1715 }
1716 despeckle_view=DestroyCacheView(despeckle_view);
1717 image_view=DestroyCacheView(image_view);
cristy65b9f392011-02-22 14:22:54 +00001718 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1719 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001720 despeckle_image->type=image->type;
1721 if (status == MagickFalse)
1722 despeckle_image=DestroyImage(despeckle_image);
1723 return(despeckle_image);
1724}
1725
1726/*
1727%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1728% %
1729% %
1730% %
1731% E d g e I m a g e %
1732% %
1733% %
1734% %
1735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1736%
1737% EdgeImage() finds edges in an image. Radius defines the radius of the
1738% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1739% radius for you.
1740%
1741% The format of the EdgeImage method is:
1742%
1743% Image *EdgeImage(const Image *image,const double radius,
1744% ExceptionInfo *exception)
1745%
1746% A description of each parameter follows:
1747%
1748% o image: the image.
1749%
1750% o radius: the radius of the pixel neighborhood.
1751%
1752% o exception: return any errors or warnings in this structure.
1753%
1754*/
1755MagickExport Image *EdgeImage(const Image *image,const double radius,
1756 ExceptionInfo *exception)
1757{
1758 Image
1759 *edge_image;
1760
cristy41cbe682011-07-15 19:12:37 +00001761 KernelInfo
1762 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001763
cristybb503372010-05-27 20:51:26 +00001764 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001765 i;
1766
cristybb503372010-05-27 20:51:26 +00001767 size_t
cristy3ed852e2009-09-05 21:47:34 +00001768 width;
1769
cristy41cbe682011-07-15 19:12:37 +00001770 ssize_t
1771 j,
1772 u,
1773 v;
1774
cristy3ed852e2009-09-05 21:47:34 +00001775 assert(image != (const Image *) NULL);
1776 assert(image->signature == MagickSignature);
1777 if (image->debug != MagickFalse)
1778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1779 assert(exception != (ExceptionInfo *) NULL);
1780 assert(exception->signature == MagickSignature);
1781 width=GetOptimalKernelWidth1D(radius,0.5);
cristy5e6be1e2011-07-16 01:23:39 +00001782 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001783 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001784 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001785 kernel_info->width=width;
1786 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001787 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1788 kernel_info->width*sizeof(*kernel_info->values));
1789 if (kernel_info->values == (double *) NULL)
1790 {
1791 kernel_info=DestroyKernelInfo(kernel_info);
1792 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1793 }
1794 j=(ssize_t) kernel_info->width/2;
1795 i=0;
1796 for (v=(-j); v <= j; v++)
1797 {
1798 for (u=(-j); u <= j; u++)
1799 {
1800 kernel_info->values[i]=(-1.0);
1801 i++;
1802 }
1803 }
1804 kernel_info->values[i/2]=(double) (width*width-1.0);
cristy0a922382011-07-16 15:30:34 +00001805 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001806 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001807 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001808 return(edge_image);
1809}
1810
1811/*
1812%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1813% %
1814% %
1815% %
1816% E m b o s s I m a g e %
1817% %
1818% %
1819% %
1820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1821%
1822% EmbossImage() returns a grayscale image with a three-dimensional effect.
1823% We convolve the image with a Gaussian operator of the given radius and
1824% standard deviation (sigma). For reasonable results, radius should be
1825% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1826% radius for you.
1827%
1828% The format of the EmbossImage method is:
1829%
1830% Image *EmbossImage(const Image *image,const double radius,
1831% const double sigma,ExceptionInfo *exception)
1832%
1833% A description of each parameter follows:
1834%
1835% o image: the image.
1836%
1837% o radius: the radius of the pixel neighborhood.
1838%
1839% o sigma: the standard deviation of the Gaussian, in pixels.
1840%
1841% o exception: return any errors or warnings in this structure.
1842%
1843*/
1844MagickExport Image *EmbossImage(const Image *image,const double radius,
1845 const double sigma,ExceptionInfo *exception)
1846{
cristy3ed852e2009-09-05 21:47:34 +00001847 Image
1848 *emboss_image;
1849
cristy41cbe682011-07-15 19:12:37 +00001850 KernelInfo
1851 *kernel_info;
1852
cristybb503372010-05-27 20:51:26 +00001853 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001854 i;
1855
cristybb503372010-05-27 20:51:26 +00001856 size_t
cristy3ed852e2009-09-05 21:47:34 +00001857 width;
1858
cristy117ff172010-08-15 21:35:32 +00001859 ssize_t
1860 j,
1861 k,
1862 u,
1863 v;
1864
cristy41cbe682011-07-15 19:12:37 +00001865 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001866 assert(image->signature == MagickSignature);
1867 if (image->debug != MagickFalse)
1868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1869 assert(exception != (ExceptionInfo *) NULL);
1870 assert(exception->signature == MagickSignature);
1871 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001872 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001873 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001874 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001875 kernel_info->width=width;
1876 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001877 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1878 kernel_info->width*sizeof(*kernel_info->values));
1879 if (kernel_info->values == (double *) NULL)
1880 {
1881 kernel_info=DestroyKernelInfo(kernel_info);
1882 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1883 }
1884 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001885 k=j;
1886 i=0;
1887 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001888 {
cristy47e00502009-12-17 19:19:57 +00001889 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001890 {
cristy41cbe682011-07-15 19:12:37 +00001891 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001892 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001893 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001894 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001895 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001896 i++;
1897 }
cristy47e00502009-12-17 19:19:57 +00001898 k--;
cristy3ed852e2009-09-05 21:47:34 +00001899 }
cristy0a922382011-07-16 15:30:34 +00001900 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001901 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001902 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001903 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001904 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001905 return(emboss_image);
1906}
1907
1908/*
1909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1910% %
1911% %
1912% %
1913% G a u s s i a n B l u r I m a g e %
1914% %
1915% %
1916% %
1917%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1918%
1919% GaussianBlurImage() blurs an image. We convolve the image with a
1920% Gaussian operator of the given radius and standard deviation (sigma).
1921% For reasonable results, the radius should be larger than sigma. Use a
1922% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1923%
1924% The format of the GaussianBlurImage method is:
1925%
1926% Image *GaussianBlurImage(const Image *image,onst double radius,
1927% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001928%
1929% A description of each parameter follows:
1930%
1931% o image: the image.
1932%
cristy3ed852e2009-09-05 21:47:34 +00001933% o radius: the radius of the Gaussian, in pixels, not counting the center
1934% pixel.
1935%
1936% o sigma: the standard deviation of the Gaussian, in pixels.
1937%
1938% o exception: return any errors or warnings in this structure.
1939%
1940*/
cristy41cbe682011-07-15 19:12:37 +00001941MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1942 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001943{
cristy3ed852e2009-09-05 21:47:34 +00001944 Image
1945 *blur_image;
1946
cristy41cbe682011-07-15 19:12:37 +00001947 KernelInfo
1948 *kernel_info;
1949
cristybb503372010-05-27 20:51:26 +00001950 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001951 i;
1952
cristybb503372010-05-27 20:51:26 +00001953 size_t
cristy3ed852e2009-09-05 21:47:34 +00001954 width;
1955
cristy117ff172010-08-15 21:35:32 +00001956 ssize_t
1957 j,
1958 u,
1959 v;
1960
cristy3ed852e2009-09-05 21:47:34 +00001961 assert(image != (const Image *) NULL);
1962 assert(image->signature == MagickSignature);
1963 if (image->debug != MagickFalse)
1964 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1965 assert(exception != (ExceptionInfo *) NULL);
1966 assert(exception->signature == MagickSignature);
1967 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001968 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001969 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001970 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001971 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1972 kernel_info->width=width;
1973 kernel_info->height=width;
1974 kernel_info->signature=MagickSignature;
1975 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1976 kernel_info->width*sizeof(*kernel_info->values));
1977 if (kernel_info->values == (double *) NULL)
1978 {
1979 kernel_info=DestroyKernelInfo(kernel_info);
1980 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1981 }
1982 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001983 i=0;
cristy47e00502009-12-17 19:19:57 +00001984 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001985 {
cristy47e00502009-12-17 19:19:57 +00001986 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001987 {
1988 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1989 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1990 i++;
1991 }
cristy3ed852e2009-09-05 21:47:34 +00001992 }
cristy0a922382011-07-16 15:30:34 +00001993 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001994 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001995 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001996 return(blur_image);
1997}
1998
1999/*
2000%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2001% %
2002% %
2003% %
cristy3ed852e2009-09-05 21:47:34 +00002004% M o t i o n B l u r I m a g e %
2005% %
2006% %
2007% %
2008%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2009%
2010% MotionBlurImage() simulates motion blur. We convolve the image with a
2011% Gaussian operator of the given radius and standard deviation (sigma).
2012% For reasonable results, radius should be larger than sigma. Use a
2013% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2014% Angle gives the angle of the blurring motion.
2015%
2016% Andrew Protano contributed this effect.
2017%
2018% The format of the MotionBlurImage method is:
2019%
2020% Image *MotionBlurImage(const Image *image,const double radius,
2021% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002022%
2023% A description of each parameter follows:
2024%
2025% o image: the image.
2026%
cristy3ed852e2009-09-05 21:47:34 +00002027% o radius: the radius of the Gaussian, in pixels, not counting
2028% the center pixel.
2029%
2030% o sigma: the standard deviation of the Gaussian, in pixels.
2031%
cristycee97112010-05-28 00:44:52 +00002032% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002033%
2034% o exception: return any errors or warnings in this structure.
2035%
2036*/
2037
cristybb503372010-05-27 20:51:26 +00002038static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002039{
cristy3ed852e2009-09-05 21:47:34 +00002040 double
cristy47e00502009-12-17 19:19:57 +00002041 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002042 normalize;
2043
cristybb503372010-05-27 20:51:26 +00002044 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002045 i;
2046
2047 /*
cristy47e00502009-12-17 19:19:57 +00002048 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002049 */
2050 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2051 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2052 if (kernel == (double *) NULL)
2053 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002054 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002055 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002056 {
cristy4205a3c2010-09-12 20:19:59 +00002057 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2058 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002059 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002060 }
cristybb503372010-05-27 20:51:26 +00002061 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002062 kernel[i]/=normalize;
2063 return(kernel);
2064}
2065
cristyf4ad9df2011-07-08 16:49:03 +00002066MagickExport Image *MotionBlurImage(const Image *image,
2067 const double radius,const double sigma,const double angle,
2068 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002069{
cristyc4c8d132010-01-07 01:58:38 +00002070 CacheView
2071 *blur_view,
2072 *image_view;
2073
cristy3ed852e2009-09-05 21:47:34 +00002074 double
2075 *kernel;
2076
2077 Image
2078 *blur_image;
2079
cristy3ed852e2009-09-05 21:47:34 +00002080 MagickBooleanType
2081 status;
2082
cristybb503372010-05-27 20:51:26 +00002083 MagickOffsetType
2084 progress;
2085
cristy4c08aed2011-07-01 19:47:50 +00002086 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002087 bias;
cristy3ed852e2009-09-05 21:47:34 +00002088
2089 OffsetInfo
2090 *offset;
2091
2092 PointInfo
2093 point;
2094
cristybb503372010-05-27 20:51:26 +00002095 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002096 i;
2097
cristybb503372010-05-27 20:51:26 +00002098 size_t
cristy3ed852e2009-09-05 21:47:34 +00002099 width;
2100
cristybb503372010-05-27 20:51:26 +00002101 ssize_t
2102 y;
2103
cristy3ed852e2009-09-05 21:47:34 +00002104 assert(image != (Image *) NULL);
2105 assert(image->signature == MagickSignature);
2106 if (image->debug != MagickFalse)
2107 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2108 assert(exception != (ExceptionInfo *) NULL);
2109 width=GetOptimalKernelWidth1D(radius,sigma);
2110 kernel=GetMotionBlurKernel(width,sigma);
2111 if (kernel == (double *) NULL)
2112 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2113 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2114 if (offset == (OffsetInfo *) NULL)
2115 {
2116 kernel=(double *) RelinquishMagickMemory(kernel);
2117 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2118 }
2119 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2120 if (blur_image == (Image *) NULL)
2121 {
2122 kernel=(double *) RelinquishMagickMemory(kernel);
2123 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2124 return((Image *) NULL);
2125 }
cristy574cc262011-08-05 01:23:58 +00002126 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002127 {
2128 kernel=(double *) RelinquishMagickMemory(kernel);
2129 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002130 blur_image=DestroyImage(blur_image);
2131 return((Image *) NULL);
2132 }
2133 point.x=(double) width*sin(DegreesToRadians(angle));
2134 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002135 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002136 {
cristybb503372010-05-27 20:51:26 +00002137 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2138 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002139 }
2140 /*
2141 Motion blur image.
2142 */
2143 status=MagickTrue;
2144 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002145 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002146 image_view=AcquireCacheView(image);
2147 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002148#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00002149 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00002150#endif
cristybb503372010-05-27 20:51:26 +00002151 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002152 {
cristy4c08aed2011-07-01 19:47:50 +00002153 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002154 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002155
cristy117ff172010-08-15 21:35:32 +00002156 register ssize_t
2157 x;
2158
cristy3ed852e2009-09-05 21:47:34 +00002159 if (status == MagickFalse)
2160 continue;
2161 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2162 exception);
cristyacd2ed22011-08-30 01:44:23 +00002163 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002164 {
2165 status=MagickFalse;
2166 continue;
2167 }
cristybb503372010-05-27 20:51:26 +00002168 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002169 {
cristy4c08aed2011-07-01 19:47:50 +00002170 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002171 qixel;
2172
2173 PixelPacket
2174 pixel;
2175
2176 register double
cristyc47d1f82009-11-26 01:44:43 +00002177 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00002178
cristybb503372010-05-27 20:51:26 +00002179 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002180 i;
2181
cristy3ed852e2009-09-05 21:47:34 +00002182 k=kernel;
cristyddd82202009-11-03 20:14:50 +00002183 qixel=bias;
cristyed231572011-07-14 02:18:59 +00002184 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002185 {
cristybb503372010-05-27 20:51:26 +00002186 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002187 {
2188 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2189 offset[i].y,&pixel,exception);
2190 qixel.red+=(*k)*pixel.red;
2191 qixel.green+=(*k)*pixel.green;
2192 qixel.blue+=(*k)*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002193 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002194 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002195 qixel.black+=(*k)*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002196 k++;
2197 }
cristyed231572011-07-14 02:18:59 +00002198 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002199 SetPixelRed(blur_image,
2200 ClampToQuantum(qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002201 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002202 SetPixelGreen(blur_image,
2203 ClampToQuantum(qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002204 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002205 SetPixelBlue(blur_image,
2206 ClampToQuantum(qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002207 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002208 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002209 SetPixelBlack(blur_image,
2210 ClampToQuantum(qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002211 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002212 SetPixelAlpha(blur_image,
2213 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002214 }
2215 else
2216 {
2217 MagickRealType
2218 alpha,
2219 gamma;
2220
2221 alpha=0.0;
2222 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00002223 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002224 {
2225 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2226 offset[i].y,&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00002227 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00002228 qixel.red+=(*k)*alpha*pixel.red;
2229 qixel.green+=(*k)*alpha*pixel.green;
2230 qixel.blue+=(*k)*alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002231 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002232 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002233 qixel.black+=(*k)*alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002234 gamma+=(*k)*alpha;
2235 k++;
2236 }
2237 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00002238 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002239 SetPixelRed(blur_image,
2240 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002241 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002242 SetPixelGreen(blur_image,
2243 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002244 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002245 SetPixelBlue(blur_image,
2246 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002247 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002248 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002249 SetPixelBlack(blur_image,
2250 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002251 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002252 SetPixelAlpha(blur_image,
2253 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002254 }
cristyed231572011-07-14 02:18:59 +00002255 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002256 }
2257 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2258 status=MagickFalse;
2259 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2260 {
2261 MagickBooleanType
2262 proceed;
2263
cristyb557a152011-02-22 12:14:30 +00002264#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002265 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002266#endif
2267 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2268 if (proceed == MagickFalse)
2269 status=MagickFalse;
2270 }
2271 }
2272 blur_view=DestroyCacheView(blur_view);
2273 image_view=DestroyCacheView(image_view);
2274 kernel=(double *) RelinquishMagickMemory(kernel);
2275 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2276 if (status == MagickFalse)
2277 blur_image=DestroyImage(blur_image);
2278 return(blur_image);
2279}
2280
2281/*
2282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2283% %
2284% %
2285% %
2286% P r e v i e w I m a g e %
2287% %
2288% %
2289% %
2290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2291%
2292% PreviewImage() tiles 9 thumbnails of the specified image with an image
2293% processing operation applied with varying parameters. This may be helpful
2294% pin-pointing an appropriate parameter for a particular image processing
2295% operation.
2296%
2297% The format of the PreviewImages method is:
2298%
2299% Image *PreviewImages(const Image *image,const PreviewType preview,
2300% ExceptionInfo *exception)
2301%
2302% A description of each parameter follows:
2303%
2304% o image: the image.
2305%
2306% o preview: the image processing operation.
2307%
2308% o exception: return any errors or warnings in this structure.
2309%
2310*/
2311MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2312 ExceptionInfo *exception)
2313{
2314#define NumberTiles 9
2315#define PreviewImageTag "Preview/Image"
2316#define DefaultPreviewGeometry "204x204+10+10"
2317
2318 char
2319 factor[MaxTextExtent],
2320 label[MaxTextExtent];
2321
2322 double
2323 degrees,
2324 gamma,
2325 percentage,
2326 radius,
2327 sigma,
2328 threshold;
2329
2330 Image
2331 *images,
2332 *montage_image,
2333 *preview_image,
2334 *thumbnail;
2335
2336 ImageInfo
2337 *preview_info;
2338
cristy3ed852e2009-09-05 21:47:34 +00002339 MagickBooleanType
2340 proceed;
2341
2342 MontageInfo
2343 *montage_info;
2344
2345 QuantizeInfo
2346 quantize_info;
2347
2348 RectangleInfo
2349 geometry;
2350
cristybb503372010-05-27 20:51:26 +00002351 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002352 i,
2353 x;
2354
cristybb503372010-05-27 20:51:26 +00002355 size_t
cristy3ed852e2009-09-05 21:47:34 +00002356 colors;
2357
cristy117ff172010-08-15 21:35:32 +00002358 ssize_t
2359 y;
2360
cristy3ed852e2009-09-05 21:47:34 +00002361 /*
2362 Open output image file.
2363 */
2364 assert(image != (Image *) NULL);
2365 assert(image->signature == MagickSignature);
2366 if (image->debug != MagickFalse)
2367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2368 colors=2;
2369 degrees=0.0;
2370 gamma=(-0.2f);
2371 preview_info=AcquireImageInfo();
2372 SetGeometry(image,&geometry);
2373 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2374 &geometry.width,&geometry.height);
2375 images=NewImageList();
2376 percentage=12.5;
2377 GetQuantizeInfo(&quantize_info);
2378 radius=0.0;
2379 sigma=1.0;
2380 threshold=0.0;
2381 x=0;
2382 y=0;
2383 for (i=0; i < NumberTiles; i++)
2384 {
2385 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2386 if (thumbnail == (Image *) NULL)
2387 break;
2388 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2389 (void *) NULL);
2390 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
2391 if (i == (NumberTiles/2))
2392 {
2393 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
2394 AppendImageToList(&images,thumbnail);
2395 continue;
2396 }
2397 switch (preview)
2398 {
2399 case RotatePreview:
2400 {
2401 degrees+=45.0;
2402 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002403 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002404 break;
2405 }
2406 case ShearPreview:
2407 {
2408 degrees+=5.0;
2409 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002410 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002411 degrees,2.0*degrees);
2412 break;
2413 }
2414 case RollPreview:
2415 {
cristybb503372010-05-27 20:51:26 +00002416 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2417 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002418 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002419 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002420 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002421 break;
2422 }
2423 case HuePreview:
2424 {
2425 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2426 if (preview_image == (Image *) NULL)
2427 break;
cristyb51dff52011-05-19 16:55:47 +00002428 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002429 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002430 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002431 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002432 break;
2433 }
2434 case SaturationPreview:
2435 {
2436 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2437 if (preview_image == (Image *) NULL)
2438 break;
cristyb51dff52011-05-19 16:55:47 +00002439 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002440 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002441 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002442 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002443 break;
2444 }
2445 case BrightnessPreview:
2446 {
2447 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2448 if (preview_image == (Image *) NULL)
2449 break;
cristyb51dff52011-05-19 16:55:47 +00002450 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002451 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002452 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002453 break;
2454 }
2455 case GammaPreview:
2456 default:
2457 {
2458 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2459 if (preview_image == (Image *) NULL)
2460 break;
2461 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002462 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002463 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002464 break;
2465 }
2466 case SpiffPreview:
2467 {
2468 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2469 if (preview_image != (Image *) NULL)
2470 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002471 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002472 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002473 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002474 break;
2475 }
2476 case DullPreview:
2477 {
2478 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2479 if (preview_image == (Image *) NULL)
2480 break;
2481 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002482 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002483 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002484 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002485 break;
2486 }
2487 case GrayscalePreview:
2488 {
2489 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2490 if (preview_image == (Image *) NULL)
2491 break;
2492 colors<<=1;
2493 quantize_info.number_colors=colors;
2494 quantize_info.colorspace=GRAYColorspace;
2495 (void) QuantizeImage(&quantize_info,preview_image);
cristyb51dff52011-05-19 16:55:47 +00002496 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002497 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002498 break;
2499 }
2500 case QuantizePreview:
2501 {
2502 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2503 if (preview_image == (Image *) NULL)
2504 break;
2505 colors<<=1;
2506 quantize_info.number_colors=colors;
2507 (void) QuantizeImage(&quantize_info,preview_image);
cristyb51dff52011-05-19 16:55:47 +00002508 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002509 colors);
cristy3ed852e2009-09-05 21:47:34 +00002510 break;
2511 }
2512 case DespecklePreview:
2513 {
2514 for (x=0; x < (i-1); x++)
2515 {
2516 preview_image=DespeckleImage(thumbnail,exception);
2517 if (preview_image == (Image *) NULL)
2518 break;
2519 thumbnail=DestroyImage(thumbnail);
2520 thumbnail=preview_image;
2521 }
2522 preview_image=DespeckleImage(thumbnail,exception);
2523 if (preview_image == (Image *) NULL)
2524 break;
cristyb51dff52011-05-19 16:55:47 +00002525 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002526 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002527 break;
2528 }
2529 case ReduceNoisePreview:
2530 {
cristy95c38342011-03-18 22:39:51 +00002531 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2532 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002533 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002534 break;
2535 }
2536 case AddNoisePreview:
2537 {
2538 switch ((int) i)
2539 {
2540 case 0:
2541 {
2542 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2543 break;
2544 }
2545 case 1:
2546 {
2547 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2548 break;
2549 }
2550 case 2:
2551 {
2552 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2553 break;
2554 }
2555 case 3:
2556 {
2557 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2558 break;
2559 }
2560 case 4:
2561 {
2562 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2563 break;
2564 }
2565 case 5:
2566 {
2567 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2568 break;
2569 }
2570 default:
2571 {
2572 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2573 break;
2574 }
2575 }
cristyd76c51e2011-03-26 00:21:26 +00002576 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2577 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002578 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002579 break;
2580 }
2581 case SharpenPreview:
2582 {
2583 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002584 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002585 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002586 break;
2587 }
2588 case BlurPreview:
2589 {
2590 preview_image=BlurImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002591 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002592 sigma);
2593 break;
2594 }
2595 case ThresholdPreview:
2596 {
2597 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2598 if (preview_image == (Image *) NULL)
2599 break;
2600 (void) BilevelImage(thumbnail,
2601 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
cristyb51dff52011-05-19 16:55:47 +00002602 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002603 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2604 break;
2605 }
2606 case EdgeDetectPreview:
2607 {
2608 preview_image=EdgeImage(thumbnail,radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002609 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002610 break;
2611 }
2612 case SpreadPreview:
2613 {
2614 preview_image=SpreadImage(thumbnail,radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002615 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002616 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002617 break;
2618 }
2619 case SolarizePreview:
2620 {
2621 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2622 if (preview_image == (Image *) NULL)
2623 break;
2624 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002625 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002626 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002627 (QuantumRange*percentage)/100.0);
2628 break;
2629 }
2630 case ShadePreview:
2631 {
2632 degrees+=10.0;
2633 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2634 exception);
cristyb51dff52011-05-19 16:55:47 +00002635 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002636 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002637 break;
2638 }
2639 case RaisePreview:
2640 {
2641 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2642 if (preview_image == (Image *) NULL)
2643 break;
cristybb503372010-05-27 20:51:26 +00002644 geometry.width=(size_t) (2*i+2);
2645 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002646 geometry.x=i/2;
2647 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002648 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002649 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002650 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002651 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002652 break;
2653 }
2654 case SegmentPreview:
2655 {
2656 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2657 if (preview_image == (Image *) NULL)
2658 break;
2659 threshold+=0.4f;
2660 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
2661 threshold);
cristyb51dff52011-05-19 16:55:47 +00002662 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002663 threshold,threshold);
2664 break;
2665 }
2666 case SwirlPreview:
2667 {
2668 preview_image=SwirlImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002669 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002670 degrees+=45.0;
2671 break;
2672 }
2673 case ImplodePreview:
2674 {
2675 degrees+=0.1f;
2676 preview_image=ImplodeImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002677 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002678 break;
2679 }
2680 case WavePreview:
2681 {
2682 degrees+=5.0f;
2683 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002684 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002685 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002686 break;
2687 }
2688 case OilPaintPreview:
2689 {
cristy14973ba2011-08-27 23:48:07 +00002690 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2691 exception);
2692 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2693 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002694 break;
2695 }
2696 case CharcoalDrawingPreview:
2697 {
2698 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2699 exception);
cristyb51dff52011-05-19 16:55:47 +00002700 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002701 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002702 break;
2703 }
2704 case JPEGPreview:
2705 {
2706 char
2707 filename[MaxTextExtent];
2708
2709 int
2710 file;
2711
2712 MagickBooleanType
2713 status;
2714
2715 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2716 if (preview_image == (Image *) NULL)
2717 break;
cristybb503372010-05-27 20:51:26 +00002718 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002719 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002720 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002721 file=AcquireUniqueFileResource(filename);
2722 if (file != -1)
2723 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002724 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002725 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002726 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002727 if (status != MagickFalse)
2728 {
2729 Image
2730 *quality_image;
2731
2732 (void) CopyMagickString(preview_info->filename,
2733 preview_image->filename,MaxTextExtent);
2734 quality_image=ReadImage(preview_info,exception);
2735 if (quality_image != (Image *) NULL)
2736 {
2737 preview_image=DestroyImage(preview_image);
2738 preview_image=quality_image;
2739 }
2740 }
2741 (void) RelinquishUniqueFileResource(preview_image->filename);
2742 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002743 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002744 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2745 1024.0/1024.0);
2746 else
2747 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002748 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002749 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002750 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002751 else
cristyb51dff52011-05-19 16:55:47 +00002752 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002753 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002754 break;
2755 }
2756 }
2757 thumbnail=DestroyImage(thumbnail);
2758 percentage+=12.5;
2759 radius+=0.5;
2760 sigma+=0.25;
2761 if (preview_image == (Image *) NULL)
2762 break;
2763 (void) DeleteImageProperty(preview_image,"label");
2764 (void) SetImageProperty(preview_image,"label",label);
2765 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002766 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2767 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002768 if (proceed == MagickFalse)
2769 break;
2770 }
2771 if (images == (Image *) NULL)
2772 {
2773 preview_info=DestroyImageInfo(preview_info);
2774 return((Image *) NULL);
2775 }
2776 /*
2777 Create the montage.
2778 */
2779 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2780 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2781 montage_info->shadow=MagickTrue;
2782 (void) CloneString(&montage_info->tile,"3x3");
2783 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2784 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2785 montage_image=MontageImages(images,montage_info,exception);
2786 montage_info=DestroyMontageInfo(montage_info);
2787 images=DestroyImageList(images);
2788 if (montage_image == (Image *) NULL)
2789 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2790 if (montage_image->montage != (char *) NULL)
2791 {
2792 /*
2793 Free image directory.
2794 */
2795 montage_image->montage=(char *) RelinquishMagickMemory(
2796 montage_image->montage);
2797 if (image->directory != (char *) NULL)
2798 montage_image->directory=(char *) RelinquishMagickMemory(
2799 montage_image->directory);
2800 }
2801 preview_info=DestroyImageInfo(preview_info);
2802 return(montage_image);
2803}
2804
2805/*
2806%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2807% %
2808% %
2809% %
2810% R a d i a l B l u r I m a g e %
2811% %
2812% %
2813% %
2814%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2815%
2816% RadialBlurImage() applies a radial blur to the image.
2817%
2818% Andrew Protano contributed this effect.
2819%
2820% The format of the RadialBlurImage method is:
2821%
2822% Image *RadialBlurImage(const Image *image,const double angle,
2823% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002824%
2825% A description of each parameter follows:
2826%
2827% o image: the image.
2828%
cristy3ed852e2009-09-05 21:47:34 +00002829% o angle: the angle of the radial blur.
2830%
2831% o exception: return any errors or warnings in this structure.
2832%
2833*/
cristyf4ad9df2011-07-08 16:49:03 +00002834MagickExport Image *RadialBlurImage(const Image *image,
2835 const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002836{
cristyc4c8d132010-01-07 01:58:38 +00002837 CacheView
2838 *blur_view,
2839 *image_view;
2840
cristy3ed852e2009-09-05 21:47:34 +00002841 Image
2842 *blur_image;
2843
cristy3ed852e2009-09-05 21:47:34 +00002844 MagickBooleanType
2845 status;
2846
cristybb503372010-05-27 20:51:26 +00002847 MagickOffsetType
2848 progress;
2849
cristy4c08aed2011-07-01 19:47:50 +00002850 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002851 bias;
cristy3ed852e2009-09-05 21:47:34 +00002852
2853 MagickRealType
2854 blur_radius,
2855 *cos_theta,
2856 offset,
2857 *sin_theta,
2858 theta;
2859
2860 PointInfo
2861 blur_center;
2862
cristybb503372010-05-27 20:51:26 +00002863 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002864 i;
2865
cristybb503372010-05-27 20:51:26 +00002866 size_t
cristy3ed852e2009-09-05 21:47:34 +00002867 n;
2868
cristybb503372010-05-27 20:51:26 +00002869 ssize_t
2870 y;
2871
cristy3ed852e2009-09-05 21:47:34 +00002872 /*
2873 Allocate blur image.
2874 */
2875 assert(image != (Image *) NULL);
2876 assert(image->signature == MagickSignature);
2877 if (image->debug != MagickFalse)
2878 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2879 assert(exception != (ExceptionInfo *) NULL);
2880 assert(exception->signature == MagickSignature);
2881 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2882 if (blur_image == (Image *) NULL)
2883 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002884 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002885 {
cristy3ed852e2009-09-05 21:47:34 +00002886 blur_image=DestroyImage(blur_image);
2887 return((Image *) NULL);
2888 }
2889 blur_center.x=(double) image->columns/2.0;
2890 blur_center.y=(double) image->rows/2.0;
2891 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002892 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002893 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2894 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2895 sizeof(*cos_theta));
2896 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2897 sizeof(*sin_theta));
2898 if ((cos_theta == (MagickRealType *) NULL) ||
2899 (sin_theta == (MagickRealType *) NULL))
2900 {
2901 blur_image=DestroyImage(blur_image);
2902 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2903 }
2904 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002905 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002906 {
2907 cos_theta[i]=cos((double) (theta*i-offset));
2908 sin_theta[i]=sin((double) (theta*i-offset));
2909 }
2910 /*
2911 Radial blur image.
2912 */
2913 status=MagickTrue;
2914 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002915 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002916 image_view=AcquireCacheView(image);
2917 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002918#if defined(MAGICKCORE_OPENMP_SUPPORT)
2919 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002920#endif
cristybb503372010-05-27 20:51:26 +00002921 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002922 {
cristy4c08aed2011-07-01 19:47:50 +00002923 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002924 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002925
cristy117ff172010-08-15 21:35:32 +00002926 register ssize_t
2927 x;
2928
cristy3ed852e2009-09-05 21:47:34 +00002929 if (status == MagickFalse)
2930 continue;
2931 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2932 exception);
cristyacd2ed22011-08-30 01:44:23 +00002933 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002934 {
2935 status=MagickFalse;
2936 continue;
2937 }
cristybb503372010-05-27 20:51:26 +00002938 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002939 {
cristy4c08aed2011-07-01 19:47:50 +00002940 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002941 qixel;
2942
2943 MagickRealType
2944 normalize,
2945 radius;
2946
2947 PixelPacket
2948 pixel;
2949
2950 PointInfo
2951 center;
2952
cristybb503372010-05-27 20:51:26 +00002953 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002954 i;
2955
cristybb503372010-05-27 20:51:26 +00002956 size_t
cristy3ed852e2009-09-05 21:47:34 +00002957 step;
2958
2959 center.x=(double) x-blur_center.x;
2960 center.y=(double) y-blur_center.y;
2961 radius=hypot((double) center.x,center.y);
2962 if (radius == 0)
2963 step=1;
2964 else
2965 {
cristybb503372010-05-27 20:51:26 +00002966 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002967 if (step == 0)
2968 step=1;
2969 else
2970 if (step >= n)
2971 step=n-1;
2972 }
2973 normalize=0.0;
cristyddd82202009-11-03 20:14:50 +00002974 qixel=bias;
cristyed231572011-07-14 02:18:59 +00002975 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002976 {
cristyeaedf062010-05-29 22:36:02 +00002977 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00002978 {
cristyeaedf062010-05-29 22:36:02 +00002979 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
2980 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
2981 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
2982 cos_theta[i]+0.5),&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002983 qixel.red+=pixel.red;
2984 qixel.green+=pixel.green;
2985 qixel.blue+=pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00002986 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002987 qixel.black+=pixel.black;
2988 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002989 normalize+=1.0;
2990 }
2991 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
2992 normalize);
cristyed231572011-07-14 02:18:59 +00002993 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002994 SetPixelRed(blur_image,
2995 ClampToQuantum(normalize*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002996 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002997 SetPixelGreen(blur_image,
2998 ClampToQuantum(normalize*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002999 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003000 SetPixelBlue(blur_image,
3001 ClampToQuantum(normalize*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003002 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003003 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003004 SetPixelBlack(blur_image,
3005 ClampToQuantum(normalize*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003006 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003007 SetPixelAlpha(blur_image,
3008 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003009 }
3010 else
3011 {
3012 MagickRealType
3013 alpha,
3014 gamma;
3015
3016 alpha=1.0;
3017 gamma=0.0;
cristyeaedf062010-05-29 22:36:02 +00003018 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00003019 {
cristyeaedf062010-05-29 22:36:02 +00003020 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3021 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3022 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3023 cos_theta[i]+0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003024 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00003025 qixel.red+=alpha*pixel.red;
3026 qixel.green+=alpha*pixel.green;
3027 qixel.blue+=alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00003028 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00003029 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00003030 qixel.black+=alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00003031 gamma+=alpha;
3032 normalize+=1.0;
3033 }
3034 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3035 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3036 normalize);
cristyed231572011-07-14 02:18:59 +00003037 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003038 SetPixelRed(blur_image,
3039 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00003040 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003041 SetPixelGreen(blur_image,
3042 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00003043 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003044 SetPixelBlue(blur_image,
3045 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003046 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003047 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003048 SetPixelBlack(blur_image,
3049 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003050 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003051 SetPixelAlpha(blur_image,
3052 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003053 }
cristyed231572011-07-14 02:18:59 +00003054 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003055 }
3056 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3057 status=MagickFalse;
3058 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3059 {
3060 MagickBooleanType
3061 proceed;
3062
cristyb5d5f722009-11-04 03:03:49 +00003063#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003064 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003065#endif
3066 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3067 if (proceed == MagickFalse)
3068 status=MagickFalse;
3069 }
3070 }
3071 blur_view=DestroyCacheView(blur_view);
3072 image_view=DestroyCacheView(image_view);
3073 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3074 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3075 if (status == MagickFalse)
3076 blur_image=DestroyImage(blur_image);
3077 return(blur_image);
3078}
3079
3080/*
3081%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3082% %
3083% %
3084% %
cristy3ed852e2009-09-05 21:47:34 +00003085% S e l e c t i v e B l u r I m a g e %
3086% %
3087% %
3088% %
3089%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3090%
3091% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3092% It is similar to the unsharpen mask that sharpens everything with contrast
3093% above a certain threshold.
3094%
3095% The format of the SelectiveBlurImage method is:
3096%
3097% Image *SelectiveBlurImage(const Image *image,const double radius,
3098% const double sigma,const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003099%
3100% A description of each parameter follows:
3101%
3102% o image: the image.
3103%
cristy3ed852e2009-09-05 21:47:34 +00003104% o radius: the radius of the Gaussian, in pixels, not counting the center
3105% pixel.
3106%
3107% o sigma: the standard deviation of the Gaussian, in pixels.
3108%
3109% o threshold: only pixels within this contrast threshold are included
3110% in the blur operation.
3111%
3112% o exception: return any errors or warnings in this structure.
3113%
3114*/
cristyf4ad9df2011-07-08 16:49:03 +00003115MagickExport Image *SelectiveBlurImage(const Image *image,
3116 const double radius,const double sigma,const double threshold,
3117 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003118{
3119#define SelectiveBlurImageTag "SelectiveBlur/Image"
3120
cristy47e00502009-12-17 19:19:57 +00003121 CacheView
3122 *blur_view,
3123 *image_view;
3124
cristy3ed852e2009-09-05 21:47:34 +00003125 double
cristy3ed852e2009-09-05 21:47:34 +00003126 *kernel;
3127
3128 Image
3129 *blur_image;
3130
cristy3ed852e2009-09-05 21:47:34 +00003131 MagickBooleanType
3132 status;
3133
cristybb503372010-05-27 20:51:26 +00003134 MagickOffsetType
3135 progress;
3136
cristy4c08aed2011-07-01 19:47:50 +00003137 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003138 bias;
3139
cristybb503372010-05-27 20:51:26 +00003140 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003141 i;
cristy3ed852e2009-09-05 21:47:34 +00003142
cristybb503372010-05-27 20:51:26 +00003143 size_t
cristy3ed852e2009-09-05 21:47:34 +00003144 width;
3145
cristybb503372010-05-27 20:51:26 +00003146 ssize_t
3147 j,
3148 u,
3149 v,
3150 y;
3151
cristy3ed852e2009-09-05 21:47:34 +00003152 /*
3153 Initialize blur image attributes.
3154 */
3155 assert(image != (Image *) NULL);
3156 assert(image->signature == MagickSignature);
3157 if (image->debug != MagickFalse)
3158 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3159 assert(exception != (ExceptionInfo *) NULL);
3160 assert(exception->signature == MagickSignature);
3161 width=GetOptimalKernelWidth1D(radius,sigma);
3162 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3163 if (kernel == (double *) NULL)
3164 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003165 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003166 i=0;
cristy47e00502009-12-17 19:19:57 +00003167 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003168 {
cristy47e00502009-12-17 19:19:57 +00003169 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003170 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3171 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003172 }
3173 if (image->debug != MagickFalse)
3174 {
3175 char
3176 format[MaxTextExtent],
3177 *message;
3178
cristy117ff172010-08-15 21:35:32 +00003179 register const double
3180 *k;
3181
cristybb503372010-05-27 20:51:26 +00003182 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003183 u,
3184 v;
3185
cristy3ed852e2009-09-05 21:47:34 +00003186 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003187 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3188 width);
cristy3ed852e2009-09-05 21:47:34 +00003189 message=AcquireString("");
3190 k=kernel;
cristybb503372010-05-27 20:51:26 +00003191 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003192 {
3193 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003194 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003195 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003196 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003197 {
cristyb51dff52011-05-19 16:55:47 +00003198 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003199 (void) ConcatenateString(&message,format);
3200 }
3201 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3202 }
3203 message=DestroyString(message);
3204 }
3205 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3206 if (blur_image == (Image *) NULL)
3207 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003208 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003209 {
cristy3ed852e2009-09-05 21:47:34 +00003210 blur_image=DestroyImage(blur_image);
3211 return((Image *) NULL);
3212 }
3213 /*
3214 Threshold blur image.
3215 */
3216 status=MagickTrue;
3217 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003218 GetPixelInfo(image,&bias);
3219 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003220 image_view=AcquireCacheView(image);
3221 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003222#if defined(MAGICKCORE_OPENMP_SUPPORT)
3223 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003224#endif
cristybb503372010-05-27 20:51:26 +00003225 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003226 {
cristy4c08aed2011-07-01 19:47:50 +00003227 double
3228 contrast;
3229
cristy3ed852e2009-09-05 21:47:34 +00003230 MagickBooleanType
3231 sync;
3232
3233 MagickRealType
3234 gamma;
3235
cristy4c08aed2011-07-01 19:47:50 +00003236 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003237 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003238
cristy4c08aed2011-07-01 19:47:50 +00003239 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003240 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003241
cristy117ff172010-08-15 21:35:32 +00003242 register ssize_t
3243 x;
3244
cristy3ed852e2009-09-05 21:47:34 +00003245 if (status == MagickFalse)
3246 continue;
cristy117ff172010-08-15 21:35:32 +00003247 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3248 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00003249 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3250 exception);
cristy4c08aed2011-07-01 19:47:50 +00003251 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003252 {
3253 status=MagickFalse;
3254 continue;
3255 }
cristybb503372010-05-27 20:51:26 +00003256 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003257 {
cristy4c08aed2011-07-01 19:47:50 +00003258 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003259 pixel;
3260
3261 register const double
cristyc47d1f82009-11-26 01:44:43 +00003262 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00003263
cristybb503372010-05-27 20:51:26 +00003264 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003265 u;
3266
cristy117ff172010-08-15 21:35:32 +00003267 ssize_t
3268 j,
3269 v;
3270
cristyddd82202009-11-03 20:14:50 +00003271 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00003272 k=kernel;
3273 gamma=0.0;
3274 j=0;
cristyed231572011-07-14 02:18:59 +00003275 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003276 {
cristybb503372010-05-27 20:51:26 +00003277 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003278 {
cristybb503372010-05-27 20:51:26 +00003279 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003280 {
cristyed231572011-07-14 02:18:59 +00003281 contrast=GetPixelIntensity(image,p+(u+j)*GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003282 (double) GetPixelIntensity(blur_image,q);
3283 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003284 {
cristy4c08aed2011-07-01 19:47:50 +00003285 pixel.red+=(*k)*
cristyed231572011-07-14 02:18:59 +00003286 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003287 pixel.green+=(*k)*
cristyed231572011-07-14 02:18:59 +00003288 GetPixelGreen(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003289 pixel.blue+=(*k)*
cristyed231572011-07-14 02:18:59 +00003290 GetPixelBlue(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003291 if (image->colorspace == CMYKColorspace)
3292 pixel.black+=(*k)*
cristyed231572011-07-14 02:18:59 +00003293 GetPixelBlack(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003294 gamma+=(*k);
3295 k++;
3296 }
3297 }
cristyd99b0962010-05-29 23:14:26 +00003298 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003299 }
3300 if (gamma != 0.0)
3301 {
3302 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003303 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003304 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003305 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003306 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003307 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003308 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003309 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003310 (image->colorspace == CMYKColorspace))
3311 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003312 }
cristyed231572011-07-14 02:18:59 +00003313 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003314 {
3315 gamma=0.0;
3316 j=0;
cristybb503372010-05-27 20:51:26 +00003317 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003318 {
cristybb503372010-05-27 20:51:26 +00003319 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003320 {
cristy4c08aed2011-07-01 19:47:50 +00003321 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003322 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003323 GetPixelIntensity(blur_image,q);
3324 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003325 {
cristy4c08aed2011-07-01 19:47:50 +00003326 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003327 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003328 gamma+=(*k);
3329 k++;
3330 }
3331 }
cristyeaedf062010-05-29 22:36:02 +00003332 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003333 }
3334 if (gamma != 0.0)
3335 {
3336 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3337 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003338 SetPixelAlpha(blur_image,ClampToQuantum(gamma*pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003339 }
3340 }
3341 }
3342 else
3343 {
3344 MagickRealType
3345 alpha;
3346
cristybb503372010-05-27 20:51:26 +00003347 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003348 {
cristybb503372010-05-27 20:51:26 +00003349 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003350 {
cristy4c08aed2011-07-01 19:47:50 +00003351 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003352 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003353 GetPixelIntensity(blur_image,q);
3354 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003355 {
cristy4c08aed2011-07-01 19:47:50 +00003356 alpha=(MagickRealType) (QuantumScale*
cristyed231572011-07-14 02:18:59 +00003357 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image)));
cristy4c08aed2011-07-01 19:47:50 +00003358 pixel.red+=(*k)*alpha*
cristyed231572011-07-14 02:18:59 +00003359 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003360 pixel.green+=(*k)*alpha*GetPixelGreen(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003361 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003362 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003363 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003364 pixel.alpha+=(*k)*GetPixelAlpha(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003365 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003366 if (image->colorspace == CMYKColorspace)
3367 pixel.black+=(*k)*GetPixelBlack(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003368 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003369 gamma+=(*k)*alpha;
3370 k++;
3371 }
3372 }
cristyeaedf062010-05-29 22:36:02 +00003373 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003374 }
3375 if (gamma != 0.0)
3376 {
3377 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003378 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003379 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003380 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003381 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003382 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003383 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003384 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003385 (image->colorspace == CMYKColorspace))
3386 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003387 }
cristyed231572011-07-14 02:18:59 +00003388 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003389 {
3390 gamma=0.0;
3391 j=0;
cristybb503372010-05-27 20:51:26 +00003392 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003393 {
cristybb503372010-05-27 20:51:26 +00003394 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003395 {
cristy4c08aed2011-07-01 19:47:50 +00003396 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003397 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003398 GetPixelIntensity(blur_image,q);
3399 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003400 {
cristy4c08aed2011-07-01 19:47:50 +00003401 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003402 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003403 gamma+=(*k);
3404 k++;
3405 }
3406 }
cristyeaedf062010-05-29 22:36:02 +00003407 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003408 }
3409 if (gamma != 0.0)
3410 {
3411 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3412 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003413 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003414 }
3415 }
3416 }
cristyed231572011-07-14 02:18:59 +00003417 p+=GetPixelChannels(image);
3418 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003419 }
3420 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3421 if (sync == MagickFalse)
3422 status=MagickFalse;
3423 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3424 {
3425 MagickBooleanType
3426 proceed;
3427
cristyb5d5f722009-11-04 03:03:49 +00003428#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003429 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003430#endif
3431 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3432 image->rows);
3433 if (proceed == MagickFalse)
3434 status=MagickFalse;
3435 }
3436 }
3437 blur_image->type=image->type;
3438 blur_view=DestroyCacheView(blur_view);
3439 image_view=DestroyCacheView(image_view);
3440 kernel=(double *) RelinquishMagickMemory(kernel);
3441 if (status == MagickFalse)
3442 blur_image=DestroyImage(blur_image);
3443 return(blur_image);
3444}
3445
3446/*
3447%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3448% %
3449% %
3450% %
3451% S h a d e I m a g e %
3452% %
3453% %
3454% %
3455%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3456%
3457% ShadeImage() shines a distant light on an image to create a
3458% three-dimensional effect. You control the positioning of the light with
3459% azimuth and elevation; azimuth is measured in degrees off the x axis
3460% and elevation is measured in pixels above the Z axis.
3461%
3462% The format of the ShadeImage method is:
3463%
3464% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3465% const double azimuth,const double elevation,ExceptionInfo *exception)
3466%
3467% A description of each parameter follows:
3468%
3469% o image: the image.
3470%
3471% o gray: A value other than zero shades the intensity of each pixel.
3472%
3473% o azimuth, elevation: Define the light source direction.
3474%
3475% o exception: return any errors or warnings in this structure.
3476%
3477*/
3478MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3479 const double azimuth,const double elevation,ExceptionInfo *exception)
3480{
3481#define ShadeImageTag "Shade/Image"
3482
cristyc4c8d132010-01-07 01:58:38 +00003483 CacheView
3484 *image_view,
3485 *shade_view;
3486
cristy3ed852e2009-09-05 21:47:34 +00003487 Image
3488 *shade_image;
3489
cristy3ed852e2009-09-05 21:47:34 +00003490 MagickBooleanType
3491 status;
3492
cristybb503372010-05-27 20:51:26 +00003493 MagickOffsetType
3494 progress;
3495
cristy3ed852e2009-09-05 21:47:34 +00003496 PrimaryInfo
3497 light;
3498
cristybb503372010-05-27 20:51:26 +00003499 ssize_t
3500 y;
3501
cristy3ed852e2009-09-05 21:47:34 +00003502 /*
3503 Initialize shaded image attributes.
3504 */
3505 assert(image != (const Image *) NULL);
3506 assert(image->signature == MagickSignature);
3507 if (image->debug != MagickFalse)
3508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3509 assert(exception != (ExceptionInfo *) NULL);
3510 assert(exception->signature == MagickSignature);
3511 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3512 if (shade_image == (Image *) NULL)
3513 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003514 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003515 {
cristy3ed852e2009-09-05 21:47:34 +00003516 shade_image=DestroyImage(shade_image);
3517 return((Image *) NULL);
3518 }
3519 /*
3520 Compute the light vector.
3521 */
3522 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3523 cos(DegreesToRadians(elevation));
3524 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3525 cos(DegreesToRadians(elevation));
3526 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3527 /*
3528 Shade image.
3529 */
3530 status=MagickTrue;
3531 progress=0;
3532 image_view=AcquireCacheView(image);
3533 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003534#if defined(MAGICKCORE_OPENMP_SUPPORT)
3535 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003536#endif
cristybb503372010-05-27 20:51:26 +00003537 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003538 {
3539 MagickRealType
3540 distance,
3541 normal_distance,
3542 shade;
3543
3544 PrimaryInfo
3545 normal;
3546
cristy4c08aed2011-07-01 19:47:50 +00003547 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003548 *restrict p,
3549 *restrict s0,
3550 *restrict s1,
3551 *restrict s2;
cristy3ed852e2009-09-05 21:47:34 +00003552
cristy4c08aed2011-07-01 19:47:50 +00003553 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003554 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003555
cristy117ff172010-08-15 21:35:32 +00003556 register ssize_t
3557 x;
3558
cristy3ed852e2009-09-05 21:47:34 +00003559 if (status == MagickFalse)
3560 continue;
3561 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3562 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3563 exception);
cristy4c08aed2011-07-01 19:47:50 +00003564 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003565 {
3566 status=MagickFalse;
3567 continue;
3568 }
3569 /*
3570 Shade this row of pixels.
3571 */
3572 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristyed231572011-07-14 02:18:59 +00003573 s0=p+GetPixelChannels(image);
3574 s1=s0+(image->columns+2)*GetPixelChannels(image);
3575 s2=s1+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003576 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003577 {
3578 /*
3579 Determine the surface normal and compute shading.
3580 */
cristyed231572011-07-14 02:18:59 +00003581 normal.x=(double) (GetPixelIntensity(image,s0-GetPixelChannels(image))+
3582 GetPixelIntensity(image,s1-GetPixelChannels(image))+
3583 GetPixelIntensity(image,s2-GetPixelChannels(image))-
3584 GetPixelIntensity(image,s0+GetPixelChannels(image))-
3585 GetPixelIntensity(image,s1+GetPixelChannels(image))-
3586 GetPixelIntensity(image,s2+GetPixelChannels(image)));
3587 normal.y=(double) (GetPixelIntensity(image,s2-GetPixelChannels(image))+
cristy4c08aed2011-07-01 19:47:50 +00003588 GetPixelIntensity(image,s2)+
cristyed231572011-07-14 02:18:59 +00003589 GetPixelIntensity(image,s2+GetPixelChannels(image))-
3590 GetPixelIntensity(image,s0-GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003591 GetPixelIntensity(image,s0)-
cristyed231572011-07-14 02:18:59 +00003592 GetPixelIntensity(image,s0+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003593 if ((normal.x == 0.0) && (normal.y == 0.0))
3594 shade=light.z;
3595 else
3596 {
3597 shade=0.0;
3598 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3599 if (distance > MagickEpsilon)
3600 {
3601 normal_distance=
3602 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3603 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3604 shade=distance/sqrt((double) normal_distance);
3605 }
3606 }
3607 if (gray != MagickFalse)
3608 {
cristy4c08aed2011-07-01 19:47:50 +00003609 SetPixelRed(shade_image,ClampToQuantum(shade),q);
3610 SetPixelGreen(shade_image,ClampToQuantum(shade),q);
3611 SetPixelBlue(shade_image,ClampToQuantum(shade),q);
cristy3ed852e2009-09-05 21:47:34 +00003612 }
3613 else
3614 {
cristy4c08aed2011-07-01 19:47:50 +00003615 SetPixelRed(shade_image,ClampToQuantum(QuantumScale*shade*
3616 GetPixelRed(image,s1)),q);
3617 SetPixelGreen(shade_image,ClampToQuantum(QuantumScale*shade*
3618 GetPixelGreen(image,s1)),q);
3619 SetPixelBlue(shade_image,ClampToQuantum(QuantumScale*shade*
3620 GetPixelBlue(image,s1)),q);
cristy3ed852e2009-09-05 21:47:34 +00003621 }
cristy4c08aed2011-07-01 19:47:50 +00003622 SetPixelAlpha(shade_image,GetPixelAlpha(image,s1),q);
cristyed231572011-07-14 02:18:59 +00003623 s0+=GetPixelChannels(image);
3624 s1+=GetPixelChannels(image);
3625 s2+=GetPixelChannels(image);
3626 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003627 }
3628 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3629 status=MagickFalse;
3630 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3631 {
3632 MagickBooleanType
3633 proceed;
3634
cristyb5d5f722009-11-04 03:03:49 +00003635#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003636 #pragma omp critical (MagickCore_ShadeImage)
3637#endif
3638 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3639 if (proceed == MagickFalse)
3640 status=MagickFalse;
3641 }
3642 }
3643 shade_view=DestroyCacheView(shade_view);
3644 image_view=DestroyCacheView(image_view);
3645 if (status == MagickFalse)
3646 shade_image=DestroyImage(shade_image);
3647 return(shade_image);
3648}
3649
3650/*
3651%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3652% %
3653% %
3654% %
3655% S h a r p e n I m a g e %
3656% %
3657% %
3658% %
3659%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3660%
3661% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3662% operator of the given radius and standard deviation (sigma). For
3663% reasonable results, radius should be larger than sigma. Use a radius of 0
3664% and SharpenImage() selects a suitable radius for you.
3665%
3666% Using a separable kernel would be faster, but the negative weights cancel
3667% out on the corners of the kernel producing often undesirable ringing in the
3668% filtered result; this can be avoided by using a 2D gaussian shaped image
3669% sharpening kernel instead.
3670%
3671% The format of the SharpenImage method is:
3672%
3673% Image *SharpenImage(const Image *image,const double radius,
3674% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003675%
3676% A description of each parameter follows:
3677%
3678% o image: the image.
3679%
cristy3ed852e2009-09-05 21:47:34 +00003680% o radius: the radius of the Gaussian, in pixels, not counting the center
3681% pixel.
3682%
3683% o sigma: the standard deviation of the Laplacian, in pixels.
3684%
3685% o exception: return any errors or warnings in this structure.
3686%
3687*/
cristy3ed852e2009-09-05 21:47:34 +00003688MagickExport Image *SharpenImage(const Image *image,const double radius,
3689 const double sigma,ExceptionInfo *exception)
3690{
cristy3ed852e2009-09-05 21:47:34 +00003691 double
cristy47e00502009-12-17 19:19:57 +00003692 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003693
3694 Image
3695 *sharp_image;
3696
cristy41cbe682011-07-15 19:12:37 +00003697 KernelInfo
3698 *kernel_info;
3699
cristybb503372010-05-27 20:51:26 +00003700 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003701 i;
3702
cristybb503372010-05-27 20:51:26 +00003703 size_t
cristy3ed852e2009-09-05 21:47:34 +00003704 width;
3705
cristy117ff172010-08-15 21:35:32 +00003706 ssize_t
3707 j,
3708 u,
3709 v;
3710
cristy3ed852e2009-09-05 21:47:34 +00003711 assert(image != (const Image *) NULL);
3712 assert(image->signature == MagickSignature);
3713 if (image->debug != MagickFalse)
3714 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3715 assert(exception != (ExceptionInfo *) NULL);
3716 assert(exception->signature == MagickSignature);
3717 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003718 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003719 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003720 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003721 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3722 kernel_info->width=width;
3723 kernel_info->height=width;
3724 kernel_info->signature=MagickSignature;
3725 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
3726 kernel_info->width*sizeof(*kernel_info->values));
3727 if (kernel_info->values == (double *) NULL)
3728 {
3729 kernel_info=DestroyKernelInfo(kernel_info);
3730 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3731 }
cristy3ed852e2009-09-05 21:47:34 +00003732 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003733 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003734 i=0;
3735 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003736 {
cristy47e00502009-12-17 19:19:57 +00003737 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003738 {
cristy41cbe682011-07-15 19:12:37 +00003739 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3740 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3741 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003742 i++;
3743 }
3744 }
cristy41cbe682011-07-15 19:12:37 +00003745 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy0a922382011-07-16 15:30:34 +00003746 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00003747 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003748 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003749 return(sharp_image);
3750}
3751
3752/*
3753%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3754% %
3755% %
3756% %
3757% S p r e a d I m a g e %
3758% %
3759% %
3760% %
3761%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3762%
3763% SpreadImage() is a special effects method that randomly displaces each
3764% pixel in a block defined by the radius parameter.
3765%
3766% The format of the SpreadImage method is:
3767%
3768% Image *SpreadImage(const Image *image,const double radius,
3769% ExceptionInfo *exception)
3770%
3771% A description of each parameter follows:
3772%
3773% o image: the image.
3774%
3775% o radius: Choose a random pixel in a neighborhood of this extent.
3776%
3777% o exception: return any errors or warnings in this structure.
3778%
3779*/
3780MagickExport Image *SpreadImage(const Image *image,const double radius,
3781 ExceptionInfo *exception)
3782{
3783#define SpreadImageTag "Spread/Image"
3784
cristyfa112112010-01-04 17:48:07 +00003785 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003786 *image_view,
3787 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003788
cristy3ed852e2009-09-05 21:47:34 +00003789 Image
3790 *spread_image;
3791
cristy3ed852e2009-09-05 21:47:34 +00003792 MagickBooleanType
3793 status;
3794
cristybb503372010-05-27 20:51:26 +00003795 MagickOffsetType
3796 progress;
3797
cristy4c08aed2011-07-01 19:47:50 +00003798 PixelInfo
cristyddd82202009-11-03 20:14:50 +00003799 bias;
cristy3ed852e2009-09-05 21:47:34 +00003800
3801 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003802 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003803
cristybb503372010-05-27 20:51:26 +00003804 size_t
cristy3ed852e2009-09-05 21:47:34 +00003805 width;
3806
cristybb503372010-05-27 20:51:26 +00003807 ssize_t
3808 y;
3809
cristy3ed852e2009-09-05 21:47:34 +00003810 /*
3811 Initialize spread image attributes.
3812 */
3813 assert(image != (Image *) NULL);
3814 assert(image->signature == MagickSignature);
3815 if (image->debug != MagickFalse)
3816 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3817 assert(exception != (ExceptionInfo *) NULL);
3818 assert(exception->signature == MagickSignature);
3819 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3820 exception);
3821 if (spread_image == (Image *) NULL)
3822 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003823 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003824 {
cristy3ed852e2009-09-05 21:47:34 +00003825 spread_image=DestroyImage(spread_image);
3826 return((Image *) NULL);
3827 }
3828 /*
3829 Spread image.
3830 */
3831 status=MagickTrue;
3832 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003833 GetPixelInfo(spread_image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003834 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003835 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003836 image_view=AcquireCacheView(image);
3837 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003838#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00003839 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00003840#endif
cristybb503372010-05-27 20:51:26 +00003841 for (y=0; y < (ssize_t) spread_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003842 {
cristy5c9e6f22010-09-17 17:31:01 +00003843 const int
3844 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003845
cristy4c08aed2011-07-01 19:47:50 +00003846 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003847 pixel;
3848
cristy4c08aed2011-07-01 19:47:50 +00003849 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003850 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003851
cristy117ff172010-08-15 21:35:32 +00003852 register ssize_t
3853 x;
3854
cristy3ed852e2009-09-05 21:47:34 +00003855 if (status == MagickFalse)
3856 continue;
cristy9f7e7cb2011-03-26 00:49:57 +00003857 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003858 exception);
cristyacd2ed22011-08-30 01:44:23 +00003859 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003860 {
3861 status=MagickFalse;
3862 continue;
3863 }
cristyddd82202009-11-03 20:14:50 +00003864 pixel=bias;
cristybb503372010-05-27 20:51:26 +00003865 for (x=0; x < (ssize_t) spread_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003866 {
cristy4c08aed2011-07-01 19:47:50 +00003867 (void) InterpolatePixelInfo(image,image_view,
cristy8a7c3e82011-03-26 02:10:53 +00003868 UndefinedInterpolatePixel,(double) x+width*(GetPseudoRandomValue(
3869 random_info[id])-0.5),(double) y+width*(GetPseudoRandomValue(
3870 random_info[id])-0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003871 SetPixelPixelInfo(spread_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00003872 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003873 }
cristy9f7e7cb2011-03-26 00:49:57 +00003874 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003875 status=MagickFalse;
3876 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3877 {
3878 MagickBooleanType
3879 proceed;
3880
cristyb557a152011-02-22 12:14:30 +00003881#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003882 #pragma omp critical (MagickCore_SpreadImage)
3883#endif
3884 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3885 if (proceed == MagickFalse)
3886 status=MagickFalse;
3887 }
3888 }
cristy9f7e7cb2011-03-26 00:49:57 +00003889 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003890 image_view=DestroyCacheView(image_view);
3891 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003892 return(spread_image);
3893}
3894
3895/*
3896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3897% %
3898% %
3899% %
cristy0834d642011-03-18 18:26:08 +00003900% S t a t i s t i c I m a g e %
3901% %
3902% %
3903% %
3904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3905%
3906% StatisticImage() makes each pixel the min / max / median / mode / etc. of
cristy8d752042011-03-19 01:00:36 +00003907% the neighborhood of the specified width and height.
cristy0834d642011-03-18 18:26:08 +00003908%
3909% The format of the StatisticImage method is:
3910%
3911% Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00003912% const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00003913%
3914% A description of each parameter follows:
3915%
3916% o image: the image.
3917%
cristy0834d642011-03-18 18:26:08 +00003918% o type: the statistic type (median, mode, etc.).
3919%
cristy95c38342011-03-18 22:39:51 +00003920% o width: the width of the pixel neighborhood.
3921%
3922% o height: the height of the pixel neighborhood.
cristy0834d642011-03-18 18:26:08 +00003923%
3924% o exception: return any errors or warnings in this structure.
3925%
3926*/
3927
cristy733678d2011-03-18 21:29:28 +00003928#define ListChannels 5
3929
3930typedef struct _ListNode
3931{
3932 size_t
3933 next[9],
3934 count,
3935 signature;
3936} ListNode;
3937
3938typedef struct _SkipList
3939{
3940 ssize_t
3941 level;
3942
3943 ListNode
3944 *nodes;
3945} SkipList;
3946
3947typedef struct _PixelList
3948{
3949 size_t
cristy6fc86bb2011-03-18 23:45:16 +00003950 length,
cristy733678d2011-03-18 21:29:28 +00003951 seed,
3952 signature;
3953
3954 SkipList
3955 lists[ListChannels];
3956} PixelList;
3957
3958static PixelList *DestroyPixelList(PixelList *pixel_list)
3959{
3960 register ssize_t
3961 i;
3962
3963 if (pixel_list == (PixelList *) NULL)
3964 return((PixelList *) NULL);
3965 for (i=0; i < ListChannels; i++)
3966 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
3967 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
3968 pixel_list->lists[i].nodes);
3969 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
3970 return(pixel_list);
3971}
3972
3973static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
3974{
3975 register ssize_t
3976 i;
3977
3978 assert(pixel_list != (PixelList **) NULL);
3979 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
3980 if (pixel_list[i] != (PixelList *) NULL)
3981 pixel_list[i]=DestroyPixelList(pixel_list[i]);
3982 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
3983 return(pixel_list);
3984}
3985
cristy6fc86bb2011-03-18 23:45:16 +00003986static PixelList *AcquirePixelList(const size_t width,const size_t height)
cristy733678d2011-03-18 21:29:28 +00003987{
3988 PixelList
3989 *pixel_list;
3990
3991 register ssize_t
3992 i;
3993
3994 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
3995 if (pixel_list == (PixelList *) NULL)
3996 return(pixel_list);
3997 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
cristy6fc86bb2011-03-18 23:45:16 +00003998 pixel_list->length=width*height;
cristy733678d2011-03-18 21:29:28 +00003999 for (i=0; i < ListChannels; i++)
4000 {
4001 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
4002 sizeof(*pixel_list->lists[i].nodes));
4003 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
4004 return(DestroyPixelList(pixel_list));
4005 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
4006 sizeof(*pixel_list->lists[i].nodes));
4007 }
4008 pixel_list->signature=MagickSignature;
4009 return(pixel_list);
4010}
4011
cristy6fc86bb2011-03-18 23:45:16 +00004012static PixelList **AcquirePixelListThreadSet(const size_t width,
4013 const size_t height)
cristy733678d2011-03-18 21:29:28 +00004014{
4015 PixelList
4016 **pixel_list;
4017
4018 register ssize_t
4019 i;
4020
4021 size_t
4022 number_threads;
4023
4024 number_threads=GetOpenMPMaximumThreads();
4025 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
4026 sizeof(*pixel_list));
4027 if (pixel_list == (PixelList **) NULL)
4028 return((PixelList **) NULL);
4029 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
4030 for (i=0; i < (ssize_t) number_threads; i++)
4031 {
cristy6fc86bb2011-03-18 23:45:16 +00004032 pixel_list[i]=AcquirePixelList(width,height);
cristy733678d2011-03-18 21:29:28 +00004033 if (pixel_list[i] == (PixelList *) NULL)
4034 return(DestroyPixelListThreadSet(pixel_list));
4035 }
4036 return(pixel_list);
4037}
4038
4039static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
4040 const size_t color)
4041{
4042 register SkipList
4043 *list;
4044
4045 register ssize_t
4046 level;
4047
4048 size_t
4049 search,
4050 update[9];
4051
4052 /*
4053 Initialize the node.
4054 */
4055 list=pixel_list->lists+channel;
4056 list->nodes[color].signature=pixel_list->signature;
4057 list->nodes[color].count=1;
4058 /*
4059 Determine where it belongs in the list.
4060 */
4061 search=65536UL;
4062 for (level=list->level; level >= 0; level--)
4063 {
4064 while (list->nodes[search].next[level] < color)
4065 search=list->nodes[search].next[level];
4066 update[level]=search;
4067 }
4068 /*
4069 Generate a pseudo-random level for this node.
4070 */
4071 for (level=0; ; level++)
4072 {
4073 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
4074 if ((pixel_list->seed & 0x300) != 0x300)
4075 break;
4076 }
4077 if (level > 8)
4078 level=8;
4079 if (level > (list->level+2))
4080 level=list->level+2;
4081 /*
4082 If we're raising the list's level, link back to the root node.
4083 */
4084 while (level > list->level)
4085 {
4086 list->level++;
4087 update[list->level]=65536UL;
4088 }
4089 /*
4090 Link the node into the skip-list.
4091 */
4092 do
4093 {
4094 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
4095 list->nodes[update[level]].next[level]=color;
cristy3cba8ca2011-03-19 01:29:12 +00004096 } while (level-- > 0);
cristy733678d2011-03-18 21:29:28 +00004097}
4098
cristy4c08aed2011-07-01 19:47:50 +00004099static PixelInfo GetMaximumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004100{
cristy4c08aed2011-07-01 19:47:50 +00004101 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004102 pixel;
4103
4104 register SkipList
4105 *list;
4106
4107 register ssize_t
4108 channel;
4109
4110 size_t
cristyd76c51e2011-03-26 00:21:26 +00004111 color,
4112 maximum;
cristy49f37242011-03-22 18:18:23 +00004113
4114 ssize_t
cristy6fc86bb2011-03-18 23:45:16 +00004115 count;
4116
4117 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004118 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004119
4120 /*
4121 Find the maximum value for each of the color.
4122 */
4123 for (channel=0; channel < 5; channel++)
4124 {
4125 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004126 color=65536L;
cristy6fc86bb2011-03-18 23:45:16 +00004127 count=0;
cristy49f37242011-03-22 18:18:23 +00004128 maximum=list->nodes[color].next[0];
cristy6fc86bb2011-03-18 23:45:16 +00004129 do
4130 {
4131 color=list->nodes[color].next[0];
cristy49f37242011-03-22 18:18:23 +00004132 if (color > maximum)
4133 maximum=color;
cristy6fc86bb2011-03-18 23:45:16 +00004134 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004135 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004136 channels[channel]=(unsigned short) maximum;
4137 }
cristy4c08aed2011-07-01 19:47:50 +00004138 GetPixelInfo((const Image *) NULL,&pixel);
cristy49f37242011-03-22 18:18:23 +00004139 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4140 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4141 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004142 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4143 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy49f37242011-03-22 18:18:23 +00004144 return(pixel);
4145}
4146
cristy4c08aed2011-07-01 19:47:50 +00004147static PixelInfo GetMeanPixelList(PixelList *pixel_list)
cristy49f37242011-03-22 18:18:23 +00004148{
cristy4c08aed2011-07-01 19:47:50 +00004149 PixelInfo
cristy49f37242011-03-22 18:18:23 +00004150 pixel;
4151
cristy80a99a32011-03-30 01:30:23 +00004152 MagickRealType
4153 sum;
4154
cristy49f37242011-03-22 18:18:23 +00004155 register SkipList
4156 *list;
4157
4158 register ssize_t
4159 channel;
4160
4161 size_t
cristy80a99a32011-03-30 01:30:23 +00004162 color;
cristy49f37242011-03-22 18:18:23 +00004163
4164 ssize_t
4165 count;
4166
4167 unsigned short
4168 channels[ListChannels];
4169
4170 /*
4171 Find the mean value for each of the color.
4172 */
4173 for (channel=0; channel < 5; channel++)
4174 {
4175 list=pixel_list->lists+channel;
4176 color=65536L;
4177 count=0;
cristy80a99a32011-03-30 01:30:23 +00004178 sum=0.0;
cristy49f37242011-03-22 18:18:23 +00004179 do
4180 {
4181 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004182 sum+=(MagickRealType) list->nodes[color].count*color;
cristy49f37242011-03-22 18:18:23 +00004183 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004184 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004185 sum/=pixel_list->length;
4186 channels[channel]=(unsigned short) sum;
cristy6fc86bb2011-03-18 23:45:16 +00004187 }
cristy4c08aed2011-07-01 19:47:50 +00004188 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004189 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4190 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4191 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004192 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4193 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004194 return(pixel);
4195}
4196
cristy4c08aed2011-07-01 19:47:50 +00004197static PixelInfo GetMedianPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004198{
cristy4c08aed2011-07-01 19:47:50 +00004199 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004200 pixel;
4201
4202 register SkipList
4203 *list;
4204
4205 register ssize_t
4206 channel;
4207
4208 size_t
cristy49f37242011-03-22 18:18:23 +00004209 color;
4210
4211 ssize_t
cristy733678d2011-03-18 21:29:28 +00004212 count;
4213
4214 unsigned short
4215 channels[ListChannels];
4216
4217 /*
4218 Find the median value for each of the color.
4219 */
cristy733678d2011-03-18 21:29:28 +00004220 for (channel=0; channel < 5; channel++)
4221 {
4222 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004223 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004224 count=0;
4225 do
4226 {
4227 color=list->nodes[color].next[0];
4228 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004229 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy6fc86bb2011-03-18 23:45:16 +00004230 channels[channel]=(unsigned short) color;
4231 }
cristy4c08aed2011-07-01 19:47:50 +00004232 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004233 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4234 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4235 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004236 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4237 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004238 return(pixel);
4239}
4240
cristy4c08aed2011-07-01 19:47:50 +00004241static PixelInfo GetMinimumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004242{
cristy4c08aed2011-07-01 19:47:50 +00004243 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004244 pixel;
4245
4246 register SkipList
4247 *list;
4248
4249 register ssize_t
4250 channel;
4251
4252 size_t
cristyd76c51e2011-03-26 00:21:26 +00004253 color,
4254 minimum;
cristy6fc86bb2011-03-18 23:45:16 +00004255
cristy49f37242011-03-22 18:18:23 +00004256 ssize_t
4257 count;
4258
cristy6fc86bb2011-03-18 23:45:16 +00004259 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004260 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004261
4262 /*
4263 Find the minimum value for each of the color.
4264 */
4265 for (channel=0; channel < 5; channel++)
4266 {
4267 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004268 count=0;
cristy6fc86bb2011-03-18 23:45:16 +00004269 color=65536UL;
cristy49f37242011-03-22 18:18:23 +00004270 minimum=list->nodes[color].next[0];
4271 do
4272 {
4273 color=list->nodes[color].next[0];
4274 if (color < minimum)
4275 minimum=color;
4276 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004277 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004278 channels[channel]=(unsigned short) minimum;
cristy733678d2011-03-18 21:29:28 +00004279 }
cristy4c08aed2011-07-01 19:47:50 +00004280 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004281 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4282 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4283 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004284 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4285 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004286 return(pixel);
4287}
4288
cristy4c08aed2011-07-01 19:47:50 +00004289static PixelInfo GetModePixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004290{
cristy4c08aed2011-07-01 19:47:50 +00004291 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004292 pixel;
4293
4294 register SkipList
4295 *list;
4296
4297 register ssize_t
4298 channel;
4299
4300 size_t
4301 color,
cristy733678d2011-03-18 21:29:28 +00004302 max_count,
cristy6fc86bb2011-03-18 23:45:16 +00004303 mode;
cristy733678d2011-03-18 21:29:28 +00004304
cristy49f37242011-03-22 18:18:23 +00004305 ssize_t
4306 count;
4307
cristy733678d2011-03-18 21:29:28 +00004308 unsigned short
4309 channels[5];
4310
4311 /*
glennrp30d2dc62011-06-25 03:17:16 +00004312 Make each pixel the 'predominant color' of the specified neighborhood.
cristy733678d2011-03-18 21:29:28 +00004313 */
cristy733678d2011-03-18 21:29:28 +00004314 for (channel=0; channel < 5; channel++)
4315 {
4316 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004317 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004318 mode=color;
4319 max_count=list->nodes[mode].count;
4320 count=0;
4321 do
4322 {
4323 color=list->nodes[color].next[0];
4324 if (list->nodes[color].count > max_count)
4325 {
4326 mode=color;
4327 max_count=list->nodes[mode].count;
4328 }
4329 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004330 } while (count < (ssize_t) pixel_list->length);
cristy733678d2011-03-18 21:29:28 +00004331 channels[channel]=(unsigned short) mode;
4332 }
cristy4c08aed2011-07-01 19:47:50 +00004333 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004334 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4335 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4336 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004337 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4338 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004339 return(pixel);
4340}
4341
cristy4c08aed2011-07-01 19:47:50 +00004342static PixelInfo GetNonpeakPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004343{
cristy4c08aed2011-07-01 19:47:50 +00004344 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004345 pixel;
4346
4347 register SkipList
4348 *list;
4349
4350 register ssize_t
4351 channel;
4352
4353 size_t
cristy733678d2011-03-18 21:29:28 +00004354 color,
cristy733678d2011-03-18 21:29:28 +00004355 next,
4356 previous;
4357
cristy49f37242011-03-22 18:18:23 +00004358 ssize_t
4359 count;
4360
cristy733678d2011-03-18 21:29:28 +00004361 unsigned short
4362 channels[5];
4363
4364 /*
cristy49f37242011-03-22 18:18:23 +00004365 Finds the non peak value for each of the colors.
cristy733678d2011-03-18 21:29:28 +00004366 */
cristy733678d2011-03-18 21:29:28 +00004367 for (channel=0; channel < 5; channel++)
4368 {
4369 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004370 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004371 next=list->nodes[color].next[0];
4372 count=0;
4373 do
4374 {
4375 previous=color;
4376 color=next;
4377 next=list->nodes[color].next[0];
4378 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004379 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy733678d2011-03-18 21:29:28 +00004380 if ((previous == 65536UL) && (next != 65536UL))
4381 color=next;
4382 else
4383 if ((previous != 65536UL) && (next == 65536UL))
4384 color=previous;
4385 channels[channel]=(unsigned short) color;
4386 }
cristy4c08aed2011-07-01 19:47:50 +00004387 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004388 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4389 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4390 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004391 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4392 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy733678d2011-03-18 21:29:28 +00004393 return(pixel);
4394}
4395
cristy4c08aed2011-07-01 19:47:50 +00004396static PixelInfo GetStandardDeviationPixelList(PixelList *pixel_list)
cristy9a68cbb2011-03-29 00:51:23 +00004397{
cristy4c08aed2011-07-01 19:47:50 +00004398 PixelInfo
cristy9a68cbb2011-03-29 00:51:23 +00004399 pixel;
4400
cristy80a99a32011-03-30 01:30:23 +00004401 MagickRealType
4402 sum,
4403 sum_squared;
4404
cristy9a68cbb2011-03-29 00:51:23 +00004405 register SkipList
4406 *list;
4407
4408 register ssize_t
4409 channel;
4410
4411 size_t
cristy80a99a32011-03-30 01:30:23 +00004412 color;
cristy9a68cbb2011-03-29 00:51:23 +00004413
4414 ssize_t
4415 count;
4416
4417 unsigned short
4418 channels[ListChannels];
4419
4420 /*
cristy80a99a32011-03-30 01:30:23 +00004421 Find the standard-deviation value for each of the color.
cristy9a68cbb2011-03-29 00:51:23 +00004422 */
4423 for (channel=0; channel < 5; channel++)
4424 {
4425 list=pixel_list->lists+channel;
4426 color=65536L;
4427 count=0;
cristy80a99a32011-03-30 01:30:23 +00004428 sum=0.0;
4429 sum_squared=0.0;
cristy9a68cbb2011-03-29 00:51:23 +00004430 do
4431 {
cristy80a99a32011-03-30 01:30:23 +00004432 register ssize_t
4433 i;
4434
cristy9a68cbb2011-03-29 00:51:23 +00004435 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004436 sum+=(MagickRealType) list->nodes[color].count*color;
4437 for (i=0; i < (ssize_t) list->nodes[color].count; i++)
4438 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
cristy9a68cbb2011-03-29 00:51:23 +00004439 count+=list->nodes[color].count;
4440 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004441 sum/=pixel_list->length;
4442 sum_squared/=pixel_list->length;
4443 channels[channel]=(unsigned short) sqrt(sum_squared-(sum*sum));
cristy9a68cbb2011-03-29 00:51:23 +00004444 }
cristy4c08aed2011-07-01 19:47:50 +00004445 GetPixelInfo((const Image *) NULL,&pixel);
cristy9a68cbb2011-03-29 00:51:23 +00004446 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4447 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4448 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004449 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4450 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy9a68cbb2011-03-29 00:51:23 +00004451 return(pixel);
4452}
4453
cristy4c08aed2011-07-01 19:47:50 +00004454static inline void InsertPixelList(const Image *image,const Quantum *pixel,
4455 PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004456{
4457 size_t
4458 signature;
4459
4460 unsigned short
4461 index;
4462
cristy4c08aed2011-07-01 19:47:50 +00004463 index=ScaleQuantumToShort(GetPixelRed(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004464 signature=pixel_list->lists[0].nodes[index].signature;
4465 if (signature == pixel_list->signature)
4466 pixel_list->lists[0].nodes[index].count++;
4467 else
4468 AddNodePixelList(pixel_list,0,index);
cristy4c08aed2011-07-01 19:47:50 +00004469 index=ScaleQuantumToShort(GetPixelGreen(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004470 signature=pixel_list->lists[1].nodes[index].signature;
4471 if (signature == pixel_list->signature)
4472 pixel_list->lists[1].nodes[index].count++;
4473 else
4474 AddNodePixelList(pixel_list,1,index);
cristy4c08aed2011-07-01 19:47:50 +00004475 index=ScaleQuantumToShort(GetPixelBlue(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004476 signature=pixel_list->lists[2].nodes[index].signature;
4477 if (signature == pixel_list->signature)
4478 pixel_list->lists[2].nodes[index].count++;
4479 else
4480 AddNodePixelList(pixel_list,2,index);
cristy4c08aed2011-07-01 19:47:50 +00004481 index=ScaleQuantumToShort(GetPixelAlpha(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004482 signature=pixel_list->lists[3].nodes[index].signature;
4483 if (signature == pixel_list->signature)
4484 pixel_list->lists[3].nodes[index].count++;
4485 else
4486 AddNodePixelList(pixel_list,3,index);
4487 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004488 index=ScaleQuantumToShort(GetPixelBlack(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004489 signature=pixel_list->lists[4].nodes[index].signature;
4490 if (signature == pixel_list->signature)
4491 pixel_list->lists[4].nodes[index].count++;
4492 else
4493 AddNodePixelList(pixel_list,4,index);
4494}
4495
cristy80c99742011-04-04 14:46:39 +00004496static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
4497{
4498 if (x < 0)
4499 return(-x);
4500 return(x);
4501}
4502
cristy733678d2011-03-18 21:29:28 +00004503static void ResetPixelList(PixelList *pixel_list)
4504{
4505 int
4506 level;
4507
4508 register ListNode
4509 *root;
4510
4511 register SkipList
4512 *list;
4513
4514 register ssize_t
4515 channel;
4516
4517 /*
4518 Reset the skip-list.
4519 */
4520 for (channel=0; channel < 5; channel++)
4521 {
4522 list=pixel_list->lists+channel;
4523 root=list->nodes+65536UL;
4524 list->level=0;
4525 for (level=0; level < 9; level++)
4526 root->next[level]=65536UL;
4527 }
4528 pixel_list->seed=pixel_list->signature++;
4529}
4530
cristy0834d642011-03-18 18:26:08 +00004531MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00004532 const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00004533{
cristy3cba8ca2011-03-19 01:29:12 +00004534#define StatisticWidth \
cristyd76c51e2011-03-26 00:21:26 +00004535 (width == 0 ? GetOptimalKernelWidth2D((double) width,0.5) : width)
cristy3cba8ca2011-03-19 01:29:12 +00004536#define StatisticHeight \
cristyd76c51e2011-03-26 00:21:26 +00004537 (height == 0 ? GetOptimalKernelWidth2D((double) height,0.5) : height)
cristy0834d642011-03-18 18:26:08 +00004538#define StatisticImageTag "Statistic/Image"
4539
4540 CacheView
4541 *image_view,
4542 *statistic_view;
4543
4544 Image
4545 *statistic_image;
4546
4547 MagickBooleanType
4548 status;
4549
4550 MagickOffsetType
4551 progress;
4552
4553 PixelList
4554 **restrict pixel_list;
4555
cristy0834d642011-03-18 18:26:08 +00004556 ssize_t
4557 y;
4558
4559 /*
4560 Initialize statistics image attributes.
4561 */
4562 assert(image != (Image *) NULL);
4563 assert(image->signature == MagickSignature);
4564 if (image->debug != MagickFalse)
4565 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4566 assert(exception != (ExceptionInfo *) NULL);
4567 assert(exception->signature == MagickSignature);
cristy0834d642011-03-18 18:26:08 +00004568 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4569 exception);
4570 if (statistic_image == (Image *) NULL)
4571 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004572 if (SetImageStorageClass(statistic_image,DirectClass,exception) == MagickFalse)
cristy0834d642011-03-18 18:26:08 +00004573 {
cristy0834d642011-03-18 18:26:08 +00004574 statistic_image=DestroyImage(statistic_image);
4575 return((Image *) NULL);
4576 }
cristy6fc86bb2011-03-18 23:45:16 +00004577 pixel_list=AcquirePixelListThreadSet(StatisticWidth,StatisticHeight);
cristy0834d642011-03-18 18:26:08 +00004578 if (pixel_list == (PixelList **) NULL)
4579 {
4580 statistic_image=DestroyImage(statistic_image);
4581 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4582 }
4583 /*
cristy8d752042011-03-19 01:00:36 +00004584 Make each pixel the min / max / median / mode / etc. of the neighborhood.
cristy0834d642011-03-18 18:26:08 +00004585 */
4586 status=MagickTrue;
4587 progress=0;
4588 image_view=AcquireCacheView(image);
4589 statistic_view=AcquireCacheView(statistic_image);
4590#if defined(MAGICKCORE_OPENMP_SUPPORT)
4591 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4592#endif
4593 for (y=0; y < (ssize_t) statistic_image->rows; y++)
4594 {
4595 const int
4596 id = GetOpenMPThreadId();
4597
cristy4c08aed2011-07-01 19:47:50 +00004598 register const Quantum
cristy0834d642011-03-18 18:26:08 +00004599 *restrict p;
4600
cristy4c08aed2011-07-01 19:47:50 +00004601 register Quantum
cristy0834d642011-03-18 18:26:08 +00004602 *restrict q;
4603
4604 register ssize_t
4605 x;
4606
4607 if (status == MagickFalse)
4608 continue;
cristy6fc86bb2011-03-18 23:45:16 +00004609 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) StatisticWidth/2L),y-
4610 (ssize_t) (StatisticHeight/2L),image->columns+StatisticWidth,
4611 StatisticHeight,exception);
cristy3cba8ca2011-03-19 01:29:12 +00004612 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004613 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy0834d642011-03-18 18:26:08 +00004614 {
4615 status=MagickFalse;
4616 continue;
4617 }
cristy0834d642011-03-18 18:26:08 +00004618 for (x=0; x < (ssize_t) statistic_image->columns; x++)
4619 {
cristy4c08aed2011-07-01 19:47:50 +00004620 PixelInfo
cristy0834d642011-03-18 18:26:08 +00004621 pixel;
4622
cristy4c08aed2011-07-01 19:47:50 +00004623 register const Quantum
cristy6e3026a2011-03-19 00:54:38 +00004624 *restrict r;
4625
cristy0834d642011-03-18 18:26:08 +00004626 register ssize_t
4627 u,
4628 v;
4629
4630 r=p;
cristy0834d642011-03-18 18:26:08 +00004631 ResetPixelList(pixel_list[id]);
cristy6e4c3292011-03-19 00:53:55 +00004632 for (v=0; v < (ssize_t) StatisticHeight; v++)
cristy0834d642011-03-18 18:26:08 +00004633 {
cristy6e4c3292011-03-19 00:53:55 +00004634 for (u=0; u < (ssize_t) StatisticWidth; u++)
cristyed231572011-07-14 02:18:59 +00004635 InsertPixelList(image,r+u*GetPixelChannels(image),pixel_list[id]);
4636 r+=(image->columns+StatisticWidth)*GetPixelChannels(image);
cristy0834d642011-03-18 18:26:08 +00004637 }
cristy4c08aed2011-07-01 19:47:50 +00004638 GetPixelInfo(image,&pixel);
cristy490408a2011-07-07 14:42:05 +00004639 SetPixelInfo(image,p+(StatisticWidth*StatisticHeight/2)*
cristyed231572011-07-14 02:18:59 +00004640 GetPixelChannels(image),&pixel);
cristy0834d642011-03-18 18:26:08 +00004641 switch (type)
4642 {
cristy80c99742011-04-04 14:46:39 +00004643 case GradientStatistic:
4644 {
cristy4c08aed2011-07-01 19:47:50 +00004645 PixelInfo
cristy80c99742011-04-04 14:46:39 +00004646 maximum,
4647 minimum;
4648
4649 minimum=GetMinimumPixelList(pixel_list[id]);
4650 maximum=GetMaximumPixelList(pixel_list[id]);
4651 pixel.red=MagickAbsoluteValue(maximum.red-minimum.red);
4652 pixel.green=MagickAbsoluteValue(maximum.green-minimum.green);
4653 pixel.blue=MagickAbsoluteValue(maximum.blue-minimum.blue);
cristy4c08aed2011-07-01 19:47:50 +00004654 pixel.alpha=MagickAbsoluteValue(maximum.alpha-minimum.alpha);
cristy80c99742011-04-04 14:46:39 +00004655 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004656 pixel.black=MagickAbsoluteValue(maximum.black-minimum.black);
cristy80c99742011-04-04 14:46:39 +00004657 break;
4658 }
cristy6fc86bb2011-03-18 23:45:16 +00004659 case MaximumStatistic:
4660 {
4661 pixel=GetMaximumPixelList(pixel_list[id]);
4662 break;
4663 }
cristy49f37242011-03-22 18:18:23 +00004664 case MeanStatistic:
4665 {
4666 pixel=GetMeanPixelList(pixel_list[id]);
4667 break;
4668 }
cristyf2ad14a2011-03-18 18:57:25 +00004669 case MedianStatistic:
cristy6fc86bb2011-03-18 23:45:16 +00004670 default:
cristyf2ad14a2011-03-18 18:57:25 +00004671 {
4672 pixel=GetMedianPixelList(pixel_list[id]);
4673 break;
4674 }
cristy6fc86bb2011-03-18 23:45:16 +00004675 case MinimumStatistic:
4676 {
4677 pixel=GetMinimumPixelList(pixel_list[id]);
4678 break;
4679 }
cristyf2ad14a2011-03-18 18:57:25 +00004680 case ModeStatistic:
4681 {
4682 pixel=GetModePixelList(pixel_list[id]);
4683 break;
4684 }
4685 case NonpeakStatistic:
4686 {
4687 pixel=GetNonpeakPixelList(pixel_list[id]);
4688 break;
4689 }
cristy9a68cbb2011-03-29 00:51:23 +00004690 case StandardDeviationStatistic:
4691 {
4692 pixel=GetStandardDeviationPixelList(pixel_list[id]);
4693 break;
4694 }
cristy0834d642011-03-18 18:26:08 +00004695 }
cristyed231572011-07-14 02:18:59 +00004696 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004697 SetPixelRed(statistic_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00004698 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004699 SetPixelGreen(statistic_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00004700 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004701 SetPixelBlue(statistic_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00004702 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy0834d642011-03-18 18:26:08 +00004703 (image->colorspace == CMYKColorspace))
cristy490408a2011-07-07 14:42:05 +00004704 SetPixelBlack(statistic_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +00004705 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00004706 (image->matte != MagickFalse))
cristy490408a2011-07-07 14:42:05 +00004707 SetPixelAlpha(statistic_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00004708 p+=GetPixelChannels(image);
4709 q+=GetPixelChannels(statistic_image);
cristy0834d642011-03-18 18:26:08 +00004710 }
4711 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
4712 status=MagickFalse;
4713 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4714 {
4715 MagickBooleanType
4716 proceed;
4717
4718#if defined(MAGICKCORE_OPENMP_SUPPORT)
4719 #pragma omp critical (MagickCore_StatisticImage)
4720#endif
4721 proceed=SetImageProgress(image,StatisticImageTag,progress++,
4722 image->rows);
4723 if (proceed == MagickFalse)
4724 status=MagickFalse;
4725 }
4726 }
4727 statistic_view=DestroyCacheView(statistic_view);
4728 image_view=DestroyCacheView(image_view);
4729 pixel_list=DestroyPixelListThreadSet(pixel_list);
4730 return(statistic_image);
4731}
4732
4733/*
4734%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4735% %
4736% %
4737% %
cristy3ed852e2009-09-05 21:47:34 +00004738% U n s h a r p M a s k I m a g e %
4739% %
4740% %
4741% %
4742%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4743%
4744% UnsharpMaskImage() sharpens one or more image channels. We convolve the
4745% image with a Gaussian operator of the given radius and standard deviation
4746% (sigma). For reasonable results, radius should be larger than sigma. Use a
4747% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
4748%
4749% The format of the UnsharpMaskImage method is:
4750%
4751% Image *UnsharpMaskImage(const Image *image,const double radius,
4752% const double sigma,const double amount,const double threshold,
4753% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004754%
4755% A description of each parameter follows:
4756%
4757% o image: the image.
4758%
cristy3ed852e2009-09-05 21:47:34 +00004759% o radius: the radius of the Gaussian, in pixels, not counting the center
4760% pixel.
4761%
4762% o sigma: the standard deviation of the Gaussian, in pixels.
4763%
4764% o amount: the percentage of the difference between the original and the
4765% blur image that is added back into the original.
4766%
4767% o threshold: the threshold in pixels needed to apply the diffence amount.
4768%
4769% o exception: return any errors or warnings in this structure.
4770%
4771*/
cristyf4ad9df2011-07-08 16:49:03 +00004772MagickExport Image *UnsharpMaskImage(const Image *image,
4773 const double radius,const double sigma,const double amount,
4774 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004775{
4776#define SharpenImageTag "Sharpen/Image"
4777
cristyc4c8d132010-01-07 01:58:38 +00004778 CacheView
4779 *image_view,
4780 *unsharp_view;
4781
cristy3ed852e2009-09-05 21:47:34 +00004782 Image
4783 *unsharp_image;
4784
cristy3ed852e2009-09-05 21:47:34 +00004785 MagickBooleanType
4786 status;
4787
cristybb503372010-05-27 20:51:26 +00004788 MagickOffsetType
4789 progress;
4790
cristy4c08aed2011-07-01 19:47:50 +00004791 PixelInfo
cristyddd82202009-11-03 20:14:50 +00004792 bias;
cristy3ed852e2009-09-05 21:47:34 +00004793
4794 MagickRealType
4795 quantum_threshold;
4796
cristybb503372010-05-27 20:51:26 +00004797 ssize_t
4798 y;
4799
cristy3ed852e2009-09-05 21:47:34 +00004800 assert(image != (const Image *) NULL);
4801 assert(image->signature == MagickSignature);
4802 if (image->debug != MagickFalse)
4803 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4804 assert(exception != (ExceptionInfo *) NULL);
cristyf4ad9df2011-07-08 16:49:03 +00004805 unsharp_image=BlurImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004806 if (unsharp_image == (Image *) NULL)
4807 return((Image *) NULL);
4808 quantum_threshold=(MagickRealType) QuantumRange*threshold;
4809 /*
4810 Unsharp-mask image.
4811 */
4812 status=MagickTrue;
4813 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00004814 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00004815 image_view=AcquireCacheView(image);
4816 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00004817#if defined(MAGICKCORE_OPENMP_SUPPORT)
4818 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004819#endif
cristybb503372010-05-27 20:51:26 +00004820 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004821 {
cristy4c08aed2011-07-01 19:47:50 +00004822 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004823 pixel;
4824
cristy4c08aed2011-07-01 19:47:50 +00004825 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004826 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004827
cristy4c08aed2011-07-01 19:47:50 +00004828 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004829 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004830
cristy117ff172010-08-15 21:35:32 +00004831 register ssize_t
4832 x;
4833
cristy3ed852e2009-09-05 21:47:34 +00004834 if (status == MagickFalse)
4835 continue;
4836 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4837 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
4838 exception);
cristy4c08aed2011-07-01 19:47:50 +00004839 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004840 {
4841 status=MagickFalse;
4842 continue;
4843 }
cristyddd82202009-11-03 20:14:50 +00004844 pixel=bias;
cristybb503372010-05-27 20:51:26 +00004845 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004846 {
cristyed231572011-07-14 02:18:59 +00004847 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004848 {
cristy4c08aed2011-07-01 19:47:50 +00004849 pixel.red=GetPixelRed(image,p)-(MagickRealType) GetPixelRed(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004850 if (fabs(2.0*pixel.red) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004851 pixel.red=(MagickRealType) GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004852 else
cristy4c08aed2011-07-01 19:47:50 +00004853 pixel.red=(MagickRealType) GetPixelRed(image,p)+(pixel.red*amount);
4854 SetPixelRed(unsharp_image,ClampToQuantum(pixel.red),q);
cristy3ed852e2009-09-05 21:47:34 +00004855 }
cristyed231572011-07-14 02:18:59 +00004856 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004857 {
cristy4c08aed2011-07-01 19:47:50 +00004858 pixel.green=GetPixelGreen(image,p)-
4859 (MagickRealType) GetPixelGreen(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004860 if (fabs(2.0*pixel.green) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004861 pixel.green=(MagickRealType)
4862 GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004863 else
cristy4c08aed2011-07-01 19:47:50 +00004864 pixel.green=(MagickRealType)
4865 GetPixelGreen(image,p)+
4866 (pixel.green*amount);
4867 SetPixelGreen(unsharp_image,
4868 ClampToQuantum(pixel.green),q);
cristy3ed852e2009-09-05 21:47:34 +00004869 }
cristyed231572011-07-14 02:18:59 +00004870 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004871 {
cristy4c08aed2011-07-01 19:47:50 +00004872 pixel.blue=GetPixelBlue(image,p)-
4873 (MagickRealType) GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004874 if (fabs(2.0*pixel.blue) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004875 pixel.blue=(MagickRealType)
4876 GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004877 else
cristy4c08aed2011-07-01 19:47:50 +00004878 pixel.blue=(MagickRealType)
4879 GetPixelBlue(image,p)+(pixel.blue*amount);
4880 SetPixelBlue(unsharp_image,
4881 ClampToQuantum(pixel.blue),q);
cristy3ed852e2009-09-05 21:47:34 +00004882 }
cristyed231572011-07-14 02:18:59 +00004883 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00004884 (image->colorspace == CMYKColorspace))
4885 {
cristy4c08aed2011-07-01 19:47:50 +00004886 pixel.black=GetPixelBlack(image,p)-
4887 (MagickRealType) GetPixelBlack(image,q);
4888 if (fabs(2.0*pixel.black) < quantum_threshold)
4889 pixel.black=(MagickRealType)
4890 GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004891 else
cristy4c08aed2011-07-01 19:47:50 +00004892 pixel.black=(MagickRealType)
4893 GetPixelBlack(image,p)+(pixel.black*
4894 amount);
4895 SetPixelBlack(unsharp_image,
4896 ClampToQuantum(pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00004897 }
cristyed231572011-07-14 02:18:59 +00004898 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004899 {
4900 pixel.alpha=GetPixelAlpha(image,p)-
4901 (MagickRealType) GetPixelAlpha(image,q);
4902 if (fabs(2.0*pixel.alpha) < quantum_threshold)
4903 pixel.alpha=(MagickRealType)
4904 GetPixelAlpha(image,p);
4905 else
4906 pixel.alpha=GetPixelAlpha(image,p)+
4907 (pixel.alpha*amount);
4908 SetPixelAlpha(unsharp_image,
4909 ClampToQuantum(pixel.alpha),q);
4910 }
cristyed231572011-07-14 02:18:59 +00004911 p+=GetPixelChannels(image);
4912 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00004913 }
4914 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
4915 status=MagickFalse;
4916 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4917 {
4918 MagickBooleanType
4919 proceed;
4920
cristyb5d5f722009-11-04 03:03:49 +00004921#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00004922 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00004923#endif
4924 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4925 if (proceed == MagickFalse)
4926 status=MagickFalse;
4927 }
4928 }
4929 unsharp_image->type=image->type;
4930 unsharp_view=DestroyCacheView(unsharp_view);
4931 image_view=DestroyCacheView(image_view);
4932 if (status == MagickFalse)
4933 unsharp_image=DestroyImage(unsharp_image);
4934 return(unsharp_image);
4935}