blob: e2366b0e6a6f000d27a3eae269fab3ec2fe56a53 [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,
124 const char *levels)
125{
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)
cristy01e9afd2011-08-10 17:38:41 +0000161 status=LevelImage(image,black_point,white_point,gamma,&image->exception);
cristyf89cb1d2011-07-07 01:24:37 +0000162 else
163 status=LevelizeImage(image,black_point,white_point,gamma);
164 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 }
cristyf89cb1d2011-07-07 01:24:37 +0000234 (void) AdaptiveLevelImage(edge_image,"20%,95%");
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 }
cristyf89cb1d2011-07-07 01:24:37 +0000241 (void) AdaptiveLevelImage(edge_image,"10%,95%");
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);
cristy4c08aed2011-07-01 19:47:50 +0000318 if ((r == (const Quantum *) NULL) || (q == (const 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 }
cristyf89cb1d2011-07-07 01:24:37 +0000525 (void) AdaptiveLevelImage(edge_image,"20%,95%");
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 }
cristyf89cb1d2011-07-07 01:24:37 +0000532 (void) AdaptiveLevelImage(edge_image,"10%,95%");
cristy3ed852e2009-09-05 21:47:34 +0000533 /*
534 Create a set of kernels from maximum (radius,sigma) to minimum.
535 */
536 width=GetOptimalKernelWidth2D(radius,sigma);
537 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
538 if (kernel == (double **) NULL)
539 {
540 edge_image=DestroyImage(edge_image);
541 sharp_image=DestroyImage(sharp_image);
542 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
543 }
544 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000545 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000546 {
547 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
548 sizeof(**kernel));
549 if (kernel[i] == (double *) NULL)
550 break;
cristy47e00502009-12-17 19:19:57 +0000551 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000552 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000553 k=0;
554 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000555 {
cristy47e00502009-12-17 19:19:57 +0000556 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000557 {
cristy4205a3c2010-09-12 20:19:59 +0000558 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
559 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000560 normalize+=kernel[i][k];
561 k++;
cristy3ed852e2009-09-05 21:47:34 +0000562 }
563 }
cristy3ed852e2009-09-05 21:47:34 +0000564 if (fabs(normalize) <= MagickEpsilon)
565 normalize=1.0;
566 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000567 for (k=0; k < (j*j); k++)
568 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000569 }
cristybb503372010-05-27 20:51:26 +0000570 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000571 {
572 for (i-=2; i >= 0; i-=2)
573 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
574 kernel=(double **) RelinquishMagickMemory(kernel);
575 edge_image=DestroyImage(edge_image);
576 sharp_image=DestroyImage(sharp_image);
577 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
578 }
579 /*
580 Adaptively sharpen image.
581 */
582 status=MagickTrue;
583 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000584 GetPixelInfo(image,&bias);
585 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000586 image_view=AcquireCacheView(image);
587 edge_view=AcquireCacheView(edge_image);
588 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000589#if defined(MAGICKCORE_OPENMP_SUPPORT)
590 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000591#endif
cristybb503372010-05-27 20:51:26 +0000592 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000593 {
cristy4c08aed2011-07-01 19:47:50 +0000594 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000595 *restrict p,
596 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000597
cristy4c08aed2011-07-01 19:47:50 +0000598 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000599 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000600
cristy117ff172010-08-15 21:35:32 +0000601 register ssize_t
602 x;
603
cristy3ed852e2009-09-05 21:47:34 +0000604 if (status == MagickFalse)
605 continue;
606 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
607 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
608 exception);
cristy4c08aed2011-07-01 19:47:50 +0000609 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000610 {
611 status=MagickFalse;
612 continue;
613 }
cristybb503372010-05-27 20:51:26 +0000614 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000615 {
cristy4c08aed2011-07-01 19:47:50 +0000616 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000617 pixel;
618
619 MagickRealType
620 alpha,
621 gamma;
622
623 register const double
cristyc47d1f82009-11-26 01:44:43 +0000624 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000625
cristybb503372010-05-27 20:51:26 +0000626 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000627 i,
628 u,
629 v;
630
631 gamma=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000632 i=(ssize_t) ceil((double) width*QuantumScale*
633 GetPixelIntensity(edge_image,r)-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000634 if (i < 0)
635 i=0;
636 else
cristybb503372010-05-27 20:51:26 +0000637 if (i > (ssize_t) width)
638 i=(ssize_t) width;
cristy3ed852e2009-09-05 21:47:34 +0000639 if ((i & 0x01) != 0)
640 i--;
cristy117ff172010-08-15 21:35:32 +0000641 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
642 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
cristy4c08aed2011-07-01 19:47:50 +0000643 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000644 break;
cristy3ed852e2009-09-05 21:47:34 +0000645 k=kernel[i];
cristyddd82202009-11-03 20:14:50 +0000646 pixel=bias;
cristybb503372010-05-27 20:51:26 +0000647 for (v=0; v < (ssize_t) (width-i); v++)
cristy3ed852e2009-09-05 21:47:34 +0000648 {
cristybb503372010-05-27 20:51:26 +0000649 for (u=0; u < (ssize_t) (width-i); u++)
cristy3ed852e2009-09-05 21:47:34 +0000650 {
651 alpha=1.0;
cristyed231572011-07-14 02:18:59 +0000652 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000653 (image->matte != MagickFalse))
cristy4c08aed2011-07-01 19:47:50 +0000654 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +0000655 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000656 pixel.red+=(*k)*alpha*GetPixelRed(image,p);
cristyed231572011-07-14 02:18:59 +0000657 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000658 pixel.green+=(*k)*alpha*GetPixelGreen(image,p);
cristyed231572011-07-14 02:18:59 +0000659 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000660 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p);
cristyed231572011-07-14 02:18:59 +0000661 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000662 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000663 pixel.black+=(*k)*alpha*GetPixelBlack(image,p);
cristyed231572011-07-14 02:18:59 +0000664 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000665 pixel.alpha+=(*k)*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000666 gamma+=(*k)*alpha;
667 k++;
cristyed231572011-07-14 02:18:59 +0000668 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000669 }
670 }
671 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +0000672 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000673 SetPixelRed(sharp_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000674 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000675 SetPixelGreen(sharp_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000676 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000677 SetPixelBlue(sharp_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000678 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000679 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000680 SetPixelBlack(sharp_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000681 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000682 SetPixelAlpha(sharp_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +0000683 q+=GetPixelChannels(sharp_image);
684 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000685 }
686 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
687 status=MagickFalse;
688 if (image->progress_monitor != (MagickProgressMonitor) NULL)
689 {
690 MagickBooleanType
691 proceed;
692
cristyb5d5f722009-11-04 03:03:49 +0000693#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +0000694 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000695#endif
696 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
697 image->rows);
698 if (proceed == MagickFalse)
699 status=MagickFalse;
700 }
701 }
702 sharp_image->type=image->type;
703 sharp_view=DestroyCacheView(sharp_view);
704 edge_view=DestroyCacheView(edge_view);
705 image_view=DestroyCacheView(image_view);
706 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000707 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000708 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
709 kernel=(double **) RelinquishMagickMemory(kernel);
710 if (status == MagickFalse)
711 sharp_image=DestroyImage(sharp_image);
712 return(sharp_image);
713}
714
715/*
716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717% %
718% %
719% %
720% B l u r I m a g e %
721% %
722% %
723% %
724%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725%
726% BlurImage() blurs an image. We convolve the image with a Gaussian operator
727% of the given radius and standard deviation (sigma). For reasonable results,
728% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
729% selects a suitable radius for you.
730%
731% BlurImage() differs from GaussianBlurImage() in that it uses a separable
732% kernel which is faster but mathematically equivalent to the non-separable
733% kernel.
734%
735% The format of the BlurImage method is:
736%
737% Image *BlurImage(const Image *image,const double radius,
738% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000739%
740% A description of each parameter follows:
741%
742% o image: the image.
743%
cristy3ed852e2009-09-05 21:47:34 +0000744% o radius: the radius of the Gaussian, in pixels, not counting the center
745% pixel.
746%
747% o sigma: the standard deviation of the Gaussian, in pixels.
748%
749% o exception: return any errors or warnings in this structure.
750%
751*/
752
cristybb503372010-05-27 20:51:26 +0000753static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000754{
cristy3ed852e2009-09-05 21:47:34 +0000755 double
cristy47e00502009-12-17 19:19:57 +0000756 *kernel,
757 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000758
cristy117ff172010-08-15 21:35:32 +0000759 register ssize_t
760 i;
761
cristybb503372010-05-27 20:51:26 +0000762 ssize_t
cristy47e00502009-12-17 19:19:57 +0000763 j,
764 k;
cristy3ed852e2009-09-05 21:47:34 +0000765
cristy3ed852e2009-09-05 21:47:34 +0000766 /*
767 Generate a 1-D convolution kernel.
768 */
769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
770 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
771 if (kernel == (double *) NULL)
772 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000773 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000774 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000775 i=0;
776 for (k=(-j); k <= j; k++)
777 {
cristy4205a3c2010-09-12 20:19:59 +0000778 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
779 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000780 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000781 i++;
782 }
cristybb503372010-05-27 20:51:26 +0000783 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000784 kernel[i]/=normalize;
785 return(kernel);
786}
787
cristyf4ad9df2011-07-08 16:49:03 +0000788MagickExport Image *BlurImage(const Image *image,const double radius,
789 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000790{
791#define BlurImageTag "Blur/Image"
792
cristyc4c8d132010-01-07 01:58:38 +0000793 CacheView
794 *blur_view,
795 *image_view;
796
cristy3ed852e2009-09-05 21:47:34 +0000797 double
798 *kernel;
799
800 Image
801 *blur_image;
802
cristy3ed852e2009-09-05 21:47:34 +0000803 MagickBooleanType
804 status;
805
cristybb503372010-05-27 20:51:26 +0000806 MagickOffsetType
807 progress;
808
cristy4c08aed2011-07-01 19:47:50 +0000809 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000810 bias;
811
cristybb503372010-05-27 20:51:26 +0000812 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000813 i;
814
cristybb503372010-05-27 20:51:26 +0000815 size_t
cristy3ed852e2009-09-05 21:47:34 +0000816 width;
817
cristybb503372010-05-27 20:51:26 +0000818 ssize_t
819 x,
820 y;
821
cristy3ed852e2009-09-05 21:47:34 +0000822 /*
823 Initialize blur image attributes.
824 */
825 assert(image != (Image *) NULL);
826 assert(image->signature == MagickSignature);
827 if (image->debug != MagickFalse)
828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
829 assert(exception != (ExceptionInfo *) NULL);
830 assert(exception->signature == MagickSignature);
831 blur_image=CloneImage(image,0,0,MagickTrue,exception);
832 if (blur_image == (Image *) NULL)
833 return((Image *) NULL);
834 if (fabs(sigma) <= MagickEpsilon)
835 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000836 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000837 {
cristy3ed852e2009-09-05 21:47:34 +0000838 blur_image=DestroyImage(blur_image);
839 return((Image *) NULL);
840 }
841 width=GetOptimalKernelWidth1D(radius,sigma);
842 kernel=GetBlurKernel(width,sigma);
843 if (kernel == (double *) NULL)
844 {
845 blur_image=DestroyImage(blur_image);
846 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
847 }
848 if (image->debug != MagickFalse)
849 {
850 char
851 format[MaxTextExtent],
852 *message;
853
854 register const double
855 *k;
856
857 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000858 " BlurImage with %.20g kernel:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000859 message=AcquireString("");
860 k=kernel;
cristybb503372010-05-27 20:51:26 +0000861 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000862 {
863 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000864 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000865 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000866 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000867 (void) ConcatenateString(&message,format);
868 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
869 }
870 message=DestroyString(message);
871 }
872 /*
873 Blur rows.
874 */
875 status=MagickTrue;
876 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000877 GetPixelInfo(image,&bias);
878 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000879 image_view=AcquireCacheView(image);
880 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000881#if defined(MAGICKCORE_OPENMP_SUPPORT)
882 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000883#endif
cristybb503372010-05-27 20:51:26 +0000884 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000885 {
cristy4c08aed2011-07-01 19:47:50 +0000886 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000887 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000888
cristy4c08aed2011-07-01 19:47:50 +0000889 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000890 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000891
cristy117ff172010-08-15 21:35:32 +0000892 register ssize_t
893 x;
894
cristy3ed852e2009-09-05 21:47:34 +0000895 if (status == MagickFalse)
896 continue;
cristy117ff172010-08-15 21:35:32 +0000897 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
898 image->columns+width,1,exception);
cristy3ed852e2009-09-05 21:47:34 +0000899 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
900 exception);
cristy4c08aed2011-07-01 19:47:50 +0000901 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000902 {
903 status=MagickFalse;
904 continue;
905 }
cristybb503372010-05-27 20:51:26 +0000906 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000907 {
cristy4c08aed2011-07-01 19:47:50 +0000908 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000909 pixel;
910
911 register const double
cristyc47d1f82009-11-26 01:44:43 +0000912 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000913
cristy4c08aed2011-07-01 19:47:50 +0000914 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000915 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000916
cristybb503372010-05-27 20:51:26 +0000917 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000918 i;
919
cristyddd82202009-11-03 20:14:50 +0000920 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000921 k=kernel;
922 kernel_pixels=p;
cristyed231572011-07-14 02:18:59 +0000923 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
cristyf4ad9df2011-07-08 16:49:03 +0000924 (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000925 {
cristybb503372010-05-27 20:51:26 +0000926 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000927 {
cristy4c08aed2011-07-01 19:47:50 +0000928 pixel.red+=(*k)*GetPixelRed(image,kernel_pixels);
929 pixel.green+=(*k)*GetPixelGreen(image,kernel_pixels);
930 pixel.blue+=(*k)*GetPixelBlue(image,kernel_pixels);
931 if (image->colorspace == CMYKColorspace)
932 pixel.black+=(*k)*GetPixelBlack(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000933 k++;
cristyed231572011-07-14 02:18:59 +0000934 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000935 }
cristyed231572011-07-14 02:18:59 +0000936 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000937 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000938 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000939 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000940 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000941 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000942 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +0000943 (blur_image->colorspace == CMYKColorspace))
944 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000945 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000946 {
947 k=kernel;
948 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +0000949 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000950 {
cristy4c08aed2011-07-01 19:47:50 +0000951 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000952 k++;
cristyed231572011-07-14 02:18:59 +0000953 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000954 }
cristy4c08aed2011-07-01 19:47:50 +0000955 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000956 }
957 }
958 else
959 {
960 MagickRealType
961 alpha,
962 gamma;
963
964 gamma=0.0;
cristybb503372010-05-27 20:51:26 +0000965 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000966 {
cristy4c08aed2011-07-01 19:47:50 +0000967 alpha=(MagickRealType) (QuantumScale*
968 GetPixelAlpha(image,kernel_pixels));
969 pixel.red+=(*k)*alpha*GetPixelRed(image,kernel_pixels);
970 pixel.green+=(*k)*alpha*GetPixelGreen(image,kernel_pixels);
971 pixel.blue+=(*k)*alpha*GetPixelBlue(image,kernel_pixels);
972 if (image->colorspace == CMYKColorspace)
973 pixel.black+=(*k)*alpha*GetPixelBlack(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000974 gamma+=(*k)*alpha;
975 k++;
cristyed231572011-07-14 02:18:59 +0000976 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000977 }
978 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +0000979 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000980 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000981 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000982 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000983 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000984 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000985 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +0000986 (blur_image->colorspace == CMYKColorspace))
987 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000988 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000989 {
990 k=kernel;
991 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +0000992 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000993 {
cristy4c08aed2011-07-01 19:47:50 +0000994 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000995 k++;
cristyed231572011-07-14 02:18:59 +0000996 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000997 }
cristy4c08aed2011-07-01 19:47:50 +0000998 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000999 }
1000 }
cristyed231572011-07-14 02:18:59 +00001001 p+=GetPixelChannels(image);
1002 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001003 }
1004 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1005 status=MagickFalse;
1006 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1007 {
1008 MagickBooleanType
1009 proceed;
1010
cristyb5d5f722009-11-04 03:03:49 +00001011#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001012 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001013#endif
1014 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1015 blur_image->columns);
1016 if (proceed == MagickFalse)
1017 status=MagickFalse;
1018 }
1019 }
1020 blur_view=DestroyCacheView(blur_view);
1021 image_view=DestroyCacheView(image_view);
1022 /*
1023 Blur columns.
1024 */
1025 image_view=AcquireCacheView(blur_image);
1026 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001027#if defined(MAGICKCORE_OPENMP_SUPPORT)
1028 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001029#endif
cristybb503372010-05-27 20:51:26 +00001030 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001031 {
cristy4c08aed2011-07-01 19:47:50 +00001032 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001033 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001034
cristy4c08aed2011-07-01 19:47:50 +00001035 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001036 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001037
cristy117ff172010-08-15 21:35:32 +00001038 register ssize_t
1039 y;
1040
cristy3ed852e2009-09-05 21:47:34 +00001041 if (status == MagickFalse)
1042 continue;
cristy117ff172010-08-15 21:35:32 +00001043 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1044 image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001045 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001046 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001047 {
1048 status=MagickFalse;
1049 continue;
1050 }
cristybb503372010-05-27 20:51:26 +00001051 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001052 {
cristy4c08aed2011-07-01 19:47:50 +00001053 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001054 pixel;
1055
1056 register const double
cristyc47d1f82009-11-26 01:44:43 +00001057 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00001058
cristy4c08aed2011-07-01 19:47:50 +00001059 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001060 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001061
cristybb503372010-05-27 20:51:26 +00001062 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001063 i;
1064
cristyddd82202009-11-03 20:14:50 +00001065 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00001066 k=kernel;
1067 kernel_pixels=p;
cristyed231572011-07-14 02:18:59 +00001068 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
cristyf4ad9df2011-07-08 16:49:03 +00001069 (blur_image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001070 {
cristybb503372010-05-27 20:51:26 +00001071 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001072 {
cristy4c08aed2011-07-01 19:47:50 +00001073 pixel.red+=(*k)*GetPixelRed(blur_image,kernel_pixels);
1074 pixel.green+=(*k)*GetPixelGreen(blur_image,kernel_pixels);
1075 pixel.blue+=(*k)*GetPixelBlue(blur_image,kernel_pixels);
1076 if (blur_image->colorspace == CMYKColorspace)
1077 pixel.black+=(*k)*GetPixelBlack(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001078 k++;
cristyed231572011-07-14 02:18:59 +00001079 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001080 }
cristyed231572011-07-14 02:18:59 +00001081 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001082 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00001083 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001084 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00001085 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001086 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00001087 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00001088 (blur_image->colorspace == CMYKColorspace))
1089 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +00001090 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001091 {
1092 k=kernel;
1093 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001094 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001095 {
cristy4c08aed2011-07-01 19:47:50 +00001096 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001097 k++;
cristyed231572011-07-14 02:18:59 +00001098 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001099 }
cristy4c08aed2011-07-01 19:47:50 +00001100 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001101 }
1102 }
1103 else
1104 {
1105 MagickRealType
1106 alpha,
1107 gamma;
1108
1109 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001110 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001111 {
cristy46f08202010-01-10 04:04:21 +00001112 alpha=(MagickRealType) (QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +00001113 GetPixelAlpha(blur_image,kernel_pixels));
1114 pixel.red+=(*k)*alpha*GetPixelRed(blur_image,kernel_pixels);
1115 pixel.green+=(*k)*alpha*GetPixelGreen(blur_image,kernel_pixels);
1116 pixel.blue+=(*k)*alpha*GetPixelBlue(blur_image,kernel_pixels);
1117 if (blur_image->colorspace == CMYKColorspace)
1118 pixel.black+=(*k)*alpha*GetPixelBlack(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001119 gamma+=(*k)*alpha;
1120 k++;
cristyed231572011-07-14 02:18:59 +00001121 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001122 }
1123 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00001124 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001125 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00001126 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001127 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00001128 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001129 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00001130 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00001131 (blur_image->colorspace == CMYKColorspace))
1132 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +00001133 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001134 {
1135 k=kernel;
1136 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001137 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001138 {
cristy4c08aed2011-07-01 19:47:50 +00001139 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001140 k++;
cristyed231572011-07-14 02:18:59 +00001141 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001142 }
cristy4c08aed2011-07-01 19:47:50 +00001143 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001144 }
1145 }
cristyed231572011-07-14 02:18:59 +00001146 p+=GetPixelChannels(blur_image);
1147 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001148 }
1149 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1150 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001151 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001152 {
1153 MagickBooleanType
1154 proceed;
1155
cristyb5d5f722009-11-04 03:03:49 +00001156#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001157 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001158#endif
cristy4c08aed2011-07-01 19:47:50 +00001159 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1160 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001161 if (proceed == MagickFalse)
1162 status=MagickFalse;
1163 }
1164 }
1165 blur_view=DestroyCacheView(blur_view);
1166 image_view=DestroyCacheView(image_view);
1167 kernel=(double *) RelinquishMagickMemory(kernel);
1168 if (status == MagickFalse)
1169 blur_image=DestroyImage(blur_image);
1170 blur_image->type=image->type;
1171 return(blur_image);
1172}
1173
1174/*
1175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1176% %
1177% %
1178% %
cristyfccdab92009-11-30 16:43:57 +00001179% C o n v o l v e I m a g e %
1180% %
1181% %
1182% %
1183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1184%
1185% ConvolveImage() applies a custom convolution kernel to the image.
1186%
1187% The format of the ConvolveImage method is:
1188%
cristy5e6be1e2011-07-16 01:23:39 +00001189% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1190% ExceptionInfo *exception)
1191%
cristyfccdab92009-11-30 16:43:57 +00001192% A description of each parameter follows:
1193%
1194% o image: the image.
1195%
cristy5e6be1e2011-07-16 01:23:39 +00001196% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001197%
1198% o exception: return any errors or warnings in this structure.
1199%
1200*/
cristy5e6be1e2011-07-16 01:23:39 +00001201MagickExport Image *ConvolveImage(const Image *image,
1202 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001203{
cristyfccdab92009-11-30 16:43:57 +00001204#define ConvolveImageTag "Convolve/Image"
1205
cristyc4c8d132010-01-07 01:58:38 +00001206 CacheView
1207 *convolve_view,
cristy105ba3c2011-07-18 02:28:38 +00001208 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00001209
cristyfccdab92009-11-30 16:43:57 +00001210 Image
1211 *convolve_image;
1212
cristyfccdab92009-11-30 16:43:57 +00001213 MagickBooleanType
1214 status;
1215
cristybb503372010-05-27 20:51:26 +00001216 MagickOffsetType
1217 progress;
1218
cristybb503372010-05-27 20:51:26 +00001219 ssize_t
cristy574cc262011-08-05 01:23:58 +00001220 center,
cristybb503372010-05-27 20:51:26 +00001221 y;
1222
cristyfccdab92009-11-30 16:43:57 +00001223 /*
1224 Initialize convolve image attributes.
1225 */
1226 assert(image != (Image *) NULL);
1227 assert(image->signature == MagickSignature);
1228 if (image->debug != MagickFalse)
1229 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1230 assert(exception != (ExceptionInfo *) NULL);
1231 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001232 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001233 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001234 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1235 exception);
cristyfccdab92009-11-30 16:43:57 +00001236 if (convolve_image == (Image *) NULL)
1237 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001238 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001239 {
cristyfccdab92009-11-30 16:43:57 +00001240 convolve_image=DestroyImage(convolve_image);
1241 return((Image *) NULL);
1242 }
1243 if (image->debug != MagickFalse)
1244 {
1245 char
1246 format[MaxTextExtent],
1247 *message;
1248
cristy117ff172010-08-15 21:35:32 +00001249 register const double
1250 *k;
1251
cristy4e154852011-07-14 13:28:53 +00001252 register ssize_t
1253 u;
1254
cristybb503372010-05-27 20:51:26 +00001255 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001256 v;
1257
cristyfccdab92009-11-30 16:43:57 +00001258 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001259 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1260 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001261 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001262 k=kernel_info->values;
1263 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001264 {
1265 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001266 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001267 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001268 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001269 {
cristyb51dff52011-05-19 16:55:47 +00001270 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001271 (void) ConcatenateString(&message,format);
1272 }
1273 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1274 }
1275 message=DestroyString(message);
1276 }
1277 /*
cristyfccdab92009-11-30 16:43:57 +00001278 Convolve image.
1279 */
cristy574cc262011-08-05 01:23:58 +00001280 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
1281 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2);
cristyfccdab92009-11-30 16:43:57 +00001282 status=MagickTrue;
1283 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001284 image_view=AcquireCacheView(image);
1285 convolve_view=AcquireCacheView(convolve_image);
1286#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy175653e2011-07-10 23:13:34 +00001287 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001288#endif
cristybb503372010-05-27 20:51:26 +00001289 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001290 {
cristy4c08aed2011-07-01 19:47:50 +00001291 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001292 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001293
cristy4c08aed2011-07-01 19:47:50 +00001294 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001295 *restrict q;
1296
cristy117ff172010-08-15 21:35:32 +00001297 register ssize_t
1298 x;
1299
cristyfccdab92009-11-30 16:43:57 +00001300 if (status == MagickFalse)
1301 continue;
cristy105ba3c2011-07-18 02:28:38 +00001302 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1303 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1304 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001305 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001306 exception);
cristy105ba3c2011-07-18 02:28:38 +00001307 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001308 {
1309 status=MagickFalse;
1310 continue;
1311 }
cristybb503372010-05-27 20:51:26 +00001312 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001313 {
cristybb503372010-05-27 20:51:26 +00001314 register ssize_t
cristyed231572011-07-14 02:18:59 +00001315 i;
cristyfccdab92009-11-30 16:43:57 +00001316
cristya30d9ba2011-07-23 21:00:48 +00001317 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001318 {
cristyed231572011-07-14 02:18:59 +00001319 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001320 alpha,
1321 gamma,
cristyed231572011-07-14 02:18:59 +00001322 pixel;
1323
1324 PixelChannel
1325 channel;
1326
1327 PixelTrait
1328 convolve_traits,
1329 traits;
1330
1331 register const double
1332 *restrict k;
1333
1334 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001335 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001336
1337 register ssize_t
1338 u;
1339
1340 ssize_t
1341 v;
1342
cristy30301712011-07-18 15:06:51 +00001343 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristy4e154852011-07-14 13:28:53 +00001344 if (traits == UndefinedPixelTrait)
cristyed231572011-07-14 02:18:59 +00001345 continue;
cristy30301712011-07-18 15:06:51 +00001346 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
cristy4e154852011-07-14 13:28:53 +00001347 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
1348 if (convolve_traits == UndefinedPixelTrait)
1349 continue;
1350 if ((convolve_traits & CopyPixelTrait) != 0)
1351 {
cristyf7dc44c2011-07-20 14:41:15 +00001352 q[channel]=p[center+i];
cristy4e154852011-07-14 13:28:53 +00001353 continue;
1354 }
cristy5e6be1e2011-07-16 01:23:39 +00001355 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001356 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001357 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001358 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001359 {
cristyed231572011-07-14 02:18:59 +00001360 /*
cristy4e154852011-07-14 13:28:53 +00001361 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001362 */
cristyeb52cde2011-07-17 01:52:52 +00001363 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001364 {
cristyeb52cde2011-07-17 01:52:52 +00001365 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001366 {
cristyeb52cde2011-07-17 01:52:52 +00001367 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001368 k++;
cristya30d9ba2011-07-23 21:00:48 +00001369 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001370 }
cristya30d9ba2011-07-23 21:00:48 +00001371 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001372 }
cristyf7dc44c2011-07-20 14:41:15 +00001373 q[channel]=ClampToQuantum(pixel);
cristy4e154852011-07-14 13:28:53 +00001374 continue;
cristyed231572011-07-14 02:18:59 +00001375 }
cristy4e154852011-07-14 13:28:53 +00001376 /*
1377 Alpha blending.
1378 */
1379 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001380 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001381 {
cristyeb52cde2011-07-17 01:52:52 +00001382 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001383 {
cristyeb52cde2011-07-17 01:52:52 +00001384 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1385 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001386 gamma+=(*k)*alpha;
1387 k++;
cristya30d9ba2011-07-23 21:00:48 +00001388 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001389 }
cristya30d9ba2011-07-23 21:00:48 +00001390 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001391 }
cristy1ce96d02011-07-14 17:57:24 +00001392 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristye7a41c92011-07-20 21:31:01 +00001393 q[channel]=ClampToQuantum(gamma*pixel);
cristyed231572011-07-14 02:18:59 +00001394 }
cristya30d9ba2011-07-23 21:00:48 +00001395 p+=GetPixelChannels(image);
1396 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001397 }
cristyed231572011-07-14 02:18:59 +00001398 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001399 status=MagickFalse;
1400 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1401 {
1402 MagickBooleanType
1403 proceed;
1404
1405#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001406 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001407#endif
1408 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1409 if (proceed == MagickFalse)
1410 status=MagickFalse;
1411 }
1412 }
1413 convolve_image->type=image->type;
1414 convolve_view=DestroyCacheView(convolve_view);
1415 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001416 if (status == MagickFalse)
1417 convolve_image=DestroyImage(convolve_image);
1418 return(convolve_image);
1419}
1420
1421/*
1422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1423% %
1424% %
1425% %
cristy3ed852e2009-09-05 21:47:34 +00001426% D e s p e c k l e I m a g e %
1427% %
1428% %
1429% %
1430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431%
1432% DespeckleImage() reduces the speckle noise in an image while perserving the
1433% edges of the original image.
1434%
1435% The format of the DespeckleImage method is:
1436%
1437% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1438%
1439% A description of each parameter follows:
1440%
1441% o image: the image.
1442%
1443% o exception: return any errors or warnings in this structure.
1444%
1445*/
1446
cristybb503372010-05-27 20:51:26 +00001447static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1448 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
cristy3ed852e2009-09-05 21:47:34 +00001449 const int polarity)
1450{
cristy3ed852e2009-09-05 21:47:34 +00001451 MagickRealType
1452 v;
1453
cristy3ed852e2009-09-05 21:47:34 +00001454 register Quantum
1455 *p,
1456 *q,
1457 *r,
1458 *s;
1459
cristy117ff172010-08-15 21:35:32 +00001460 register ssize_t
1461 x;
1462
1463 ssize_t
1464 y;
1465
cristy3ed852e2009-09-05 21:47:34 +00001466 assert(f != (Quantum *) NULL);
1467 assert(g != (Quantum *) NULL);
1468 p=f+(columns+2);
1469 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001470 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1471 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001472 {
1473 p++;
1474 q++;
1475 r++;
1476 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001477 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001478 {
1479 v=(MagickRealType) (*p);
1480 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1481 v+=ScaleCharToQuantum(1);
1482 *q=(Quantum) v;
1483 p++;
1484 q++;
1485 r++;
1486 }
1487 else
cristybb503372010-05-27 20:51:26 +00001488 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001489 {
1490 v=(MagickRealType) (*p);
1491 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
cristybb503372010-05-27 20:51:26 +00001492 v-=(ssize_t) ScaleCharToQuantum(1);
cristy3ed852e2009-09-05 21:47:34 +00001493 *q=(Quantum) v;
1494 p++;
1495 q++;
1496 r++;
1497 }
1498 p++;
1499 q++;
1500 r++;
1501 }
1502 p=f+(columns+2);
1503 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001504 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1505 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1506 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001507 {
1508 p++;
1509 q++;
1510 r++;
1511 s++;
1512 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001513 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001514 {
1515 v=(MagickRealType) (*q);
1516 if (((MagickRealType) *s >=
1517 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1518 ((MagickRealType) *r > v))
1519 v+=ScaleCharToQuantum(1);
1520 *p=(Quantum) v;
1521 p++;
1522 q++;
1523 r++;
1524 s++;
1525 }
1526 else
cristybb503372010-05-27 20:51:26 +00001527 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001528 {
1529 v=(MagickRealType) (*q);
1530 if (((MagickRealType) *s <=
1531 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1532 ((MagickRealType) *r < v))
1533 v-=(MagickRealType) ScaleCharToQuantum(1);
1534 *p=(Quantum) v;
1535 p++;
1536 q++;
1537 r++;
1538 s++;
1539 }
1540 p++;
1541 q++;
1542 r++;
1543 s++;
1544 }
1545}
1546
1547MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1548{
1549#define DespeckleImageTag "Despeckle/Image"
1550
cristy2407fc22009-09-11 00:55:25 +00001551 CacheView
1552 *despeckle_view,
1553 *image_view;
1554
cristy3ed852e2009-09-05 21:47:34 +00001555 Image
1556 *despeckle_image;
1557
cristy3ed852e2009-09-05 21:47:34 +00001558 MagickBooleanType
1559 status;
1560
cristya58c3172011-02-19 19:23:11 +00001561 register ssize_t
1562 i;
1563
cristy3ed852e2009-09-05 21:47:34 +00001564 Quantum
cristy65b9f392011-02-22 14:22:54 +00001565 *restrict buffers,
1566 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +00001567
1568 size_t
cristya58c3172011-02-19 19:23:11 +00001569 length,
1570 number_channels;
cristy117ff172010-08-15 21:35:32 +00001571
cristybb503372010-05-27 20:51:26 +00001572 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001573 X[4] = {0, 1, 1,-1},
1574 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001575
cristy3ed852e2009-09-05 21:47:34 +00001576 /*
1577 Allocate despeckled image.
1578 */
1579 assert(image != (const Image *) NULL);
1580 assert(image->signature == MagickSignature);
1581 if (image->debug != MagickFalse)
1582 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1583 assert(exception != (ExceptionInfo *) NULL);
1584 assert(exception->signature == MagickSignature);
1585 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1586 exception);
1587 if (despeckle_image == (Image *) NULL)
1588 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001589 if (SetImageStorageClass(despeckle_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001590 {
cristy3ed852e2009-09-05 21:47:34 +00001591 despeckle_image=DestroyImage(despeckle_image);
1592 return((Image *) NULL);
1593 }
1594 /*
1595 Allocate image buffers.
1596 */
1597 length=(size_t) ((image->columns+2)*(image->rows+2));
cristy65b9f392011-02-22 14:22:54 +00001598 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1599 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1600 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001601 {
cristy65b9f392011-02-22 14:22:54 +00001602 if (buffers != (Quantum *) NULL)
1603 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1604 if (pixels != (Quantum *) NULL)
1605 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001606 despeckle_image=DestroyImage(despeckle_image);
1607 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1608 }
1609 /*
1610 Reduce speckle in the image.
1611 */
1612 status=MagickTrue;
cristy109695a2011-02-19 19:38:14 +00001613 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
cristy3ed852e2009-09-05 21:47:34 +00001614 image_view=AcquireCacheView(image);
1615 despeckle_view=AcquireCacheView(despeckle_image);
cristy8df3d002011-02-19 19:40:59 +00001616 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001617 {
cristy3ed852e2009-09-05 21:47:34 +00001618 register Quantum
1619 *buffer,
1620 *pixel;
1621
cristyc1488b52011-02-19 18:54:15 +00001622 register ssize_t
cristya58c3172011-02-19 19:23:11 +00001623 k,
cristyc1488b52011-02-19 18:54:15 +00001624 x;
1625
cristy117ff172010-08-15 21:35:32 +00001626 ssize_t
1627 j,
1628 y;
1629
cristy3ed852e2009-09-05 21:47:34 +00001630 if (status == MagickFalse)
1631 continue;
cristy65b9f392011-02-22 14:22:54 +00001632 pixel=pixels;
cristy3ed852e2009-09-05 21:47:34 +00001633 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
cristy65b9f392011-02-22 14:22:54 +00001634 buffer=buffers;
cristybb503372010-05-27 20:51:26 +00001635 j=(ssize_t) image->columns+2;
1636 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001637 {
cristy4c08aed2011-07-01 19:47:50 +00001638 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001639 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001640
1641 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001642 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001643 break;
1644 j++;
cristybb503372010-05-27 20:51:26 +00001645 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001646 {
cristya58c3172011-02-19 19:23:11 +00001647 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001648 {
cristy4c08aed2011-07-01 19:47:50 +00001649 case 0: pixel[j]=GetPixelRed(image,p); break;
1650 case 1: pixel[j]=GetPixelGreen(image,p); break;
1651 case 2: pixel[j]=GetPixelBlue(image,p); break;
1652 case 3: pixel[j]=GetPixelAlpha(image,p); break;
1653 case 4: pixel[j]=GetPixelBlack(image,p); break;
cristy3ed852e2009-09-05 21:47:34 +00001654 default: break;
1655 }
cristyed231572011-07-14 02:18:59 +00001656 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001657 j++;
1658 }
1659 j++;
1660 }
cristy3ed852e2009-09-05 21:47:34 +00001661 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
cristya58c3172011-02-19 19:23:11 +00001662 for (k=0; k < 4; k++)
cristy3ed852e2009-09-05 21:47:34 +00001663 {
cristya58c3172011-02-19 19:23:11 +00001664 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1665 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1666 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1667 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
cristy3ed852e2009-09-05 21:47:34 +00001668 }
cristybb503372010-05-27 20:51:26 +00001669 j=(ssize_t) image->columns+2;
1670 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001671 {
1672 MagickBooleanType
1673 sync;
1674
cristy4c08aed2011-07-01 19:47:50 +00001675 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001676 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001677
1678 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1679 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001680 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001681 break;
1682 j++;
cristybb503372010-05-27 20:51:26 +00001683 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001684 {
cristya58c3172011-02-19 19:23:11 +00001685 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001686 {
cristy4c08aed2011-07-01 19:47:50 +00001687 case 0: SetPixelRed(despeckle_image,pixel[j],q); break;
1688 case 1: SetPixelGreen(despeckle_image,pixel[j],q); break;
1689 case 2: SetPixelBlue(despeckle_image,pixel[j],q); break;
1690 case 3: SetPixelAlpha(despeckle_image,pixel[j],q); break;
1691 case 4: SetPixelBlack(despeckle_image,pixel[j],q); break;
cristy3ed852e2009-09-05 21:47:34 +00001692 default: break;
1693 }
cristyed231572011-07-14 02:18:59 +00001694 q+=GetPixelChannels(despeckle_image);
cristy3ed852e2009-09-05 21:47:34 +00001695 j++;
1696 }
1697 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1698 if (sync == MagickFalse)
1699 {
1700 status=MagickFalse;
1701 break;
1702 }
1703 j++;
1704 }
1705 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1706 {
1707 MagickBooleanType
1708 proceed;
1709
cristya58c3172011-02-19 19:23:11 +00001710 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1711 number_channels);
cristy3ed852e2009-09-05 21:47:34 +00001712 if (proceed == MagickFalse)
1713 status=MagickFalse;
1714 }
1715 }
1716 despeckle_view=DestroyCacheView(despeckle_view);
1717 image_view=DestroyCacheView(image_view);
cristy65b9f392011-02-22 14:22:54 +00001718 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1719 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001720 despeckle_image->type=image->type;
1721 if (status == MagickFalse)
1722 despeckle_image=DestroyImage(despeckle_image);
1723 return(despeckle_image);
1724}
1725
1726/*
1727%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1728% %
1729% %
1730% %
1731% E d g e I m a g e %
1732% %
1733% %
1734% %
1735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1736%
1737% EdgeImage() finds edges in an image. Radius defines the radius of the
1738% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1739% radius for you.
1740%
1741% The format of the EdgeImage method is:
1742%
1743% Image *EdgeImage(const Image *image,const double radius,
1744% ExceptionInfo *exception)
1745%
1746% A description of each parameter follows:
1747%
1748% o image: the image.
1749%
1750% o radius: the radius of the pixel neighborhood.
1751%
1752% o exception: return any errors or warnings in this structure.
1753%
1754*/
1755MagickExport Image *EdgeImage(const Image *image,const double radius,
1756 ExceptionInfo *exception)
1757{
1758 Image
1759 *edge_image;
1760
cristy41cbe682011-07-15 19:12:37 +00001761 KernelInfo
1762 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001763
cristybb503372010-05-27 20:51:26 +00001764 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001765 i;
1766
cristybb503372010-05-27 20:51:26 +00001767 size_t
cristy3ed852e2009-09-05 21:47:34 +00001768 width;
1769
cristy41cbe682011-07-15 19:12:37 +00001770 ssize_t
1771 j,
1772 u,
1773 v;
1774
cristy3ed852e2009-09-05 21:47:34 +00001775 assert(image != (const Image *) NULL);
1776 assert(image->signature == MagickSignature);
1777 if (image->debug != MagickFalse)
1778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1779 assert(exception != (ExceptionInfo *) NULL);
1780 assert(exception->signature == MagickSignature);
1781 width=GetOptimalKernelWidth1D(radius,0.5);
cristy5e6be1e2011-07-16 01:23:39 +00001782 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001783 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001784 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001785 kernel_info->width=width;
1786 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001787 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1788 kernel_info->width*sizeof(*kernel_info->values));
1789 if (kernel_info->values == (double *) NULL)
1790 {
1791 kernel_info=DestroyKernelInfo(kernel_info);
1792 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1793 }
1794 j=(ssize_t) kernel_info->width/2;
1795 i=0;
1796 for (v=(-j); v <= j; v++)
1797 {
1798 for (u=(-j); u <= j; u++)
1799 {
1800 kernel_info->values[i]=(-1.0);
1801 i++;
1802 }
1803 }
1804 kernel_info->values[i/2]=(double) (width*width-1.0);
cristy0a922382011-07-16 15:30:34 +00001805 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001806 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001807 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001808 return(edge_image);
1809}
1810
1811/*
1812%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1813% %
1814% %
1815% %
1816% E m b o s s I m a g e %
1817% %
1818% %
1819% %
1820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1821%
1822% EmbossImage() returns a grayscale image with a three-dimensional effect.
1823% We convolve the image with a Gaussian operator of the given radius and
1824% standard deviation (sigma). For reasonable results, radius should be
1825% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1826% radius for you.
1827%
1828% The format of the EmbossImage method is:
1829%
1830% Image *EmbossImage(const Image *image,const double radius,
1831% const double sigma,ExceptionInfo *exception)
1832%
1833% A description of each parameter follows:
1834%
1835% o image: the image.
1836%
1837% o radius: the radius of the pixel neighborhood.
1838%
1839% o sigma: the standard deviation of the Gaussian, in pixels.
1840%
1841% o exception: return any errors or warnings in this structure.
1842%
1843*/
1844MagickExport Image *EmbossImage(const Image *image,const double radius,
1845 const double sigma,ExceptionInfo *exception)
1846{
cristy3ed852e2009-09-05 21:47:34 +00001847 Image
1848 *emboss_image;
1849
cristy41cbe682011-07-15 19:12:37 +00001850 KernelInfo
1851 *kernel_info;
1852
cristybb503372010-05-27 20:51:26 +00001853 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001854 i;
1855
cristybb503372010-05-27 20:51:26 +00001856 size_t
cristy3ed852e2009-09-05 21:47:34 +00001857 width;
1858
cristy117ff172010-08-15 21:35:32 +00001859 ssize_t
1860 j,
1861 k,
1862 u,
1863 v;
1864
cristy41cbe682011-07-15 19:12:37 +00001865 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001866 assert(image->signature == MagickSignature);
1867 if (image->debug != MagickFalse)
1868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1869 assert(exception != (ExceptionInfo *) NULL);
1870 assert(exception->signature == MagickSignature);
1871 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001872 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001873 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001874 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001875 kernel_info->width=width;
1876 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001877 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1878 kernel_info->width*sizeof(*kernel_info->values));
1879 if (kernel_info->values == (double *) NULL)
1880 {
1881 kernel_info=DestroyKernelInfo(kernel_info);
1882 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1883 }
1884 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001885 k=j;
1886 i=0;
1887 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001888 {
cristy47e00502009-12-17 19:19:57 +00001889 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001890 {
cristy41cbe682011-07-15 19:12:37 +00001891 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001892 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001893 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001894 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001895 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001896 i++;
1897 }
cristy47e00502009-12-17 19:19:57 +00001898 k--;
cristy3ed852e2009-09-05 21:47:34 +00001899 }
cristy0a922382011-07-16 15:30:34 +00001900 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001901 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001902 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001903 if (emboss_image != (Image *) NULL)
1904 (void) EqualizeImage(emboss_image);
cristy3ed852e2009-09-05 21:47:34 +00001905 return(emboss_image);
1906}
1907
1908/*
1909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1910% %
1911% %
1912% %
1913% G a u s s i a n B l u r I m a g e %
1914% %
1915% %
1916% %
1917%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1918%
1919% GaussianBlurImage() blurs an image. We convolve the image with a
1920% Gaussian operator of the given radius and standard deviation (sigma).
1921% For reasonable results, the radius should be larger than sigma. Use a
1922% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1923%
1924% The format of the GaussianBlurImage method is:
1925%
1926% Image *GaussianBlurImage(const Image *image,onst double radius,
1927% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001928%
1929% A description of each parameter follows:
1930%
1931% o image: the image.
1932%
cristy3ed852e2009-09-05 21:47:34 +00001933% o radius: the radius of the Gaussian, in pixels, not counting the center
1934% pixel.
1935%
1936% o sigma: the standard deviation of the Gaussian, in pixels.
1937%
1938% o exception: return any errors or warnings in this structure.
1939%
1940*/
cristy41cbe682011-07-15 19:12:37 +00001941MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1942 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001943{
cristy3ed852e2009-09-05 21:47:34 +00001944 Image
1945 *blur_image;
1946
cristy41cbe682011-07-15 19:12:37 +00001947 KernelInfo
1948 *kernel_info;
1949
cristybb503372010-05-27 20:51:26 +00001950 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001951 i;
1952
cristybb503372010-05-27 20:51:26 +00001953 size_t
cristy3ed852e2009-09-05 21:47:34 +00001954 width;
1955
cristy117ff172010-08-15 21:35:32 +00001956 ssize_t
1957 j,
1958 u,
1959 v;
1960
cristy3ed852e2009-09-05 21:47:34 +00001961 assert(image != (const Image *) NULL);
1962 assert(image->signature == MagickSignature);
1963 if (image->debug != MagickFalse)
1964 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1965 assert(exception != (ExceptionInfo *) NULL);
1966 assert(exception->signature == MagickSignature);
1967 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001968 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001969 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001970 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001971 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1972 kernel_info->width=width;
1973 kernel_info->height=width;
1974 kernel_info->signature=MagickSignature;
1975 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1976 kernel_info->width*sizeof(*kernel_info->values));
1977 if (kernel_info->values == (double *) NULL)
1978 {
1979 kernel_info=DestroyKernelInfo(kernel_info);
1980 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1981 }
1982 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001983 i=0;
cristy47e00502009-12-17 19:19:57 +00001984 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001985 {
cristy47e00502009-12-17 19:19:57 +00001986 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001987 {
1988 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1989 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1990 i++;
1991 }
cristy3ed852e2009-09-05 21:47:34 +00001992 }
cristy0a922382011-07-16 15:30:34 +00001993 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001994 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001995 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001996 return(blur_image);
1997}
1998
1999/*
2000%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2001% %
2002% %
2003% %
cristy3ed852e2009-09-05 21:47:34 +00002004% M o t i o n B l u r I m a g e %
2005% %
2006% %
2007% %
2008%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2009%
2010% MotionBlurImage() simulates motion blur. We convolve the image with a
2011% Gaussian operator of the given radius and standard deviation (sigma).
2012% For reasonable results, radius should be larger than sigma. Use a
2013% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2014% Angle gives the angle of the blurring motion.
2015%
2016% Andrew Protano contributed this effect.
2017%
2018% The format of the MotionBlurImage method is:
2019%
2020% Image *MotionBlurImage(const Image *image,const double radius,
2021% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002022%
2023% A description of each parameter follows:
2024%
2025% o image: the image.
2026%
cristy3ed852e2009-09-05 21:47:34 +00002027% o radius: the radius of the Gaussian, in pixels, not counting
2028% the center pixel.
2029%
2030% o sigma: the standard deviation of the Gaussian, in pixels.
2031%
cristycee97112010-05-28 00:44:52 +00002032% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002033%
2034% o exception: return any errors or warnings in this structure.
2035%
2036*/
2037
cristybb503372010-05-27 20:51:26 +00002038static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002039{
cristy3ed852e2009-09-05 21:47:34 +00002040 double
cristy47e00502009-12-17 19:19:57 +00002041 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002042 normalize;
2043
cristybb503372010-05-27 20:51:26 +00002044 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002045 i;
2046
2047 /*
cristy47e00502009-12-17 19:19:57 +00002048 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002049 */
2050 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2051 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2052 if (kernel == (double *) NULL)
2053 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002054 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002055 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002056 {
cristy4205a3c2010-09-12 20:19:59 +00002057 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2058 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002059 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002060 }
cristybb503372010-05-27 20:51:26 +00002061 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002062 kernel[i]/=normalize;
2063 return(kernel);
2064}
2065
cristyf4ad9df2011-07-08 16:49:03 +00002066MagickExport Image *MotionBlurImage(const Image *image,
2067 const double radius,const double sigma,const double angle,
2068 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002069{
cristyc4c8d132010-01-07 01:58:38 +00002070 CacheView
2071 *blur_view,
2072 *image_view;
2073
cristy3ed852e2009-09-05 21:47:34 +00002074 double
2075 *kernel;
2076
2077 Image
2078 *blur_image;
2079
cristy3ed852e2009-09-05 21:47:34 +00002080 MagickBooleanType
2081 status;
2082
cristybb503372010-05-27 20:51:26 +00002083 MagickOffsetType
2084 progress;
2085
cristy4c08aed2011-07-01 19:47:50 +00002086 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002087 bias;
cristy3ed852e2009-09-05 21:47:34 +00002088
2089 OffsetInfo
2090 *offset;
2091
2092 PointInfo
2093 point;
2094
cristybb503372010-05-27 20:51:26 +00002095 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002096 i;
2097
cristybb503372010-05-27 20:51:26 +00002098 size_t
cristy3ed852e2009-09-05 21:47:34 +00002099 width;
2100
cristybb503372010-05-27 20:51:26 +00002101 ssize_t
2102 y;
2103
cristy3ed852e2009-09-05 21:47:34 +00002104 assert(image != (Image *) NULL);
2105 assert(image->signature == MagickSignature);
2106 if (image->debug != MagickFalse)
2107 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2108 assert(exception != (ExceptionInfo *) NULL);
2109 width=GetOptimalKernelWidth1D(radius,sigma);
2110 kernel=GetMotionBlurKernel(width,sigma);
2111 if (kernel == (double *) NULL)
2112 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2113 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2114 if (offset == (OffsetInfo *) NULL)
2115 {
2116 kernel=(double *) RelinquishMagickMemory(kernel);
2117 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2118 }
2119 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2120 if (blur_image == (Image *) NULL)
2121 {
2122 kernel=(double *) RelinquishMagickMemory(kernel);
2123 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2124 return((Image *) NULL);
2125 }
cristy574cc262011-08-05 01:23:58 +00002126 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002127 {
2128 kernel=(double *) RelinquishMagickMemory(kernel);
2129 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002130 blur_image=DestroyImage(blur_image);
2131 return((Image *) NULL);
2132 }
2133 point.x=(double) width*sin(DegreesToRadians(angle));
2134 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002135 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002136 {
cristybb503372010-05-27 20:51:26 +00002137 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2138 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002139 }
2140 /*
2141 Motion blur image.
2142 */
2143 status=MagickTrue;
2144 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002145 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002146 image_view=AcquireCacheView(image);
2147 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002148#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00002149 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00002150#endif
cristybb503372010-05-27 20:51:26 +00002151 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002152 {
cristy4c08aed2011-07-01 19:47:50 +00002153 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002154 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002155
cristy117ff172010-08-15 21:35:32 +00002156 register ssize_t
2157 x;
2158
cristy3ed852e2009-09-05 21:47:34 +00002159 if (status == MagickFalse)
2160 continue;
2161 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2162 exception);
cristy4c08aed2011-07-01 19:47:50 +00002163 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002164 {
2165 status=MagickFalse;
2166 continue;
2167 }
cristybb503372010-05-27 20:51:26 +00002168 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002169 {
cristy4c08aed2011-07-01 19:47:50 +00002170 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002171 qixel;
2172
2173 PixelPacket
2174 pixel;
2175
2176 register double
cristyc47d1f82009-11-26 01:44:43 +00002177 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00002178
cristybb503372010-05-27 20:51:26 +00002179 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002180 i;
2181
cristy3ed852e2009-09-05 21:47:34 +00002182 k=kernel;
cristyddd82202009-11-03 20:14:50 +00002183 qixel=bias;
cristyed231572011-07-14 02:18:59 +00002184 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002185 {
cristybb503372010-05-27 20:51:26 +00002186 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002187 {
2188 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2189 offset[i].y,&pixel,exception);
2190 qixel.red+=(*k)*pixel.red;
2191 qixel.green+=(*k)*pixel.green;
2192 qixel.blue+=(*k)*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002193 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002194 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002195 qixel.black+=(*k)*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002196 k++;
2197 }
cristyed231572011-07-14 02:18:59 +00002198 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002199 SetPixelRed(blur_image,
2200 ClampToQuantum(qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002201 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002202 SetPixelGreen(blur_image,
2203 ClampToQuantum(qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002204 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002205 SetPixelBlue(blur_image,
2206 ClampToQuantum(qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002207 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002208 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002209 SetPixelBlack(blur_image,
2210 ClampToQuantum(qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002211 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002212 SetPixelAlpha(blur_image,
2213 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002214 }
2215 else
2216 {
2217 MagickRealType
2218 alpha,
2219 gamma;
2220
2221 alpha=0.0;
2222 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00002223 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002224 {
2225 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2226 offset[i].y,&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00002227 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00002228 qixel.red+=(*k)*alpha*pixel.red;
2229 qixel.green+=(*k)*alpha*pixel.green;
2230 qixel.blue+=(*k)*alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002231 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002232 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002233 qixel.black+=(*k)*alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002234 gamma+=(*k)*alpha;
2235 k++;
2236 }
2237 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00002238 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002239 SetPixelRed(blur_image,
2240 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002241 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002242 SetPixelGreen(blur_image,
2243 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002244 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002245 SetPixelBlue(blur_image,
2246 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002247 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002248 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002249 SetPixelBlack(blur_image,
2250 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002251 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002252 SetPixelAlpha(blur_image,
2253 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002254 }
cristyed231572011-07-14 02:18:59 +00002255 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002256 }
2257 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2258 status=MagickFalse;
2259 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2260 {
2261 MagickBooleanType
2262 proceed;
2263
cristyb557a152011-02-22 12:14:30 +00002264#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002265 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002266#endif
2267 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2268 if (proceed == MagickFalse)
2269 status=MagickFalse;
2270 }
2271 }
2272 blur_view=DestroyCacheView(blur_view);
2273 image_view=DestroyCacheView(image_view);
2274 kernel=(double *) RelinquishMagickMemory(kernel);
2275 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2276 if (status == MagickFalse)
2277 blur_image=DestroyImage(blur_image);
2278 return(blur_image);
2279}
2280
2281/*
2282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2283% %
2284% %
2285% %
2286% P r e v i e w I m a g e %
2287% %
2288% %
2289% %
2290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2291%
2292% PreviewImage() tiles 9 thumbnails of the specified image with an image
2293% processing operation applied with varying parameters. This may be helpful
2294% pin-pointing an appropriate parameter for a particular image processing
2295% operation.
2296%
2297% The format of the PreviewImages method is:
2298%
2299% Image *PreviewImages(const Image *image,const PreviewType preview,
2300% ExceptionInfo *exception)
2301%
2302% A description of each parameter follows:
2303%
2304% o image: the image.
2305%
2306% o preview: the image processing operation.
2307%
2308% o exception: return any errors or warnings in this structure.
2309%
2310*/
2311MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2312 ExceptionInfo *exception)
2313{
2314#define NumberTiles 9
2315#define PreviewImageTag "Preview/Image"
2316#define DefaultPreviewGeometry "204x204+10+10"
2317
2318 char
2319 factor[MaxTextExtent],
2320 label[MaxTextExtent];
2321
2322 double
2323 degrees,
2324 gamma,
2325 percentage,
2326 radius,
2327 sigma,
2328 threshold;
2329
2330 Image
2331 *images,
2332 *montage_image,
2333 *preview_image,
2334 *thumbnail;
2335
2336 ImageInfo
2337 *preview_info;
2338
cristy3ed852e2009-09-05 21:47:34 +00002339 MagickBooleanType
2340 proceed;
2341
2342 MontageInfo
2343 *montage_info;
2344
2345 QuantizeInfo
2346 quantize_info;
2347
2348 RectangleInfo
2349 geometry;
2350
cristybb503372010-05-27 20:51:26 +00002351 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002352 i,
2353 x;
2354
cristybb503372010-05-27 20:51:26 +00002355 size_t
cristy3ed852e2009-09-05 21:47:34 +00002356 colors;
2357
cristy117ff172010-08-15 21:35:32 +00002358 ssize_t
2359 y;
2360
cristy3ed852e2009-09-05 21:47:34 +00002361 /*
2362 Open output image file.
2363 */
2364 assert(image != (Image *) NULL);
2365 assert(image->signature == MagickSignature);
2366 if (image->debug != MagickFalse)
2367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2368 colors=2;
2369 degrees=0.0;
2370 gamma=(-0.2f);
2371 preview_info=AcquireImageInfo();
2372 SetGeometry(image,&geometry);
2373 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2374 &geometry.width,&geometry.height);
2375 images=NewImageList();
2376 percentage=12.5;
2377 GetQuantizeInfo(&quantize_info);
2378 radius=0.0;
2379 sigma=1.0;
2380 threshold=0.0;
2381 x=0;
2382 y=0;
2383 for (i=0; i < NumberTiles; i++)
2384 {
2385 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2386 if (thumbnail == (Image *) NULL)
2387 break;
2388 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2389 (void *) NULL);
2390 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
2391 if (i == (NumberTiles/2))
2392 {
2393 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
2394 AppendImageToList(&images,thumbnail);
2395 continue;
2396 }
2397 switch (preview)
2398 {
2399 case RotatePreview:
2400 {
2401 degrees+=45.0;
2402 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002403 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002404 break;
2405 }
2406 case ShearPreview:
2407 {
2408 degrees+=5.0;
2409 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002410 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002411 degrees,2.0*degrees);
2412 break;
2413 }
2414 case RollPreview:
2415 {
cristybb503372010-05-27 20:51:26 +00002416 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2417 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002418 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002419 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002420 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002421 break;
2422 }
2423 case HuePreview:
2424 {
2425 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2426 if (preview_image == (Image *) NULL)
2427 break;
cristyb51dff52011-05-19 16:55:47 +00002428 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002429 2.0*percentage);
2430 (void) ModulateImage(preview_image,factor);
cristyb51dff52011-05-19 16:55:47 +00002431 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002432 break;
2433 }
2434 case SaturationPreview:
2435 {
2436 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2437 if (preview_image == (Image *) NULL)
2438 break;
cristyb51dff52011-05-19 16:55:47 +00002439 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002440 2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00002441 (void) ModulateImage(preview_image,factor);
cristyb51dff52011-05-19 16:55:47 +00002442 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002443 break;
2444 }
2445 case BrightnessPreview:
2446 {
2447 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2448 if (preview_image == (Image *) NULL)
2449 break;
cristyb51dff52011-05-19 16:55:47 +00002450 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00002451 (void) ModulateImage(preview_image,factor);
cristyb51dff52011-05-19 16:55:47 +00002452 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002453 break;
2454 }
2455 case GammaPreview:
2456 default:
2457 {
2458 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2459 if (preview_image == (Image *) NULL)
2460 break;
2461 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002462 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002463 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002464 break;
2465 }
2466 case SpiffPreview:
2467 {
2468 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2469 if (preview_image != (Image *) NULL)
2470 for (x=0; x < i; x++)
2471 (void) ContrastImage(preview_image,MagickTrue);
cristyb51dff52011-05-19 16:55:47 +00002472 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002473 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002474 break;
2475 }
2476 case DullPreview:
2477 {
2478 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2479 if (preview_image == (Image *) NULL)
2480 break;
2481 for (x=0; x < i; x++)
2482 (void) ContrastImage(preview_image,MagickFalse);
cristyb51dff52011-05-19 16:55:47 +00002483 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002484 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002485 break;
2486 }
2487 case GrayscalePreview:
2488 {
2489 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2490 if (preview_image == (Image *) NULL)
2491 break;
2492 colors<<=1;
2493 quantize_info.number_colors=colors;
2494 quantize_info.colorspace=GRAYColorspace;
2495 (void) QuantizeImage(&quantize_info,preview_image);
cristyb51dff52011-05-19 16:55:47 +00002496 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002497 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002498 break;
2499 }
2500 case QuantizePreview:
2501 {
2502 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2503 if (preview_image == (Image *) NULL)
2504 break;
2505 colors<<=1;
2506 quantize_info.number_colors=colors;
2507 (void) QuantizeImage(&quantize_info,preview_image);
cristyb51dff52011-05-19 16:55:47 +00002508 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002509 colors);
cristy3ed852e2009-09-05 21:47:34 +00002510 break;
2511 }
2512 case DespecklePreview:
2513 {
2514 for (x=0; x < (i-1); x++)
2515 {
2516 preview_image=DespeckleImage(thumbnail,exception);
2517 if (preview_image == (Image *) NULL)
2518 break;
2519 thumbnail=DestroyImage(thumbnail);
2520 thumbnail=preview_image;
2521 }
2522 preview_image=DespeckleImage(thumbnail,exception);
2523 if (preview_image == (Image *) NULL)
2524 break;
cristyb51dff52011-05-19 16:55:47 +00002525 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002526 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002527 break;
2528 }
2529 case ReduceNoisePreview:
2530 {
cristy95c38342011-03-18 22:39:51 +00002531 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2532 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002533 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002534 break;
2535 }
2536 case AddNoisePreview:
2537 {
2538 switch ((int) i)
2539 {
2540 case 0:
2541 {
2542 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2543 break;
2544 }
2545 case 1:
2546 {
2547 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2548 break;
2549 }
2550 case 2:
2551 {
2552 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2553 break;
2554 }
2555 case 3:
2556 {
2557 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2558 break;
2559 }
2560 case 4:
2561 {
2562 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2563 break;
2564 }
2565 case 5:
2566 {
2567 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2568 break;
2569 }
2570 default:
2571 {
2572 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2573 break;
2574 }
2575 }
cristyd76c51e2011-03-26 00:21:26 +00002576 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2577 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002578 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002579 break;
2580 }
2581 case SharpenPreview:
2582 {
2583 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002584 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002585 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002586 break;
2587 }
2588 case BlurPreview:
2589 {
2590 preview_image=BlurImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002591 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002592 sigma);
2593 break;
2594 }
2595 case ThresholdPreview:
2596 {
2597 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2598 if (preview_image == (Image *) NULL)
2599 break;
2600 (void) BilevelImage(thumbnail,
2601 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
cristyb51dff52011-05-19 16:55:47 +00002602 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002603 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2604 break;
2605 }
2606 case EdgeDetectPreview:
2607 {
2608 preview_image=EdgeImage(thumbnail,radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002609 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002610 break;
2611 }
2612 case SpreadPreview:
2613 {
2614 preview_image=SpreadImage(thumbnail,radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002615 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002616 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002617 break;
2618 }
2619 case SolarizePreview:
2620 {
2621 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2622 if (preview_image == (Image *) NULL)
2623 break;
2624 (void) SolarizeImage(preview_image,(double) QuantumRange*
2625 percentage/100.0);
cristyb51dff52011-05-19 16:55:47 +00002626 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002627 (QuantumRange*percentage)/100.0);
2628 break;
2629 }
2630 case ShadePreview:
2631 {
2632 degrees+=10.0;
2633 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2634 exception);
cristyb51dff52011-05-19 16:55:47 +00002635 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002636 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002637 break;
2638 }
2639 case RaisePreview:
2640 {
2641 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2642 if (preview_image == (Image *) NULL)
2643 break;
cristybb503372010-05-27 20:51:26 +00002644 geometry.width=(size_t) (2*i+2);
2645 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002646 geometry.x=i/2;
2647 geometry.y=i/2;
2648 (void) RaiseImage(preview_image,&geometry,MagickTrue);
cristyb51dff52011-05-19 16:55:47 +00002649 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002650 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002651 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002652 break;
2653 }
2654 case SegmentPreview:
2655 {
2656 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2657 if (preview_image == (Image *) NULL)
2658 break;
2659 threshold+=0.4f;
2660 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
2661 threshold);
cristyb51dff52011-05-19 16:55:47 +00002662 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002663 threshold,threshold);
2664 break;
2665 }
2666 case SwirlPreview:
2667 {
2668 preview_image=SwirlImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002669 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002670 degrees+=45.0;
2671 break;
2672 }
2673 case ImplodePreview:
2674 {
2675 degrees+=0.1f;
2676 preview_image=ImplodeImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002677 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002678 break;
2679 }
2680 case WavePreview:
2681 {
2682 degrees+=5.0f;
2683 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002684 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002685 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002686 break;
2687 }
2688 case OilPaintPreview:
2689 {
2690 preview_image=OilPaintImage(thumbnail,(double) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002691 (void) FormatLocaleString(label,MaxTextExtent,"paint %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002692 break;
2693 }
2694 case CharcoalDrawingPreview:
2695 {
2696 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2697 exception);
cristyb51dff52011-05-19 16:55:47 +00002698 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002699 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002700 break;
2701 }
2702 case JPEGPreview:
2703 {
2704 char
2705 filename[MaxTextExtent];
2706
2707 int
2708 file;
2709
2710 MagickBooleanType
2711 status;
2712
2713 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2714 if (preview_image == (Image *) NULL)
2715 break;
cristybb503372010-05-27 20:51:26 +00002716 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002717 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002718 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002719 file=AcquireUniqueFileResource(filename);
2720 if (file != -1)
2721 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002722 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002723 "jpeg:%s",filename);
2724 status=WriteImage(preview_info,preview_image);
2725 if (status != MagickFalse)
2726 {
2727 Image
2728 *quality_image;
2729
2730 (void) CopyMagickString(preview_info->filename,
2731 preview_image->filename,MaxTextExtent);
2732 quality_image=ReadImage(preview_info,exception);
2733 if (quality_image != (Image *) NULL)
2734 {
2735 preview_image=DestroyImage(preview_image);
2736 preview_image=quality_image;
2737 }
2738 }
2739 (void) RelinquishUniqueFileResource(preview_image->filename);
2740 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002741 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002742 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2743 1024.0/1024.0);
2744 else
2745 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002746 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002747 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002748 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002749 else
cristyb51dff52011-05-19 16:55:47 +00002750 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002751 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002752 break;
2753 }
2754 }
2755 thumbnail=DestroyImage(thumbnail);
2756 percentage+=12.5;
2757 radius+=0.5;
2758 sigma+=0.25;
2759 if (preview_image == (Image *) NULL)
2760 break;
2761 (void) DeleteImageProperty(preview_image,"label");
2762 (void) SetImageProperty(preview_image,"label",label);
2763 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002764 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2765 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002766 if (proceed == MagickFalse)
2767 break;
2768 }
2769 if (images == (Image *) NULL)
2770 {
2771 preview_info=DestroyImageInfo(preview_info);
2772 return((Image *) NULL);
2773 }
2774 /*
2775 Create the montage.
2776 */
2777 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2778 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2779 montage_info->shadow=MagickTrue;
2780 (void) CloneString(&montage_info->tile,"3x3");
2781 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2782 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2783 montage_image=MontageImages(images,montage_info,exception);
2784 montage_info=DestroyMontageInfo(montage_info);
2785 images=DestroyImageList(images);
2786 if (montage_image == (Image *) NULL)
2787 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2788 if (montage_image->montage != (char *) NULL)
2789 {
2790 /*
2791 Free image directory.
2792 */
2793 montage_image->montage=(char *) RelinquishMagickMemory(
2794 montage_image->montage);
2795 if (image->directory != (char *) NULL)
2796 montage_image->directory=(char *) RelinquishMagickMemory(
2797 montage_image->directory);
2798 }
2799 preview_info=DestroyImageInfo(preview_info);
2800 return(montage_image);
2801}
2802
2803/*
2804%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2805% %
2806% %
2807% %
2808% R a d i a l B l u r I m a g e %
2809% %
2810% %
2811% %
2812%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2813%
2814% RadialBlurImage() applies a radial blur to the image.
2815%
2816% Andrew Protano contributed this effect.
2817%
2818% The format of the RadialBlurImage method is:
2819%
2820% Image *RadialBlurImage(const Image *image,const double angle,
2821% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002822%
2823% A description of each parameter follows:
2824%
2825% o image: the image.
2826%
cristy3ed852e2009-09-05 21:47:34 +00002827% o angle: the angle of the radial blur.
2828%
2829% o exception: return any errors or warnings in this structure.
2830%
2831*/
cristyf4ad9df2011-07-08 16:49:03 +00002832MagickExport Image *RadialBlurImage(const Image *image,
2833 const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002834{
cristyc4c8d132010-01-07 01:58:38 +00002835 CacheView
2836 *blur_view,
2837 *image_view;
2838
cristy3ed852e2009-09-05 21:47:34 +00002839 Image
2840 *blur_image;
2841
cristy3ed852e2009-09-05 21:47:34 +00002842 MagickBooleanType
2843 status;
2844
cristybb503372010-05-27 20:51:26 +00002845 MagickOffsetType
2846 progress;
2847
cristy4c08aed2011-07-01 19:47:50 +00002848 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002849 bias;
cristy3ed852e2009-09-05 21:47:34 +00002850
2851 MagickRealType
2852 blur_radius,
2853 *cos_theta,
2854 offset,
2855 *sin_theta,
2856 theta;
2857
2858 PointInfo
2859 blur_center;
2860
cristybb503372010-05-27 20:51:26 +00002861 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002862 i;
2863
cristybb503372010-05-27 20:51:26 +00002864 size_t
cristy3ed852e2009-09-05 21:47:34 +00002865 n;
2866
cristybb503372010-05-27 20:51:26 +00002867 ssize_t
2868 y;
2869
cristy3ed852e2009-09-05 21:47:34 +00002870 /*
2871 Allocate blur image.
2872 */
2873 assert(image != (Image *) NULL);
2874 assert(image->signature == MagickSignature);
2875 if (image->debug != MagickFalse)
2876 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2877 assert(exception != (ExceptionInfo *) NULL);
2878 assert(exception->signature == MagickSignature);
2879 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2880 if (blur_image == (Image *) NULL)
2881 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002882 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002883 {
cristy3ed852e2009-09-05 21:47:34 +00002884 blur_image=DestroyImage(blur_image);
2885 return((Image *) NULL);
2886 }
2887 blur_center.x=(double) image->columns/2.0;
2888 blur_center.y=(double) image->rows/2.0;
2889 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002890 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002891 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2892 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2893 sizeof(*cos_theta));
2894 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2895 sizeof(*sin_theta));
2896 if ((cos_theta == (MagickRealType *) NULL) ||
2897 (sin_theta == (MagickRealType *) NULL))
2898 {
2899 blur_image=DestroyImage(blur_image);
2900 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2901 }
2902 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002903 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002904 {
2905 cos_theta[i]=cos((double) (theta*i-offset));
2906 sin_theta[i]=sin((double) (theta*i-offset));
2907 }
2908 /*
2909 Radial blur image.
2910 */
2911 status=MagickTrue;
2912 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002913 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002914 image_view=AcquireCacheView(image);
2915 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002916#if defined(MAGICKCORE_OPENMP_SUPPORT)
2917 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002918#endif
cristybb503372010-05-27 20:51:26 +00002919 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002920 {
cristy4c08aed2011-07-01 19:47:50 +00002921 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002922 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002923
cristy117ff172010-08-15 21:35:32 +00002924 register ssize_t
2925 x;
2926
cristy3ed852e2009-09-05 21:47:34 +00002927 if (status == MagickFalse)
2928 continue;
2929 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2930 exception);
cristy4c08aed2011-07-01 19:47:50 +00002931 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002932 {
2933 status=MagickFalse;
2934 continue;
2935 }
cristybb503372010-05-27 20:51:26 +00002936 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002937 {
cristy4c08aed2011-07-01 19:47:50 +00002938 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002939 qixel;
2940
2941 MagickRealType
2942 normalize,
2943 radius;
2944
2945 PixelPacket
2946 pixel;
2947
2948 PointInfo
2949 center;
2950
cristybb503372010-05-27 20:51:26 +00002951 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002952 i;
2953
cristybb503372010-05-27 20:51:26 +00002954 size_t
cristy3ed852e2009-09-05 21:47:34 +00002955 step;
2956
2957 center.x=(double) x-blur_center.x;
2958 center.y=(double) y-blur_center.y;
2959 radius=hypot((double) center.x,center.y);
2960 if (radius == 0)
2961 step=1;
2962 else
2963 {
cristybb503372010-05-27 20:51:26 +00002964 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002965 if (step == 0)
2966 step=1;
2967 else
2968 if (step >= n)
2969 step=n-1;
2970 }
2971 normalize=0.0;
cristyddd82202009-11-03 20:14:50 +00002972 qixel=bias;
cristyed231572011-07-14 02:18:59 +00002973 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002974 {
cristyeaedf062010-05-29 22:36:02 +00002975 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00002976 {
cristyeaedf062010-05-29 22:36:02 +00002977 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
2978 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
2979 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
2980 cos_theta[i]+0.5),&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002981 qixel.red+=pixel.red;
2982 qixel.green+=pixel.green;
2983 qixel.blue+=pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00002984 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002985 qixel.black+=pixel.black;
2986 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002987 normalize+=1.0;
2988 }
2989 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
2990 normalize);
cristyed231572011-07-14 02:18:59 +00002991 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002992 SetPixelRed(blur_image,
2993 ClampToQuantum(normalize*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002994 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002995 SetPixelGreen(blur_image,
2996 ClampToQuantum(normalize*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002997 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002998 SetPixelBlue(blur_image,
2999 ClampToQuantum(normalize*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003000 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003001 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003002 SetPixelBlack(blur_image,
3003 ClampToQuantum(normalize*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003004 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003005 SetPixelAlpha(blur_image,
3006 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003007 }
3008 else
3009 {
3010 MagickRealType
3011 alpha,
3012 gamma;
3013
3014 alpha=1.0;
3015 gamma=0.0;
cristyeaedf062010-05-29 22:36:02 +00003016 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00003017 {
cristyeaedf062010-05-29 22:36:02 +00003018 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3019 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3020 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3021 cos_theta[i]+0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003022 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00003023 qixel.red+=alpha*pixel.red;
3024 qixel.green+=alpha*pixel.green;
3025 qixel.blue+=alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00003026 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00003027 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00003028 qixel.black+=alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00003029 gamma+=alpha;
3030 normalize+=1.0;
3031 }
3032 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3033 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3034 normalize);
cristyed231572011-07-14 02:18:59 +00003035 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003036 SetPixelRed(blur_image,
3037 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00003038 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003039 SetPixelGreen(blur_image,
3040 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00003041 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003042 SetPixelBlue(blur_image,
3043 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003044 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003045 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003046 SetPixelBlack(blur_image,
3047 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003048 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003049 SetPixelAlpha(blur_image,
3050 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003051 }
cristyed231572011-07-14 02:18:59 +00003052 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003053 }
3054 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3055 status=MagickFalse;
3056 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3057 {
3058 MagickBooleanType
3059 proceed;
3060
cristyb5d5f722009-11-04 03:03:49 +00003061#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003062 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003063#endif
3064 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3065 if (proceed == MagickFalse)
3066 status=MagickFalse;
3067 }
3068 }
3069 blur_view=DestroyCacheView(blur_view);
3070 image_view=DestroyCacheView(image_view);
3071 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3072 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3073 if (status == MagickFalse)
3074 blur_image=DestroyImage(blur_image);
3075 return(blur_image);
3076}
3077
3078/*
3079%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3080% %
3081% %
3082% %
cristy3ed852e2009-09-05 21:47:34 +00003083% S e l e c t i v e B l u r I m a g e %
3084% %
3085% %
3086% %
3087%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3088%
3089% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3090% It is similar to the unsharpen mask that sharpens everything with contrast
3091% above a certain threshold.
3092%
3093% The format of the SelectiveBlurImage method is:
3094%
3095% Image *SelectiveBlurImage(const Image *image,const double radius,
3096% const double sigma,const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003097%
3098% A description of each parameter follows:
3099%
3100% o image: the image.
3101%
cristy3ed852e2009-09-05 21:47:34 +00003102% o radius: the radius of the Gaussian, in pixels, not counting the center
3103% pixel.
3104%
3105% o sigma: the standard deviation of the Gaussian, in pixels.
3106%
3107% o threshold: only pixels within this contrast threshold are included
3108% in the blur operation.
3109%
3110% o exception: return any errors or warnings in this structure.
3111%
3112*/
cristyf4ad9df2011-07-08 16:49:03 +00003113MagickExport Image *SelectiveBlurImage(const Image *image,
3114 const double radius,const double sigma,const double threshold,
3115 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003116{
3117#define SelectiveBlurImageTag "SelectiveBlur/Image"
3118
cristy47e00502009-12-17 19:19:57 +00003119 CacheView
3120 *blur_view,
3121 *image_view;
3122
cristy3ed852e2009-09-05 21:47:34 +00003123 double
cristy3ed852e2009-09-05 21:47:34 +00003124 *kernel;
3125
3126 Image
3127 *blur_image;
3128
cristy3ed852e2009-09-05 21:47:34 +00003129 MagickBooleanType
3130 status;
3131
cristybb503372010-05-27 20:51:26 +00003132 MagickOffsetType
3133 progress;
3134
cristy4c08aed2011-07-01 19:47:50 +00003135 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003136 bias;
3137
cristybb503372010-05-27 20:51:26 +00003138 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003139 i;
cristy3ed852e2009-09-05 21:47:34 +00003140
cristybb503372010-05-27 20:51:26 +00003141 size_t
cristy3ed852e2009-09-05 21:47:34 +00003142 width;
3143
cristybb503372010-05-27 20:51:26 +00003144 ssize_t
3145 j,
3146 u,
3147 v,
3148 y;
3149
cristy3ed852e2009-09-05 21:47:34 +00003150 /*
3151 Initialize blur image attributes.
3152 */
3153 assert(image != (Image *) NULL);
3154 assert(image->signature == MagickSignature);
3155 if (image->debug != MagickFalse)
3156 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3157 assert(exception != (ExceptionInfo *) NULL);
3158 assert(exception->signature == MagickSignature);
3159 width=GetOptimalKernelWidth1D(radius,sigma);
3160 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3161 if (kernel == (double *) NULL)
3162 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003163 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003164 i=0;
cristy47e00502009-12-17 19:19:57 +00003165 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003166 {
cristy47e00502009-12-17 19:19:57 +00003167 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003168 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3169 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003170 }
3171 if (image->debug != MagickFalse)
3172 {
3173 char
3174 format[MaxTextExtent],
3175 *message;
3176
cristy117ff172010-08-15 21:35:32 +00003177 register const double
3178 *k;
3179
cristybb503372010-05-27 20:51:26 +00003180 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003181 u,
3182 v;
3183
cristy3ed852e2009-09-05 21:47:34 +00003184 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003185 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3186 width);
cristy3ed852e2009-09-05 21:47:34 +00003187 message=AcquireString("");
3188 k=kernel;
cristybb503372010-05-27 20:51:26 +00003189 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003190 {
3191 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003192 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003193 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003194 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003195 {
cristyb51dff52011-05-19 16:55:47 +00003196 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003197 (void) ConcatenateString(&message,format);
3198 }
3199 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3200 }
3201 message=DestroyString(message);
3202 }
3203 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3204 if (blur_image == (Image *) NULL)
3205 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003206 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003207 {
cristy3ed852e2009-09-05 21:47:34 +00003208 blur_image=DestroyImage(blur_image);
3209 return((Image *) NULL);
3210 }
3211 /*
3212 Threshold blur image.
3213 */
3214 status=MagickTrue;
3215 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003216 GetPixelInfo(image,&bias);
3217 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003218 image_view=AcquireCacheView(image);
3219 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003220#if defined(MAGICKCORE_OPENMP_SUPPORT)
3221 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003222#endif
cristybb503372010-05-27 20:51:26 +00003223 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003224 {
cristy4c08aed2011-07-01 19:47:50 +00003225 double
3226 contrast;
3227
cristy3ed852e2009-09-05 21:47:34 +00003228 MagickBooleanType
3229 sync;
3230
3231 MagickRealType
3232 gamma;
3233
cristy4c08aed2011-07-01 19:47:50 +00003234 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003235 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003236
cristy4c08aed2011-07-01 19:47:50 +00003237 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003238 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003239
cristy117ff172010-08-15 21:35:32 +00003240 register ssize_t
3241 x;
3242
cristy3ed852e2009-09-05 21:47:34 +00003243 if (status == MagickFalse)
3244 continue;
cristy117ff172010-08-15 21:35:32 +00003245 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3246 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00003247 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3248 exception);
cristy4c08aed2011-07-01 19:47:50 +00003249 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003250 {
3251 status=MagickFalse;
3252 continue;
3253 }
cristybb503372010-05-27 20:51:26 +00003254 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003255 {
cristy4c08aed2011-07-01 19:47:50 +00003256 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003257 pixel;
3258
3259 register const double
cristyc47d1f82009-11-26 01:44:43 +00003260 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00003261
cristybb503372010-05-27 20:51:26 +00003262 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003263 u;
3264
cristy117ff172010-08-15 21:35:32 +00003265 ssize_t
3266 j,
3267 v;
3268
cristyddd82202009-11-03 20:14:50 +00003269 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00003270 k=kernel;
3271 gamma=0.0;
3272 j=0;
cristyed231572011-07-14 02:18:59 +00003273 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003274 {
cristybb503372010-05-27 20:51:26 +00003275 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003276 {
cristybb503372010-05-27 20:51:26 +00003277 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003278 {
cristyed231572011-07-14 02:18:59 +00003279 contrast=GetPixelIntensity(image,p+(u+j)*GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003280 (double) GetPixelIntensity(blur_image,q);
3281 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003282 {
cristy4c08aed2011-07-01 19:47:50 +00003283 pixel.red+=(*k)*
cristyed231572011-07-14 02:18:59 +00003284 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003285 pixel.green+=(*k)*
cristyed231572011-07-14 02:18:59 +00003286 GetPixelGreen(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003287 pixel.blue+=(*k)*
cristyed231572011-07-14 02:18:59 +00003288 GetPixelBlue(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003289 if (image->colorspace == CMYKColorspace)
3290 pixel.black+=(*k)*
cristyed231572011-07-14 02:18:59 +00003291 GetPixelBlack(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003292 gamma+=(*k);
3293 k++;
3294 }
3295 }
cristyd99b0962010-05-29 23:14:26 +00003296 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003297 }
3298 if (gamma != 0.0)
3299 {
3300 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003301 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003302 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003303 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003304 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003305 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003306 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003307 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003308 (image->colorspace == CMYKColorspace))
3309 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003310 }
cristyed231572011-07-14 02:18:59 +00003311 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003312 {
3313 gamma=0.0;
3314 j=0;
cristybb503372010-05-27 20:51:26 +00003315 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003316 {
cristybb503372010-05-27 20:51:26 +00003317 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003318 {
cristy4c08aed2011-07-01 19:47:50 +00003319 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003320 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003321 GetPixelIntensity(blur_image,q);
3322 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003323 {
cristy4c08aed2011-07-01 19:47:50 +00003324 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003325 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003326 gamma+=(*k);
3327 k++;
3328 }
3329 }
cristyeaedf062010-05-29 22:36:02 +00003330 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003331 }
3332 if (gamma != 0.0)
3333 {
3334 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3335 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003336 SetPixelAlpha(blur_image,ClampToQuantum(gamma*pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003337 }
3338 }
3339 }
3340 else
3341 {
3342 MagickRealType
3343 alpha;
3344
cristybb503372010-05-27 20:51:26 +00003345 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003346 {
cristybb503372010-05-27 20:51:26 +00003347 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003348 {
cristy4c08aed2011-07-01 19:47:50 +00003349 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003350 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003351 GetPixelIntensity(blur_image,q);
3352 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003353 {
cristy4c08aed2011-07-01 19:47:50 +00003354 alpha=(MagickRealType) (QuantumScale*
cristyed231572011-07-14 02:18:59 +00003355 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image)));
cristy4c08aed2011-07-01 19:47:50 +00003356 pixel.red+=(*k)*alpha*
cristyed231572011-07-14 02:18:59 +00003357 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003358 pixel.green+=(*k)*alpha*GetPixelGreen(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003359 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003360 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003361 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003362 pixel.alpha+=(*k)*GetPixelAlpha(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003363 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003364 if (image->colorspace == CMYKColorspace)
3365 pixel.black+=(*k)*GetPixelBlack(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003366 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003367 gamma+=(*k)*alpha;
3368 k++;
3369 }
3370 }
cristyeaedf062010-05-29 22:36:02 +00003371 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003372 }
3373 if (gamma != 0.0)
3374 {
3375 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003376 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003377 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003378 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003379 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003380 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003381 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003382 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003383 (image->colorspace == CMYKColorspace))
3384 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003385 }
cristyed231572011-07-14 02:18:59 +00003386 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003387 {
3388 gamma=0.0;
3389 j=0;
cristybb503372010-05-27 20:51:26 +00003390 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003391 {
cristybb503372010-05-27 20:51:26 +00003392 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003393 {
cristy4c08aed2011-07-01 19:47:50 +00003394 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003395 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003396 GetPixelIntensity(blur_image,q);
3397 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003398 {
cristy4c08aed2011-07-01 19:47:50 +00003399 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003400 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003401 gamma+=(*k);
3402 k++;
3403 }
3404 }
cristyeaedf062010-05-29 22:36:02 +00003405 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003406 }
3407 if (gamma != 0.0)
3408 {
3409 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3410 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003411 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003412 }
3413 }
3414 }
cristyed231572011-07-14 02:18:59 +00003415 p+=GetPixelChannels(image);
3416 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003417 }
3418 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3419 if (sync == MagickFalse)
3420 status=MagickFalse;
3421 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3422 {
3423 MagickBooleanType
3424 proceed;
3425
cristyb5d5f722009-11-04 03:03:49 +00003426#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003427 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003428#endif
3429 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3430 image->rows);
3431 if (proceed == MagickFalse)
3432 status=MagickFalse;
3433 }
3434 }
3435 blur_image->type=image->type;
3436 blur_view=DestroyCacheView(blur_view);
3437 image_view=DestroyCacheView(image_view);
3438 kernel=(double *) RelinquishMagickMemory(kernel);
3439 if (status == MagickFalse)
3440 blur_image=DestroyImage(blur_image);
3441 return(blur_image);
3442}
3443
3444/*
3445%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3446% %
3447% %
3448% %
3449% S h a d e I m a g e %
3450% %
3451% %
3452% %
3453%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3454%
3455% ShadeImage() shines a distant light on an image to create a
3456% three-dimensional effect. You control the positioning of the light with
3457% azimuth and elevation; azimuth is measured in degrees off the x axis
3458% and elevation is measured in pixels above the Z axis.
3459%
3460% The format of the ShadeImage method is:
3461%
3462% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3463% const double azimuth,const double elevation,ExceptionInfo *exception)
3464%
3465% A description of each parameter follows:
3466%
3467% o image: the image.
3468%
3469% o gray: A value other than zero shades the intensity of each pixel.
3470%
3471% o azimuth, elevation: Define the light source direction.
3472%
3473% o exception: return any errors or warnings in this structure.
3474%
3475*/
3476MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3477 const double azimuth,const double elevation,ExceptionInfo *exception)
3478{
3479#define ShadeImageTag "Shade/Image"
3480
cristyc4c8d132010-01-07 01:58:38 +00003481 CacheView
3482 *image_view,
3483 *shade_view;
3484
cristy3ed852e2009-09-05 21:47:34 +00003485 Image
3486 *shade_image;
3487
cristy3ed852e2009-09-05 21:47:34 +00003488 MagickBooleanType
3489 status;
3490
cristybb503372010-05-27 20:51:26 +00003491 MagickOffsetType
3492 progress;
3493
cristy3ed852e2009-09-05 21:47:34 +00003494 PrimaryInfo
3495 light;
3496
cristybb503372010-05-27 20:51:26 +00003497 ssize_t
3498 y;
3499
cristy3ed852e2009-09-05 21:47:34 +00003500 /*
3501 Initialize shaded image attributes.
3502 */
3503 assert(image != (const Image *) NULL);
3504 assert(image->signature == MagickSignature);
3505 if (image->debug != MagickFalse)
3506 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3507 assert(exception != (ExceptionInfo *) NULL);
3508 assert(exception->signature == MagickSignature);
3509 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3510 if (shade_image == (Image *) NULL)
3511 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003512 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003513 {
cristy3ed852e2009-09-05 21:47:34 +00003514 shade_image=DestroyImage(shade_image);
3515 return((Image *) NULL);
3516 }
3517 /*
3518 Compute the light vector.
3519 */
3520 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3521 cos(DegreesToRadians(elevation));
3522 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3523 cos(DegreesToRadians(elevation));
3524 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3525 /*
3526 Shade image.
3527 */
3528 status=MagickTrue;
3529 progress=0;
3530 image_view=AcquireCacheView(image);
3531 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003532#if defined(MAGICKCORE_OPENMP_SUPPORT)
3533 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003534#endif
cristybb503372010-05-27 20:51:26 +00003535 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003536 {
3537 MagickRealType
3538 distance,
3539 normal_distance,
3540 shade;
3541
3542 PrimaryInfo
3543 normal;
3544
cristy4c08aed2011-07-01 19:47:50 +00003545 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003546 *restrict p,
3547 *restrict s0,
3548 *restrict s1,
3549 *restrict s2;
cristy3ed852e2009-09-05 21:47:34 +00003550
cristy4c08aed2011-07-01 19:47:50 +00003551 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003552 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003553
cristy117ff172010-08-15 21:35:32 +00003554 register ssize_t
3555 x;
3556
cristy3ed852e2009-09-05 21:47:34 +00003557 if (status == MagickFalse)
3558 continue;
3559 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3560 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3561 exception);
cristy4c08aed2011-07-01 19:47:50 +00003562 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003563 {
3564 status=MagickFalse;
3565 continue;
3566 }
3567 /*
3568 Shade this row of pixels.
3569 */
3570 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristyed231572011-07-14 02:18:59 +00003571 s0=p+GetPixelChannels(image);
3572 s1=s0+(image->columns+2)*GetPixelChannels(image);
3573 s2=s1+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003574 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003575 {
3576 /*
3577 Determine the surface normal and compute shading.
3578 */
cristyed231572011-07-14 02:18:59 +00003579 normal.x=(double) (GetPixelIntensity(image,s0-GetPixelChannels(image))+
3580 GetPixelIntensity(image,s1-GetPixelChannels(image))+
3581 GetPixelIntensity(image,s2-GetPixelChannels(image))-
3582 GetPixelIntensity(image,s0+GetPixelChannels(image))-
3583 GetPixelIntensity(image,s1+GetPixelChannels(image))-
3584 GetPixelIntensity(image,s2+GetPixelChannels(image)));
3585 normal.y=(double) (GetPixelIntensity(image,s2-GetPixelChannels(image))+
cristy4c08aed2011-07-01 19:47:50 +00003586 GetPixelIntensity(image,s2)+
cristyed231572011-07-14 02:18:59 +00003587 GetPixelIntensity(image,s2+GetPixelChannels(image))-
3588 GetPixelIntensity(image,s0-GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003589 GetPixelIntensity(image,s0)-
cristyed231572011-07-14 02:18:59 +00003590 GetPixelIntensity(image,s0+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003591 if ((normal.x == 0.0) && (normal.y == 0.0))
3592 shade=light.z;
3593 else
3594 {
3595 shade=0.0;
3596 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3597 if (distance > MagickEpsilon)
3598 {
3599 normal_distance=
3600 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3601 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3602 shade=distance/sqrt((double) normal_distance);
3603 }
3604 }
3605 if (gray != MagickFalse)
3606 {
cristy4c08aed2011-07-01 19:47:50 +00003607 SetPixelRed(shade_image,ClampToQuantum(shade),q);
3608 SetPixelGreen(shade_image,ClampToQuantum(shade),q);
3609 SetPixelBlue(shade_image,ClampToQuantum(shade),q);
cristy3ed852e2009-09-05 21:47:34 +00003610 }
3611 else
3612 {
cristy4c08aed2011-07-01 19:47:50 +00003613 SetPixelRed(shade_image,ClampToQuantum(QuantumScale*shade*
3614 GetPixelRed(image,s1)),q);
3615 SetPixelGreen(shade_image,ClampToQuantum(QuantumScale*shade*
3616 GetPixelGreen(image,s1)),q);
3617 SetPixelBlue(shade_image,ClampToQuantum(QuantumScale*shade*
3618 GetPixelBlue(image,s1)),q);
cristy3ed852e2009-09-05 21:47:34 +00003619 }
cristy4c08aed2011-07-01 19:47:50 +00003620 SetPixelAlpha(shade_image,GetPixelAlpha(image,s1),q);
cristyed231572011-07-14 02:18:59 +00003621 s0+=GetPixelChannels(image);
3622 s1+=GetPixelChannels(image);
3623 s2+=GetPixelChannels(image);
3624 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003625 }
3626 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3627 status=MagickFalse;
3628 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3629 {
3630 MagickBooleanType
3631 proceed;
3632
cristyb5d5f722009-11-04 03:03:49 +00003633#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003634 #pragma omp critical (MagickCore_ShadeImage)
3635#endif
3636 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3637 if (proceed == MagickFalse)
3638 status=MagickFalse;
3639 }
3640 }
3641 shade_view=DestroyCacheView(shade_view);
3642 image_view=DestroyCacheView(image_view);
3643 if (status == MagickFalse)
3644 shade_image=DestroyImage(shade_image);
3645 return(shade_image);
3646}
3647
3648/*
3649%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3650% %
3651% %
3652% %
3653% S h a r p e n I m a g e %
3654% %
3655% %
3656% %
3657%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3658%
3659% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3660% operator of the given radius and standard deviation (sigma). For
3661% reasonable results, radius should be larger than sigma. Use a radius of 0
3662% and SharpenImage() selects a suitable radius for you.
3663%
3664% Using a separable kernel would be faster, but the negative weights cancel
3665% out on the corners of the kernel producing often undesirable ringing in the
3666% filtered result; this can be avoided by using a 2D gaussian shaped image
3667% sharpening kernel instead.
3668%
3669% The format of the SharpenImage method is:
3670%
3671% Image *SharpenImage(const Image *image,const double radius,
3672% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003673%
3674% A description of each parameter follows:
3675%
3676% o image: the image.
3677%
cristy3ed852e2009-09-05 21:47:34 +00003678% o radius: the radius of the Gaussian, in pixels, not counting the center
3679% pixel.
3680%
3681% o sigma: the standard deviation of the Laplacian, in pixels.
3682%
3683% o exception: return any errors or warnings in this structure.
3684%
3685*/
cristy3ed852e2009-09-05 21:47:34 +00003686MagickExport Image *SharpenImage(const Image *image,const double radius,
3687 const double sigma,ExceptionInfo *exception)
3688{
cristy3ed852e2009-09-05 21:47:34 +00003689 double
cristy47e00502009-12-17 19:19:57 +00003690 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003691
3692 Image
3693 *sharp_image;
3694
cristy41cbe682011-07-15 19:12:37 +00003695 KernelInfo
3696 *kernel_info;
3697
cristybb503372010-05-27 20:51:26 +00003698 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003699 i;
3700
cristybb503372010-05-27 20:51:26 +00003701 size_t
cristy3ed852e2009-09-05 21:47:34 +00003702 width;
3703
cristy117ff172010-08-15 21:35:32 +00003704 ssize_t
3705 j,
3706 u,
3707 v;
3708
cristy3ed852e2009-09-05 21:47:34 +00003709 assert(image != (const Image *) NULL);
3710 assert(image->signature == MagickSignature);
3711 if (image->debug != MagickFalse)
3712 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3713 assert(exception != (ExceptionInfo *) NULL);
3714 assert(exception->signature == MagickSignature);
3715 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003716 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003717 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003718 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003719 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3720 kernel_info->width=width;
3721 kernel_info->height=width;
3722 kernel_info->signature=MagickSignature;
3723 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
3724 kernel_info->width*sizeof(*kernel_info->values));
3725 if (kernel_info->values == (double *) NULL)
3726 {
3727 kernel_info=DestroyKernelInfo(kernel_info);
3728 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3729 }
cristy3ed852e2009-09-05 21:47:34 +00003730 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003731 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003732 i=0;
3733 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003734 {
cristy47e00502009-12-17 19:19:57 +00003735 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003736 {
cristy41cbe682011-07-15 19:12:37 +00003737 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3738 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3739 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003740 i++;
3741 }
3742 }
cristy41cbe682011-07-15 19:12:37 +00003743 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy0a922382011-07-16 15:30:34 +00003744 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00003745 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003746 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003747 return(sharp_image);
3748}
3749
3750/*
3751%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3752% %
3753% %
3754% %
3755% S p r e a d I m a g e %
3756% %
3757% %
3758% %
3759%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3760%
3761% SpreadImage() is a special effects method that randomly displaces each
3762% pixel in a block defined by the radius parameter.
3763%
3764% The format of the SpreadImage method is:
3765%
3766% Image *SpreadImage(const Image *image,const double radius,
3767% ExceptionInfo *exception)
3768%
3769% A description of each parameter follows:
3770%
3771% o image: the image.
3772%
3773% o radius: Choose a random pixel in a neighborhood of this extent.
3774%
3775% o exception: return any errors or warnings in this structure.
3776%
3777*/
3778MagickExport Image *SpreadImage(const Image *image,const double radius,
3779 ExceptionInfo *exception)
3780{
3781#define SpreadImageTag "Spread/Image"
3782
cristyfa112112010-01-04 17:48:07 +00003783 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003784 *image_view,
3785 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003786
cristy3ed852e2009-09-05 21:47:34 +00003787 Image
3788 *spread_image;
3789
cristy3ed852e2009-09-05 21:47:34 +00003790 MagickBooleanType
3791 status;
3792
cristybb503372010-05-27 20:51:26 +00003793 MagickOffsetType
3794 progress;
3795
cristy4c08aed2011-07-01 19:47:50 +00003796 PixelInfo
cristyddd82202009-11-03 20:14:50 +00003797 bias;
cristy3ed852e2009-09-05 21:47:34 +00003798
3799 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003800 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003801
cristybb503372010-05-27 20:51:26 +00003802 size_t
cristy3ed852e2009-09-05 21:47:34 +00003803 width;
3804
cristybb503372010-05-27 20:51:26 +00003805 ssize_t
3806 y;
3807
cristy3ed852e2009-09-05 21:47:34 +00003808 /*
3809 Initialize spread image attributes.
3810 */
3811 assert(image != (Image *) NULL);
3812 assert(image->signature == MagickSignature);
3813 if (image->debug != MagickFalse)
3814 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3815 assert(exception != (ExceptionInfo *) NULL);
3816 assert(exception->signature == MagickSignature);
3817 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3818 exception);
3819 if (spread_image == (Image *) NULL)
3820 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003821 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003822 {
cristy3ed852e2009-09-05 21:47:34 +00003823 spread_image=DestroyImage(spread_image);
3824 return((Image *) NULL);
3825 }
3826 /*
3827 Spread image.
3828 */
3829 status=MagickTrue;
3830 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003831 GetPixelInfo(spread_image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003832 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003833 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003834 image_view=AcquireCacheView(image);
3835 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003836#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00003837 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00003838#endif
cristybb503372010-05-27 20:51:26 +00003839 for (y=0; y < (ssize_t) spread_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003840 {
cristy5c9e6f22010-09-17 17:31:01 +00003841 const int
3842 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003843
cristy4c08aed2011-07-01 19:47:50 +00003844 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003845 pixel;
3846
cristy4c08aed2011-07-01 19:47:50 +00003847 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003848 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003849
cristy117ff172010-08-15 21:35:32 +00003850 register ssize_t
3851 x;
3852
cristy3ed852e2009-09-05 21:47:34 +00003853 if (status == MagickFalse)
3854 continue;
cristy9f7e7cb2011-03-26 00:49:57 +00003855 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003856 exception);
cristy4c08aed2011-07-01 19:47:50 +00003857 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003858 {
3859 status=MagickFalse;
3860 continue;
3861 }
cristyddd82202009-11-03 20:14:50 +00003862 pixel=bias;
cristybb503372010-05-27 20:51:26 +00003863 for (x=0; x < (ssize_t) spread_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003864 {
cristy4c08aed2011-07-01 19:47:50 +00003865 (void) InterpolatePixelInfo(image,image_view,
cristy8a7c3e82011-03-26 02:10:53 +00003866 UndefinedInterpolatePixel,(double) x+width*(GetPseudoRandomValue(
3867 random_info[id])-0.5),(double) y+width*(GetPseudoRandomValue(
3868 random_info[id])-0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003869 SetPixelPixelInfo(spread_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00003870 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003871 }
cristy9f7e7cb2011-03-26 00:49:57 +00003872 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003873 status=MagickFalse;
3874 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3875 {
3876 MagickBooleanType
3877 proceed;
3878
cristyb557a152011-02-22 12:14:30 +00003879#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003880 #pragma omp critical (MagickCore_SpreadImage)
3881#endif
3882 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3883 if (proceed == MagickFalse)
3884 status=MagickFalse;
3885 }
3886 }
cristy9f7e7cb2011-03-26 00:49:57 +00003887 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003888 image_view=DestroyCacheView(image_view);
3889 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003890 return(spread_image);
3891}
3892
3893/*
3894%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3895% %
3896% %
3897% %
cristy0834d642011-03-18 18:26:08 +00003898% S t a t i s t i c I m a g e %
3899% %
3900% %
3901% %
3902%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3903%
3904% StatisticImage() makes each pixel the min / max / median / mode / etc. of
cristy8d752042011-03-19 01:00:36 +00003905% the neighborhood of the specified width and height.
cristy0834d642011-03-18 18:26:08 +00003906%
3907% The format of the StatisticImage method is:
3908%
3909% Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00003910% const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00003911%
3912% A description of each parameter follows:
3913%
3914% o image: the image.
3915%
cristy0834d642011-03-18 18:26:08 +00003916% o type: the statistic type (median, mode, etc.).
3917%
cristy95c38342011-03-18 22:39:51 +00003918% o width: the width of the pixel neighborhood.
3919%
3920% o height: the height of the pixel neighborhood.
cristy0834d642011-03-18 18:26:08 +00003921%
3922% o exception: return any errors or warnings in this structure.
3923%
3924*/
3925
cristy733678d2011-03-18 21:29:28 +00003926#define ListChannels 5
3927
3928typedef struct _ListNode
3929{
3930 size_t
3931 next[9],
3932 count,
3933 signature;
3934} ListNode;
3935
3936typedef struct _SkipList
3937{
3938 ssize_t
3939 level;
3940
3941 ListNode
3942 *nodes;
3943} SkipList;
3944
3945typedef struct _PixelList
3946{
3947 size_t
cristy6fc86bb2011-03-18 23:45:16 +00003948 length,
cristy733678d2011-03-18 21:29:28 +00003949 seed,
3950 signature;
3951
3952 SkipList
3953 lists[ListChannels];
3954} PixelList;
3955
3956static PixelList *DestroyPixelList(PixelList *pixel_list)
3957{
3958 register ssize_t
3959 i;
3960
3961 if (pixel_list == (PixelList *) NULL)
3962 return((PixelList *) NULL);
3963 for (i=0; i < ListChannels; i++)
3964 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
3965 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
3966 pixel_list->lists[i].nodes);
3967 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
3968 return(pixel_list);
3969}
3970
3971static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
3972{
3973 register ssize_t
3974 i;
3975
3976 assert(pixel_list != (PixelList **) NULL);
3977 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
3978 if (pixel_list[i] != (PixelList *) NULL)
3979 pixel_list[i]=DestroyPixelList(pixel_list[i]);
3980 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
3981 return(pixel_list);
3982}
3983
cristy6fc86bb2011-03-18 23:45:16 +00003984static PixelList *AcquirePixelList(const size_t width,const size_t height)
cristy733678d2011-03-18 21:29:28 +00003985{
3986 PixelList
3987 *pixel_list;
3988
3989 register ssize_t
3990 i;
3991
3992 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
3993 if (pixel_list == (PixelList *) NULL)
3994 return(pixel_list);
3995 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
cristy6fc86bb2011-03-18 23:45:16 +00003996 pixel_list->length=width*height;
cristy733678d2011-03-18 21:29:28 +00003997 for (i=0; i < ListChannels; i++)
3998 {
3999 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
4000 sizeof(*pixel_list->lists[i].nodes));
4001 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
4002 return(DestroyPixelList(pixel_list));
4003 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
4004 sizeof(*pixel_list->lists[i].nodes));
4005 }
4006 pixel_list->signature=MagickSignature;
4007 return(pixel_list);
4008}
4009
cristy6fc86bb2011-03-18 23:45:16 +00004010static PixelList **AcquirePixelListThreadSet(const size_t width,
4011 const size_t height)
cristy733678d2011-03-18 21:29:28 +00004012{
4013 PixelList
4014 **pixel_list;
4015
4016 register ssize_t
4017 i;
4018
4019 size_t
4020 number_threads;
4021
4022 number_threads=GetOpenMPMaximumThreads();
4023 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
4024 sizeof(*pixel_list));
4025 if (pixel_list == (PixelList **) NULL)
4026 return((PixelList **) NULL);
4027 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
4028 for (i=0; i < (ssize_t) number_threads; i++)
4029 {
cristy6fc86bb2011-03-18 23:45:16 +00004030 pixel_list[i]=AcquirePixelList(width,height);
cristy733678d2011-03-18 21:29:28 +00004031 if (pixel_list[i] == (PixelList *) NULL)
4032 return(DestroyPixelListThreadSet(pixel_list));
4033 }
4034 return(pixel_list);
4035}
4036
4037static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
4038 const size_t color)
4039{
4040 register SkipList
4041 *list;
4042
4043 register ssize_t
4044 level;
4045
4046 size_t
4047 search,
4048 update[9];
4049
4050 /*
4051 Initialize the node.
4052 */
4053 list=pixel_list->lists+channel;
4054 list->nodes[color].signature=pixel_list->signature;
4055 list->nodes[color].count=1;
4056 /*
4057 Determine where it belongs in the list.
4058 */
4059 search=65536UL;
4060 for (level=list->level; level >= 0; level--)
4061 {
4062 while (list->nodes[search].next[level] < color)
4063 search=list->nodes[search].next[level];
4064 update[level]=search;
4065 }
4066 /*
4067 Generate a pseudo-random level for this node.
4068 */
4069 for (level=0; ; level++)
4070 {
4071 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
4072 if ((pixel_list->seed & 0x300) != 0x300)
4073 break;
4074 }
4075 if (level > 8)
4076 level=8;
4077 if (level > (list->level+2))
4078 level=list->level+2;
4079 /*
4080 If we're raising the list's level, link back to the root node.
4081 */
4082 while (level > list->level)
4083 {
4084 list->level++;
4085 update[list->level]=65536UL;
4086 }
4087 /*
4088 Link the node into the skip-list.
4089 */
4090 do
4091 {
4092 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
4093 list->nodes[update[level]].next[level]=color;
cristy3cba8ca2011-03-19 01:29:12 +00004094 } while (level-- > 0);
cristy733678d2011-03-18 21:29:28 +00004095}
4096
cristy4c08aed2011-07-01 19:47:50 +00004097static PixelInfo GetMaximumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004098{
cristy4c08aed2011-07-01 19:47:50 +00004099 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004100 pixel;
4101
4102 register SkipList
4103 *list;
4104
4105 register ssize_t
4106 channel;
4107
4108 size_t
cristyd76c51e2011-03-26 00:21:26 +00004109 color,
4110 maximum;
cristy49f37242011-03-22 18:18:23 +00004111
4112 ssize_t
cristy6fc86bb2011-03-18 23:45:16 +00004113 count;
4114
4115 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004116 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004117
4118 /*
4119 Find the maximum value for each of the color.
4120 */
4121 for (channel=0; channel < 5; channel++)
4122 {
4123 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004124 color=65536L;
cristy6fc86bb2011-03-18 23:45:16 +00004125 count=0;
cristy49f37242011-03-22 18:18:23 +00004126 maximum=list->nodes[color].next[0];
cristy6fc86bb2011-03-18 23:45:16 +00004127 do
4128 {
4129 color=list->nodes[color].next[0];
cristy49f37242011-03-22 18:18:23 +00004130 if (color > maximum)
4131 maximum=color;
cristy6fc86bb2011-03-18 23:45:16 +00004132 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004133 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004134 channels[channel]=(unsigned short) maximum;
4135 }
cristy4c08aed2011-07-01 19:47:50 +00004136 GetPixelInfo((const Image *) NULL,&pixel);
cristy49f37242011-03-22 18:18:23 +00004137 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4138 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4139 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004140 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4141 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy49f37242011-03-22 18:18:23 +00004142 return(pixel);
4143}
4144
cristy4c08aed2011-07-01 19:47:50 +00004145static PixelInfo GetMeanPixelList(PixelList *pixel_list)
cristy49f37242011-03-22 18:18:23 +00004146{
cristy4c08aed2011-07-01 19:47:50 +00004147 PixelInfo
cristy49f37242011-03-22 18:18:23 +00004148 pixel;
4149
cristy80a99a32011-03-30 01:30:23 +00004150 MagickRealType
4151 sum;
4152
cristy49f37242011-03-22 18:18:23 +00004153 register SkipList
4154 *list;
4155
4156 register ssize_t
4157 channel;
4158
4159 size_t
cristy80a99a32011-03-30 01:30:23 +00004160 color;
cristy49f37242011-03-22 18:18:23 +00004161
4162 ssize_t
4163 count;
4164
4165 unsigned short
4166 channels[ListChannels];
4167
4168 /*
4169 Find the mean value for each of the color.
4170 */
4171 for (channel=0; channel < 5; channel++)
4172 {
4173 list=pixel_list->lists+channel;
4174 color=65536L;
4175 count=0;
cristy80a99a32011-03-30 01:30:23 +00004176 sum=0.0;
cristy49f37242011-03-22 18:18:23 +00004177 do
4178 {
4179 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004180 sum+=(MagickRealType) list->nodes[color].count*color;
cristy49f37242011-03-22 18:18:23 +00004181 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004182 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004183 sum/=pixel_list->length;
4184 channels[channel]=(unsigned short) sum;
cristy6fc86bb2011-03-18 23:45:16 +00004185 }
cristy4c08aed2011-07-01 19:47:50 +00004186 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004187 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4188 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4189 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004190 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4191 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004192 return(pixel);
4193}
4194
cristy4c08aed2011-07-01 19:47:50 +00004195static PixelInfo GetMedianPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004196{
cristy4c08aed2011-07-01 19:47:50 +00004197 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004198 pixel;
4199
4200 register SkipList
4201 *list;
4202
4203 register ssize_t
4204 channel;
4205
4206 size_t
cristy49f37242011-03-22 18:18:23 +00004207 color;
4208
4209 ssize_t
cristy733678d2011-03-18 21:29:28 +00004210 count;
4211
4212 unsigned short
4213 channels[ListChannels];
4214
4215 /*
4216 Find the median value for each of the color.
4217 */
cristy733678d2011-03-18 21:29:28 +00004218 for (channel=0; channel < 5; channel++)
4219 {
4220 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004221 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004222 count=0;
4223 do
4224 {
4225 color=list->nodes[color].next[0];
4226 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004227 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy6fc86bb2011-03-18 23:45:16 +00004228 channels[channel]=(unsigned short) color;
4229 }
cristy4c08aed2011-07-01 19:47:50 +00004230 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004231 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4232 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4233 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004234 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4235 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004236 return(pixel);
4237}
4238
cristy4c08aed2011-07-01 19:47:50 +00004239static PixelInfo GetMinimumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004240{
cristy4c08aed2011-07-01 19:47:50 +00004241 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004242 pixel;
4243
4244 register SkipList
4245 *list;
4246
4247 register ssize_t
4248 channel;
4249
4250 size_t
cristyd76c51e2011-03-26 00:21:26 +00004251 color,
4252 minimum;
cristy6fc86bb2011-03-18 23:45:16 +00004253
cristy49f37242011-03-22 18:18:23 +00004254 ssize_t
4255 count;
4256
cristy6fc86bb2011-03-18 23:45:16 +00004257 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004258 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004259
4260 /*
4261 Find the minimum value for each of the color.
4262 */
4263 for (channel=0; channel < 5; channel++)
4264 {
4265 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004266 count=0;
cristy6fc86bb2011-03-18 23:45:16 +00004267 color=65536UL;
cristy49f37242011-03-22 18:18:23 +00004268 minimum=list->nodes[color].next[0];
4269 do
4270 {
4271 color=list->nodes[color].next[0];
4272 if (color < minimum)
4273 minimum=color;
4274 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004275 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004276 channels[channel]=(unsigned short) minimum;
cristy733678d2011-03-18 21:29:28 +00004277 }
cristy4c08aed2011-07-01 19:47:50 +00004278 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004279 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4280 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4281 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004282 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4283 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004284 return(pixel);
4285}
4286
cristy4c08aed2011-07-01 19:47:50 +00004287static PixelInfo GetModePixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004288{
cristy4c08aed2011-07-01 19:47:50 +00004289 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004290 pixel;
4291
4292 register SkipList
4293 *list;
4294
4295 register ssize_t
4296 channel;
4297
4298 size_t
4299 color,
cristy733678d2011-03-18 21:29:28 +00004300 max_count,
cristy6fc86bb2011-03-18 23:45:16 +00004301 mode;
cristy733678d2011-03-18 21:29:28 +00004302
cristy49f37242011-03-22 18:18:23 +00004303 ssize_t
4304 count;
4305
cristy733678d2011-03-18 21:29:28 +00004306 unsigned short
4307 channels[5];
4308
4309 /*
glennrp30d2dc62011-06-25 03:17:16 +00004310 Make each pixel the 'predominant color' of the specified neighborhood.
cristy733678d2011-03-18 21:29:28 +00004311 */
cristy733678d2011-03-18 21:29:28 +00004312 for (channel=0; channel < 5; channel++)
4313 {
4314 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004315 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004316 mode=color;
4317 max_count=list->nodes[mode].count;
4318 count=0;
4319 do
4320 {
4321 color=list->nodes[color].next[0];
4322 if (list->nodes[color].count > max_count)
4323 {
4324 mode=color;
4325 max_count=list->nodes[mode].count;
4326 }
4327 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004328 } while (count < (ssize_t) pixel_list->length);
cristy733678d2011-03-18 21:29:28 +00004329 channels[channel]=(unsigned short) mode;
4330 }
cristy4c08aed2011-07-01 19:47:50 +00004331 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004332 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4333 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4334 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004335 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4336 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004337 return(pixel);
4338}
4339
cristy4c08aed2011-07-01 19:47:50 +00004340static PixelInfo GetNonpeakPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004341{
cristy4c08aed2011-07-01 19:47:50 +00004342 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004343 pixel;
4344
4345 register SkipList
4346 *list;
4347
4348 register ssize_t
4349 channel;
4350
4351 size_t
cristy733678d2011-03-18 21:29:28 +00004352 color,
cristy733678d2011-03-18 21:29:28 +00004353 next,
4354 previous;
4355
cristy49f37242011-03-22 18:18:23 +00004356 ssize_t
4357 count;
4358
cristy733678d2011-03-18 21:29:28 +00004359 unsigned short
4360 channels[5];
4361
4362 /*
cristy49f37242011-03-22 18:18:23 +00004363 Finds the non peak value for each of the colors.
cristy733678d2011-03-18 21:29:28 +00004364 */
cristy733678d2011-03-18 21:29:28 +00004365 for (channel=0; channel < 5; channel++)
4366 {
4367 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004368 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004369 next=list->nodes[color].next[0];
4370 count=0;
4371 do
4372 {
4373 previous=color;
4374 color=next;
4375 next=list->nodes[color].next[0];
4376 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004377 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy733678d2011-03-18 21:29:28 +00004378 if ((previous == 65536UL) && (next != 65536UL))
4379 color=next;
4380 else
4381 if ((previous != 65536UL) && (next == 65536UL))
4382 color=previous;
4383 channels[channel]=(unsigned short) color;
4384 }
cristy4c08aed2011-07-01 19:47:50 +00004385 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004386 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4387 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4388 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004389 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4390 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy733678d2011-03-18 21:29:28 +00004391 return(pixel);
4392}
4393
cristy4c08aed2011-07-01 19:47:50 +00004394static PixelInfo GetStandardDeviationPixelList(PixelList *pixel_list)
cristy9a68cbb2011-03-29 00:51:23 +00004395{
cristy4c08aed2011-07-01 19:47:50 +00004396 PixelInfo
cristy9a68cbb2011-03-29 00:51:23 +00004397 pixel;
4398
cristy80a99a32011-03-30 01:30:23 +00004399 MagickRealType
4400 sum,
4401 sum_squared;
4402
cristy9a68cbb2011-03-29 00:51:23 +00004403 register SkipList
4404 *list;
4405
4406 register ssize_t
4407 channel;
4408
4409 size_t
cristy80a99a32011-03-30 01:30:23 +00004410 color;
cristy9a68cbb2011-03-29 00:51:23 +00004411
4412 ssize_t
4413 count;
4414
4415 unsigned short
4416 channels[ListChannels];
4417
4418 /*
cristy80a99a32011-03-30 01:30:23 +00004419 Find the standard-deviation value for each of the color.
cristy9a68cbb2011-03-29 00:51:23 +00004420 */
4421 for (channel=0; channel < 5; channel++)
4422 {
4423 list=pixel_list->lists+channel;
4424 color=65536L;
4425 count=0;
cristy80a99a32011-03-30 01:30:23 +00004426 sum=0.0;
4427 sum_squared=0.0;
cristy9a68cbb2011-03-29 00:51:23 +00004428 do
4429 {
cristy80a99a32011-03-30 01:30:23 +00004430 register ssize_t
4431 i;
4432
cristy9a68cbb2011-03-29 00:51:23 +00004433 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004434 sum+=(MagickRealType) list->nodes[color].count*color;
4435 for (i=0; i < (ssize_t) list->nodes[color].count; i++)
4436 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
cristy9a68cbb2011-03-29 00:51:23 +00004437 count+=list->nodes[color].count;
4438 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004439 sum/=pixel_list->length;
4440 sum_squared/=pixel_list->length;
4441 channels[channel]=(unsigned short) sqrt(sum_squared-(sum*sum));
cristy9a68cbb2011-03-29 00:51:23 +00004442 }
cristy4c08aed2011-07-01 19:47:50 +00004443 GetPixelInfo((const Image *) NULL,&pixel);
cristy9a68cbb2011-03-29 00:51:23 +00004444 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4445 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4446 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004447 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4448 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy9a68cbb2011-03-29 00:51:23 +00004449 return(pixel);
4450}
4451
cristy4c08aed2011-07-01 19:47:50 +00004452static inline void InsertPixelList(const Image *image,const Quantum *pixel,
4453 PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004454{
4455 size_t
4456 signature;
4457
4458 unsigned short
4459 index;
4460
cristy4c08aed2011-07-01 19:47:50 +00004461 index=ScaleQuantumToShort(GetPixelRed(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004462 signature=pixel_list->lists[0].nodes[index].signature;
4463 if (signature == pixel_list->signature)
4464 pixel_list->lists[0].nodes[index].count++;
4465 else
4466 AddNodePixelList(pixel_list,0,index);
cristy4c08aed2011-07-01 19:47:50 +00004467 index=ScaleQuantumToShort(GetPixelGreen(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004468 signature=pixel_list->lists[1].nodes[index].signature;
4469 if (signature == pixel_list->signature)
4470 pixel_list->lists[1].nodes[index].count++;
4471 else
4472 AddNodePixelList(pixel_list,1,index);
cristy4c08aed2011-07-01 19:47:50 +00004473 index=ScaleQuantumToShort(GetPixelBlue(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004474 signature=pixel_list->lists[2].nodes[index].signature;
4475 if (signature == pixel_list->signature)
4476 pixel_list->lists[2].nodes[index].count++;
4477 else
4478 AddNodePixelList(pixel_list,2,index);
cristy4c08aed2011-07-01 19:47:50 +00004479 index=ScaleQuantumToShort(GetPixelAlpha(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004480 signature=pixel_list->lists[3].nodes[index].signature;
4481 if (signature == pixel_list->signature)
4482 pixel_list->lists[3].nodes[index].count++;
4483 else
4484 AddNodePixelList(pixel_list,3,index);
4485 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004486 index=ScaleQuantumToShort(GetPixelBlack(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004487 signature=pixel_list->lists[4].nodes[index].signature;
4488 if (signature == pixel_list->signature)
4489 pixel_list->lists[4].nodes[index].count++;
4490 else
4491 AddNodePixelList(pixel_list,4,index);
4492}
4493
cristy80c99742011-04-04 14:46:39 +00004494static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
4495{
4496 if (x < 0)
4497 return(-x);
4498 return(x);
4499}
4500
cristy733678d2011-03-18 21:29:28 +00004501static void ResetPixelList(PixelList *pixel_list)
4502{
4503 int
4504 level;
4505
4506 register ListNode
4507 *root;
4508
4509 register SkipList
4510 *list;
4511
4512 register ssize_t
4513 channel;
4514
4515 /*
4516 Reset the skip-list.
4517 */
4518 for (channel=0; channel < 5; channel++)
4519 {
4520 list=pixel_list->lists+channel;
4521 root=list->nodes+65536UL;
4522 list->level=0;
4523 for (level=0; level < 9; level++)
4524 root->next[level]=65536UL;
4525 }
4526 pixel_list->seed=pixel_list->signature++;
4527}
4528
cristy0834d642011-03-18 18:26:08 +00004529MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00004530 const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00004531{
cristy3cba8ca2011-03-19 01:29:12 +00004532#define StatisticWidth \
cristyd76c51e2011-03-26 00:21:26 +00004533 (width == 0 ? GetOptimalKernelWidth2D((double) width,0.5) : width)
cristy3cba8ca2011-03-19 01:29:12 +00004534#define StatisticHeight \
cristyd76c51e2011-03-26 00:21:26 +00004535 (height == 0 ? GetOptimalKernelWidth2D((double) height,0.5) : height)
cristy0834d642011-03-18 18:26:08 +00004536#define StatisticImageTag "Statistic/Image"
4537
4538 CacheView
4539 *image_view,
4540 *statistic_view;
4541
4542 Image
4543 *statistic_image;
4544
4545 MagickBooleanType
4546 status;
4547
4548 MagickOffsetType
4549 progress;
4550
4551 PixelList
4552 **restrict pixel_list;
4553
cristy0834d642011-03-18 18:26:08 +00004554 ssize_t
4555 y;
4556
4557 /*
4558 Initialize statistics image attributes.
4559 */
4560 assert(image != (Image *) NULL);
4561 assert(image->signature == MagickSignature);
4562 if (image->debug != MagickFalse)
4563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4564 assert(exception != (ExceptionInfo *) NULL);
4565 assert(exception->signature == MagickSignature);
cristy0834d642011-03-18 18:26:08 +00004566 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4567 exception);
4568 if (statistic_image == (Image *) NULL)
4569 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004570 if (SetImageStorageClass(statistic_image,DirectClass,exception) == MagickFalse)
cristy0834d642011-03-18 18:26:08 +00004571 {
cristy0834d642011-03-18 18:26:08 +00004572 statistic_image=DestroyImage(statistic_image);
4573 return((Image *) NULL);
4574 }
cristy6fc86bb2011-03-18 23:45:16 +00004575 pixel_list=AcquirePixelListThreadSet(StatisticWidth,StatisticHeight);
cristy0834d642011-03-18 18:26:08 +00004576 if (pixel_list == (PixelList **) NULL)
4577 {
4578 statistic_image=DestroyImage(statistic_image);
4579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4580 }
4581 /*
cristy8d752042011-03-19 01:00:36 +00004582 Make each pixel the min / max / median / mode / etc. of the neighborhood.
cristy0834d642011-03-18 18:26:08 +00004583 */
4584 status=MagickTrue;
4585 progress=0;
4586 image_view=AcquireCacheView(image);
4587 statistic_view=AcquireCacheView(statistic_image);
4588#if defined(MAGICKCORE_OPENMP_SUPPORT)
4589 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4590#endif
4591 for (y=0; y < (ssize_t) statistic_image->rows; y++)
4592 {
4593 const int
4594 id = GetOpenMPThreadId();
4595
cristy4c08aed2011-07-01 19:47:50 +00004596 register const Quantum
cristy0834d642011-03-18 18:26:08 +00004597 *restrict p;
4598
cristy4c08aed2011-07-01 19:47:50 +00004599 register Quantum
cristy0834d642011-03-18 18:26:08 +00004600 *restrict q;
4601
4602 register ssize_t
4603 x;
4604
4605 if (status == MagickFalse)
4606 continue;
cristy6fc86bb2011-03-18 23:45:16 +00004607 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) StatisticWidth/2L),y-
4608 (ssize_t) (StatisticHeight/2L),image->columns+StatisticWidth,
4609 StatisticHeight,exception);
cristy3cba8ca2011-03-19 01:29:12 +00004610 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004611 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy0834d642011-03-18 18:26:08 +00004612 {
4613 status=MagickFalse;
4614 continue;
4615 }
cristy0834d642011-03-18 18:26:08 +00004616 for (x=0; x < (ssize_t) statistic_image->columns; x++)
4617 {
cristy4c08aed2011-07-01 19:47:50 +00004618 PixelInfo
cristy0834d642011-03-18 18:26:08 +00004619 pixel;
4620
cristy4c08aed2011-07-01 19:47:50 +00004621 register const Quantum
cristy6e3026a2011-03-19 00:54:38 +00004622 *restrict r;
4623
cristy0834d642011-03-18 18:26:08 +00004624 register ssize_t
4625 u,
4626 v;
4627
4628 r=p;
cristy0834d642011-03-18 18:26:08 +00004629 ResetPixelList(pixel_list[id]);
cristy6e4c3292011-03-19 00:53:55 +00004630 for (v=0; v < (ssize_t) StatisticHeight; v++)
cristy0834d642011-03-18 18:26:08 +00004631 {
cristy6e4c3292011-03-19 00:53:55 +00004632 for (u=0; u < (ssize_t) StatisticWidth; u++)
cristyed231572011-07-14 02:18:59 +00004633 InsertPixelList(image,r+u*GetPixelChannels(image),pixel_list[id]);
4634 r+=(image->columns+StatisticWidth)*GetPixelChannels(image);
cristy0834d642011-03-18 18:26:08 +00004635 }
cristy4c08aed2011-07-01 19:47:50 +00004636 GetPixelInfo(image,&pixel);
cristy490408a2011-07-07 14:42:05 +00004637 SetPixelInfo(image,p+(StatisticWidth*StatisticHeight/2)*
cristyed231572011-07-14 02:18:59 +00004638 GetPixelChannels(image),&pixel);
cristy0834d642011-03-18 18:26:08 +00004639 switch (type)
4640 {
cristy80c99742011-04-04 14:46:39 +00004641 case GradientStatistic:
4642 {
cristy4c08aed2011-07-01 19:47:50 +00004643 PixelInfo
cristy80c99742011-04-04 14:46:39 +00004644 maximum,
4645 minimum;
4646
4647 minimum=GetMinimumPixelList(pixel_list[id]);
4648 maximum=GetMaximumPixelList(pixel_list[id]);
4649 pixel.red=MagickAbsoluteValue(maximum.red-minimum.red);
4650 pixel.green=MagickAbsoluteValue(maximum.green-minimum.green);
4651 pixel.blue=MagickAbsoluteValue(maximum.blue-minimum.blue);
cristy4c08aed2011-07-01 19:47:50 +00004652 pixel.alpha=MagickAbsoluteValue(maximum.alpha-minimum.alpha);
cristy80c99742011-04-04 14:46:39 +00004653 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004654 pixel.black=MagickAbsoluteValue(maximum.black-minimum.black);
cristy80c99742011-04-04 14:46:39 +00004655 break;
4656 }
cristy6fc86bb2011-03-18 23:45:16 +00004657 case MaximumStatistic:
4658 {
4659 pixel=GetMaximumPixelList(pixel_list[id]);
4660 break;
4661 }
cristy49f37242011-03-22 18:18:23 +00004662 case MeanStatistic:
4663 {
4664 pixel=GetMeanPixelList(pixel_list[id]);
4665 break;
4666 }
cristyf2ad14a2011-03-18 18:57:25 +00004667 case MedianStatistic:
cristy6fc86bb2011-03-18 23:45:16 +00004668 default:
cristyf2ad14a2011-03-18 18:57:25 +00004669 {
4670 pixel=GetMedianPixelList(pixel_list[id]);
4671 break;
4672 }
cristy6fc86bb2011-03-18 23:45:16 +00004673 case MinimumStatistic:
4674 {
4675 pixel=GetMinimumPixelList(pixel_list[id]);
4676 break;
4677 }
cristyf2ad14a2011-03-18 18:57:25 +00004678 case ModeStatistic:
4679 {
4680 pixel=GetModePixelList(pixel_list[id]);
4681 break;
4682 }
4683 case NonpeakStatistic:
4684 {
4685 pixel=GetNonpeakPixelList(pixel_list[id]);
4686 break;
4687 }
cristy9a68cbb2011-03-29 00:51:23 +00004688 case StandardDeviationStatistic:
4689 {
4690 pixel=GetStandardDeviationPixelList(pixel_list[id]);
4691 break;
4692 }
cristy0834d642011-03-18 18:26:08 +00004693 }
cristyed231572011-07-14 02:18:59 +00004694 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004695 SetPixelRed(statistic_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00004696 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004697 SetPixelGreen(statistic_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00004698 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004699 SetPixelBlue(statistic_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00004700 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy0834d642011-03-18 18:26:08 +00004701 (image->colorspace == CMYKColorspace))
cristy490408a2011-07-07 14:42:05 +00004702 SetPixelBlack(statistic_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +00004703 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00004704 (image->matte != MagickFalse))
cristy490408a2011-07-07 14:42:05 +00004705 SetPixelAlpha(statistic_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00004706 p+=GetPixelChannels(image);
4707 q+=GetPixelChannels(statistic_image);
cristy0834d642011-03-18 18:26:08 +00004708 }
4709 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
4710 status=MagickFalse;
4711 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4712 {
4713 MagickBooleanType
4714 proceed;
4715
4716#if defined(MAGICKCORE_OPENMP_SUPPORT)
4717 #pragma omp critical (MagickCore_StatisticImage)
4718#endif
4719 proceed=SetImageProgress(image,StatisticImageTag,progress++,
4720 image->rows);
4721 if (proceed == MagickFalse)
4722 status=MagickFalse;
4723 }
4724 }
4725 statistic_view=DestroyCacheView(statistic_view);
4726 image_view=DestroyCacheView(image_view);
4727 pixel_list=DestroyPixelListThreadSet(pixel_list);
4728 return(statistic_image);
4729}
4730
4731/*
4732%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4733% %
4734% %
4735% %
cristy3ed852e2009-09-05 21:47:34 +00004736% U n s h a r p M a s k I m a g e %
4737% %
4738% %
4739% %
4740%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4741%
4742% UnsharpMaskImage() sharpens one or more image channels. We convolve the
4743% image with a Gaussian operator of the given radius and standard deviation
4744% (sigma). For reasonable results, radius should be larger than sigma. Use a
4745% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
4746%
4747% The format of the UnsharpMaskImage method is:
4748%
4749% Image *UnsharpMaskImage(const Image *image,const double radius,
4750% const double sigma,const double amount,const double threshold,
4751% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004752%
4753% A description of each parameter follows:
4754%
4755% o image: the image.
4756%
cristy3ed852e2009-09-05 21:47:34 +00004757% o radius: the radius of the Gaussian, in pixels, not counting the center
4758% pixel.
4759%
4760% o sigma: the standard deviation of the Gaussian, in pixels.
4761%
4762% o amount: the percentage of the difference between the original and the
4763% blur image that is added back into the original.
4764%
4765% o threshold: the threshold in pixels needed to apply the diffence amount.
4766%
4767% o exception: return any errors or warnings in this structure.
4768%
4769*/
cristyf4ad9df2011-07-08 16:49:03 +00004770MagickExport Image *UnsharpMaskImage(const Image *image,
4771 const double radius,const double sigma,const double amount,
4772 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004773{
4774#define SharpenImageTag "Sharpen/Image"
4775
cristyc4c8d132010-01-07 01:58:38 +00004776 CacheView
4777 *image_view,
4778 *unsharp_view;
4779
cristy3ed852e2009-09-05 21:47:34 +00004780 Image
4781 *unsharp_image;
4782
cristy3ed852e2009-09-05 21:47:34 +00004783 MagickBooleanType
4784 status;
4785
cristybb503372010-05-27 20:51:26 +00004786 MagickOffsetType
4787 progress;
4788
cristy4c08aed2011-07-01 19:47:50 +00004789 PixelInfo
cristyddd82202009-11-03 20:14:50 +00004790 bias;
cristy3ed852e2009-09-05 21:47:34 +00004791
4792 MagickRealType
4793 quantum_threshold;
4794
cristybb503372010-05-27 20:51:26 +00004795 ssize_t
4796 y;
4797
cristy3ed852e2009-09-05 21:47:34 +00004798 assert(image != (const Image *) NULL);
4799 assert(image->signature == MagickSignature);
4800 if (image->debug != MagickFalse)
4801 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4802 assert(exception != (ExceptionInfo *) NULL);
cristyf4ad9df2011-07-08 16:49:03 +00004803 unsharp_image=BlurImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004804 if (unsharp_image == (Image *) NULL)
4805 return((Image *) NULL);
4806 quantum_threshold=(MagickRealType) QuantumRange*threshold;
4807 /*
4808 Unsharp-mask image.
4809 */
4810 status=MagickTrue;
4811 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00004812 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00004813 image_view=AcquireCacheView(image);
4814 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00004815#if defined(MAGICKCORE_OPENMP_SUPPORT)
4816 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004817#endif
cristybb503372010-05-27 20:51:26 +00004818 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004819 {
cristy4c08aed2011-07-01 19:47:50 +00004820 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004821 pixel;
4822
cristy4c08aed2011-07-01 19:47:50 +00004823 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004824 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004825
cristy4c08aed2011-07-01 19:47:50 +00004826 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004827 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004828
cristy117ff172010-08-15 21:35:32 +00004829 register ssize_t
4830 x;
4831
cristy3ed852e2009-09-05 21:47:34 +00004832 if (status == MagickFalse)
4833 continue;
4834 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4835 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
4836 exception);
cristy4c08aed2011-07-01 19:47:50 +00004837 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004838 {
4839 status=MagickFalse;
4840 continue;
4841 }
cristyddd82202009-11-03 20:14:50 +00004842 pixel=bias;
cristybb503372010-05-27 20:51:26 +00004843 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004844 {
cristyed231572011-07-14 02:18:59 +00004845 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004846 {
cristy4c08aed2011-07-01 19:47:50 +00004847 pixel.red=GetPixelRed(image,p)-(MagickRealType) GetPixelRed(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004848 if (fabs(2.0*pixel.red) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004849 pixel.red=(MagickRealType) GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004850 else
cristy4c08aed2011-07-01 19:47:50 +00004851 pixel.red=(MagickRealType) GetPixelRed(image,p)+(pixel.red*amount);
4852 SetPixelRed(unsharp_image,ClampToQuantum(pixel.red),q);
cristy3ed852e2009-09-05 21:47:34 +00004853 }
cristyed231572011-07-14 02:18:59 +00004854 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004855 {
cristy4c08aed2011-07-01 19:47:50 +00004856 pixel.green=GetPixelGreen(image,p)-
4857 (MagickRealType) GetPixelGreen(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004858 if (fabs(2.0*pixel.green) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004859 pixel.green=(MagickRealType)
4860 GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004861 else
cristy4c08aed2011-07-01 19:47:50 +00004862 pixel.green=(MagickRealType)
4863 GetPixelGreen(image,p)+
4864 (pixel.green*amount);
4865 SetPixelGreen(unsharp_image,
4866 ClampToQuantum(pixel.green),q);
cristy3ed852e2009-09-05 21:47:34 +00004867 }
cristyed231572011-07-14 02:18:59 +00004868 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004869 {
cristy4c08aed2011-07-01 19:47:50 +00004870 pixel.blue=GetPixelBlue(image,p)-
4871 (MagickRealType) GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004872 if (fabs(2.0*pixel.blue) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004873 pixel.blue=(MagickRealType)
4874 GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004875 else
cristy4c08aed2011-07-01 19:47:50 +00004876 pixel.blue=(MagickRealType)
4877 GetPixelBlue(image,p)+(pixel.blue*amount);
4878 SetPixelBlue(unsharp_image,
4879 ClampToQuantum(pixel.blue),q);
cristy3ed852e2009-09-05 21:47:34 +00004880 }
cristyed231572011-07-14 02:18:59 +00004881 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00004882 (image->colorspace == CMYKColorspace))
4883 {
cristy4c08aed2011-07-01 19:47:50 +00004884 pixel.black=GetPixelBlack(image,p)-
4885 (MagickRealType) GetPixelBlack(image,q);
4886 if (fabs(2.0*pixel.black) < quantum_threshold)
4887 pixel.black=(MagickRealType)
4888 GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004889 else
cristy4c08aed2011-07-01 19:47:50 +00004890 pixel.black=(MagickRealType)
4891 GetPixelBlack(image,p)+(pixel.black*
4892 amount);
4893 SetPixelBlack(unsharp_image,
4894 ClampToQuantum(pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00004895 }
cristyed231572011-07-14 02:18:59 +00004896 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004897 {
4898 pixel.alpha=GetPixelAlpha(image,p)-
4899 (MagickRealType) GetPixelAlpha(image,q);
4900 if (fabs(2.0*pixel.alpha) < quantum_threshold)
4901 pixel.alpha=(MagickRealType)
4902 GetPixelAlpha(image,p);
4903 else
4904 pixel.alpha=GetPixelAlpha(image,p)+
4905 (pixel.alpha*amount);
4906 SetPixelAlpha(unsharp_image,
4907 ClampToQuantum(pixel.alpha),q);
4908 }
cristyed231572011-07-14 02:18:59 +00004909 p+=GetPixelChannels(image);
4910 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00004911 }
4912 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
4913 status=MagickFalse;
4914 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4915 {
4916 MagickBooleanType
4917 proceed;
4918
cristyb5d5f722009-11-04 03:03:49 +00004919#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00004920 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00004921#endif
4922 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4923 if (proceed == MagickFalse)
4924 status=MagickFalse;
4925 }
4926 }
4927 unsharp_image->type=image->type;
4928 unsharp_view=DestroyCacheView(unsharp_view);
4929 image_view=DestroyCacheView(image_view);
4930 if (status == MagickFalse)
4931 unsharp_image=DestroyImage(unsharp_image);
4932 return(unsharp_image);
4933}