blob: 9c57202a10e942dfbc5f288d89fa646f3e0af139 [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);
cristy30301712011-07-18 15:06:51 +00001344 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
cristy4e154852011-07-14 13:28:53 +00001345 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001346 if ((traits == UndefinedPixelTrait) ||
1347 (convolve_traits == UndefinedPixelTrait))
cristy4e154852011-07-14 13:28:53 +00001348 continue;
1349 if ((convolve_traits & CopyPixelTrait) != 0)
1350 {
cristyf7dc44c2011-07-20 14:41:15 +00001351 q[channel]=p[center+i];
cristy4e154852011-07-14 13:28:53 +00001352 continue;
1353 }
cristy5e6be1e2011-07-16 01:23:39 +00001354 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001355 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001356 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001357 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001358 {
cristyed231572011-07-14 02:18:59 +00001359 /*
cristy4e154852011-07-14 13:28:53 +00001360 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001361 */
cristyeb52cde2011-07-17 01:52:52 +00001362 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001363 {
cristyeb52cde2011-07-17 01:52:52 +00001364 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001365 {
cristyeb52cde2011-07-17 01:52:52 +00001366 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001367 k++;
cristya30d9ba2011-07-23 21:00:48 +00001368 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001369 }
cristya30d9ba2011-07-23 21:00:48 +00001370 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001371 }
cristyf7dc44c2011-07-20 14:41:15 +00001372 q[channel]=ClampToQuantum(pixel);
cristy4e154852011-07-14 13:28:53 +00001373 continue;
cristyed231572011-07-14 02:18:59 +00001374 }
cristy4e154852011-07-14 13:28:53 +00001375 /*
1376 Alpha blending.
1377 */
1378 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001379 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001380 {
cristyeb52cde2011-07-17 01:52:52 +00001381 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001382 {
cristyeb52cde2011-07-17 01:52:52 +00001383 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1384 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001385 gamma+=(*k)*alpha;
1386 k++;
cristya30d9ba2011-07-23 21:00:48 +00001387 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001388 }
cristya30d9ba2011-07-23 21:00:48 +00001389 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001390 }
cristy1ce96d02011-07-14 17:57:24 +00001391 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristye7a41c92011-07-20 21:31:01 +00001392 q[channel]=ClampToQuantum(gamma*pixel);
cristyed231572011-07-14 02:18:59 +00001393 }
cristya30d9ba2011-07-23 21:00:48 +00001394 p+=GetPixelChannels(image);
1395 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001396 }
cristyed231572011-07-14 02:18:59 +00001397 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001398 status=MagickFalse;
1399 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1400 {
1401 MagickBooleanType
1402 proceed;
1403
1404#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001405 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001406#endif
1407 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1408 if (proceed == MagickFalse)
1409 status=MagickFalse;
1410 }
1411 }
1412 convolve_image->type=image->type;
1413 convolve_view=DestroyCacheView(convolve_view);
1414 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001415 if (status == MagickFalse)
1416 convolve_image=DestroyImage(convolve_image);
1417 return(convolve_image);
1418}
1419
1420/*
1421%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1422% %
1423% %
1424% %
cristy3ed852e2009-09-05 21:47:34 +00001425% D e s p e c k l e I m a g e %
1426% %
1427% %
1428% %
1429%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1430%
1431% DespeckleImage() reduces the speckle noise in an image while perserving the
1432% edges of the original image.
1433%
1434% The format of the DespeckleImage method is:
1435%
1436% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1437%
1438% A description of each parameter follows:
1439%
1440% o image: the image.
1441%
1442% o exception: return any errors or warnings in this structure.
1443%
1444*/
1445
cristybb503372010-05-27 20:51:26 +00001446static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1447 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
cristy3ed852e2009-09-05 21:47:34 +00001448 const int polarity)
1449{
cristy3ed852e2009-09-05 21:47:34 +00001450 MagickRealType
1451 v;
1452
cristy3ed852e2009-09-05 21:47:34 +00001453 register Quantum
1454 *p,
1455 *q,
1456 *r,
1457 *s;
1458
cristy117ff172010-08-15 21:35:32 +00001459 register ssize_t
1460 x;
1461
1462 ssize_t
1463 y;
1464
cristy3ed852e2009-09-05 21:47:34 +00001465 assert(f != (Quantum *) NULL);
1466 assert(g != (Quantum *) NULL);
1467 p=f+(columns+2);
1468 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001469 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1470 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001471 {
1472 p++;
1473 q++;
1474 r++;
1475 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001476 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001477 {
1478 v=(MagickRealType) (*p);
1479 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1480 v+=ScaleCharToQuantum(1);
1481 *q=(Quantum) v;
1482 p++;
1483 q++;
1484 r++;
1485 }
1486 else
cristybb503372010-05-27 20:51:26 +00001487 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001488 {
1489 v=(MagickRealType) (*p);
1490 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
cristybb503372010-05-27 20:51:26 +00001491 v-=(ssize_t) ScaleCharToQuantum(1);
cristy3ed852e2009-09-05 21:47:34 +00001492 *q=(Quantum) v;
1493 p++;
1494 q++;
1495 r++;
1496 }
1497 p++;
1498 q++;
1499 r++;
1500 }
1501 p=f+(columns+2);
1502 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001503 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1504 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1505 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001506 {
1507 p++;
1508 q++;
1509 r++;
1510 s++;
1511 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001512 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001513 {
1514 v=(MagickRealType) (*q);
1515 if (((MagickRealType) *s >=
1516 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1517 ((MagickRealType) *r > v))
1518 v+=ScaleCharToQuantum(1);
1519 *p=(Quantum) v;
1520 p++;
1521 q++;
1522 r++;
1523 s++;
1524 }
1525 else
cristybb503372010-05-27 20:51:26 +00001526 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001527 {
1528 v=(MagickRealType) (*q);
1529 if (((MagickRealType) *s <=
1530 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1531 ((MagickRealType) *r < v))
1532 v-=(MagickRealType) ScaleCharToQuantum(1);
1533 *p=(Quantum) v;
1534 p++;
1535 q++;
1536 r++;
1537 s++;
1538 }
1539 p++;
1540 q++;
1541 r++;
1542 s++;
1543 }
1544}
1545
1546MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1547{
1548#define DespeckleImageTag "Despeckle/Image"
1549
cristy2407fc22009-09-11 00:55:25 +00001550 CacheView
1551 *despeckle_view,
1552 *image_view;
1553
cristy3ed852e2009-09-05 21:47:34 +00001554 Image
1555 *despeckle_image;
1556
cristy3ed852e2009-09-05 21:47:34 +00001557 MagickBooleanType
1558 status;
1559
cristya58c3172011-02-19 19:23:11 +00001560 register ssize_t
1561 i;
1562
cristy3ed852e2009-09-05 21:47:34 +00001563 Quantum
cristy65b9f392011-02-22 14:22:54 +00001564 *restrict buffers,
1565 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +00001566
1567 size_t
cristya58c3172011-02-19 19:23:11 +00001568 length,
1569 number_channels;
cristy117ff172010-08-15 21:35:32 +00001570
cristybb503372010-05-27 20:51:26 +00001571 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001572 X[4] = {0, 1, 1,-1},
1573 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001574
cristy3ed852e2009-09-05 21:47:34 +00001575 /*
1576 Allocate despeckled image.
1577 */
1578 assert(image != (const Image *) NULL);
1579 assert(image->signature == MagickSignature);
1580 if (image->debug != MagickFalse)
1581 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1582 assert(exception != (ExceptionInfo *) NULL);
1583 assert(exception->signature == MagickSignature);
1584 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1585 exception);
1586 if (despeckle_image == (Image *) NULL)
1587 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001588 if (SetImageStorageClass(despeckle_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001589 {
cristy3ed852e2009-09-05 21:47:34 +00001590 despeckle_image=DestroyImage(despeckle_image);
1591 return((Image *) NULL);
1592 }
1593 /*
1594 Allocate image buffers.
1595 */
1596 length=(size_t) ((image->columns+2)*(image->rows+2));
cristy65b9f392011-02-22 14:22:54 +00001597 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1598 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1599 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001600 {
cristy65b9f392011-02-22 14:22:54 +00001601 if (buffers != (Quantum *) NULL)
1602 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1603 if (pixels != (Quantum *) NULL)
1604 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001605 despeckle_image=DestroyImage(despeckle_image);
1606 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1607 }
1608 /*
1609 Reduce speckle in the image.
1610 */
1611 status=MagickTrue;
cristy109695a2011-02-19 19:38:14 +00001612 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
cristy3ed852e2009-09-05 21:47:34 +00001613 image_view=AcquireCacheView(image);
1614 despeckle_view=AcquireCacheView(despeckle_image);
cristy8df3d002011-02-19 19:40:59 +00001615 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001616 {
cristy3ed852e2009-09-05 21:47:34 +00001617 register Quantum
1618 *buffer,
1619 *pixel;
1620
cristyc1488b52011-02-19 18:54:15 +00001621 register ssize_t
cristya58c3172011-02-19 19:23:11 +00001622 k,
cristyc1488b52011-02-19 18:54:15 +00001623 x;
1624
cristy117ff172010-08-15 21:35:32 +00001625 ssize_t
1626 j,
1627 y;
1628
cristy3ed852e2009-09-05 21:47:34 +00001629 if (status == MagickFalse)
1630 continue;
cristy65b9f392011-02-22 14:22:54 +00001631 pixel=pixels;
cristy3ed852e2009-09-05 21:47:34 +00001632 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
cristy65b9f392011-02-22 14:22:54 +00001633 buffer=buffers;
cristybb503372010-05-27 20:51:26 +00001634 j=(ssize_t) image->columns+2;
1635 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001636 {
cristy4c08aed2011-07-01 19:47:50 +00001637 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001638 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001639
1640 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001641 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001642 break;
1643 j++;
cristybb503372010-05-27 20:51:26 +00001644 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001645 {
cristya58c3172011-02-19 19:23:11 +00001646 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001647 {
cristy4c08aed2011-07-01 19:47:50 +00001648 case 0: pixel[j]=GetPixelRed(image,p); break;
1649 case 1: pixel[j]=GetPixelGreen(image,p); break;
1650 case 2: pixel[j]=GetPixelBlue(image,p); break;
1651 case 3: pixel[j]=GetPixelAlpha(image,p); break;
1652 case 4: pixel[j]=GetPixelBlack(image,p); break;
cristy3ed852e2009-09-05 21:47:34 +00001653 default: break;
1654 }
cristyed231572011-07-14 02:18:59 +00001655 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001656 j++;
1657 }
1658 j++;
1659 }
cristy3ed852e2009-09-05 21:47:34 +00001660 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
cristya58c3172011-02-19 19:23:11 +00001661 for (k=0; k < 4; k++)
cristy3ed852e2009-09-05 21:47:34 +00001662 {
cristya58c3172011-02-19 19:23:11 +00001663 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1664 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);
cristy3ed852e2009-09-05 21:47:34 +00001667 }
cristybb503372010-05-27 20:51:26 +00001668 j=(ssize_t) image->columns+2;
1669 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001670 {
1671 MagickBooleanType
1672 sync;
1673
cristy4c08aed2011-07-01 19:47:50 +00001674 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001675 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001676
1677 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1678 1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001679 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001680 break;
1681 j++;
cristybb503372010-05-27 20:51:26 +00001682 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001683 {
cristya58c3172011-02-19 19:23:11 +00001684 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001685 {
cristy4c08aed2011-07-01 19:47:50 +00001686 case 0: SetPixelRed(despeckle_image,pixel[j],q); break;
1687 case 1: SetPixelGreen(despeckle_image,pixel[j],q); break;
1688 case 2: SetPixelBlue(despeckle_image,pixel[j],q); break;
1689 case 3: SetPixelAlpha(despeckle_image,pixel[j],q); break;
1690 case 4: SetPixelBlack(despeckle_image,pixel[j],q); break;
cristy3ed852e2009-09-05 21:47:34 +00001691 default: break;
1692 }
cristyed231572011-07-14 02:18:59 +00001693 q+=GetPixelChannels(despeckle_image);
cristy3ed852e2009-09-05 21:47:34 +00001694 j++;
1695 }
1696 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1697 if (sync == MagickFalse)
1698 {
1699 status=MagickFalse;
1700 break;
1701 }
1702 j++;
1703 }
1704 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1705 {
1706 MagickBooleanType
1707 proceed;
1708
cristya58c3172011-02-19 19:23:11 +00001709 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1710 number_channels);
cristy3ed852e2009-09-05 21:47:34 +00001711 if (proceed == MagickFalse)
1712 status=MagickFalse;
1713 }
1714 }
1715 despeckle_view=DestroyCacheView(despeckle_view);
1716 image_view=DestroyCacheView(image_view);
cristy65b9f392011-02-22 14:22:54 +00001717 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1718 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001719 despeckle_image->type=image->type;
1720 if (status == MagickFalse)
1721 despeckle_image=DestroyImage(despeckle_image);
1722 return(despeckle_image);
1723}
1724
1725/*
1726%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727% %
1728% %
1729% %
1730% E d g e I m a g e %
1731% %
1732% %
1733% %
1734%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1735%
1736% EdgeImage() finds edges in an image. Radius defines the radius of the
1737% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1738% radius for you.
1739%
1740% The format of the EdgeImage method is:
1741%
1742% Image *EdgeImage(const Image *image,const double radius,
1743% ExceptionInfo *exception)
1744%
1745% A description of each parameter follows:
1746%
1747% o image: the image.
1748%
1749% o radius: the radius of the pixel neighborhood.
1750%
1751% o exception: return any errors or warnings in this structure.
1752%
1753*/
1754MagickExport Image *EdgeImage(const Image *image,const double radius,
1755 ExceptionInfo *exception)
1756{
1757 Image
1758 *edge_image;
1759
cristy41cbe682011-07-15 19:12:37 +00001760 KernelInfo
1761 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001762
cristybb503372010-05-27 20:51:26 +00001763 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001764 i;
1765
cristybb503372010-05-27 20:51:26 +00001766 size_t
cristy3ed852e2009-09-05 21:47:34 +00001767 width;
1768
cristy41cbe682011-07-15 19:12:37 +00001769 ssize_t
1770 j,
1771 u,
1772 v;
1773
cristy3ed852e2009-09-05 21:47:34 +00001774 assert(image != (const Image *) NULL);
1775 assert(image->signature == MagickSignature);
1776 if (image->debug != MagickFalse)
1777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1778 assert(exception != (ExceptionInfo *) NULL);
1779 assert(exception->signature == MagickSignature);
1780 width=GetOptimalKernelWidth1D(radius,0.5);
cristy5e6be1e2011-07-16 01:23:39 +00001781 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001782 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001783 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001784 kernel_info->width=width;
1785 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001786 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1787 kernel_info->width*sizeof(*kernel_info->values));
1788 if (kernel_info->values == (double *) NULL)
1789 {
1790 kernel_info=DestroyKernelInfo(kernel_info);
1791 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1792 }
1793 j=(ssize_t) kernel_info->width/2;
1794 i=0;
1795 for (v=(-j); v <= j; v++)
1796 {
1797 for (u=(-j); u <= j; u++)
1798 {
1799 kernel_info->values[i]=(-1.0);
1800 i++;
1801 }
1802 }
1803 kernel_info->values[i/2]=(double) (width*width-1.0);
cristy0a922382011-07-16 15:30:34 +00001804 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001805 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001806 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001807 return(edge_image);
1808}
1809
1810/*
1811%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1812% %
1813% %
1814% %
1815% E m b o s s I m a g e %
1816% %
1817% %
1818% %
1819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1820%
1821% EmbossImage() returns a grayscale image with a three-dimensional effect.
1822% We convolve the image with a Gaussian operator of the given radius and
1823% standard deviation (sigma). For reasonable results, radius should be
1824% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1825% radius for you.
1826%
1827% The format of the EmbossImage method is:
1828%
1829% Image *EmbossImage(const Image *image,const double radius,
1830% const double sigma,ExceptionInfo *exception)
1831%
1832% A description of each parameter follows:
1833%
1834% o image: the image.
1835%
1836% o radius: the radius of the pixel neighborhood.
1837%
1838% o sigma: the standard deviation of the Gaussian, in pixels.
1839%
1840% o exception: return any errors or warnings in this structure.
1841%
1842*/
1843MagickExport Image *EmbossImage(const Image *image,const double radius,
1844 const double sigma,ExceptionInfo *exception)
1845{
cristy3ed852e2009-09-05 21:47:34 +00001846 Image
1847 *emboss_image;
1848
cristy41cbe682011-07-15 19:12:37 +00001849 KernelInfo
1850 *kernel_info;
1851
cristybb503372010-05-27 20:51:26 +00001852 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001853 i;
1854
cristybb503372010-05-27 20:51:26 +00001855 size_t
cristy3ed852e2009-09-05 21:47:34 +00001856 width;
1857
cristy117ff172010-08-15 21:35:32 +00001858 ssize_t
1859 j,
1860 k,
1861 u,
1862 v;
1863
cristy41cbe682011-07-15 19:12:37 +00001864 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001865 assert(image->signature == MagickSignature);
1866 if (image->debug != MagickFalse)
1867 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1868 assert(exception != (ExceptionInfo *) NULL);
1869 assert(exception->signature == MagickSignature);
1870 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001871 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001872 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001873 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001874 kernel_info->width=width;
1875 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001876 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1877 kernel_info->width*sizeof(*kernel_info->values));
1878 if (kernel_info->values == (double *) NULL)
1879 {
1880 kernel_info=DestroyKernelInfo(kernel_info);
1881 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1882 }
1883 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001884 k=j;
1885 i=0;
1886 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001887 {
cristy47e00502009-12-17 19:19:57 +00001888 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001889 {
cristy41cbe682011-07-15 19:12:37 +00001890 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001891 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001892 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001893 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001894 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001895 i++;
1896 }
cristy47e00502009-12-17 19:19:57 +00001897 k--;
cristy3ed852e2009-09-05 21:47:34 +00001898 }
cristy0a922382011-07-16 15:30:34 +00001899 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001900 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001901 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001902 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001903 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001904 return(emboss_image);
1905}
1906
1907/*
1908%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1909% %
1910% %
1911% %
1912% G a u s s i a n B l u r I m a g e %
1913% %
1914% %
1915% %
1916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1917%
1918% GaussianBlurImage() blurs an image. We convolve the image with a
1919% Gaussian operator of the given radius and standard deviation (sigma).
1920% For reasonable results, the radius should be larger than sigma. Use a
1921% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1922%
1923% The format of the GaussianBlurImage method is:
1924%
1925% Image *GaussianBlurImage(const Image *image,onst double radius,
1926% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001927%
1928% A description of each parameter follows:
1929%
1930% o image: the image.
1931%
cristy3ed852e2009-09-05 21:47:34 +00001932% o radius: the radius of the Gaussian, in pixels, not counting the center
1933% pixel.
1934%
1935% o sigma: the standard deviation of the Gaussian, in pixels.
1936%
1937% o exception: return any errors or warnings in this structure.
1938%
1939*/
cristy41cbe682011-07-15 19:12:37 +00001940MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1941 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001942{
cristy3ed852e2009-09-05 21:47:34 +00001943 Image
1944 *blur_image;
1945
cristy41cbe682011-07-15 19:12:37 +00001946 KernelInfo
1947 *kernel_info;
1948
cristybb503372010-05-27 20:51:26 +00001949 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001950 i;
1951
cristybb503372010-05-27 20:51:26 +00001952 size_t
cristy3ed852e2009-09-05 21:47:34 +00001953 width;
1954
cristy117ff172010-08-15 21:35:32 +00001955 ssize_t
1956 j,
1957 u,
1958 v;
1959
cristy3ed852e2009-09-05 21:47:34 +00001960 assert(image != (const Image *) NULL);
1961 assert(image->signature == MagickSignature);
1962 if (image->debug != MagickFalse)
1963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1964 assert(exception != (ExceptionInfo *) NULL);
1965 assert(exception->signature == MagickSignature);
1966 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001967 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001968 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001969 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001970 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1971 kernel_info->width=width;
1972 kernel_info->height=width;
1973 kernel_info->signature=MagickSignature;
1974 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1975 kernel_info->width*sizeof(*kernel_info->values));
1976 if (kernel_info->values == (double *) NULL)
1977 {
1978 kernel_info=DestroyKernelInfo(kernel_info);
1979 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1980 }
1981 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001982 i=0;
cristy47e00502009-12-17 19:19:57 +00001983 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001984 {
cristy47e00502009-12-17 19:19:57 +00001985 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001986 {
1987 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1988 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1989 i++;
1990 }
cristy3ed852e2009-09-05 21:47:34 +00001991 }
cristy0a922382011-07-16 15:30:34 +00001992 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001993 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001994 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001995 return(blur_image);
1996}
1997
1998/*
1999%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2000% %
2001% %
2002% %
cristy3ed852e2009-09-05 21:47:34 +00002003% M o t i o n B l u r I m a g e %
2004% %
2005% %
2006% %
2007%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2008%
2009% MotionBlurImage() simulates motion blur. We convolve the image with a
2010% Gaussian operator of the given radius and standard deviation (sigma).
2011% For reasonable results, radius should be larger than sigma. Use a
2012% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2013% Angle gives the angle of the blurring motion.
2014%
2015% Andrew Protano contributed this effect.
2016%
2017% The format of the MotionBlurImage method is:
2018%
2019% Image *MotionBlurImage(const Image *image,const double radius,
2020% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002021%
2022% A description of each parameter follows:
2023%
2024% o image: the image.
2025%
cristy3ed852e2009-09-05 21:47:34 +00002026% o radius: the radius of the Gaussian, in pixels, not counting
2027% the center pixel.
2028%
2029% o sigma: the standard deviation of the Gaussian, in pixels.
2030%
cristycee97112010-05-28 00:44:52 +00002031% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002032%
2033% o exception: return any errors or warnings in this structure.
2034%
2035*/
2036
cristybb503372010-05-27 20:51:26 +00002037static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002038{
cristy3ed852e2009-09-05 21:47:34 +00002039 double
cristy47e00502009-12-17 19:19:57 +00002040 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002041 normalize;
2042
cristybb503372010-05-27 20:51:26 +00002043 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002044 i;
2045
2046 /*
cristy47e00502009-12-17 19:19:57 +00002047 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002048 */
2049 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2050 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2051 if (kernel == (double *) NULL)
2052 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002053 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002054 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002055 {
cristy4205a3c2010-09-12 20:19:59 +00002056 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2057 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002058 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002059 }
cristybb503372010-05-27 20:51:26 +00002060 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002061 kernel[i]/=normalize;
2062 return(kernel);
2063}
2064
cristyf4ad9df2011-07-08 16:49:03 +00002065MagickExport Image *MotionBlurImage(const Image *image,
2066 const double radius,const double sigma,const double angle,
2067 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002068{
cristyc4c8d132010-01-07 01:58:38 +00002069 CacheView
2070 *blur_view,
2071 *image_view;
2072
cristy3ed852e2009-09-05 21:47:34 +00002073 double
2074 *kernel;
2075
2076 Image
2077 *blur_image;
2078
cristy3ed852e2009-09-05 21:47:34 +00002079 MagickBooleanType
2080 status;
2081
cristybb503372010-05-27 20:51:26 +00002082 MagickOffsetType
2083 progress;
2084
cristy4c08aed2011-07-01 19:47:50 +00002085 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002086 bias;
cristy3ed852e2009-09-05 21:47:34 +00002087
2088 OffsetInfo
2089 *offset;
2090
2091 PointInfo
2092 point;
2093
cristybb503372010-05-27 20:51:26 +00002094 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002095 i;
2096
cristybb503372010-05-27 20:51:26 +00002097 size_t
cristy3ed852e2009-09-05 21:47:34 +00002098 width;
2099
cristybb503372010-05-27 20:51:26 +00002100 ssize_t
2101 y;
2102
cristy3ed852e2009-09-05 21:47:34 +00002103 assert(image != (Image *) NULL);
2104 assert(image->signature == MagickSignature);
2105 if (image->debug != MagickFalse)
2106 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2107 assert(exception != (ExceptionInfo *) NULL);
2108 width=GetOptimalKernelWidth1D(radius,sigma);
2109 kernel=GetMotionBlurKernel(width,sigma);
2110 if (kernel == (double *) NULL)
2111 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2112 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2113 if (offset == (OffsetInfo *) NULL)
2114 {
2115 kernel=(double *) RelinquishMagickMemory(kernel);
2116 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2117 }
2118 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2119 if (blur_image == (Image *) NULL)
2120 {
2121 kernel=(double *) RelinquishMagickMemory(kernel);
2122 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2123 return((Image *) NULL);
2124 }
cristy574cc262011-08-05 01:23:58 +00002125 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002126 {
2127 kernel=(double *) RelinquishMagickMemory(kernel);
2128 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002129 blur_image=DestroyImage(blur_image);
2130 return((Image *) NULL);
2131 }
2132 point.x=(double) width*sin(DegreesToRadians(angle));
2133 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002134 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002135 {
cristybb503372010-05-27 20:51:26 +00002136 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2137 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002138 }
2139 /*
2140 Motion blur image.
2141 */
2142 status=MagickTrue;
2143 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002144 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002145 image_view=AcquireCacheView(image);
2146 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002147#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00002148 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00002149#endif
cristybb503372010-05-27 20:51:26 +00002150 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002151 {
cristy4c08aed2011-07-01 19:47:50 +00002152 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002153 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002154
cristy117ff172010-08-15 21:35:32 +00002155 register ssize_t
2156 x;
2157
cristy3ed852e2009-09-05 21:47:34 +00002158 if (status == MagickFalse)
2159 continue;
2160 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2161 exception);
cristyacd2ed22011-08-30 01:44:23 +00002162 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002163 {
2164 status=MagickFalse;
2165 continue;
2166 }
cristybb503372010-05-27 20:51:26 +00002167 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002168 {
cristy4c08aed2011-07-01 19:47:50 +00002169 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002170 qixel;
2171
2172 PixelPacket
2173 pixel;
2174
2175 register double
cristyc47d1f82009-11-26 01:44:43 +00002176 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00002177
cristybb503372010-05-27 20:51:26 +00002178 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002179 i;
2180
cristy3ed852e2009-09-05 21:47:34 +00002181 k=kernel;
cristyddd82202009-11-03 20:14:50 +00002182 qixel=bias;
cristyed231572011-07-14 02:18:59 +00002183 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002184 {
cristybb503372010-05-27 20:51:26 +00002185 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002186 {
2187 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2188 offset[i].y,&pixel,exception);
2189 qixel.red+=(*k)*pixel.red;
2190 qixel.green+=(*k)*pixel.green;
2191 qixel.blue+=(*k)*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002192 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002193 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002194 qixel.black+=(*k)*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002195 k++;
2196 }
cristyed231572011-07-14 02:18:59 +00002197 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002198 SetPixelRed(blur_image,
2199 ClampToQuantum(qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002200 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002201 SetPixelGreen(blur_image,
2202 ClampToQuantum(qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002203 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002204 SetPixelBlue(blur_image,
2205 ClampToQuantum(qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002206 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002207 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002208 SetPixelBlack(blur_image,
2209 ClampToQuantum(qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002210 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002211 SetPixelAlpha(blur_image,
2212 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002213 }
2214 else
2215 {
2216 MagickRealType
2217 alpha,
2218 gamma;
2219
2220 alpha=0.0;
2221 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00002222 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002223 {
2224 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2225 offset[i].y,&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00002226 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00002227 qixel.red+=(*k)*alpha*pixel.red;
2228 qixel.green+=(*k)*alpha*pixel.green;
2229 qixel.blue+=(*k)*alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002230 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002231 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002232 qixel.black+=(*k)*alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002233 gamma+=(*k)*alpha;
2234 k++;
2235 }
2236 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00002237 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002238 SetPixelRed(blur_image,
2239 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002240 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002241 SetPixelGreen(blur_image,
2242 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002243 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002244 SetPixelBlue(blur_image,
2245 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002246 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002247 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002248 SetPixelBlack(blur_image,
2249 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002250 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002251 SetPixelAlpha(blur_image,
2252 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002253 }
cristyed231572011-07-14 02:18:59 +00002254 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002255 }
2256 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2257 status=MagickFalse;
2258 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2259 {
2260 MagickBooleanType
2261 proceed;
2262
cristyb557a152011-02-22 12:14:30 +00002263#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002264 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002265#endif
2266 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2267 if (proceed == MagickFalse)
2268 status=MagickFalse;
2269 }
2270 }
2271 blur_view=DestroyCacheView(blur_view);
2272 image_view=DestroyCacheView(image_view);
2273 kernel=(double *) RelinquishMagickMemory(kernel);
2274 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2275 if (status == MagickFalse)
2276 blur_image=DestroyImage(blur_image);
2277 return(blur_image);
2278}
2279
2280/*
2281%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2282% %
2283% %
2284% %
2285% P r e v i e w I m a g e %
2286% %
2287% %
2288% %
2289%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2290%
2291% PreviewImage() tiles 9 thumbnails of the specified image with an image
2292% processing operation applied with varying parameters. This may be helpful
2293% pin-pointing an appropriate parameter for a particular image processing
2294% operation.
2295%
2296% The format of the PreviewImages method is:
2297%
2298% Image *PreviewImages(const Image *image,const PreviewType preview,
2299% ExceptionInfo *exception)
2300%
2301% A description of each parameter follows:
2302%
2303% o image: the image.
2304%
2305% o preview: the image processing operation.
2306%
2307% o exception: return any errors or warnings in this structure.
2308%
2309*/
2310MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2311 ExceptionInfo *exception)
2312{
2313#define NumberTiles 9
2314#define PreviewImageTag "Preview/Image"
2315#define DefaultPreviewGeometry "204x204+10+10"
2316
2317 char
2318 factor[MaxTextExtent],
2319 label[MaxTextExtent];
2320
2321 double
2322 degrees,
2323 gamma,
2324 percentage,
2325 radius,
2326 sigma,
2327 threshold;
2328
2329 Image
2330 *images,
2331 *montage_image,
2332 *preview_image,
2333 *thumbnail;
2334
2335 ImageInfo
2336 *preview_info;
2337
cristy3ed852e2009-09-05 21:47:34 +00002338 MagickBooleanType
2339 proceed;
2340
2341 MontageInfo
2342 *montage_info;
2343
2344 QuantizeInfo
2345 quantize_info;
2346
2347 RectangleInfo
2348 geometry;
2349
cristybb503372010-05-27 20:51:26 +00002350 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002351 i,
2352 x;
2353
cristybb503372010-05-27 20:51:26 +00002354 size_t
cristy3ed852e2009-09-05 21:47:34 +00002355 colors;
2356
cristy117ff172010-08-15 21:35:32 +00002357 ssize_t
2358 y;
2359
cristy3ed852e2009-09-05 21:47:34 +00002360 /*
2361 Open output image file.
2362 */
2363 assert(image != (Image *) NULL);
2364 assert(image->signature == MagickSignature);
2365 if (image->debug != MagickFalse)
2366 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2367 colors=2;
2368 degrees=0.0;
2369 gamma=(-0.2f);
2370 preview_info=AcquireImageInfo();
2371 SetGeometry(image,&geometry);
2372 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2373 &geometry.width,&geometry.height);
2374 images=NewImageList();
2375 percentage=12.5;
2376 GetQuantizeInfo(&quantize_info);
2377 radius=0.0;
2378 sigma=1.0;
2379 threshold=0.0;
2380 x=0;
2381 y=0;
2382 for (i=0; i < NumberTiles; i++)
2383 {
2384 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2385 if (thumbnail == (Image *) NULL)
2386 break;
2387 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2388 (void *) NULL);
2389 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
2390 if (i == (NumberTiles/2))
2391 {
2392 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
2393 AppendImageToList(&images,thumbnail);
2394 continue;
2395 }
2396 switch (preview)
2397 {
2398 case RotatePreview:
2399 {
2400 degrees+=45.0;
2401 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002402 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002403 break;
2404 }
2405 case ShearPreview:
2406 {
2407 degrees+=5.0;
2408 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002409 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002410 degrees,2.0*degrees);
2411 break;
2412 }
2413 case RollPreview:
2414 {
cristybb503372010-05-27 20:51:26 +00002415 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2416 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002417 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002418 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002419 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002420 break;
2421 }
2422 case HuePreview:
2423 {
2424 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2425 if (preview_image == (Image *) NULL)
2426 break;
cristyb51dff52011-05-19 16:55:47 +00002427 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002428 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002429 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002430 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002431 break;
2432 }
2433 case SaturationPreview:
2434 {
2435 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2436 if (preview_image == (Image *) NULL)
2437 break;
cristyb51dff52011-05-19 16:55:47 +00002438 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002439 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002440 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002441 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002442 break;
2443 }
2444 case BrightnessPreview:
2445 {
2446 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2447 if (preview_image == (Image *) NULL)
2448 break;
cristyb51dff52011-05-19 16:55:47 +00002449 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002450 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002451 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002452 break;
2453 }
2454 case GammaPreview:
2455 default:
2456 {
2457 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2458 if (preview_image == (Image *) NULL)
2459 break;
2460 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002461 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002462 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002463 break;
2464 }
2465 case SpiffPreview:
2466 {
2467 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2468 if (preview_image != (Image *) NULL)
2469 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002470 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002471 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002472 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002473 break;
2474 }
2475 case DullPreview:
2476 {
2477 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2478 if (preview_image == (Image *) NULL)
2479 break;
2480 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002481 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002482 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002483 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002484 break;
2485 }
2486 case GrayscalePreview:
2487 {
2488 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2489 if (preview_image == (Image *) NULL)
2490 break;
2491 colors<<=1;
2492 quantize_info.number_colors=colors;
2493 quantize_info.colorspace=GRAYColorspace;
2494 (void) QuantizeImage(&quantize_info,preview_image);
cristyb51dff52011-05-19 16:55:47 +00002495 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002496 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002497 break;
2498 }
2499 case QuantizePreview:
2500 {
2501 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2502 if (preview_image == (Image *) NULL)
2503 break;
2504 colors<<=1;
2505 quantize_info.number_colors=colors;
2506 (void) QuantizeImage(&quantize_info,preview_image);
cristyb51dff52011-05-19 16:55:47 +00002507 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002508 colors);
cristy3ed852e2009-09-05 21:47:34 +00002509 break;
2510 }
2511 case DespecklePreview:
2512 {
2513 for (x=0; x < (i-1); x++)
2514 {
2515 preview_image=DespeckleImage(thumbnail,exception);
2516 if (preview_image == (Image *) NULL)
2517 break;
2518 thumbnail=DestroyImage(thumbnail);
2519 thumbnail=preview_image;
2520 }
2521 preview_image=DespeckleImage(thumbnail,exception);
2522 if (preview_image == (Image *) NULL)
2523 break;
cristyb51dff52011-05-19 16:55:47 +00002524 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002525 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002526 break;
2527 }
2528 case ReduceNoisePreview:
2529 {
cristy95c38342011-03-18 22:39:51 +00002530 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2531 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002532 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002533 break;
2534 }
2535 case AddNoisePreview:
2536 {
2537 switch ((int) i)
2538 {
2539 case 0:
2540 {
2541 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2542 break;
2543 }
2544 case 1:
2545 {
2546 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2547 break;
2548 }
2549 case 2:
2550 {
2551 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2552 break;
2553 }
2554 case 3:
2555 {
2556 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2557 break;
2558 }
2559 case 4:
2560 {
2561 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2562 break;
2563 }
2564 case 5:
2565 {
2566 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2567 break;
2568 }
2569 default:
2570 {
2571 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2572 break;
2573 }
2574 }
cristyd76c51e2011-03-26 00:21:26 +00002575 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2576 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002577 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002578 break;
2579 }
2580 case SharpenPreview:
2581 {
2582 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002583 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002584 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002585 break;
2586 }
2587 case BlurPreview:
2588 {
2589 preview_image=BlurImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002590 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002591 sigma);
2592 break;
2593 }
2594 case ThresholdPreview:
2595 {
2596 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2597 if (preview_image == (Image *) NULL)
2598 break;
2599 (void) BilevelImage(thumbnail,
2600 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
cristyb51dff52011-05-19 16:55:47 +00002601 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002602 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2603 break;
2604 }
2605 case EdgeDetectPreview:
2606 {
2607 preview_image=EdgeImage(thumbnail,radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002608 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002609 break;
2610 }
2611 case SpreadPreview:
2612 {
2613 preview_image=SpreadImage(thumbnail,radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002614 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002615 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002616 break;
2617 }
2618 case SolarizePreview:
2619 {
2620 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2621 if (preview_image == (Image *) NULL)
2622 break;
2623 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002624 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002625 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002626 (QuantumRange*percentage)/100.0);
2627 break;
2628 }
2629 case ShadePreview:
2630 {
2631 degrees+=10.0;
2632 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2633 exception);
cristyb51dff52011-05-19 16:55:47 +00002634 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002635 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002636 break;
2637 }
2638 case RaisePreview:
2639 {
2640 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2641 if (preview_image == (Image *) NULL)
2642 break;
cristybb503372010-05-27 20:51:26 +00002643 geometry.width=(size_t) (2*i+2);
2644 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002645 geometry.x=i/2;
2646 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002647 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002648 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002649 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002650 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002651 break;
2652 }
2653 case SegmentPreview:
2654 {
2655 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2656 if (preview_image == (Image *) NULL)
2657 break;
2658 threshold+=0.4f;
2659 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
2660 threshold);
cristyb51dff52011-05-19 16:55:47 +00002661 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002662 threshold,threshold);
2663 break;
2664 }
2665 case SwirlPreview:
2666 {
2667 preview_image=SwirlImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002668 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002669 degrees+=45.0;
2670 break;
2671 }
2672 case ImplodePreview:
2673 {
2674 degrees+=0.1f;
2675 preview_image=ImplodeImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002676 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002677 break;
2678 }
2679 case WavePreview:
2680 {
2681 degrees+=5.0f;
2682 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002683 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002684 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002685 break;
2686 }
2687 case OilPaintPreview:
2688 {
cristy14973ba2011-08-27 23:48:07 +00002689 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2690 exception);
2691 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2692 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002693 break;
2694 }
2695 case CharcoalDrawingPreview:
2696 {
2697 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2698 exception);
cristyb51dff52011-05-19 16:55:47 +00002699 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002700 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002701 break;
2702 }
2703 case JPEGPreview:
2704 {
2705 char
2706 filename[MaxTextExtent];
2707
2708 int
2709 file;
2710
2711 MagickBooleanType
2712 status;
2713
2714 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2715 if (preview_image == (Image *) NULL)
2716 break;
cristybb503372010-05-27 20:51:26 +00002717 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002718 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002719 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002720 file=AcquireUniqueFileResource(filename);
2721 if (file != -1)
2722 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002723 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002724 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002725 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002726 if (status != MagickFalse)
2727 {
2728 Image
2729 *quality_image;
2730
2731 (void) CopyMagickString(preview_info->filename,
2732 preview_image->filename,MaxTextExtent);
2733 quality_image=ReadImage(preview_info,exception);
2734 if (quality_image != (Image *) NULL)
2735 {
2736 preview_image=DestroyImage(preview_image);
2737 preview_image=quality_image;
2738 }
2739 }
2740 (void) RelinquishUniqueFileResource(preview_image->filename);
2741 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002742 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002743 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2744 1024.0/1024.0);
2745 else
2746 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002747 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002748 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002749 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002750 else
cristyb51dff52011-05-19 16:55:47 +00002751 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002752 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002753 break;
2754 }
2755 }
2756 thumbnail=DestroyImage(thumbnail);
2757 percentage+=12.5;
2758 radius+=0.5;
2759 sigma+=0.25;
2760 if (preview_image == (Image *) NULL)
2761 break;
2762 (void) DeleteImageProperty(preview_image,"label");
2763 (void) SetImageProperty(preview_image,"label",label);
2764 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002765 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2766 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002767 if (proceed == MagickFalse)
2768 break;
2769 }
2770 if (images == (Image *) NULL)
2771 {
2772 preview_info=DestroyImageInfo(preview_info);
2773 return((Image *) NULL);
2774 }
2775 /*
2776 Create the montage.
2777 */
2778 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2779 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2780 montage_info->shadow=MagickTrue;
2781 (void) CloneString(&montage_info->tile,"3x3");
2782 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2783 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2784 montage_image=MontageImages(images,montage_info,exception);
2785 montage_info=DestroyMontageInfo(montage_info);
2786 images=DestroyImageList(images);
2787 if (montage_image == (Image *) NULL)
2788 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2789 if (montage_image->montage != (char *) NULL)
2790 {
2791 /*
2792 Free image directory.
2793 */
2794 montage_image->montage=(char *) RelinquishMagickMemory(
2795 montage_image->montage);
2796 if (image->directory != (char *) NULL)
2797 montage_image->directory=(char *) RelinquishMagickMemory(
2798 montage_image->directory);
2799 }
2800 preview_info=DestroyImageInfo(preview_info);
2801 return(montage_image);
2802}
2803
2804/*
2805%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2806% %
2807% %
2808% %
2809% R a d i a l B l u r I m a g e %
2810% %
2811% %
2812% %
2813%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2814%
2815% RadialBlurImage() applies a radial blur to the image.
2816%
2817% Andrew Protano contributed this effect.
2818%
2819% The format of the RadialBlurImage method is:
2820%
2821% Image *RadialBlurImage(const Image *image,const double angle,
2822% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002823%
2824% A description of each parameter follows:
2825%
2826% o image: the image.
2827%
cristy3ed852e2009-09-05 21:47:34 +00002828% o angle: the angle of the radial blur.
2829%
2830% o exception: return any errors or warnings in this structure.
2831%
2832*/
cristyf4ad9df2011-07-08 16:49:03 +00002833MagickExport Image *RadialBlurImage(const Image *image,
2834 const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002835{
cristyc4c8d132010-01-07 01:58:38 +00002836 CacheView
2837 *blur_view,
2838 *image_view;
2839
cristy3ed852e2009-09-05 21:47:34 +00002840 Image
2841 *blur_image;
2842
cristy3ed852e2009-09-05 21:47:34 +00002843 MagickBooleanType
2844 status;
2845
cristybb503372010-05-27 20:51:26 +00002846 MagickOffsetType
2847 progress;
2848
cristy4c08aed2011-07-01 19:47:50 +00002849 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002850 bias;
cristy3ed852e2009-09-05 21:47:34 +00002851
2852 MagickRealType
2853 blur_radius,
2854 *cos_theta,
2855 offset,
2856 *sin_theta,
2857 theta;
2858
2859 PointInfo
2860 blur_center;
2861
cristybb503372010-05-27 20:51:26 +00002862 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002863 i;
2864
cristybb503372010-05-27 20:51:26 +00002865 size_t
cristy3ed852e2009-09-05 21:47:34 +00002866 n;
2867
cristybb503372010-05-27 20:51:26 +00002868 ssize_t
2869 y;
2870
cristy3ed852e2009-09-05 21:47:34 +00002871 /*
2872 Allocate blur image.
2873 */
2874 assert(image != (Image *) NULL);
2875 assert(image->signature == MagickSignature);
2876 if (image->debug != MagickFalse)
2877 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2878 assert(exception != (ExceptionInfo *) NULL);
2879 assert(exception->signature == MagickSignature);
2880 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2881 if (blur_image == (Image *) NULL)
2882 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002883 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002884 {
cristy3ed852e2009-09-05 21:47:34 +00002885 blur_image=DestroyImage(blur_image);
2886 return((Image *) NULL);
2887 }
2888 blur_center.x=(double) image->columns/2.0;
2889 blur_center.y=(double) image->rows/2.0;
2890 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002891 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002892 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2893 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2894 sizeof(*cos_theta));
2895 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2896 sizeof(*sin_theta));
2897 if ((cos_theta == (MagickRealType *) NULL) ||
2898 (sin_theta == (MagickRealType *) NULL))
2899 {
2900 blur_image=DestroyImage(blur_image);
2901 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2902 }
2903 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002904 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002905 {
2906 cos_theta[i]=cos((double) (theta*i-offset));
2907 sin_theta[i]=sin((double) (theta*i-offset));
2908 }
2909 /*
2910 Radial blur image.
2911 */
2912 status=MagickTrue;
2913 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002914 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002915 image_view=AcquireCacheView(image);
2916 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002917#if defined(MAGICKCORE_OPENMP_SUPPORT)
2918 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002919#endif
cristybb503372010-05-27 20:51:26 +00002920 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002921 {
cristy4c08aed2011-07-01 19:47:50 +00002922 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002923 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002924
cristy117ff172010-08-15 21:35:32 +00002925 register ssize_t
2926 x;
2927
cristy3ed852e2009-09-05 21:47:34 +00002928 if (status == MagickFalse)
2929 continue;
2930 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2931 exception);
cristyacd2ed22011-08-30 01:44:23 +00002932 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002933 {
2934 status=MagickFalse;
2935 continue;
2936 }
cristybb503372010-05-27 20:51:26 +00002937 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002938 {
cristy4c08aed2011-07-01 19:47:50 +00002939 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002940 qixel;
2941
2942 MagickRealType
2943 normalize,
2944 radius;
2945
2946 PixelPacket
2947 pixel;
2948
2949 PointInfo
2950 center;
2951
cristybb503372010-05-27 20:51:26 +00002952 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002953 i;
2954
cristybb503372010-05-27 20:51:26 +00002955 size_t
cristy3ed852e2009-09-05 21:47:34 +00002956 step;
2957
2958 center.x=(double) x-blur_center.x;
2959 center.y=(double) y-blur_center.y;
2960 radius=hypot((double) center.x,center.y);
2961 if (radius == 0)
2962 step=1;
2963 else
2964 {
cristybb503372010-05-27 20:51:26 +00002965 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002966 if (step == 0)
2967 step=1;
2968 else
2969 if (step >= n)
2970 step=n-1;
2971 }
2972 normalize=0.0;
cristyddd82202009-11-03 20:14:50 +00002973 qixel=bias;
cristyed231572011-07-14 02:18:59 +00002974 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002975 {
cristyeaedf062010-05-29 22:36:02 +00002976 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00002977 {
cristyeaedf062010-05-29 22:36:02 +00002978 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
2979 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
2980 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
2981 cos_theta[i]+0.5),&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002982 qixel.red+=pixel.red;
2983 qixel.green+=pixel.green;
2984 qixel.blue+=pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00002985 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002986 qixel.black+=pixel.black;
2987 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002988 normalize+=1.0;
2989 }
2990 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
2991 normalize);
cristyed231572011-07-14 02:18:59 +00002992 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002993 SetPixelRed(blur_image,
2994 ClampToQuantum(normalize*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002995 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002996 SetPixelGreen(blur_image,
2997 ClampToQuantum(normalize*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002998 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002999 SetPixelBlue(blur_image,
3000 ClampToQuantum(normalize*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003001 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003002 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003003 SetPixelBlack(blur_image,
3004 ClampToQuantum(normalize*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003005 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003006 SetPixelAlpha(blur_image,
3007 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003008 }
3009 else
3010 {
3011 MagickRealType
3012 alpha,
3013 gamma;
3014
3015 alpha=1.0;
3016 gamma=0.0;
cristyeaedf062010-05-29 22:36:02 +00003017 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00003018 {
cristyeaedf062010-05-29 22:36:02 +00003019 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3020 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3021 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3022 cos_theta[i]+0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003023 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00003024 qixel.red+=alpha*pixel.red;
3025 qixel.green+=alpha*pixel.green;
3026 qixel.blue+=alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00003027 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00003028 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00003029 qixel.black+=alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00003030 gamma+=alpha;
3031 normalize+=1.0;
3032 }
3033 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3034 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3035 normalize);
cristyed231572011-07-14 02:18:59 +00003036 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003037 SetPixelRed(blur_image,
3038 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00003039 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003040 SetPixelGreen(blur_image,
3041 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00003042 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003043 SetPixelBlue(blur_image,
3044 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003045 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003046 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003047 SetPixelBlack(blur_image,
3048 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003049 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003050 SetPixelAlpha(blur_image,
3051 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003052 }
cristyed231572011-07-14 02:18:59 +00003053 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003054 }
3055 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3056 status=MagickFalse;
3057 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3058 {
3059 MagickBooleanType
3060 proceed;
3061
cristyb5d5f722009-11-04 03:03:49 +00003062#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003063 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003064#endif
3065 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3066 if (proceed == MagickFalse)
3067 status=MagickFalse;
3068 }
3069 }
3070 blur_view=DestroyCacheView(blur_view);
3071 image_view=DestroyCacheView(image_view);
3072 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3073 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3074 if (status == MagickFalse)
3075 blur_image=DestroyImage(blur_image);
3076 return(blur_image);
3077}
3078
3079/*
3080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3081% %
3082% %
3083% %
cristy3ed852e2009-09-05 21:47:34 +00003084% S e l e c t i v e B l u r I m a g e %
3085% %
3086% %
3087% %
3088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3089%
3090% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3091% It is similar to the unsharpen mask that sharpens everything with contrast
3092% above a certain threshold.
3093%
3094% The format of the SelectiveBlurImage method is:
3095%
3096% Image *SelectiveBlurImage(const Image *image,const double radius,
3097% const double sigma,const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003098%
3099% A description of each parameter follows:
3100%
3101% o image: the image.
3102%
cristy3ed852e2009-09-05 21:47:34 +00003103% o radius: the radius of the Gaussian, in pixels, not counting the center
3104% pixel.
3105%
3106% o sigma: the standard deviation of the Gaussian, in pixels.
3107%
3108% o threshold: only pixels within this contrast threshold are included
3109% in the blur operation.
3110%
3111% o exception: return any errors or warnings in this structure.
3112%
3113*/
cristyf4ad9df2011-07-08 16:49:03 +00003114MagickExport Image *SelectiveBlurImage(const Image *image,
3115 const double radius,const double sigma,const double threshold,
3116 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003117{
3118#define SelectiveBlurImageTag "SelectiveBlur/Image"
3119
cristy47e00502009-12-17 19:19:57 +00003120 CacheView
3121 *blur_view,
3122 *image_view;
3123
cristy3ed852e2009-09-05 21:47:34 +00003124 double
cristy3ed852e2009-09-05 21:47:34 +00003125 *kernel;
3126
3127 Image
3128 *blur_image;
3129
cristy3ed852e2009-09-05 21:47:34 +00003130 MagickBooleanType
3131 status;
3132
cristybb503372010-05-27 20:51:26 +00003133 MagickOffsetType
3134 progress;
3135
cristy4c08aed2011-07-01 19:47:50 +00003136 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003137 bias;
3138
cristybb503372010-05-27 20:51:26 +00003139 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003140 i;
cristy3ed852e2009-09-05 21:47:34 +00003141
cristybb503372010-05-27 20:51:26 +00003142 size_t
cristy3ed852e2009-09-05 21:47:34 +00003143 width;
3144
cristybb503372010-05-27 20:51:26 +00003145 ssize_t
3146 j,
3147 u,
3148 v,
3149 y;
3150
cristy3ed852e2009-09-05 21:47:34 +00003151 /*
3152 Initialize blur image attributes.
3153 */
3154 assert(image != (Image *) NULL);
3155 assert(image->signature == MagickSignature);
3156 if (image->debug != MagickFalse)
3157 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3158 assert(exception != (ExceptionInfo *) NULL);
3159 assert(exception->signature == MagickSignature);
3160 width=GetOptimalKernelWidth1D(radius,sigma);
3161 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3162 if (kernel == (double *) NULL)
3163 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003164 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003165 i=0;
cristy47e00502009-12-17 19:19:57 +00003166 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003167 {
cristy47e00502009-12-17 19:19:57 +00003168 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003169 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3170 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003171 }
3172 if (image->debug != MagickFalse)
3173 {
3174 char
3175 format[MaxTextExtent],
3176 *message;
3177
cristy117ff172010-08-15 21:35:32 +00003178 register const double
3179 *k;
3180
cristybb503372010-05-27 20:51:26 +00003181 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003182 u,
3183 v;
3184
cristy3ed852e2009-09-05 21:47:34 +00003185 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003186 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3187 width);
cristy3ed852e2009-09-05 21:47:34 +00003188 message=AcquireString("");
3189 k=kernel;
cristybb503372010-05-27 20:51:26 +00003190 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003191 {
3192 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003193 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003194 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003195 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003196 {
cristyb51dff52011-05-19 16:55:47 +00003197 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003198 (void) ConcatenateString(&message,format);
3199 }
3200 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3201 }
3202 message=DestroyString(message);
3203 }
3204 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3205 if (blur_image == (Image *) NULL)
3206 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003207 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003208 {
cristy3ed852e2009-09-05 21:47:34 +00003209 blur_image=DestroyImage(blur_image);
3210 return((Image *) NULL);
3211 }
3212 /*
3213 Threshold blur image.
3214 */
3215 status=MagickTrue;
3216 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003217 GetPixelInfo(image,&bias);
3218 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003219 image_view=AcquireCacheView(image);
3220 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003221#if defined(MAGICKCORE_OPENMP_SUPPORT)
3222 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003223#endif
cristybb503372010-05-27 20:51:26 +00003224 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003225 {
cristy4c08aed2011-07-01 19:47:50 +00003226 double
3227 contrast;
3228
cristy3ed852e2009-09-05 21:47:34 +00003229 MagickBooleanType
3230 sync;
3231
3232 MagickRealType
3233 gamma;
3234
cristy4c08aed2011-07-01 19:47:50 +00003235 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003236 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003237
cristy4c08aed2011-07-01 19:47:50 +00003238 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003239 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003240
cristy117ff172010-08-15 21:35:32 +00003241 register ssize_t
3242 x;
3243
cristy3ed852e2009-09-05 21:47:34 +00003244 if (status == MagickFalse)
3245 continue;
cristy117ff172010-08-15 21:35:32 +00003246 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3247 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00003248 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3249 exception);
cristy4c08aed2011-07-01 19:47:50 +00003250 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003251 {
3252 status=MagickFalse;
3253 continue;
3254 }
cristybb503372010-05-27 20:51:26 +00003255 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003256 {
cristy4c08aed2011-07-01 19:47:50 +00003257 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003258 pixel;
3259
3260 register const double
cristyc47d1f82009-11-26 01:44:43 +00003261 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00003262
cristybb503372010-05-27 20:51:26 +00003263 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003264 u;
3265
cristy117ff172010-08-15 21:35:32 +00003266 ssize_t
3267 j,
3268 v;
3269
cristyddd82202009-11-03 20:14:50 +00003270 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00003271 k=kernel;
3272 gamma=0.0;
3273 j=0;
cristyed231572011-07-14 02:18:59 +00003274 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003275 {
cristybb503372010-05-27 20:51:26 +00003276 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003277 {
cristybb503372010-05-27 20:51:26 +00003278 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003279 {
cristyed231572011-07-14 02:18:59 +00003280 contrast=GetPixelIntensity(image,p+(u+j)*GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003281 (double) GetPixelIntensity(blur_image,q);
3282 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003283 {
cristy4c08aed2011-07-01 19:47:50 +00003284 pixel.red+=(*k)*
cristyed231572011-07-14 02:18:59 +00003285 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003286 pixel.green+=(*k)*
cristyed231572011-07-14 02:18:59 +00003287 GetPixelGreen(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003288 pixel.blue+=(*k)*
cristyed231572011-07-14 02:18:59 +00003289 GetPixelBlue(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003290 if (image->colorspace == CMYKColorspace)
3291 pixel.black+=(*k)*
cristyed231572011-07-14 02:18:59 +00003292 GetPixelBlack(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003293 gamma+=(*k);
3294 k++;
3295 }
3296 }
cristyd99b0962010-05-29 23:14:26 +00003297 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003298 }
3299 if (gamma != 0.0)
3300 {
3301 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003302 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003303 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003304 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003305 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003306 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003307 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003308 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003309 (image->colorspace == CMYKColorspace))
3310 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003311 }
cristyed231572011-07-14 02:18:59 +00003312 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003313 {
3314 gamma=0.0;
3315 j=0;
cristybb503372010-05-27 20:51:26 +00003316 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003317 {
cristybb503372010-05-27 20:51:26 +00003318 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003319 {
cristy4c08aed2011-07-01 19:47:50 +00003320 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003321 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003322 GetPixelIntensity(blur_image,q);
3323 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003324 {
cristy4c08aed2011-07-01 19:47:50 +00003325 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003326 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003327 gamma+=(*k);
3328 k++;
3329 }
3330 }
cristyeaedf062010-05-29 22:36:02 +00003331 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003332 }
3333 if (gamma != 0.0)
3334 {
3335 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3336 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003337 SetPixelAlpha(blur_image,ClampToQuantum(gamma*pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003338 }
3339 }
3340 }
3341 else
3342 {
3343 MagickRealType
3344 alpha;
3345
cristybb503372010-05-27 20:51:26 +00003346 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003347 {
cristybb503372010-05-27 20:51:26 +00003348 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003349 {
cristy4c08aed2011-07-01 19:47:50 +00003350 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003351 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003352 GetPixelIntensity(blur_image,q);
3353 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003354 {
cristy4c08aed2011-07-01 19:47:50 +00003355 alpha=(MagickRealType) (QuantumScale*
cristyed231572011-07-14 02:18:59 +00003356 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image)));
cristy4c08aed2011-07-01 19:47:50 +00003357 pixel.red+=(*k)*alpha*
cristyed231572011-07-14 02:18:59 +00003358 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003359 pixel.green+=(*k)*alpha*GetPixelGreen(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003360 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003361 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003362 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003363 pixel.alpha+=(*k)*GetPixelAlpha(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003364 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003365 if (image->colorspace == CMYKColorspace)
3366 pixel.black+=(*k)*GetPixelBlack(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003367 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003368 gamma+=(*k)*alpha;
3369 k++;
3370 }
3371 }
cristyeaedf062010-05-29 22:36:02 +00003372 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003373 }
3374 if (gamma != 0.0)
3375 {
3376 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003377 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003378 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003379 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003380 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003381 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003382 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003383 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003384 (image->colorspace == CMYKColorspace))
3385 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003386 }
cristyed231572011-07-14 02:18:59 +00003387 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003388 {
3389 gamma=0.0;
3390 j=0;
cristybb503372010-05-27 20:51:26 +00003391 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003392 {
cristybb503372010-05-27 20:51:26 +00003393 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003394 {
cristy4c08aed2011-07-01 19:47:50 +00003395 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003396 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003397 GetPixelIntensity(blur_image,q);
3398 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003399 {
cristy4c08aed2011-07-01 19:47:50 +00003400 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003401 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003402 gamma+=(*k);
3403 k++;
3404 }
3405 }
cristyeaedf062010-05-29 22:36:02 +00003406 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003407 }
3408 if (gamma != 0.0)
3409 {
3410 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3411 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003412 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003413 }
3414 }
3415 }
cristyed231572011-07-14 02:18:59 +00003416 p+=GetPixelChannels(image);
3417 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003418 }
3419 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3420 if (sync == MagickFalse)
3421 status=MagickFalse;
3422 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3423 {
3424 MagickBooleanType
3425 proceed;
3426
cristyb5d5f722009-11-04 03:03:49 +00003427#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003428 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003429#endif
3430 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3431 image->rows);
3432 if (proceed == MagickFalse)
3433 status=MagickFalse;
3434 }
3435 }
3436 blur_image->type=image->type;
3437 blur_view=DestroyCacheView(blur_view);
3438 image_view=DestroyCacheView(image_view);
3439 kernel=(double *) RelinquishMagickMemory(kernel);
3440 if (status == MagickFalse)
3441 blur_image=DestroyImage(blur_image);
3442 return(blur_image);
3443}
3444
3445/*
3446%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3447% %
3448% %
3449% %
3450% S h a d e I m a g e %
3451% %
3452% %
3453% %
3454%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3455%
3456% ShadeImage() shines a distant light on an image to create a
3457% three-dimensional effect. You control the positioning of the light with
3458% azimuth and elevation; azimuth is measured in degrees off the x axis
3459% and elevation is measured in pixels above the Z axis.
3460%
3461% The format of the ShadeImage method is:
3462%
3463% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3464% const double azimuth,const double elevation,ExceptionInfo *exception)
3465%
3466% A description of each parameter follows:
3467%
3468% o image: the image.
3469%
3470% o gray: A value other than zero shades the intensity of each pixel.
3471%
3472% o azimuth, elevation: Define the light source direction.
3473%
3474% o exception: return any errors or warnings in this structure.
3475%
3476*/
3477MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3478 const double azimuth,const double elevation,ExceptionInfo *exception)
3479{
3480#define ShadeImageTag "Shade/Image"
3481
cristyc4c8d132010-01-07 01:58:38 +00003482 CacheView
3483 *image_view,
3484 *shade_view;
3485
cristy3ed852e2009-09-05 21:47:34 +00003486 Image
3487 *shade_image;
3488
cristy3ed852e2009-09-05 21:47:34 +00003489 MagickBooleanType
3490 status;
3491
cristybb503372010-05-27 20:51:26 +00003492 MagickOffsetType
3493 progress;
3494
cristy3ed852e2009-09-05 21:47:34 +00003495 PrimaryInfo
3496 light;
3497
cristybb503372010-05-27 20:51:26 +00003498 ssize_t
3499 y;
3500
cristy3ed852e2009-09-05 21:47:34 +00003501 /*
3502 Initialize shaded image attributes.
3503 */
3504 assert(image != (const Image *) NULL);
3505 assert(image->signature == MagickSignature);
3506 if (image->debug != MagickFalse)
3507 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3508 assert(exception != (ExceptionInfo *) NULL);
3509 assert(exception->signature == MagickSignature);
3510 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3511 if (shade_image == (Image *) NULL)
3512 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003513 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003514 {
cristy3ed852e2009-09-05 21:47:34 +00003515 shade_image=DestroyImage(shade_image);
3516 return((Image *) NULL);
3517 }
3518 /*
3519 Compute the light vector.
3520 */
3521 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3522 cos(DegreesToRadians(elevation));
3523 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3524 cos(DegreesToRadians(elevation));
3525 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3526 /*
3527 Shade image.
3528 */
3529 status=MagickTrue;
3530 progress=0;
3531 image_view=AcquireCacheView(image);
3532 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003533#if defined(MAGICKCORE_OPENMP_SUPPORT)
3534 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003535#endif
cristybb503372010-05-27 20:51:26 +00003536 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003537 {
3538 MagickRealType
3539 distance,
3540 normal_distance,
3541 shade;
3542
3543 PrimaryInfo
3544 normal;
3545
cristy4c08aed2011-07-01 19:47:50 +00003546 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003547 *restrict p,
3548 *restrict s0,
3549 *restrict s1,
3550 *restrict s2;
cristy3ed852e2009-09-05 21:47:34 +00003551
cristy4c08aed2011-07-01 19:47:50 +00003552 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003553 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003554
cristy117ff172010-08-15 21:35:32 +00003555 register ssize_t
3556 x;
3557
cristy3ed852e2009-09-05 21:47:34 +00003558 if (status == MagickFalse)
3559 continue;
3560 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3561 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3562 exception);
cristy4c08aed2011-07-01 19:47:50 +00003563 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003564 {
3565 status=MagickFalse;
3566 continue;
3567 }
3568 /*
3569 Shade this row of pixels.
3570 */
3571 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristyed231572011-07-14 02:18:59 +00003572 s0=p+GetPixelChannels(image);
3573 s1=s0+(image->columns+2)*GetPixelChannels(image);
3574 s2=s1+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003575 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003576 {
3577 /*
3578 Determine the surface normal and compute shading.
3579 */
cristyed231572011-07-14 02:18:59 +00003580 normal.x=(double) (GetPixelIntensity(image,s0-GetPixelChannels(image))+
3581 GetPixelIntensity(image,s1-GetPixelChannels(image))+
3582 GetPixelIntensity(image,s2-GetPixelChannels(image))-
3583 GetPixelIntensity(image,s0+GetPixelChannels(image))-
3584 GetPixelIntensity(image,s1+GetPixelChannels(image))-
3585 GetPixelIntensity(image,s2+GetPixelChannels(image)));
3586 normal.y=(double) (GetPixelIntensity(image,s2-GetPixelChannels(image))+
cristy4c08aed2011-07-01 19:47:50 +00003587 GetPixelIntensity(image,s2)+
cristyed231572011-07-14 02:18:59 +00003588 GetPixelIntensity(image,s2+GetPixelChannels(image))-
3589 GetPixelIntensity(image,s0-GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003590 GetPixelIntensity(image,s0)-
cristyed231572011-07-14 02:18:59 +00003591 GetPixelIntensity(image,s0+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003592 if ((normal.x == 0.0) && (normal.y == 0.0))
3593 shade=light.z;
3594 else
3595 {
3596 shade=0.0;
3597 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3598 if (distance > MagickEpsilon)
3599 {
3600 normal_distance=
3601 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3602 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3603 shade=distance/sqrt((double) normal_distance);
3604 }
3605 }
3606 if (gray != MagickFalse)
3607 {
cristy4c08aed2011-07-01 19:47:50 +00003608 SetPixelRed(shade_image,ClampToQuantum(shade),q);
3609 SetPixelGreen(shade_image,ClampToQuantum(shade),q);
3610 SetPixelBlue(shade_image,ClampToQuantum(shade),q);
cristy3ed852e2009-09-05 21:47:34 +00003611 }
3612 else
3613 {
cristy4c08aed2011-07-01 19:47:50 +00003614 SetPixelRed(shade_image,ClampToQuantum(QuantumScale*shade*
3615 GetPixelRed(image,s1)),q);
3616 SetPixelGreen(shade_image,ClampToQuantum(QuantumScale*shade*
3617 GetPixelGreen(image,s1)),q);
3618 SetPixelBlue(shade_image,ClampToQuantum(QuantumScale*shade*
3619 GetPixelBlue(image,s1)),q);
cristy3ed852e2009-09-05 21:47:34 +00003620 }
cristy4c08aed2011-07-01 19:47:50 +00003621 SetPixelAlpha(shade_image,GetPixelAlpha(image,s1),q);
cristyed231572011-07-14 02:18:59 +00003622 s0+=GetPixelChannels(image);
3623 s1+=GetPixelChannels(image);
3624 s2+=GetPixelChannels(image);
3625 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003626 }
3627 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3628 status=MagickFalse;
3629 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3630 {
3631 MagickBooleanType
3632 proceed;
3633
cristyb5d5f722009-11-04 03:03:49 +00003634#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003635 #pragma omp critical (MagickCore_ShadeImage)
3636#endif
3637 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3638 if (proceed == MagickFalse)
3639 status=MagickFalse;
3640 }
3641 }
3642 shade_view=DestroyCacheView(shade_view);
3643 image_view=DestroyCacheView(image_view);
3644 if (status == MagickFalse)
3645 shade_image=DestroyImage(shade_image);
3646 return(shade_image);
3647}
3648
3649/*
3650%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3651% %
3652% %
3653% %
3654% S h a r p e n I m a g e %
3655% %
3656% %
3657% %
3658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3659%
3660% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3661% operator of the given radius and standard deviation (sigma). For
3662% reasonable results, radius should be larger than sigma. Use a radius of 0
3663% and SharpenImage() selects a suitable radius for you.
3664%
3665% Using a separable kernel would be faster, but the negative weights cancel
3666% out on the corners of the kernel producing often undesirable ringing in the
3667% filtered result; this can be avoided by using a 2D gaussian shaped image
3668% sharpening kernel instead.
3669%
3670% The format of the SharpenImage method is:
3671%
3672% Image *SharpenImage(const Image *image,const double radius,
3673% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003674%
3675% A description of each parameter follows:
3676%
3677% o image: the image.
3678%
cristy3ed852e2009-09-05 21:47:34 +00003679% o radius: the radius of the Gaussian, in pixels, not counting the center
3680% pixel.
3681%
3682% o sigma: the standard deviation of the Laplacian, in pixels.
3683%
3684% o exception: return any errors or warnings in this structure.
3685%
3686*/
cristy3ed852e2009-09-05 21:47:34 +00003687MagickExport Image *SharpenImage(const Image *image,const double radius,
3688 const double sigma,ExceptionInfo *exception)
3689{
cristy3ed852e2009-09-05 21:47:34 +00003690 double
cristy47e00502009-12-17 19:19:57 +00003691 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003692
3693 Image
3694 *sharp_image;
3695
cristy41cbe682011-07-15 19:12:37 +00003696 KernelInfo
3697 *kernel_info;
3698
cristybb503372010-05-27 20:51:26 +00003699 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003700 i;
3701
cristybb503372010-05-27 20:51:26 +00003702 size_t
cristy3ed852e2009-09-05 21:47:34 +00003703 width;
3704
cristy117ff172010-08-15 21:35:32 +00003705 ssize_t
3706 j,
3707 u,
3708 v;
3709
cristy3ed852e2009-09-05 21:47:34 +00003710 assert(image != (const Image *) NULL);
3711 assert(image->signature == MagickSignature);
3712 if (image->debug != MagickFalse)
3713 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3714 assert(exception != (ExceptionInfo *) NULL);
3715 assert(exception->signature == MagickSignature);
3716 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003717 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003718 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003719 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003720 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3721 kernel_info->width=width;
3722 kernel_info->height=width;
3723 kernel_info->signature=MagickSignature;
3724 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
3725 kernel_info->width*sizeof(*kernel_info->values));
3726 if (kernel_info->values == (double *) NULL)
3727 {
3728 kernel_info=DestroyKernelInfo(kernel_info);
3729 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3730 }
cristy3ed852e2009-09-05 21:47:34 +00003731 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003732 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003733 i=0;
3734 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003735 {
cristy47e00502009-12-17 19:19:57 +00003736 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003737 {
cristy41cbe682011-07-15 19:12:37 +00003738 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3739 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3740 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003741 i++;
3742 }
3743 }
cristy41cbe682011-07-15 19:12:37 +00003744 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy0a922382011-07-16 15:30:34 +00003745 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00003746 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003747 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003748 return(sharp_image);
3749}
3750
3751/*
3752%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3753% %
3754% %
3755% %
3756% S p r e a d I m a g e %
3757% %
3758% %
3759% %
3760%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3761%
3762% SpreadImage() is a special effects method that randomly displaces each
3763% pixel in a block defined by the radius parameter.
3764%
3765% The format of the SpreadImage method is:
3766%
3767% Image *SpreadImage(const Image *image,const double radius,
3768% ExceptionInfo *exception)
3769%
3770% A description of each parameter follows:
3771%
3772% o image: the image.
3773%
3774% o radius: Choose a random pixel in a neighborhood of this extent.
3775%
3776% o exception: return any errors or warnings in this structure.
3777%
3778*/
3779MagickExport Image *SpreadImage(const Image *image,const double radius,
3780 ExceptionInfo *exception)
3781{
3782#define SpreadImageTag "Spread/Image"
3783
cristyfa112112010-01-04 17:48:07 +00003784 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003785 *image_view,
3786 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003787
cristy3ed852e2009-09-05 21:47:34 +00003788 Image
3789 *spread_image;
3790
cristy3ed852e2009-09-05 21:47:34 +00003791 MagickBooleanType
3792 status;
3793
cristybb503372010-05-27 20:51:26 +00003794 MagickOffsetType
3795 progress;
3796
cristy4c08aed2011-07-01 19:47:50 +00003797 PixelInfo
cristyddd82202009-11-03 20:14:50 +00003798 bias;
cristy3ed852e2009-09-05 21:47:34 +00003799
3800 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003801 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003802
cristybb503372010-05-27 20:51:26 +00003803 size_t
cristy3ed852e2009-09-05 21:47:34 +00003804 width;
3805
cristybb503372010-05-27 20:51:26 +00003806 ssize_t
3807 y;
3808
cristy3ed852e2009-09-05 21:47:34 +00003809 /*
3810 Initialize spread image attributes.
3811 */
3812 assert(image != (Image *) NULL);
3813 assert(image->signature == MagickSignature);
3814 if (image->debug != MagickFalse)
3815 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3816 assert(exception != (ExceptionInfo *) NULL);
3817 assert(exception->signature == MagickSignature);
3818 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3819 exception);
3820 if (spread_image == (Image *) NULL)
3821 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003822 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003823 {
cristy3ed852e2009-09-05 21:47:34 +00003824 spread_image=DestroyImage(spread_image);
3825 return((Image *) NULL);
3826 }
3827 /*
3828 Spread image.
3829 */
3830 status=MagickTrue;
3831 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003832 GetPixelInfo(spread_image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003833 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003834 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003835 image_view=AcquireCacheView(image);
3836 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003837#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00003838 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00003839#endif
cristybb503372010-05-27 20:51:26 +00003840 for (y=0; y < (ssize_t) spread_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003841 {
cristy5c9e6f22010-09-17 17:31:01 +00003842 const int
3843 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003844
cristy4c08aed2011-07-01 19:47:50 +00003845 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003846 pixel;
3847
cristy4c08aed2011-07-01 19:47:50 +00003848 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003849 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003850
cristy117ff172010-08-15 21:35:32 +00003851 register ssize_t
3852 x;
3853
cristy3ed852e2009-09-05 21:47:34 +00003854 if (status == MagickFalse)
3855 continue;
cristy9f7e7cb2011-03-26 00:49:57 +00003856 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003857 exception);
cristyacd2ed22011-08-30 01:44:23 +00003858 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003859 {
3860 status=MagickFalse;
3861 continue;
3862 }
cristyddd82202009-11-03 20:14:50 +00003863 pixel=bias;
cristybb503372010-05-27 20:51:26 +00003864 for (x=0; x < (ssize_t) spread_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003865 {
cristy4c08aed2011-07-01 19:47:50 +00003866 (void) InterpolatePixelInfo(image,image_view,
cristy8a7c3e82011-03-26 02:10:53 +00003867 UndefinedInterpolatePixel,(double) x+width*(GetPseudoRandomValue(
3868 random_info[id])-0.5),(double) y+width*(GetPseudoRandomValue(
3869 random_info[id])-0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003870 SetPixelPixelInfo(spread_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00003871 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003872 }
cristy9f7e7cb2011-03-26 00:49:57 +00003873 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003874 status=MagickFalse;
3875 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3876 {
3877 MagickBooleanType
3878 proceed;
3879
cristyb557a152011-02-22 12:14:30 +00003880#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003881 #pragma omp critical (MagickCore_SpreadImage)
3882#endif
3883 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3884 if (proceed == MagickFalse)
3885 status=MagickFalse;
3886 }
3887 }
cristy9f7e7cb2011-03-26 00:49:57 +00003888 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003889 image_view=DestroyCacheView(image_view);
3890 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003891 return(spread_image);
3892}
3893
3894/*
3895%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3896% %
3897% %
3898% %
cristy0834d642011-03-18 18:26:08 +00003899% S t a t i s t i c I m a g e %
3900% %
3901% %
3902% %
3903%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3904%
3905% StatisticImage() makes each pixel the min / max / median / mode / etc. of
cristy8d752042011-03-19 01:00:36 +00003906% the neighborhood of the specified width and height.
cristy0834d642011-03-18 18:26:08 +00003907%
3908% The format of the StatisticImage method is:
3909%
3910% Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00003911% const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00003912%
3913% A description of each parameter follows:
3914%
3915% o image: the image.
3916%
cristy0834d642011-03-18 18:26:08 +00003917% o type: the statistic type (median, mode, etc.).
3918%
cristy95c38342011-03-18 22:39:51 +00003919% o width: the width of the pixel neighborhood.
3920%
3921% o height: the height of the pixel neighborhood.
cristy0834d642011-03-18 18:26:08 +00003922%
3923% o exception: return any errors or warnings in this structure.
3924%
3925*/
3926
cristy733678d2011-03-18 21:29:28 +00003927#define ListChannels 5
3928
3929typedef struct _ListNode
3930{
3931 size_t
3932 next[9],
3933 count,
3934 signature;
3935} ListNode;
3936
3937typedef struct _SkipList
3938{
3939 ssize_t
3940 level;
3941
3942 ListNode
3943 *nodes;
3944} SkipList;
3945
3946typedef struct _PixelList
3947{
3948 size_t
cristy6fc86bb2011-03-18 23:45:16 +00003949 length,
cristy733678d2011-03-18 21:29:28 +00003950 seed,
3951 signature;
3952
3953 SkipList
3954 lists[ListChannels];
3955} PixelList;
3956
3957static PixelList *DestroyPixelList(PixelList *pixel_list)
3958{
3959 register ssize_t
3960 i;
3961
3962 if (pixel_list == (PixelList *) NULL)
3963 return((PixelList *) NULL);
3964 for (i=0; i < ListChannels; i++)
3965 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
3966 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
3967 pixel_list->lists[i].nodes);
3968 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
3969 return(pixel_list);
3970}
3971
3972static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
3973{
3974 register ssize_t
3975 i;
3976
3977 assert(pixel_list != (PixelList **) NULL);
3978 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
3979 if (pixel_list[i] != (PixelList *) NULL)
3980 pixel_list[i]=DestroyPixelList(pixel_list[i]);
3981 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
3982 return(pixel_list);
3983}
3984
cristy6fc86bb2011-03-18 23:45:16 +00003985static PixelList *AcquirePixelList(const size_t width,const size_t height)
cristy733678d2011-03-18 21:29:28 +00003986{
3987 PixelList
3988 *pixel_list;
3989
3990 register ssize_t
3991 i;
3992
3993 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
3994 if (pixel_list == (PixelList *) NULL)
3995 return(pixel_list);
3996 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
cristy6fc86bb2011-03-18 23:45:16 +00003997 pixel_list->length=width*height;
cristy733678d2011-03-18 21:29:28 +00003998 for (i=0; i < ListChannels; i++)
3999 {
4000 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
4001 sizeof(*pixel_list->lists[i].nodes));
4002 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
4003 return(DestroyPixelList(pixel_list));
4004 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
4005 sizeof(*pixel_list->lists[i].nodes));
4006 }
4007 pixel_list->signature=MagickSignature;
4008 return(pixel_list);
4009}
4010
cristy6fc86bb2011-03-18 23:45:16 +00004011static PixelList **AcquirePixelListThreadSet(const size_t width,
4012 const size_t height)
cristy733678d2011-03-18 21:29:28 +00004013{
4014 PixelList
4015 **pixel_list;
4016
4017 register ssize_t
4018 i;
4019
4020 size_t
4021 number_threads;
4022
4023 number_threads=GetOpenMPMaximumThreads();
4024 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
4025 sizeof(*pixel_list));
4026 if (pixel_list == (PixelList **) NULL)
4027 return((PixelList **) NULL);
4028 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
4029 for (i=0; i < (ssize_t) number_threads; i++)
4030 {
cristy6fc86bb2011-03-18 23:45:16 +00004031 pixel_list[i]=AcquirePixelList(width,height);
cristy733678d2011-03-18 21:29:28 +00004032 if (pixel_list[i] == (PixelList *) NULL)
4033 return(DestroyPixelListThreadSet(pixel_list));
4034 }
4035 return(pixel_list);
4036}
4037
4038static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
4039 const size_t color)
4040{
4041 register SkipList
4042 *list;
4043
4044 register ssize_t
4045 level;
4046
4047 size_t
4048 search,
4049 update[9];
4050
4051 /*
4052 Initialize the node.
4053 */
4054 list=pixel_list->lists+channel;
4055 list->nodes[color].signature=pixel_list->signature;
4056 list->nodes[color].count=1;
4057 /*
4058 Determine where it belongs in the list.
4059 */
4060 search=65536UL;
4061 for (level=list->level; level >= 0; level--)
4062 {
4063 while (list->nodes[search].next[level] < color)
4064 search=list->nodes[search].next[level];
4065 update[level]=search;
4066 }
4067 /*
4068 Generate a pseudo-random level for this node.
4069 */
4070 for (level=0; ; level++)
4071 {
4072 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
4073 if ((pixel_list->seed & 0x300) != 0x300)
4074 break;
4075 }
4076 if (level > 8)
4077 level=8;
4078 if (level > (list->level+2))
4079 level=list->level+2;
4080 /*
4081 If we're raising the list's level, link back to the root node.
4082 */
4083 while (level > list->level)
4084 {
4085 list->level++;
4086 update[list->level]=65536UL;
4087 }
4088 /*
4089 Link the node into the skip-list.
4090 */
4091 do
4092 {
4093 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
4094 list->nodes[update[level]].next[level]=color;
cristy3cba8ca2011-03-19 01:29:12 +00004095 } while (level-- > 0);
cristy733678d2011-03-18 21:29:28 +00004096}
4097
cristy4c08aed2011-07-01 19:47:50 +00004098static PixelInfo GetMaximumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004099{
cristy4c08aed2011-07-01 19:47:50 +00004100 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004101 pixel;
4102
4103 register SkipList
4104 *list;
4105
4106 register ssize_t
4107 channel;
4108
4109 size_t
cristyd76c51e2011-03-26 00:21:26 +00004110 color,
4111 maximum;
cristy49f37242011-03-22 18:18:23 +00004112
4113 ssize_t
cristy6fc86bb2011-03-18 23:45:16 +00004114 count;
4115
4116 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004117 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004118
4119 /*
4120 Find the maximum value for each of the color.
4121 */
4122 for (channel=0; channel < 5; channel++)
4123 {
4124 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004125 color=65536L;
cristy6fc86bb2011-03-18 23:45:16 +00004126 count=0;
cristy49f37242011-03-22 18:18:23 +00004127 maximum=list->nodes[color].next[0];
cristy6fc86bb2011-03-18 23:45:16 +00004128 do
4129 {
4130 color=list->nodes[color].next[0];
cristy49f37242011-03-22 18:18:23 +00004131 if (color > maximum)
4132 maximum=color;
cristy6fc86bb2011-03-18 23:45:16 +00004133 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004134 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004135 channels[channel]=(unsigned short) maximum;
4136 }
cristy4c08aed2011-07-01 19:47:50 +00004137 GetPixelInfo((const Image *) NULL,&pixel);
cristy49f37242011-03-22 18:18:23 +00004138 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4139 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4140 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004141 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4142 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy49f37242011-03-22 18:18:23 +00004143 return(pixel);
4144}
4145
cristy4c08aed2011-07-01 19:47:50 +00004146static PixelInfo GetMeanPixelList(PixelList *pixel_list)
cristy49f37242011-03-22 18:18:23 +00004147{
cristy4c08aed2011-07-01 19:47:50 +00004148 PixelInfo
cristy49f37242011-03-22 18:18:23 +00004149 pixel;
4150
cristy80a99a32011-03-30 01:30:23 +00004151 MagickRealType
4152 sum;
4153
cristy49f37242011-03-22 18:18:23 +00004154 register SkipList
4155 *list;
4156
4157 register ssize_t
4158 channel;
4159
4160 size_t
cristy80a99a32011-03-30 01:30:23 +00004161 color;
cristy49f37242011-03-22 18:18:23 +00004162
4163 ssize_t
4164 count;
4165
4166 unsigned short
4167 channels[ListChannels];
4168
4169 /*
4170 Find the mean value for each of the color.
4171 */
4172 for (channel=0; channel < 5; channel++)
4173 {
4174 list=pixel_list->lists+channel;
4175 color=65536L;
4176 count=0;
cristy80a99a32011-03-30 01:30:23 +00004177 sum=0.0;
cristy49f37242011-03-22 18:18:23 +00004178 do
4179 {
4180 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004181 sum+=(MagickRealType) list->nodes[color].count*color;
cristy49f37242011-03-22 18:18:23 +00004182 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004183 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004184 sum/=pixel_list->length;
4185 channels[channel]=(unsigned short) sum;
cristy6fc86bb2011-03-18 23:45:16 +00004186 }
cristy4c08aed2011-07-01 19:47:50 +00004187 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004188 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4189 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4190 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004191 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4192 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004193 return(pixel);
4194}
4195
cristy4c08aed2011-07-01 19:47:50 +00004196static PixelInfo GetMedianPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004197{
cristy4c08aed2011-07-01 19:47:50 +00004198 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004199 pixel;
4200
4201 register SkipList
4202 *list;
4203
4204 register ssize_t
4205 channel;
4206
4207 size_t
cristy49f37242011-03-22 18:18:23 +00004208 color;
4209
4210 ssize_t
cristy733678d2011-03-18 21:29:28 +00004211 count;
4212
4213 unsigned short
4214 channels[ListChannels];
4215
4216 /*
4217 Find the median value for each of the color.
4218 */
cristy733678d2011-03-18 21:29:28 +00004219 for (channel=0; channel < 5; channel++)
4220 {
4221 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004222 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004223 count=0;
4224 do
4225 {
4226 color=list->nodes[color].next[0];
4227 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004228 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy6fc86bb2011-03-18 23:45:16 +00004229 channels[channel]=(unsigned short) color;
4230 }
cristy4c08aed2011-07-01 19:47:50 +00004231 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004232 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4233 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4234 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004235 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4236 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004237 return(pixel);
4238}
4239
cristy4c08aed2011-07-01 19:47:50 +00004240static PixelInfo GetMinimumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004241{
cristy4c08aed2011-07-01 19:47:50 +00004242 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004243 pixel;
4244
4245 register SkipList
4246 *list;
4247
4248 register ssize_t
4249 channel;
4250
4251 size_t
cristyd76c51e2011-03-26 00:21:26 +00004252 color,
4253 minimum;
cristy6fc86bb2011-03-18 23:45:16 +00004254
cristy49f37242011-03-22 18:18:23 +00004255 ssize_t
4256 count;
4257
cristy6fc86bb2011-03-18 23:45:16 +00004258 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004259 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004260
4261 /*
4262 Find the minimum value for each of the color.
4263 */
4264 for (channel=0; channel < 5; channel++)
4265 {
4266 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004267 count=0;
cristy6fc86bb2011-03-18 23:45:16 +00004268 color=65536UL;
cristy49f37242011-03-22 18:18:23 +00004269 minimum=list->nodes[color].next[0];
4270 do
4271 {
4272 color=list->nodes[color].next[0];
4273 if (color < minimum)
4274 minimum=color;
4275 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004276 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004277 channels[channel]=(unsigned short) minimum;
cristy733678d2011-03-18 21:29:28 +00004278 }
cristy4c08aed2011-07-01 19:47:50 +00004279 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004280 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4281 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4282 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004283 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4284 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004285 return(pixel);
4286}
4287
cristy4c08aed2011-07-01 19:47:50 +00004288static PixelInfo GetModePixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004289{
cristy4c08aed2011-07-01 19:47:50 +00004290 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004291 pixel;
4292
4293 register SkipList
4294 *list;
4295
4296 register ssize_t
4297 channel;
4298
4299 size_t
4300 color,
cristy733678d2011-03-18 21:29:28 +00004301 max_count,
cristy6fc86bb2011-03-18 23:45:16 +00004302 mode;
cristy733678d2011-03-18 21:29:28 +00004303
cristy49f37242011-03-22 18:18:23 +00004304 ssize_t
4305 count;
4306
cristy733678d2011-03-18 21:29:28 +00004307 unsigned short
4308 channels[5];
4309
4310 /*
glennrp30d2dc62011-06-25 03:17:16 +00004311 Make each pixel the 'predominant color' of the specified neighborhood.
cristy733678d2011-03-18 21:29:28 +00004312 */
cristy733678d2011-03-18 21:29:28 +00004313 for (channel=0; channel < 5; channel++)
4314 {
4315 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004316 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004317 mode=color;
4318 max_count=list->nodes[mode].count;
4319 count=0;
4320 do
4321 {
4322 color=list->nodes[color].next[0];
4323 if (list->nodes[color].count > max_count)
4324 {
4325 mode=color;
4326 max_count=list->nodes[mode].count;
4327 }
4328 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004329 } while (count < (ssize_t) pixel_list->length);
cristy733678d2011-03-18 21:29:28 +00004330 channels[channel]=(unsigned short) mode;
4331 }
cristy4c08aed2011-07-01 19:47:50 +00004332 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004333 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4334 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4335 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004336 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4337 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004338 return(pixel);
4339}
4340
cristy4c08aed2011-07-01 19:47:50 +00004341static PixelInfo GetNonpeakPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004342{
cristy4c08aed2011-07-01 19:47:50 +00004343 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004344 pixel;
4345
4346 register SkipList
4347 *list;
4348
4349 register ssize_t
4350 channel;
4351
4352 size_t
cristy733678d2011-03-18 21:29:28 +00004353 color,
cristy733678d2011-03-18 21:29:28 +00004354 next,
4355 previous;
4356
cristy49f37242011-03-22 18:18:23 +00004357 ssize_t
4358 count;
4359
cristy733678d2011-03-18 21:29:28 +00004360 unsigned short
4361 channels[5];
4362
4363 /*
cristy49f37242011-03-22 18:18:23 +00004364 Finds the non peak value for each of the colors.
cristy733678d2011-03-18 21:29:28 +00004365 */
cristy733678d2011-03-18 21:29:28 +00004366 for (channel=0; channel < 5; channel++)
4367 {
4368 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004369 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004370 next=list->nodes[color].next[0];
4371 count=0;
4372 do
4373 {
4374 previous=color;
4375 color=next;
4376 next=list->nodes[color].next[0];
4377 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004378 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy733678d2011-03-18 21:29:28 +00004379 if ((previous == 65536UL) && (next != 65536UL))
4380 color=next;
4381 else
4382 if ((previous != 65536UL) && (next == 65536UL))
4383 color=previous;
4384 channels[channel]=(unsigned short) color;
4385 }
cristy4c08aed2011-07-01 19:47:50 +00004386 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004387 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4388 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4389 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004390 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4391 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy733678d2011-03-18 21:29:28 +00004392 return(pixel);
4393}
4394
cristy4c08aed2011-07-01 19:47:50 +00004395static PixelInfo GetStandardDeviationPixelList(PixelList *pixel_list)
cristy9a68cbb2011-03-29 00:51:23 +00004396{
cristy4c08aed2011-07-01 19:47:50 +00004397 PixelInfo
cristy9a68cbb2011-03-29 00:51:23 +00004398 pixel;
4399
cristy80a99a32011-03-30 01:30:23 +00004400 MagickRealType
4401 sum,
4402 sum_squared;
4403
cristy9a68cbb2011-03-29 00:51:23 +00004404 register SkipList
4405 *list;
4406
4407 register ssize_t
4408 channel;
4409
4410 size_t
cristy80a99a32011-03-30 01:30:23 +00004411 color;
cristy9a68cbb2011-03-29 00:51:23 +00004412
4413 ssize_t
4414 count;
4415
4416 unsigned short
4417 channels[ListChannels];
4418
4419 /*
cristy80a99a32011-03-30 01:30:23 +00004420 Find the standard-deviation value for each of the color.
cristy9a68cbb2011-03-29 00:51:23 +00004421 */
4422 for (channel=0; channel < 5; channel++)
4423 {
4424 list=pixel_list->lists+channel;
4425 color=65536L;
4426 count=0;
cristy80a99a32011-03-30 01:30:23 +00004427 sum=0.0;
4428 sum_squared=0.0;
cristy9a68cbb2011-03-29 00:51:23 +00004429 do
4430 {
cristy80a99a32011-03-30 01:30:23 +00004431 register ssize_t
4432 i;
4433
cristy9a68cbb2011-03-29 00:51:23 +00004434 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004435 sum+=(MagickRealType) list->nodes[color].count*color;
4436 for (i=0; i < (ssize_t) list->nodes[color].count; i++)
4437 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
cristy9a68cbb2011-03-29 00:51:23 +00004438 count+=list->nodes[color].count;
4439 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004440 sum/=pixel_list->length;
4441 sum_squared/=pixel_list->length;
4442 channels[channel]=(unsigned short) sqrt(sum_squared-(sum*sum));
cristy9a68cbb2011-03-29 00:51:23 +00004443 }
cristy4c08aed2011-07-01 19:47:50 +00004444 GetPixelInfo((const Image *) NULL,&pixel);
cristy9a68cbb2011-03-29 00:51:23 +00004445 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4446 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4447 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004448 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4449 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy9a68cbb2011-03-29 00:51:23 +00004450 return(pixel);
4451}
4452
cristy4c08aed2011-07-01 19:47:50 +00004453static inline void InsertPixelList(const Image *image,const Quantum *pixel,
4454 PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004455{
4456 size_t
4457 signature;
4458
4459 unsigned short
4460 index;
4461
cristy4c08aed2011-07-01 19:47:50 +00004462 index=ScaleQuantumToShort(GetPixelRed(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004463 signature=pixel_list->lists[0].nodes[index].signature;
4464 if (signature == pixel_list->signature)
4465 pixel_list->lists[0].nodes[index].count++;
4466 else
4467 AddNodePixelList(pixel_list,0,index);
cristy4c08aed2011-07-01 19:47:50 +00004468 index=ScaleQuantumToShort(GetPixelGreen(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004469 signature=pixel_list->lists[1].nodes[index].signature;
4470 if (signature == pixel_list->signature)
4471 pixel_list->lists[1].nodes[index].count++;
4472 else
4473 AddNodePixelList(pixel_list,1,index);
cristy4c08aed2011-07-01 19:47:50 +00004474 index=ScaleQuantumToShort(GetPixelBlue(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004475 signature=pixel_list->lists[2].nodes[index].signature;
4476 if (signature == pixel_list->signature)
4477 pixel_list->lists[2].nodes[index].count++;
4478 else
4479 AddNodePixelList(pixel_list,2,index);
cristy4c08aed2011-07-01 19:47:50 +00004480 index=ScaleQuantumToShort(GetPixelAlpha(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004481 signature=pixel_list->lists[3].nodes[index].signature;
4482 if (signature == pixel_list->signature)
4483 pixel_list->lists[3].nodes[index].count++;
4484 else
4485 AddNodePixelList(pixel_list,3,index);
4486 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004487 index=ScaleQuantumToShort(GetPixelBlack(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004488 signature=pixel_list->lists[4].nodes[index].signature;
4489 if (signature == pixel_list->signature)
4490 pixel_list->lists[4].nodes[index].count++;
4491 else
4492 AddNodePixelList(pixel_list,4,index);
4493}
4494
cristy80c99742011-04-04 14:46:39 +00004495static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
4496{
4497 if (x < 0)
4498 return(-x);
4499 return(x);
4500}
4501
cristy733678d2011-03-18 21:29:28 +00004502static void ResetPixelList(PixelList *pixel_list)
4503{
4504 int
4505 level;
4506
4507 register ListNode
4508 *root;
4509
4510 register SkipList
4511 *list;
4512
4513 register ssize_t
4514 channel;
4515
4516 /*
4517 Reset the skip-list.
4518 */
4519 for (channel=0; channel < 5; channel++)
4520 {
4521 list=pixel_list->lists+channel;
4522 root=list->nodes+65536UL;
4523 list->level=0;
4524 for (level=0; level < 9; level++)
4525 root->next[level]=65536UL;
4526 }
4527 pixel_list->seed=pixel_list->signature++;
4528}
4529
cristy0834d642011-03-18 18:26:08 +00004530MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00004531 const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00004532{
cristy3cba8ca2011-03-19 01:29:12 +00004533#define StatisticWidth \
cristyd76c51e2011-03-26 00:21:26 +00004534 (width == 0 ? GetOptimalKernelWidth2D((double) width,0.5) : width)
cristy3cba8ca2011-03-19 01:29:12 +00004535#define StatisticHeight \
cristyd76c51e2011-03-26 00:21:26 +00004536 (height == 0 ? GetOptimalKernelWidth2D((double) height,0.5) : height)
cristy0834d642011-03-18 18:26:08 +00004537#define StatisticImageTag "Statistic/Image"
4538
4539 CacheView
4540 *image_view,
4541 *statistic_view;
4542
4543 Image
4544 *statistic_image;
4545
4546 MagickBooleanType
4547 status;
4548
4549 MagickOffsetType
4550 progress;
4551
4552 PixelList
4553 **restrict pixel_list;
4554
cristy0834d642011-03-18 18:26:08 +00004555 ssize_t
4556 y;
4557
4558 /*
4559 Initialize statistics image attributes.
4560 */
4561 assert(image != (Image *) NULL);
4562 assert(image->signature == MagickSignature);
4563 if (image->debug != MagickFalse)
4564 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4565 assert(exception != (ExceptionInfo *) NULL);
4566 assert(exception->signature == MagickSignature);
cristy0834d642011-03-18 18:26:08 +00004567 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4568 exception);
4569 if (statistic_image == (Image *) NULL)
4570 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004571 if (SetImageStorageClass(statistic_image,DirectClass,exception) == MagickFalse)
cristy0834d642011-03-18 18:26:08 +00004572 {
cristy0834d642011-03-18 18:26:08 +00004573 statistic_image=DestroyImage(statistic_image);
4574 return((Image *) NULL);
4575 }
cristy6fc86bb2011-03-18 23:45:16 +00004576 pixel_list=AcquirePixelListThreadSet(StatisticWidth,StatisticHeight);
cristy0834d642011-03-18 18:26:08 +00004577 if (pixel_list == (PixelList **) NULL)
4578 {
4579 statistic_image=DestroyImage(statistic_image);
4580 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4581 }
4582 /*
cristy8d752042011-03-19 01:00:36 +00004583 Make each pixel the min / max / median / mode / etc. of the neighborhood.
cristy0834d642011-03-18 18:26:08 +00004584 */
4585 status=MagickTrue;
4586 progress=0;
4587 image_view=AcquireCacheView(image);
4588 statistic_view=AcquireCacheView(statistic_image);
4589#if defined(MAGICKCORE_OPENMP_SUPPORT)
4590 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4591#endif
4592 for (y=0; y < (ssize_t) statistic_image->rows; y++)
4593 {
4594 const int
4595 id = GetOpenMPThreadId();
4596
cristy4c08aed2011-07-01 19:47:50 +00004597 register const Quantum
cristy0834d642011-03-18 18:26:08 +00004598 *restrict p;
4599
cristy4c08aed2011-07-01 19:47:50 +00004600 register Quantum
cristy0834d642011-03-18 18:26:08 +00004601 *restrict q;
4602
4603 register ssize_t
4604 x;
4605
4606 if (status == MagickFalse)
4607 continue;
cristy6fc86bb2011-03-18 23:45:16 +00004608 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) StatisticWidth/2L),y-
4609 (ssize_t) (StatisticHeight/2L),image->columns+StatisticWidth,
4610 StatisticHeight,exception);
cristy3cba8ca2011-03-19 01:29:12 +00004611 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004612 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy0834d642011-03-18 18:26:08 +00004613 {
4614 status=MagickFalse;
4615 continue;
4616 }
cristy0834d642011-03-18 18:26:08 +00004617 for (x=0; x < (ssize_t) statistic_image->columns; x++)
4618 {
cristy4c08aed2011-07-01 19:47:50 +00004619 PixelInfo
cristy0834d642011-03-18 18:26:08 +00004620 pixel;
4621
cristy4c08aed2011-07-01 19:47:50 +00004622 register const Quantum
cristy6e3026a2011-03-19 00:54:38 +00004623 *restrict r;
4624
cristy0834d642011-03-18 18:26:08 +00004625 register ssize_t
4626 u,
4627 v;
4628
4629 r=p;
cristy0834d642011-03-18 18:26:08 +00004630 ResetPixelList(pixel_list[id]);
cristy6e4c3292011-03-19 00:53:55 +00004631 for (v=0; v < (ssize_t) StatisticHeight; v++)
cristy0834d642011-03-18 18:26:08 +00004632 {
cristy6e4c3292011-03-19 00:53:55 +00004633 for (u=0; u < (ssize_t) StatisticWidth; u++)
cristyed231572011-07-14 02:18:59 +00004634 InsertPixelList(image,r+u*GetPixelChannels(image),pixel_list[id]);
4635 r+=(image->columns+StatisticWidth)*GetPixelChannels(image);
cristy0834d642011-03-18 18:26:08 +00004636 }
cristy4c08aed2011-07-01 19:47:50 +00004637 GetPixelInfo(image,&pixel);
cristy490408a2011-07-07 14:42:05 +00004638 SetPixelInfo(image,p+(StatisticWidth*StatisticHeight/2)*
cristyed231572011-07-14 02:18:59 +00004639 GetPixelChannels(image),&pixel);
cristy0834d642011-03-18 18:26:08 +00004640 switch (type)
4641 {
cristy80c99742011-04-04 14:46:39 +00004642 case GradientStatistic:
4643 {
cristy4c08aed2011-07-01 19:47:50 +00004644 PixelInfo
cristy80c99742011-04-04 14:46:39 +00004645 maximum,
4646 minimum;
4647
4648 minimum=GetMinimumPixelList(pixel_list[id]);
4649 maximum=GetMaximumPixelList(pixel_list[id]);
4650 pixel.red=MagickAbsoluteValue(maximum.red-minimum.red);
4651 pixel.green=MagickAbsoluteValue(maximum.green-minimum.green);
4652 pixel.blue=MagickAbsoluteValue(maximum.blue-minimum.blue);
cristy4c08aed2011-07-01 19:47:50 +00004653 pixel.alpha=MagickAbsoluteValue(maximum.alpha-minimum.alpha);
cristy80c99742011-04-04 14:46:39 +00004654 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004655 pixel.black=MagickAbsoluteValue(maximum.black-minimum.black);
cristy80c99742011-04-04 14:46:39 +00004656 break;
4657 }
cristy6fc86bb2011-03-18 23:45:16 +00004658 case MaximumStatistic:
4659 {
4660 pixel=GetMaximumPixelList(pixel_list[id]);
4661 break;
4662 }
cristy49f37242011-03-22 18:18:23 +00004663 case MeanStatistic:
4664 {
4665 pixel=GetMeanPixelList(pixel_list[id]);
4666 break;
4667 }
cristyf2ad14a2011-03-18 18:57:25 +00004668 case MedianStatistic:
cristy6fc86bb2011-03-18 23:45:16 +00004669 default:
cristyf2ad14a2011-03-18 18:57:25 +00004670 {
4671 pixel=GetMedianPixelList(pixel_list[id]);
4672 break;
4673 }
cristy6fc86bb2011-03-18 23:45:16 +00004674 case MinimumStatistic:
4675 {
4676 pixel=GetMinimumPixelList(pixel_list[id]);
4677 break;
4678 }
cristyf2ad14a2011-03-18 18:57:25 +00004679 case ModeStatistic:
4680 {
4681 pixel=GetModePixelList(pixel_list[id]);
4682 break;
4683 }
4684 case NonpeakStatistic:
4685 {
4686 pixel=GetNonpeakPixelList(pixel_list[id]);
4687 break;
4688 }
cristy9a68cbb2011-03-29 00:51:23 +00004689 case StandardDeviationStatistic:
4690 {
4691 pixel=GetStandardDeviationPixelList(pixel_list[id]);
4692 break;
4693 }
cristy0834d642011-03-18 18:26:08 +00004694 }
cristyed231572011-07-14 02:18:59 +00004695 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004696 SetPixelRed(statistic_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00004697 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004698 SetPixelGreen(statistic_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00004699 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004700 SetPixelBlue(statistic_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00004701 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy0834d642011-03-18 18:26:08 +00004702 (image->colorspace == CMYKColorspace))
cristy490408a2011-07-07 14:42:05 +00004703 SetPixelBlack(statistic_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +00004704 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00004705 (image->matte != MagickFalse))
cristy490408a2011-07-07 14:42:05 +00004706 SetPixelAlpha(statistic_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00004707 p+=GetPixelChannels(image);
4708 q+=GetPixelChannels(statistic_image);
cristy0834d642011-03-18 18:26:08 +00004709 }
4710 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
4711 status=MagickFalse;
4712 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4713 {
4714 MagickBooleanType
4715 proceed;
4716
4717#if defined(MAGICKCORE_OPENMP_SUPPORT)
4718 #pragma omp critical (MagickCore_StatisticImage)
4719#endif
4720 proceed=SetImageProgress(image,StatisticImageTag,progress++,
4721 image->rows);
4722 if (proceed == MagickFalse)
4723 status=MagickFalse;
4724 }
4725 }
4726 statistic_view=DestroyCacheView(statistic_view);
4727 image_view=DestroyCacheView(image_view);
4728 pixel_list=DestroyPixelListThreadSet(pixel_list);
4729 return(statistic_image);
4730}
4731
4732/*
4733%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4734% %
4735% %
4736% %
cristy3ed852e2009-09-05 21:47:34 +00004737% U n s h a r p M a s k I m a g e %
4738% %
4739% %
4740% %
4741%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4742%
4743% UnsharpMaskImage() sharpens one or more image channels. We convolve the
4744% image with a Gaussian operator of the given radius and standard deviation
4745% (sigma). For reasonable results, radius should be larger than sigma. Use a
4746% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
4747%
4748% The format of the UnsharpMaskImage method is:
4749%
4750% Image *UnsharpMaskImage(const Image *image,const double radius,
4751% const double sigma,const double amount,const double threshold,
4752% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004753%
4754% A description of each parameter follows:
4755%
4756% o image: the image.
4757%
cristy3ed852e2009-09-05 21:47:34 +00004758% o radius: the radius of the Gaussian, in pixels, not counting the center
4759% pixel.
4760%
4761% o sigma: the standard deviation of the Gaussian, in pixels.
4762%
4763% o amount: the percentage of the difference between the original and the
4764% blur image that is added back into the original.
4765%
4766% o threshold: the threshold in pixels needed to apply the diffence amount.
4767%
4768% o exception: return any errors or warnings in this structure.
4769%
4770*/
cristyf4ad9df2011-07-08 16:49:03 +00004771MagickExport Image *UnsharpMaskImage(const Image *image,
4772 const double radius,const double sigma,const double amount,
4773 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004774{
4775#define SharpenImageTag "Sharpen/Image"
4776
cristyc4c8d132010-01-07 01:58:38 +00004777 CacheView
4778 *image_view,
4779 *unsharp_view;
4780
cristy3ed852e2009-09-05 21:47:34 +00004781 Image
4782 *unsharp_image;
4783
cristy3ed852e2009-09-05 21:47:34 +00004784 MagickBooleanType
4785 status;
4786
cristybb503372010-05-27 20:51:26 +00004787 MagickOffsetType
4788 progress;
4789
cristy4c08aed2011-07-01 19:47:50 +00004790 PixelInfo
cristyddd82202009-11-03 20:14:50 +00004791 bias;
cristy3ed852e2009-09-05 21:47:34 +00004792
4793 MagickRealType
4794 quantum_threshold;
4795
cristybb503372010-05-27 20:51:26 +00004796 ssize_t
4797 y;
4798
cristy3ed852e2009-09-05 21:47:34 +00004799 assert(image != (const Image *) NULL);
4800 assert(image->signature == MagickSignature);
4801 if (image->debug != MagickFalse)
4802 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4803 assert(exception != (ExceptionInfo *) NULL);
cristyf4ad9df2011-07-08 16:49:03 +00004804 unsharp_image=BlurImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004805 if (unsharp_image == (Image *) NULL)
4806 return((Image *) NULL);
4807 quantum_threshold=(MagickRealType) QuantumRange*threshold;
4808 /*
4809 Unsharp-mask image.
4810 */
4811 status=MagickTrue;
4812 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00004813 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00004814 image_view=AcquireCacheView(image);
4815 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00004816#if defined(MAGICKCORE_OPENMP_SUPPORT)
4817 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004818#endif
cristybb503372010-05-27 20:51:26 +00004819 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004820 {
cristy4c08aed2011-07-01 19:47:50 +00004821 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004822 pixel;
4823
cristy4c08aed2011-07-01 19:47:50 +00004824 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004825 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004826
cristy4c08aed2011-07-01 19:47:50 +00004827 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004828 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004829
cristy117ff172010-08-15 21:35:32 +00004830 register ssize_t
4831 x;
4832
cristy3ed852e2009-09-05 21:47:34 +00004833 if (status == MagickFalse)
4834 continue;
4835 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4836 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
4837 exception);
cristy4c08aed2011-07-01 19:47:50 +00004838 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004839 {
4840 status=MagickFalse;
4841 continue;
4842 }
cristyddd82202009-11-03 20:14:50 +00004843 pixel=bias;
cristybb503372010-05-27 20:51:26 +00004844 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004845 {
cristyed231572011-07-14 02:18:59 +00004846 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004847 {
cristy4c08aed2011-07-01 19:47:50 +00004848 pixel.red=GetPixelRed(image,p)-(MagickRealType) GetPixelRed(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004849 if (fabs(2.0*pixel.red) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004850 pixel.red=(MagickRealType) GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004851 else
cristy4c08aed2011-07-01 19:47:50 +00004852 pixel.red=(MagickRealType) GetPixelRed(image,p)+(pixel.red*amount);
4853 SetPixelRed(unsharp_image,ClampToQuantum(pixel.red),q);
cristy3ed852e2009-09-05 21:47:34 +00004854 }
cristyed231572011-07-14 02:18:59 +00004855 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004856 {
cristy4c08aed2011-07-01 19:47:50 +00004857 pixel.green=GetPixelGreen(image,p)-
4858 (MagickRealType) GetPixelGreen(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004859 if (fabs(2.0*pixel.green) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004860 pixel.green=(MagickRealType)
4861 GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004862 else
cristy4c08aed2011-07-01 19:47:50 +00004863 pixel.green=(MagickRealType)
4864 GetPixelGreen(image,p)+
4865 (pixel.green*amount);
4866 SetPixelGreen(unsharp_image,
4867 ClampToQuantum(pixel.green),q);
cristy3ed852e2009-09-05 21:47:34 +00004868 }
cristyed231572011-07-14 02:18:59 +00004869 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004870 {
cristy4c08aed2011-07-01 19:47:50 +00004871 pixel.blue=GetPixelBlue(image,p)-
4872 (MagickRealType) GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004873 if (fabs(2.0*pixel.blue) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004874 pixel.blue=(MagickRealType)
4875 GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004876 else
cristy4c08aed2011-07-01 19:47:50 +00004877 pixel.blue=(MagickRealType)
4878 GetPixelBlue(image,p)+(pixel.blue*amount);
4879 SetPixelBlue(unsharp_image,
4880 ClampToQuantum(pixel.blue),q);
cristy3ed852e2009-09-05 21:47:34 +00004881 }
cristyed231572011-07-14 02:18:59 +00004882 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00004883 (image->colorspace == CMYKColorspace))
4884 {
cristy4c08aed2011-07-01 19:47:50 +00004885 pixel.black=GetPixelBlack(image,p)-
4886 (MagickRealType) GetPixelBlack(image,q);
4887 if (fabs(2.0*pixel.black) < quantum_threshold)
4888 pixel.black=(MagickRealType)
4889 GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004890 else
cristy4c08aed2011-07-01 19:47:50 +00004891 pixel.black=(MagickRealType)
4892 GetPixelBlack(image,p)+(pixel.black*
4893 amount);
4894 SetPixelBlack(unsharp_image,
4895 ClampToQuantum(pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00004896 }
cristyed231572011-07-14 02:18:59 +00004897 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004898 {
4899 pixel.alpha=GetPixelAlpha(image,p)-
4900 (MagickRealType) GetPixelAlpha(image,q);
4901 if (fabs(2.0*pixel.alpha) < quantum_threshold)
4902 pixel.alpha=(MagickRealType)
4903 GetPixelAlpha(image,p);
4904 else
4905 pixel.alpha=GetPixelAlpha(image,p)+
4906 (pixel.alpha*amount);
4907 SetPixelAlpha(unsharp_image,
4908 ClampToQuantum(pixel.alpha),q);
4909 }
cristyed231572011-07-14 02:18:59 +00004910 p+=GetPixelChannels(image);
4911 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00004912 }
4913 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
4914 status=MagickFalse;
4915 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4916 {
4917 MagickBooleanType
4918 proceed;
4919
cristyb5d5f722009-11-04 03:03:49 +00004920#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00004921 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00004922#endif
4923 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4924 if (proceed == MagickFalse)
4925 status=MagickFalse;
4926 }
4927 }
4928 unsharp_image->type=image->type;
4929 unsharp_view=DestroyCacheView(unsharp_view);
4930 image_view=DestroyCacheView(image_view);
4931 if (status == MagickFalse)
4932 unsharp_image=DestroyImage(unsharp_image);
4933 return(unsharp_image);
4934}