blob: 65a3f6dfc17834835f1e67de1910ac23356cde5b [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)
161 status=LevelImage(image,black_point,white_point,gamma);
162 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);
220 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
221 {
222 InheritException(exception,&blur_image->exception);
223 blur_image=DestroyImage(blur_image);
224 return((Image *) NULL);
225 }
226 /*
227 Edge detect the image brighness channel, level, blur, and level again.
228 */
229 edge_image=EdgeImage(image,radius,exception);
230 if (edge_image == (Image *) NULL)
231 {
232 blur_image=DestroyImage(blur_image);
233 return((Image *) NULL);
234 }
cristyf89cb1d2011-07-07 01:24:37 +0000235 (void) AdaptiveLevelImage(edge_image,"20%,95%");
cristy3ed852e2009-09-05 21:47:34 +0000236 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
237 if (gaussian_image != (Image *) NULL)
238 {
239 edge_image=DestroyImage(edge_image);
240 edge_image=gaussian_image;
241 }
cristyf89cb1d2011-07-07 01:24:37 +0000242 (void) AdaptiveLevelImage(edge_image,"10%,95%");
cristy3ed852e2009-09-05 21:47:34 +0000243 /*
244 Create a set of kernels from maximum (radius,sigma) to minimum.
245 */
246 width=GetOptimalKernelWidth2D(radius,sigma);
247 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
248 if (kernel == (double **) NULL)
249 {
250 edge_image=DestroyImage(edge_image);
251 blur_image=DestroyImage(blur_image);
252 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
253 }
254 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000255 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000256 {
257 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
258 sizeof(**kernel));
259 if (kernel[i] == (double *) NULL)
260 break;
cristy47e00502009-12-17 19:19:57 +0000261 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000262 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000263 k=0;
264 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000265 {
cristy47e00502009-12-17 19:19:57 +0000266 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000267 {
cristy4205a3c2010-09-12 20:19:59 +0000268 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
269 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000270 normalize+=kernel[i][k];
271 k++;
cristy3ed852e2009-09-05 21:47:34 +0000272 }
273 }
cristy3ed852e2009-09-05 21:47:34 +0000274 if (fabs(normalize) <= MagickEpsilon)
275 normalize=1.0;
276 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000277 for (k=0; k < (j*j); k++)
278 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000279 }
cristybb503372010-05-27 20:51:26 +0000280 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000281 {
282 for (i-=2; i >= 0; i-=2)
283 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
284 kernel=(double **) RelinquishMagickMemory(kernel);
285 edge_image=DestroyImage(edge_image);
286 blur_image=DestroyImage(blur_image);
287 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
288 }
289 /*
290 Adaptively blur image.
291 */
292 status=MagickTrue;
293 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000294 GetPixelInfo(image,&bias);
295 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000296 image_view=AcquireCacheView(image);
297 edge_view=AcquireCacheView(edge_image);
298 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000299#if defined(MAGICKCORE_OPENMP_SUPPORT)
300 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000301#endif
cristybb503372010-05-27 20:51:26 +0000302 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000303 {
cristy4c08aed2011-07-01 19:47:50 +0000304 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000305 *restrict p,
306 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000307
cristy4c08aed2011-07-01 19:47:50 +0000308 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000309 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000310
cristy117ff172010-08-15 21:35:32 +0000311 register ssize_t
312 x;
313
cristy3ed852e2009-09-05 21:47:34 +0000314 if (status == MagickFalse)
315 continue;
316 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
317 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
318 exception);
cristy4c08aed2011-07-01 19:47:50 +0000319 if ((r == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000320 {
321 status=MagickFalse;
322 continue;
323 }
cristybb503372010-05-27 20:51:26 +0000324 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000325 {
cristy4c08aed2011-07-01 19:47:50 +0000326 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000327 pixel;
328
329 MagickRealType
330 alpha,
331 gamma;
332
333 register const double
cristyc47d1f82009-11-26 01:44:43 +0000334 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000335
cristybb503372010-05-27 20:51:26 +0000336 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000337 i,
338 u,
339 v;
340
341 gamma=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000342 i=(ssize_t) ceil((double) width*QuantumScale*
343 GetPixelIntensity(edge_image,r)-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000344 if (i < 0)
345 i=0;
346 else
cristybb503372010-05-27 20:51:26 +0000347 if (i > (ssize_t) width)
348 i=(ssize_t) width;
cristy3ed852e2009-09-05 21:47:34 +0000349 if ((i & 0x01) != 0)
350 i--;
cristya21afde2010-07-02 00:45:40 +0000351 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
352 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
cristy4c08aed2011-07-01 19:47:50 +0000353 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000354 break;
cristyddd82202009-11-03 20:14:50 +0000355 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000356 k=kernel[i];
cristybb503372010-05-27 20:51:26 +0000357 for (v=0; v < (ssize_t) (width-i); v++)
cristy3ed852e2009-09-05 21:47:34 +0000358 {
cristybb503372010-05-27 20:51:26 +0000359 for (u=0; u < (ssize_t) (width-i); u++)
cristy3ed852e2009-09-05 21:47:34 +0000360 {
361 alpha=1.0;
cristyed231572011-07-14 02:18:59 +0000362 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000363 (image->matte != MagickFalse))
cristy4c08aed2011-07-01 19:47:50 +0000364 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +0000365 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000366 pixel.red+=(*k)*alpha*GetPixelRed(image,p);
cristyed231572011-07-14 02:18:59 +0000367 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000368 pixel.green+=(*k)*alpha*GetPixelGreen(image,p);
cristyed231572011-07-14 02:18:59 +0000369 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000370 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p);
cristyed231572011-07-14 02:18:59 +0000371 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000372 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000373 pixel.black+=(*k)*alpha*GetPixelBlack(image,p);
cristyed231572011-07-14 02:18:59 +0000374 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000375 pixel.alpha+=(*k)*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000376 gamma+=(*k)*alpha;
377 k++;
cristyed231572011-07-14 02:18:59 +0000378 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000379 }
380 }
381 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +0000382 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000383 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000384 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000385 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000386 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000387 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000388 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000389 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000390 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000391 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000392 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +0000393 q+=GetPixelChannels(blur_image);
394 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000395 }
396 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
397 status=MagickFalse;
398 if (image->progress_monitor != (MagickProgressMonitor) NULL)
399 {
400 MagickBooleanType
401 proceed;
402
cristyb5d5f722009-11-04 03:03:49 +0000403#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +0000404 #pragma omp critical (MagickCore_AdaptiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +0000405#endif
406 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
407 image->rows);
408 if (proceed == MagickFalse)
409 status=MagickFalse;
410 }
411 }
412 blur_image->type=image->type;
413 blur_view=DestroyCacheView(blur_view);
414 edge_view=DestroyCacheView(edge_view);
415 image_view=DestroyCacheView(image_view);
416 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000417 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000418 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
419 kernel=(double **) RelinquishMagickMemory(kernel);
420 if (status == MagickFalse)
421 blur_image=DestroyImage(blur_image);
422 return(blur_image);
423}
424
425/*
426%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427% %
428% %
429% %
430% A d a p t i v e S h a r p e n I m a g e %
431% %
432% %
433% %
434%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435%
436% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
437% intensely near image edges and less intensely far from edges. We sharpen the
438% image with a Gaussian operator of the given radius and standard deviation
439% (sigma). For reasonable results, radius should be larger than sigma. Use a
440% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
441%
442% The format of the AdaptiveSharpenImage method is:
443%
444% Image *AdaptiveSharpenImage(const Image *image,const double radius,
445% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000446%
447% A description of each parameter follows:
448%
449% o image: the image.
450%
cristy3ed852e2009-09-05 21:47:34 +0000451% o radius: the radius of the Gaussian, in pixels, not counting the center
452% pixel.
453%
454% o sigma: the standard deviation of the Laplacian, in pixels.
455%
456% o exception: return any errors or warnings in this structure.
457%
458*/
cristy3ed852e2009-09-05 21:47:34 +0000459MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
460 const double sigma,ExceptionInfo *exception)
461{
cristy3ed852e2009-09-05 21:47:34 +0000462#define AdaptiveSharpenImageTag "Convolve/Image"
463#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
464
cristyc4c8d132010-01-07 01:58:38 +0000465 CacheView
466 *sharp_view,
467 *edge_view,
468 *image_view;
469
cristy3ed852e2009-09-05 21:47:34 +0000470 double
cristy47e00502009-12-17 19:19:57 +0000471 **kernel,
472 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000473
474 Image
475 *sharp_image,
476 *edge_image,
477 *gaussian_image;
478
cristy3ed852e2009-09-05 21:47:34 +0000479 MagickBooleanType
480 status;
481
cristybb503372010-05-27 20:51:26 +0000482 MagickOffsetType
483 progress;
484
cristy4c08aed2011-07-01 19:47:50 +0000485 PixelInfo
cristyddd82202009-11-03 20:14:50 +0000486 bias;
cristy3ed852e2009-09-05 21:47:34 +0000487
cristybb503372010-05-27 20:51:26 +0000488 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000489 i;
cristy3ed852e2009-09-05 21:47:34 +0000490
cristybb503372010-05-27 20:51:26 +0000491 size_t
cristy3ed852e2009-09-05 21:47:34 +0000492 width;
493
cristybb503372010-05-27 20:51:26 +0000494 ssize_t
495 j,
496 k,
497 u,
498 v,
499 y;
500
cristy3ed852e2009-09-05 21:47:34 +0000501 assert(image != (const Image *) NULL);
502 assert(image->signature == MagickSignature);
503 if (image->debug != MagickFalse)
504 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
505 assert(exception != (ExceptionInfo *) NULL);
506 assert(exception->signature == MagickSignature);
507 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
508 if (sharp_image == (Image *) NULL)
509 return((Image *) NULL);
510 if (fabs(sigma) <= MagickEpsilon)
511 return(sharp_image);
512 if (SetImageStorageClass(sharp_image,DirectClass) == MagickFalse)
513 {
514 InheritException(exception,&sharp_image->exception);
515 sharp_image=DestroyImage(sharp_image);
516 return((Image *) NULL);
517 }
518 /*
519 Edge detect the image brighness channel, level, sharp, and level again.
520 */
521 edge_image=EdgeImage(image,radius,exception);
522 if (edge_image == (Image *) NULL)
523 {
524 sharp_image=DestroyImage(sharp_image);
525 return((Image *) NULL);
526 }
cristyf89cb1d2011-07-07 01:24:37 +0000527 (void) AdaptiveLevelImage(edge_image,"20%,95%");
cristy3ed852e2009-09-05 21:47:34 +0000528 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
529 if (gaussian_image != (Image *) NULL)
530 {
531 edge_image=DestroyImage(edge_image);
532 edge_image=gaussian_image;
533 }
cristyf89cb1d2011-07-07 01:24:37 +0000534 (void) AdaptiveLevelImage(edge_image,"10%,95%");
cristy3ed852e2009-09-05 21:47:34 +0000535 /*
536 Create a set of kernels from maximum (radius,sigma) to minimum.
537 */
538 width=GetOptimalKernelWidth2D(radius,sigma);
539 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
540 if (kernel == (double **) NULL)
541 {
542 edge_image=DestroyImage(edge_image);
543 sharp_image=DestroyImage(sharp_image);
544 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
545 }
546 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000547 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000548 {
549 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
550 sizeof(**kernel));
551 if (kernel[i] == (double *) NULL)
552 break;
cristy47e00502009-12-17 19:19:57 +0000553 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000554 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000555 k=0;
556 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000557 {
cristy47e00502009-12-17 19:19:57 +0000558 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000559 {
cristy4205a3c2010-09-12 20:19:59 +0000560 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
561 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000562 normalize+=kernel[i][k];
563 k++;
cristy3ed852e2009-09-05 21:47:34 +0000564 }
565 }
cristy3ed852e2009-09-05 21:47:34 +0000566 if (fabs(normalize) <= MagickEpsilon)
567 normalize=1.0;
568 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000569 for (k=0; k < (j*j); k++)
570 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000571 }
cristybb503372010-05-27 20:51:26 +0000572 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000573 {
574 for (i-=2; i >= 0; i-=2)
575 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
576 kernel=(double **) RelinquishMagickMemory(kernel);
577 edge_image=DestroyImage(edge_image);
578 sharp_image=DestroyImage(sharp_image);
579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
580 }
581 /*
582 Adaptively sharpen image.
583 */
584 status=MagickTrue;
585 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000586 GetPixelInfo(image,&bias);
587 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000588 image_view=AcquireCacheView(image);
589 edge_view=AcquireCacheView(edge_image);
590 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000591#if defined(MAGICKCORE_OPENMP_SUPPORT)
592 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000593#endif
cristybb503372010-05-27 20:51:26 +0000594 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000595 {
cristy4c08aed2011-07-01 19:47:50 +0000596 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000597 *restrict p,
598 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000599
cristy4c08aed2011-07-01 19:47:50 +0000600 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000601 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000602
cristy117ff172010-08-15 21:35:32 +0000603 register ssize_t
604 x;
605
cristy3ed852e2009-09-05 21:47:34 +0000606 if (status == MagickFalse)
607 continue;
608 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
609 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
610 exception);
cristy4c08aed2011-07-01 19:47:50 +0000611 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000612 {
613 status=MagickFalse;
614 continue;
615 }
cristybb503372010-05-27 20:51:26 +0000616 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000617 {
cristy4c08aed2011-07-01 19:47:50 +0000618 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000619 pixel;
620
621 MagickRealType
622 alpha,
623 gamma;
624
625 register const double
cristyc47d1f82009-11-26 01:44:43 +0000626 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000627
cristybb503372010-05-27 20:51:26 +0000628 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000629 i,
630 u,
631 v;
632
633 gamma=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000634 i=(ssize_t) ceil((double) width*QuantumScale*
635 GetPixelIntensity(edge_image,r)-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000636 if (i < 0)
637 i=0;
638 else
cristybb503372010-05-27 20:51:26 +0000639 if (i > (ssize_t) width)
640 i=(ssize_t) width;
cristy3ed852e2009-09-05 21:47:34 +0000641 if ((i & 0x01) != 0)
642 i--;
cristy117ff172010-08-15 21:35:32 +0000643 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
644 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
cristy4c08aed2011-07-01 19:47:50 +0000645 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000646 break;
cristy3ed852e2009-09-05 21:47:34 +0000647 k=kernel[i];
cristyddd82202009-11-03 20:14:50 +0000648 pixel=bias;
cristybb503372010-05-27 20:51:26 +0000649 for (v=0; v < (ssize_t) (width-i); v++)
cristy3ed852e2009-09-05 21:47:34 +0000650 {
cristybb503372010-05-27 20:51:26 +0000651 for (u=0; u < (ssize_t) (width-i); u++)
cristy3ed852e2009-09-05 21:47:34 +0000652 {
653 alpha=1.0;
cristyed231572011-07-14 02:18:59 +0000654 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000655 (image->matte != MagickFalse))
cristy4c08aed2011-07-01 19:47:50 +0000656 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +0000657 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000658 pixel.red+=(*k)*alpha*GetPixelRed(image,p);
cristyed231572011-07-14 02:18:59 +0000659 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000660 pixel.green+=(*k)*alpha*GetPixelGreen(image,p);
cristyed231572011-07-14 02:18:59 +0000661 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000662 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p);
cristyed231572011-07-14 02:18:59 +0000663 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000664 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000665 pixel.black+=(*k)*alpha*GetPixelBlack(image,p);
cristyed231572011-07-14 02:18:59 +0000666 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000667 pixel.alpha+=(*k)*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000668 gamma+=(*k)*alpha;
669 k++;
cristyed231572011-07-14 02:18:59 +0000670 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000671 }
672 }
673 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +0000674 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000675 SetPixelRed(sharp_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000676 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000677 SetPixelGreen(sharp_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000678 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000679 SetPixelBlue(sharp_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000680 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000681 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000682 SetPixelBlack(sharp_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000683 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000684 SetPixelAlpha(sharp_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +0000685 q+=GetPixelChannels(sharp_image);
686 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000687 }
688 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
689 status=MagickFalse;
690 if (image->progress_monitor != (MagickProgressMonitor) NULL)
691 {
692 MagickBooleanType
693 proceed;
694
cristyb5d5f722009-11-04 03:03:49 +0000695#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +0000696 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000697#endif
698 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
699 image->rows);
700 if (proceed == MagickFalse)
701 status=MagickFalse;
702 }
703 }
704 sharp_image->type=image->type;
705 sharp_view=DestroyCacheView(sharp_view);
706 edge_view=DestroyCacheView(edge_view);
707 image_view=DestroyCacheView(image_view);
708 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000709 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000710 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
711 kernel=(double **) RelinquishMagickMemory(kernel);
712 if (status == MagickFalse)
713 sharp_image=DestroyImage(sharp_image);
714 return(sharp_image);
715}
716
717/*
718%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719% %
720% %
721% %
722% B l u r I m a g e %
723% %
724% %
725% %
726%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727%
728% BlurImage() blurs an image. We convolve the image with a Gaussian operator
729% of the given radius and standard deviation (sigma). For reasonable results,
730% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
731% selects a suitable radius for you.
732%
733% BlurImage() differs from GaussianBlurImage() in that it uses a separable
734% kernel which is faster but mathematically equivalent to the non-separable
735% kernel.
736%
737% The format of the BlurImage method is:
738%
739% Image *BlurImage(const Image *image,const double radius,
740% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000741%
742% A description of each parameter follows:
743%
744% o image: the image.
745%
cristy3ed852e2009-09-05 21:47:34 +0000746% o radius: the radius of the Gaussian, in pixels, not counting the center
747% pixel.
748%
749% o sigma: the standard deviation of the Gaussian, in pixels.
750%
751% o exception: return any errors or warnings in this structure.
752%
753*/
754
cristybb503372010-05-27 20:51:26 +0000755static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000756{
cristy3ed852e2009-09-05 21:47:34 +0000757 double
cristy47e00502009-12-17 19:19:57 +0000758 *kernel,
759 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000760
cristy117ff172010-08-15 21:35:32 +0000761 register ssize_t
762 i;
763
cristybb503372010-05-27 20:51:26 +0000764 ssize_t
cristy47e00502009-12-17 19:19:57 +0000765 j,
766 k;
cristy3ed852e2009-09-05 21:47:34 +0000767
cristy3ed852e2009-09-05 21:47:34 +0000768 /*
769 Generate a 1-D convolution kernel.
770 */
771 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
772 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
773 if (kernel == (double *) NULL)
774 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000775 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000776 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000777 i=0;
778 for (k=(-j); k <= j; k++)
779 {
cristy4205a3c2010-09-12 20:19:59 +0000780 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
781 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000782 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000783 i++;
784 }
cristybb503372010-05-27 20:51:26 +0000785 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000786 kernel[i]/=normalize;
787 return(kernel);
788}
789
cristyf4ad9df2011-07-08 16:49:03 +0000790MagickExport Image *BlurImage(const Image *image,const double radius,
791 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000792{
793#define BlurImageTag "Blur/Image"
794
cristyc4c8d132010-01-07 01:58:38 +0000795 CacheView
796 *blur_view,
797 *image_view;
798
cristy3ed852e2009-09-05 21:47:34 +0000799 double
800 *kernel;
801
802 Image
803 *blur_image;
804
cristy3ed852e2009-09-05 21:47:34 +0000805 MagickBooleanType
806 status;
807
cristybb503372010-05-27 20:51:26 +0000808 MagickOffsetType
809 progress;
810
cristy4c08aed2011-07-01 19:47:50 +0000811 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000812 bias;
813
cristybb503372010-05-27 20:51:26 +0000814 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000815 i;
816
cristybb503372010-05-27 20:51:26 +0000817 size_t
cristy3ed852e2009-09-05 21:47:34 +0000818 width;
819
cristybb503372010-05-27 20:51:26 +0000820 ssize_t
821 x,
822 y;
823
cristy3ed852e2009-09-05 21:47:34 +0000824 /*
825 Initialize blur image attributes.
826 */
827 assert(image != (Image *) NULL);
828 assert(image->signature == MagickSignature);
829 if (image->debug != MagickFalse)
830 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
831 assert(exception != (ExceptionInfo *) NULL);
832 assert(exception->signature == MagickSignature);
833 blur_image=CloneImage(image,0,0,MagickTrue,exception);
834 if (blur_image == (Image *) NULL)
835 return((Image *) NULL);
836 if (fabs(sigma) <= MagickEpsilon)
837 return(blur_image);
838 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
839 {
840 InheritException(exception,&blur_image->exception);
841 blur_image=DestroyImage(blur_image);
842 return((Image *) NULL);
843 }
844 width=GetOptimalKernelWidth1D(radius,sigma);
845 kernel=GetBlurKernel(width,sigma);
846 if (kernel == (double *) NULL)
847 {
848 blur_image=DestroyImage(blur_image);
849 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
850 }
851 if (image->debug != MagickFalse)
852 {
853 char
854 format[MaxTextExtent],
855 *message;
856
857 register const double
858 *k;
859
860 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000861 " BlurImage with %.20g kernel:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000862 message=AcquireString("");
863 k=kernel;
cristybb503372010-05-27 20:51:26 +0000864 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000865 {
866 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000867 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000868 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000869 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000870 (void) ConcatenateString(&message,format);
871 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
872 }
873 message=DestroyString(message);
874 }
875 /*
876 Blur rows.
877 */
878 status=MagickTrue;
879 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000880 GetPixelInfo(image,&bias);
881 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000882 image_view=AcquireCacheView(image);
883 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000884#if defined(MAGICKCORE_OPENMP_SUPPORT)
885 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000886#endif
cristybb503372010-05-27 20:51:26 +0000887 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000888 {
cristy4c08aed2011-07-01 19:47:50 +0000889 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000890 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000891
cristy4c08aed2011-07-01 19:47:50 +0000892 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000893 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000894
cristy117ff172010-08-15 21:35:32 +0000895 register ssize_t
896 x;
897
cristy3ed852e2009-09-05 21:47:34 +0000898 if (status == MagickFalse)
899 continue;
cristy117ff172010-08-15 21:35:32 +0000900 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
901 image->columns+width,1,exception);
cristy3ed852e2009-09-05 21:47:34 +0000902 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
903 exception);
cristy4c08aed2011-07-01 19:47:50 +0000904 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000905 {
906 status=MagickFalse;
907 continue;
908 }
cristybb503372010-05-27 20:51:26 +0000909 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000910 {
cristy4c08aed2011-07-01 19:47:50 +0000911 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000912 pixel;
913
914 register const double
cristyc47d1f82009-11-26 01:44:43 +0000915 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000916
cristy4c08aed2011-07-01 19:47:50 +0000917 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000918 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000919
cristybb503372010-05-27 20:51:26 +0000920 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000921 i;
922
cristyddd82202009-11-03 20:14:50 +0000923 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000924 k=kernel;
925 kernel_pixels=p;
cristyed231572011-07-14 02:18:59 +0000926 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
cristyf4ad9df2011-07-08 16:49:03 +0000927 (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000928 {
cristybb503372010-05-27 20:51:26 +0000929 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000930 {
cristy4c08aed2011-07-01 19:47:50 +0000931 pixel.red+=(*k)*GetPixelRed(image,kernel_pixels);
932 pixel.green+=(*k)*GetPixelGreen(image,kernel_pixels);
933 pixel.blue+=(*k)*GetPixelBlue(image,kernel_pixels);
934 if (image->colorspace == CMYKColorspace)
935 pixel.black+=(*k)*GetPixelBlack(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000936 k++;
cristyed231572011-07-14 02:18:59 +0000937 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000938 }
cristyed231572011-07-14 02:18:59 +0000939 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000940 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000941 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000942 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000943 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000944 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000945 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +0000946 (blur_image->colorspace == CMYKColorspace))
947 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000948 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000949 {
950 k=kernel;
951 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +0000952 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000953 {
cristy4c08aed2011-07-01 19:47:50 +0000954 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000955 k++;
cristyed231572011-07-14 02:18:59 +0000956 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000957 }
cristy4c08aed2011-07-01 19:47:50 +0000958 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000959 }
960 }
961 else
962 {
963 MagickRealType
964 alpha,
965 gamma;
966
967 gamma=0.0;
cristybb503372010-05-27 20:51:26 +0000968 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000969 {
cristy4c08aed2011-07-01 19:47:50 +0000970 alpha=(MagickRealType) (QuantumScale*
971 GetPixelAlpha(image,kernel_pixels));
972 pixel.red+=(*k)*alpha*GetPixelRed(image,kernel_pixels);
973 pixel.green+=(*k)*alpha*GetPixelGreen(image,kernel_pixels);
974 pixel.blue+=(*k)*alpha*GetPixelBlue(image,kernel_pixels);
975 if (image->colorspace == CMYKColorspace)
976 pixel.black+=(*k)*alpha*GetPixelBlack(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000977 gamma+=(*k)*alpha;
978 k++;
cristyed231572011-07-14 02:18:59 +0000979 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000980 }
981 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +0000982 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000983 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +0000984 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000985 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +0000986 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000987 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000988 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +0000989 (blur_image->colorspace == CMYKColorspace))
990 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +0000991 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000992 {
993 k=kernel;
994 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +0000995 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000996 {
cristy4c08aed2011-07-01 19:47:50 +0000997 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000998 k++;
cristyed231572011-07-14 02:18:59 +0000999 kernel_pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001000 }
cristy4c08aed2011-07-01 19:47:50 +00001001 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001002 }
1003 }
cristyed231572011-07-14 02:18:59 +00001004 p+=GetPixelChannels(image);
1005 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001006 }
1007 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1008 status=MagickFalse;
1009 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1010 {
1011 MagickBooleanType
1012 proceed;
1013
cristyb5d5f722009-11-04 03:03:49 +00001014#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001015 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001016#endif
1017 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1018 blur_image->columns);
1019 if (proceed == MagickFalse)
1020 status=MagickFalse;
1021 }
1022 }
1023 blur_view=DestroyCacheView(blur_view);
1024 image_view=DestroyCacheView(image_view);
1025 /*
1026 Blur columns.
1027 */
1028 image_view=AcquireCacheView(blur_image);
1029 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001030#if defined(MAGICKCORE_OPENMP_SUPPORT)
1031 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001032#endif
cristybb503372010-05-27 20:51:26 +00001033 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001034 {
cristy4c08aed2011-07-01 19:47:50 +00001035 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001036 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001037
cristy4c08aed2011-07-01 19:47:50 +00001038 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001039 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001040
cristy117ff172010-08-15 21:35:32 +00001041 register ssize_t
1042 y;
1043
cristy3ed852e2009-09-05 21:47:34 +00001044 if (status == MagickFalse)
1045 continue;
cristy117ff172010-08-15 21:35:32 +00001046 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1047 image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001048 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001049 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001050 {
1051 status=MagickFalse;
1052 continue;
1053 }
cristybb503372010-05-27 20:51:26 +00001054 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001055 {
cristy4c08aed2011-07-01 19:47:50 +00001056 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001057 pixel;
1058
1059 register const double
cristyc47d1f82009-11-26 01:44:43 +00001060 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00001061
cristy4c08aed2011-07-01 19:47:50 +00001062 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001063 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001064
cristybb503372010-05-27 20:51:26 +00001065 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001066 i;
1067
cristyddd82202009-11-03 20:14:50 +00001068 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00001069 k=kernel;
1070 kernel_pixels=p;
cristyed231572011-07-14 02:18:59 +00001071 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
cristyf4ad9df2011-07-08 16:49:03 +00001072 (blur_image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001073 {
cristybb503372010-05-27 20:51:26 +00001074 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001075 {
cristy4c08aed2011-07-01 19:47:50 +00001076 pixel.red+=(*k)*GetPixelRed(blur_image,kernel_pixels);
1077 pixel.green+=(*k)*GetPixelGreen(blur_image,kernel_pixels);
1078 pixel.blue+=(*k)*GetPixelBlue(blur_image,kernel_pixels);
1079 if (blur_image->colorspace == CMYKColorspace)
1080 pixel.black+=(*k)*GetPixelBlack(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001081 k++;
cristyed231572011-07-14 02:18:59 +00001082 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001083 }
cristyed231572011-07-14 02:18:59 +00001084 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001085 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00001086 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001087 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00001088 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001089 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00001090 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00001091 (blur_image->colorspace == CMYKColorspace))
1092 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +00001093 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001094 {
1095 k=kernel;
1096 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001097 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001098 {
cristy4c08aed2011-07-01 19:47:50 +00001099 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001100 k++;
cristyed231572011-07-14 02:18:59 +00001101 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001102 }
cristy4c08aed2011-07-01 19:47:50 +00001103 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001104 }
1105 }
1106 else
1107 {
1108 MagickRealType
1109 alpha,
1110 gamma;
1111
1112 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00001113 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001114 {
cristy46f08202010-01-10 04:04:21 +00001115 alpha=(MagickRealType) (QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +00001116 GetPixelAlpha(blur_image,kernel_pixels));
1117 pixel.red+=(*k)*alpha*GetPixelRed(blur_image,kernel_pixels);
1118 pixel.green+=(*k)*alpha*GetPixelGreen(blur_image,kernel_pixels);
1119 pixel.blue+=(*k)*alpha*GetPixelBlue(blur_image,kernel_pixels);
1120 if (blur_image->colorspace == CMYKColorspace)
1121 pixel.black+=(*k)*alpha*GetPixelBlack(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001122 gamma+=(*k)*alpha;
1123 k++;
cristyed231572011-07-14 02:18:59 +00001124 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001125 }
1126 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00001127 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001128 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00001129 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001130 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00001131 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00001132 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00001133 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00001134 (blur_image->colorspace == CMYKColorspace))
1135 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristyed231572011-07-14 02:18:59 +00001136 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001137 {
1138 k=kernel;
1139 kernel_pixels=p;
cristybb503372010-05-27 20:51:26 +00001140 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001141 {
cristy4c08aed2011-07-01 19:47:50 +00001142 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001143 k++;
cristyed231572011-07-14 02:18:59 +00001144 kernel_pixels+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001145 }
cristy4c08aed2011-07-01 19:47:50 +00001146 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001147 }
1148 }
cristyed231572011-07-14 02:18:59 +00001149 p+=GetPixelChannels(blur_image);
1150 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001151 }
1152 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1153 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001154 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001155 {
1156 MagickBooleanType
1157 proceed;
1158
cristyb5d5f722009-11-04 03:03:49 +00001159#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001160 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001161#endif
cristy4c08aed2011-07-01 19:47:50 +00001162 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1163 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001164 if (proceed == MagickFalse)
1165 status=MagickFalse;
1166 }
1167 }
1168 blur_view=DestroyCacheView(blur_view);
1169 image_view=DestroyCacheView(image_view);
1170 kernel=(double *) RelinquishMagickMemory(kernel);
1171 if (status == MagickFalse)
1172 blur_image=DestroyImage(blur_image);
1173 blur_image->type=image->type;
1174 return(blur_image);
1175}
1176
1177/*
1178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1179% %
1180% %
1181% %
cristyfccdab92009-11-30 16:43:57 +00001182% C o n v o l v e I m a g e %
1183% %
1184% %
1185% %
1186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1187%
1188% ConvolveImage() applies a custom convolution kernel to the image.
1189%
1190% The format of the ConvolveImage method is:
1191%
cristy5e6be1e2011-07-16 01:23:39 +00001192% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1193% ExceptionInfo *exception)
1194%
cristyfccdab92009-11-30 16:43:57 +00001195% A description of each parameter follows:
1196%
1197% o image: the image.
1198%
cristy5e6be1e2011-07-16 01:23:39 +00001199% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001200%
1201% o exception: return any errors or warnings in this structure.
1202%
1203*/
cristy5e6be1e2011-07-16 01:23:39 +00001204MagickExport Image *ConvolveImage(const Image *image,
1205 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001206{
cristyfccdab92009-11-30 16:43:57 +00001207#define ConvolveImageTag "Convolve/Image"
1208
cristyc4c8d132010-01-07 01:58:38 +00001209 CacheView
1210 *convolve_view,
1211 *image_view;
1212
cristyfccdab92009-11-30 16:43:57 +00001213 Image
1214 *convolve_image;
1215
cristyfccdab92009-11-30 16:43:57 +00001216 MagickBooleanType
1217 status;
1218
cristybb503372010-05-27 20:51:26 +00001219 MagickOffsetType
1220 progress;
1221
cristybb503372010-05-27 20:51:26 +00001222 ssize_t
1223 y;
1224
cristyfccdab92009-11-30 16:43:57 +00001225 /*
1226 Initialize convolve image attributes.
1227 */
1228 assert(image != (Image *) NULL);
1229 assert(image->signature == MagickSignature);
1230 if (image->debug != MagickFalse)
1231 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1232 assert(exception != (ExceptionInfo *) NULL);
1233 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001234 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001235 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001236 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1237 exception);
cristyfccdab92009-11-30 16:43:57 +00001238 if (convolve_image == (Image *) NULL)
1239 return((Image *) NULL);
1240 if (SetImageStorageClass(convolve_image,DirectClass) == MagickFalse)
1241 {
1242 InheritException(exception,&convolve_image->exception);
1243 convolve_image=DestroyImage(convolve_image);
1244 return((Image *) NULL);
1245 }
1246 if (image->debug != MagickFalse)
1247 {
1248 char
1249 format[MaxTextExtent],
1250 *message;
1251
cristy117ff172010-08-15 21:35:32 +00001252 register const double
1253 *k;
1254
cristy4e154852011-07-14 13:28:53 +00001255 register ssize_t
1256 u;
1257
cristybb503372010-05-27 20:51:26 +00001258 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001259 v;
1260
cristyfccdab92009-11-30 16:43:57 +00001261 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001262 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1263 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001264 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001265 k=kernel_info->values;
1266 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001267 {
1268 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001269 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001270 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001271 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001272 {
cristyb51dff52011-05-19 16:55:47 +00001273 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001274 (void) ConcatenateString(&message,format);
1275 }
1276 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1277 }
1278 message=DestroyString(message);
1279 }
1280 /*
cristyfccdab92009-11-30 16:43:57 +00001281 Convolve image.
1282 */
1283 status=MagickTrue;
1284 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001285 image_view=AcquireCacheView(image);
1286 convolve_view=AcquireCacheView(convolve_image);
1287#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy175653e2011-07-10 23:13:34 +00001288 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001289#endif
cristybb503372010-05-27 20:51:26 +00001290 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001291 {
cristy4c08aed2011-07-01 19:47:50 +00001292 register const Quantum
cristyfccdab92009-11-30 16:43:57 +00001293 *restrict p;
1294
cristy4c08aed2011-07-01 19:47:50 +00001295 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001296 *restrict q;
1297
cristy117ff172010-08-15 21:35:32 +00001298 register ssize_t
1299 x;
1300
cristy63852c12011-07-14 23:47:58 +00001301 size_t
cristyed231572011-07-14 02:18:59 +00001302 channels,
1303 convolve_channels;
1304
cristyfccdab92009-11-30 16:43:57 +00001305 if (status == MagickFalse)
1306 continue;
cristy5e6be1e2011-07-16 01:23:39 +00001307 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1308 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1309 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001310 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001311 exception);
cristy4c08aed2011-07-01 19:47:50 +00001312 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001313 {
1314 status=MagickFalse;
1315 continue;
1316 }
cristyed231572011-07-14 02:18:59 +00001317 channels=GetPixelChannels(image);
1318 convolve_channels=GetPixelChannels(convolve_image);
cristybb503372010-05-27 20:51:26 +00001319 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001320 {
cristybb503372010-05-27 20:51:26 +00001321 register ssize_t
cristyed231572011-07-14 02:18:59 +00001322 i;
cristyfccdab92009-11-30 16:43:57 +00001323
cristyed231572011-07-14 02:18:59 +00001324 for (i=0; i < (ssize_t) channels; i++)
1325 {
cristyed231572011-07-14 02:18:59 +00001326 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001327 alpha,
1328 gamma,
cristyed231572011-07-14 02:18:59 +00001329 pixel;
1330
1331 PixelChannel
1332 channel;
1333
1334 PixelTrait
1335 convolve_traits,
1336 traits;
1337
1338 register const double
1339 *restrict k;
1340
1341 register const Quantum
1342 *restrict kernel_pixels;
1343
1344 register ssize_t
1345 u;
1346
1347 ssize_t
1348 v;
1349
cristyed231572011-07-14 02:18:59 +00001350 traits=GetPixelChannelMapTraits(image,i);
cristy4e154852011-07-14 13:28:53 +00001351 if (traits == UndefinedPixelTrait)
cristyed231572011-07-14 02:18:59 +00001352 continue;
cristy4e154852011-07-14 13:28:53 +00001353 channel=GetPixelChannelMapChannel(image,i);
1354 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
1355 if (convolve_traits == UndefinedPixelTrait)
1356 continue;
1357 if ((convolve_traits & CopyPixelTrait) != 0)
1358 {
cristy39177402011-07-14 16:45:04 +00001359 size_t
cristyf8500872011-07-14 14:02:41 +00001360 center;
cristy4e154852011-07-14 13:28:53 +00001361
cristy5e6be1e2011-07-16 01:23:39 +00001362 center=((image->columns+kernel_info->width)*kernel_info->height/2)*
1363 channels+i;
cristyf8500872011-07-14 14:02:41 +00001364 SetPixelChannel(convolve_image,channel,p[center],q);
cristy4e154852011-07-14 13:28:53 +00001365 continue;
1366 }
cristy5e6be1e2011-07-16 01:23:39 +00001367 k=kernel_info->values;
cristyed231572011-07-14 02:18:59 +00001368 kernel_pixels=p;
cristy4e154852011-07-14 13:28:53 +00001369 pixel=image->bias;
cristy1ce96d02011-07-14 17:57:24 +00001370 if (((convolve_traits & BlendPixelTrait) == 0) ||
1371 (GetPixelAlphaTraits(image) == UndefinedPixelTrait) ||
cristyed231572011-07-14 02:18:59 +00001372 (image->matte == MagickFalse))
cristyfccdab92009-11-30 16:43:57 +00001373 {
cristyed231572011-07-14 02:18:59 +00001374 /*
cristy4e154852011-07-14 13:28:53 +00001375 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001376 */
cristy5e6be1e2011-07-16 01:23:39 +00001377 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001378 {
cristy5e6be1e2011-07-16 01:23:39 +00001379 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristy175653e2011-07-10 23:13:34 +00001380 {
cristyed231572011-07-14 02:18:59 +00001381 pixel+=(*k)*kernel_pixels[u*channels+i];
1382 k++;
cristy175653e2011-07-10 23:13:34 +00001383 }
cristy5e6be1e2011-07-16 01:23:39 +00001384 kernel_pixels+=(image->columns+kernel_info->width)*channels;
cristy175653e2011-07-10 23:13:34 +00001385 }
cristy4e154852011-07-14 13:28:53 +00001386 SetPixelChannel(convolve_image,channel,ClampToQuantum(pixel),q);
1387 continue;
cristyed231572011-07-14 02:18:59 +00001388 }
cristy4e154852011-07-14 13:28:53 +00001389 /*
1390 Alpha blending.
1391 */
1392 gamma=0.0;
cristy5e6be1e2011-07-16 01:23:39 +00001393 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristy4e154852011-07-14 13:28:53 +00001394 {
cristy5e6be1e2011-07-16 01:23:39 +00001395 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristy4e154852011-07-14 13:28:53 +00001396 {
1397 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,
1398 kernel_pixels+u*channels));
cristy1ce96d02011-07-14 17:57:24 +00001399 pixel+=(*k)*alpha*kernel_pixels[u*channels+i];
cristy4e154852011-07-14 13:28:53 +00001400 gamma+=(*k)*alpha;
1401 k++;
1402 }
cristy5e6be1e2011-07-16 01:23:39 +00001403 kernel_pixels+=(image->columns+kernel_info->width)*channels;
cristy4e154852011-07-14 13:28:53 +00001404 }
cristy1ce96d02011-07-14 17:57:24 +00001405 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1406 SetPixelChannel(convolve_image,channel,ClampToQuantum(gamma*pixel),q);
cristyed231572011-07-14 02:18:59 +00001407 }
1408 p+=channels;
1409 q+=convolve_channels;
cristyfccdab92009-11-30 16:43:57 +00001410 }
cristyed231572011-07-14 02:18:59 +00001411 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001412 status=MagickFalse;
1413 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1414 {
1415 MagickBooleanType
1416 proceed;
1417
1418#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001419 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001420#endif
1421 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1422 if (proceed == MagickFalse)
1423 status=MagickFalse;
1424 }
1425 }
1426 convolve_image->type=image->type;
1427 convolve_view=DestroyCacheView(convolve_view);
1428 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001429 if (status == MagickFalse)
1430 convolve_image=DestroyImage(convolve_image);
1431 return(convolve_image);
1432}
1433
1434/*
1435%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436% %
1437% %
1438% %
cristy3ed852e2009-09-05 21:47:34 +00001439% D e s p e c k l e I m a g e %
1440% %
1441% %
1442% %
1443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1444%
1445% DespeckleImage() reduces the speckle noise in an image while perserving the
1446% edges of the original image.
1447%
1448% The format of the DespeckleImage method is:
1449%
1450% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1451%
1452% A description of each parameter follows:
1453%
1454% o image: the image.
1455%
1456% o exception: return any errors or warnings in this structure.
1457%
1458*/
1459
cristybb503372010-05-27 20:51:26 +00001460static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1461 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
cristy3ed852e2009-09-05 21:47:34 +00001462 const int polarity)
1463{
cristy3ed852e2009-09-05 21:47:34 +00001464 MagickRealType
1465 v;
1466
cristy3ed852e2009-09-05 21:47:34 +00001467 register Quantum
1468 *p,
1469 *q,
1470 *r,
1471 *s;
1472
cristy117ff172010-08-15 21:35:32 +00001473 register ssize_t
1474 x;
1475
1476 ssize_t
1477 y;
1478
cristy3ed852e2009-09-05 21:47:34 +00001479 assert(f != (Quantum *) NULL);
1480 assert(g != (Quantum *) NULL);
1481 p=f+(columns+2);
1482 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001483 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1484 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001485 {
1486 p++;
1487 q++;
1488 r++;
1489 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001490 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001491 {
1492 v=(MagickRealType) (*p);
1493 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1494 v+=ScaleCharToQuantum(1);
1495 *q=(Quantum) v;
1496 p++;
1497 q++;
1498 r++;
1499 }
1500 else
cristybb503372010-05-27 20:51:26 +00001501 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001502 {
1503 v=(MagickRealType) (*p);
1504 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
cristybb503372010-05-27 20:51:26 +00001505 v-=(ssize_t) ScaleCharToQuantum(1);
cristy3ed852e2009-09-05 21:47:34 +00001506 *q=(Quantum) v;
1507 p++;
1508 q++;
1509 r++;
1510 }
1511 p++;
1512 q++;
1513 r++;
1514 }
1515 p=f+(columns+2);
1516 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001517 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1518 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1519 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001520 {
1521 p++;
1522 q++;
1523 r++;
1524 s++;
1525 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001526 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001527 {
1528 v=(MagickRealType) (*q);
1529 if (((MagickRealType) *s >=
1530 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1531 ((MagickRealType) *r > v))
1532 v+=ScaleCharToQuantum(1);
1533 *p=(Quantum) v;
1534 p++;
1535 q++;
1536 r++;
1537 s++;
1538 }
1539 else
cristybb503372010-05-27 20:51:26 +00001540 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001541 {
1542 v=(MagickRealType) (*q);
1543 if (((MagickRealType) *s <=
1544 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1545 ((MagickRealType) *r < v))
1546 v-=(MagickRealType) ScaleCharToQuantum(1);
1547 *p=(Quantum) v;
1548 p++;
1549 q++;
1550 r++;
1551 s++;
1552 }
1553 p++;
1554 q++;
1555 r++;
1556 s++;
1557 }
1558}
1559
1560MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1561{
1562#define DespeckleImageTag "Despeckle/Image"
1563
cristy2407fc22009-09-11 00:55:25 +00001564 CacheView
1565 *despeckle_view,
1566 *image_view;
1567
cristy3ed852e2009-09-05 21:47:34 +00001568 Image
1569 *despeckle_image;
1570
cristy3ed852e2009-09-05 21:47:34 +00001571 MagickBooleanType
1572 status;
1573
cristya58c3172011-02-19 19:23:11 +00001574 register ssize_t
1575 i;
1576
cristy3ed852e2009-09-05 21:47:34 +00001577 Quantum
cristy65b9f392011-02-22 14:22:54 +00001578 *restrict buffers,
1579 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +00001580
1581 size_t
cristya58c3172011-02-19 19:23:11 +00001582 length,
1583 number_channels;
cristy117ff172010-08-15 21:35:32 +00001584
cristybb503372010-05-27 20:51:26 +00001585 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001586 X[4] = {0, 1, 1,-1},
1587 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001588
cristy3ed852e2009-09-05 21:47:34 +00001589 /*
1590 Allocate despeckled image.
1591 */
1592 assert(image != (const Image *) NULL);
1593 assert(image->signature == MagickSignature);
1594 if (image->debug != MagickFalse)
1595 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1596 assert(exception != (ExceptionInfo *) NULL);
1597 assert(exception->signature == MagickSignature);
1598 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1599 exception);
1600 if (despeckle_image == (Image *) NULL)
1601 return((Image *) NULL);
1602 if (SetImageStorageClass(despeckle_image,DirectClass) == MagickFalse)
1603 {
1604 InheritException(exception,&despeckle_image->exception);
1605 despeckle_image=DestroyImage(despeckle_image);
1606 return((Image *) NULL);
1607 }
1608 /*
1609 Allocate image buffers.
1610 */
1611 length=(size_t) ((image->columns+2)*(image->rows+2));
cristy65b9f392011-02-22 14:22:54 +00001612 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1613 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1614 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001615 {
cristy65b9f392011-02-22 14:22:54 +00001616 if (buffers != (Quantum *) NULL)
1617 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1618 if (pixels != (Quantum *) NULL)
1619 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001620 despeckle_image=DestroyImage(despeckle_image);
1621 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1622 }
1623 /*
1624 Reduce speckle in the image.
1625 */
1626 status=MagickTrue;
cristy109695a2011-02-19 19:38:14 +00001627 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
cristy3ed852e2009-09-05 21:47:34 +00001628 image_view=AcquireCacheView(image);
1629 despeckle_view=AcquireCacheView(despeckle_image);
cristy8df3d002011-02-19 19:40:59 +00001630 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001631 {
cristy3ed852e2009-09-05 21:47:34 +00001632 register Quantum
1633 *buffer,
1634 *pixel;
1635
cristyc1488b52011-02-19 18:54:15 +00001636 register ssize_t
cristya58c3172011-02-19 19:23:11 +00001637 k,
cristyc1488b52011-02-19 18:54:15 +00001638 x;
1639
cristy117ff172010-08-15 21:35:32 +00001640 ssize_t
1641 j,
1642 y;
1643
cristy3ed852e2009-09-05 21:47:34 +00001644 if (status == MagickFalse)
1645 continue;
cristy65b9f392011-02-22 14:22:54 +00001646 pixel=pixels;
cristy3ed852e2009-09-05 21:47:34 +00001647 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
cristy65b9f392011-02-22 14:22:54 +00001648 buffer=buffers;
cristybb503372010-05-27 20:51:26 +00001649 j=(ssize_t) image->columns+2;
1650 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001651 {
cristy4c08aed2011-07-01 19:47:50 +00001652 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001653 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001654
1655 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001656 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001657 break;
1658 j++;
cristybb503372010-05-27 20:51:26 +00001659 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001660 {
cristya58c3172011-02-19 19:23:11 +00001661 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001662 {
cristy4c08aed2011-07-01 19:47:50 +00001663 case 0: pixel[j]=GetPixelRed(image,p); break;
1664 case 1: pixel[j]=GetPixelGreen(image,p); break;
1665 case 2: pixel[j]=GetPixelBlue(image,p); break;
1666 case 3: pixel[j]=GetPixelAlpha(image,p); break;
1667 case 4: pixel[j]=GetPixelBlack(image,p); break;
cristy3ed852e2009-09-05 21:47:34 +00001668 default: break;
1669 }
cristyed231572011-07-14 02:18:59 +00001670 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001671 j++;
1672 }
1673 j++;
1674 }
cristy3ed852e2009-09-05 21:47:34 +00001675 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
cristya58c3172011-02-19 19:23:11 +00001676 for (k=0; k < 4; k++)
cristy3ed852e2009-09-05 21:47:34 +00001677 {
cristya58c3172011-02-19 19:23:11 +00001678 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1679 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1680 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1681 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
cristy3ed852e2009-09-05 21:47:34 +00001682 }
cristybb503372010-05-27 20:51:26 +00001683 j=(ssize_t) image->columns+2;
1684 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001685 {
1686 MagickBooleanType
1687 sync;
1688
cristy4c08aed2011-07-01 19:47:50 +00001689 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001690 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001691
1692 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1693 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001694 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001695 break;
1696 j++;
cristybb503372010-05-27 20:51:26 +00001697 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001698 {
cristya58c3172011-02-19 19:23:11 +00001699 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001700 {
cristy4c08aed2011-07-01 19:47:50 +00001701 case 0: SetPixelRed(despeckle_image,pixel[j],q); break;
1702 case 1: SetPixelGreen(despeckle_image,pixel[j],q); break;
1703 case 2: SetPixelBlue(despeckle_image,pixel[j],q); break;
1704 case 3: SetPixelAlpha(despeckle_image,pixel[j],q); break;
1705 case 4: SetPixelBlack(despeckle_image,pixel[j],q); break;
cristy3ed852e2009-09-05 21:47:34 +00001706 default: break;
1707 }
cristyed231572011-07-14 02:18:59 +00001708 q+=GetPixelChannels(despeckle_image);
cristy3ed852e2009-09-05 21:47:34 +00001709 j++;
1710 }
1711 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1712 if (sync == MagickFalse)
1713 {
1714 status=MagickFalse;
1715 break;
1716 }
1717 j++;
1718 }
1719 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1720 {
1721 MagickBooleanType
1722 proceed;
1723
cristya58c3172011-02-19 19:23:11 +00001724 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1725 number_channels);
cristy3ed852e2009-09-05 21:47:34 +00001726 if (proceed == MagickFalse)
1727 status=MagickFalse;
1728 }
1729 }
1730 despeckle_view=DestroyCacheView(despeckle_view);
1731 image_view=DestroyCacheView(image_view);
cristy65b9f392011-02-22 14:22:54 +00001732 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1733 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001734 despeckle_image->type=image->type;
1735 if (status == MagickFalse)
1736 despeckle_image=DestroyImage(despeckle_image);
1737 return(despeckle_image);
1738}
1739
1740/*
1741%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1742% %
1743% %
1744% %
1745% E d g e I m a g e %
1746% %
1747% %
1748% %
1749%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1750%
1751% EdgeImage() finds edges in an image. Radius defines the radius of the
1752% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1753% radius for you.
1754%
1755% The format of the EdgeImage method is:
1756%
1757% Image *EdgeImage(const Image *image,const double radius,
1758% ExceptionInfo *exception)
1759%
1760% A description of each parameter follows:
1761%
1762% o image: the image.
1763%
1764% o radius: the radius of the pixel neighborhood.
1765%
1766% o exception: return any errors or warnings in this structure.
1767%
1768*/
1769MagickExport Image *EdgeImage(const Image *image,const double radius,
1770 ExceptionInfo *exception)
1771{
1772 Image
1773 *edge_image;
1774
cristy41cbe682011-07-15 19:12:37 +00001775 KernelInfo
1776 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001777
cristybb503372010-05-27 20:51:26 +00001778 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001779 i;
1780
cristybb503372010-05-27 20:51:26 +00001781 size_t
cristy3ed852e2009-09-05 21:47:34 +00001782 width;
1783
cristy41cbe682011-07-15 19:12:37 +00001784 ssize_t
1785 j,
1786 u,
1787 v;
1788
cristy3ed852e2009-09-05 21:47:34 +00001789 assert(image != (const Image *) NULL);
1790 assert(image->signature == MagickSignature);
1791 if (image->debug != MagickFalse)
1792 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1793 assert(exception != (ExceptionInfo *) NULL);
1794 assert(exception->signature == MagickSignature);
1795 width=GetOptimalKernelWidth1D(radius,0.5);
cristy5e6be1e2011-07-16 01:23:39 +00001796 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001797 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001798 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001799 kernel_info->width=width;
1800 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001801 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1802 kernel_info->width*sizeof(*kernel_info->values));
1803 if (kernel_info->values == (double *) NULL)
1804 {
1805 kernel_info=DestroyKernelInfo(kernel_info);
1806 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1807 }
1808 j=(ssize_t) kernel_info->width/2;
1809 i=0;
1810 for (v=(-j); v <= j; v++)
1811 {
1812 for (u=(-j); u <= j; u++)
1813 {
1814 kernel_info->values[i]=(-1.0);
1815 i++;
1816 }
1817 }
1818 kernel_info->values[i/2]=(double) (width*width-1.0);
cristy5e6be1e2011-07-16 01:23:39 +00001819 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001820 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001821 return(edge_image);
1822}
1823
1824/*
1825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1826% %
1827% %
1828% %
1829% E m b o s s I m a g e %
1830% %
1831% %
1832% %
1833%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1834%
1835% EmbossImage() returns a grayscale image with a three-dimensional effect.
1836% We convolve the image with a Gaussian operator of the given radius and
1837% standard deviation (sigma). For reasonable results, radius should be
1838% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1839% radius for you.
1840%
1841% The format of the EmbossImage method is:
1842%
1843% Image *EmbossImage(const Image *image,const double radius,
1844% const double sigma,ExceptionInfo *exception)
1845%
1846% A description of each parameter follows:
1847%
1848% o image: the image.
1849%
1850% o radius: the radius of the pixel neighborhood.
1851%
1852% o sigma: the standard deviation of the Gaussian, in pixels.
1853%
1854% o exception: return any errors or warnings in this structure.
1855%
1856*/
1857MagickExport Image *EmbossImage(const Image *image,const double radius,
1858 const double sigma,ExceptionInfo *exception)
1859{
cristy3ed852e2009-09-05 21:47:34 +00001860 Image
1861 *emboss_image;
1862
cristy41cbe682011-07-15 19:12:37 +00001863 KernelInfo
1864 *kernel_info;
1865
cristybb503372010-05-27 20:51:26 +00001866 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001867 i;
1868
cristybb503372010-05-27 20:51:26 +00001869 size_t
cristy3ed852e2009-09-05 21:47:34 +00001870 width;
1871
cristy117ff172010-08-15 21:35:32 +00001872 ssize_t
1873 j,
1874 k,
1875 u,
1876 v;
1877
cristy41cbe682011-07-15 19:12:37 +00001878 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001879 assert(image->signature == MagickSignature);
1880 if (image->debug != MagickFalse)
1881 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1882 assert(exception != (ExceptionInfo *) NULL);
1883 assert(exception->signature == MagickSignature);
1884 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001885 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001886 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001887 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001888 kernel_info->width=width;
1889 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001890 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1891 kernel_info->width*sizeof(*kernel_info->values));
1892 if (kernel_info->values == (double *) NULL)
1893 {
1894 kernel_info=DestroyKernelInfo(kernel_info);
1895 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1896 }
1897 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001898 k=j;
1899 i=0;
1900 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001901 {
cristy47e00502009-12-17 19:19:57 +00001902 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001903 {
cristy41cbe682011-07-15 19:12:37 +00001904 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001905 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001906 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001907 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001908 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001909 i++;
1910 }
cristy47e00502009-12-17 19:19:57 +00001911 k--;
cristy3ed852e2009-09-05 21:47:34 +00001912 }
cristy5e6be1e2011-07-16 01:23:39 +00001913 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001914 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001915 if (emboss_image != (Image *) NULL)
1916 (void) EqualizeImage(emboss_image);
cristy3ed852e2009-09-05 21:47:34 +00001917 return(emboss_image);
1918}
1919
1920/*
1921%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1922% %
1923% %
1924% %
1925% G a u s s i a n B l u r I m a g e %
1926% %
1927% %
1928% %
1929%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1930%
1931% GaussianBlurImage() blurs an image. We convolve the image with a
1932% Gaussian operator of the given radius and standard deviation (sigma).
1933% For reasonable results, the radius should be larger than sigma. Use a
1934% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1935%
1936% The format of the GaussianBlurImage method is:
1937%
1938% Image *GaussianBlurImage(const Image *image,onst double radius,
1939% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001940%
1941% A description of each parameter follows:
1942%
1943% o image: the image.
1944%
cristy3ed852e2009-09-05 21:47:34 +00001945% o radius: the radius of the Gaussian, in pixels, not counting the center
1946% pixel.
1947%
1948% o sigma: the standard deviation of the Gaussian, in pixels.
1949%
1950% o exception: return any errors or warnings in this structure.
1951%
1952*/
cristy41cbe682011-07-15 19:12:37 +00001953MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1954 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001955{
cristy3ed852e2009-09-05 21:47:34 +00001956 Image
1957 *blur_image;
1958
cristy41cbe682011-07-15 19:12:37 +00001959 KernelInfo
1960 *kernel_info;
1961
cristybb503372010-05-27 20:51:26 +00001962 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001963 i;
1964
cristybb503372010-05-27 20:51:26 +00001965 size_t
cristy3ed852e2009-09-05 21:47:34 +00001966 width;
1967
cristy117ff172010-08-15 21:35:32 +00001968 ssize_t
1969 j,
1970 u,
1971 v;
1972
cristy3ed852e2009-09-05 21:47:34 +00001973 assert(image != (const Image *) NULL);
1974 assert(image->signature == MagickSignature);
1975 if (image->debug != MagickFalse)
1976 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1977 assert(exception != (ExceptionInfo *) NULL);
1978 assert(exception->signature == MagickSignature);
1979 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001980 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001981 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001982 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001983 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1984 kernel_info->width=width;
1985 kernel_info->height=width;
1986 kernel_info->signature=MagickSignature;
1987 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1988 kernel_info->width*sizeof(*kernel_info->values));
1989 if (kernel_info->values == (double *) NULL)
1990 {
1991 kernel_info=DestroyKernelInfo(kernel_info);
1992 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1993 }
1994 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001995 i=0;
cristy47e00502009-12-17 19:19:57 +00001996 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001997 {
cristy47e00502009-12-17 19:19:57 +00001998 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001999 {
2000 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
2001 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2002 i++;
2003 }
cristy3ed852e2009-09-05 21:47:34 +00002004 }
cristy5e6be1e2011-07-16 01:23:39 +00002005 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00002006 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00002007 return(blur_image);
2008}
2009
2010/*
2011%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2012% %
2013% %
2014% %
cristy3ed852e2009-09-05 21:47:34 +00002015% M o t i o n B l u r I m a g e %
2016% %
2017% %
2018% %
2019%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2020%
2021% MotionBlurImage() simulates motion blur. We convolve the image with a
2022% Gaussian operator of the given radius and standard deviation (sigma).
2023% For reasonable results, radius should be larger than sigma. Use a
2024% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2025% Angle gives the angle of the blurring motion.
2026%
2027% Andrew Protano contributed this effect.
2028%
2029% The format of the MotionBlurImage method is:
2030%
2031% Image *MotionBlurImage(const Image *image,const double radius,
2032% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002033%
2034% A description of each parameter follows:
2035%
2036% o image: the image.
2037%
cristy3ed852e2009-09-05 21:47:34 +00002038% o radius: the radius of the Gaussian, in pixels, not counting
2039% the center pixel.
2040%
2041% o sigma: the standard deviation of the Gaussian, in pixels.
2042%
cristycee97112010-05-28 00:44:52 +00002043% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002044%
2045% o exception: return any errors or warnings in this structure.
2046%
2047*/
2048
cristybb503372010-05-27 20:51:26 +00002049static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002050{
cristy3ed852e2009-09-05 21:47:34 +00002051 double
cristy47e00502009-12-17 19:19:57 +00002052 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002053 normalize;
2054
cristybb503372010-05-27 20:51:26 +00002055 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002056 i;
2057
2058 /*
cristy47e00502009-12-17 19:19:57 +00002059 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002060 */
2061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2062 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2063 if (kernel == (double *) NULL)
2064 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002065 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002066 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002067 {
cristy4205a3c2010-09-12 20:19:59 +00002068 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2069 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002070 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002071 }
cristybb503372010-05-27 20:51:26 +00002072 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002073 kernel[i]/=normalize;
2074 return(kernel);
2075}
2076
cristyf4ad9df2011-07-08 16:49:03 +00002077MagickExport Image *MotionBlurImage(const Image *image,
2078 const double radius,const double sigma,const double angle,
2079 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002080{
cristyc4c8d132010-01-07 01:58:38 +00002081 CacheView
2082 *blur_view,
2083 *image_view;
2084
cristy3ed852e2009-09-05 21:47:34 +00002085 double
2086 *kernel;
2087
2088 Image
2089 *blur_image;
2090
cristy3ed852e2009-09-05 21:47:34 +00002091 MagickBooleanType
2092 status;
2093
cristybb503372010-05-27 20:51:26 +00002094 MagickOffsetType
2095 progress;
2096
cristy4c08aed2011-07-01 19:47:50 +00002097 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002098 bias;
cristy3ed852e2009-09-05 21:47:34 +00002099
2100 OffsetInfo
2101 *offset;
2102
2103 PointInfo
2104 point;
2105
cristybb503372010-05-27 20:51:26 +00002106 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002107 i;
2108
cristybb503372010-05-27 20:51:26 +00002109 size_t
cristy3ed852e2009-09-05 21:47:34 +00002110 width;
2111
cristybb503372010-05-27 20:51:26 +00002112 ssize_t
2113 y;
2114
cristy3ed852e2009-09-05 21:47:34 +00002115 assert(image != (Image *) NULL);
2116 assert(image->signature == MagickSignature);
2117 if (image->debug != MagickFalse)
2118 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2119 assert(exception != (ExceptionInfo *) NULL);
2120 width=GetOptimalKernelWidth1D(radius,sigma);
2121 kernel=GetMotionBlurKernel(width,sigma);
2122 if (kernel == (double *) NULL)
2123 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2124 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2125 if (offset == (OffsetInfo *) NULL)
2126 {
2127 kernel=(double *) RelinquishMagickMemory(kernel);
2128 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2129 }
2130 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2131 if (blur_image == (Image *) NULL)
2132 {
2133 kernel=(double *) RelinquishMagickMemory(kernel);
2134 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2135 return((Image *) NULL);
2136 }
2137 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
2138 {
2139 kernel=(double *) RelinquishMagickMemory(kernel);
2140 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2141 InheritException(exception,&blur_image->exception);
2142 blur_image=DestroyImage(blur_image);
2143 return((Image *) NULL);
2144 }
2145 point.x=(double) width*sin(DegreesToRadians(angle));
2146 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002147 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002148 {
cristybb503372010-05-27 20:51:26 +00002149 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2150 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002151 }
2152 /*
2153 Motion blur image.
2154 */
2155 status=MagickTrue;
2156 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002157 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002158 image_view=AcquireCacheView(image);
2159 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002160#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00002161 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00002162#endif
cristybb503372010-05-27 20:51:26 +00002163 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002164 {
cristy4c08aed2011-07-01 19:47:50 +00002165 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002166 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002167
cristy117ff172010-08-15 21:35:32 +00002168 register ssize_t
2169 x;
2170
cristy3ed852e2009-09-05 21:47:34 +00002171 if (status == MagickFalse)
2172 continue;
2173 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2174 exception);
cristy4c08aed2011-07-01 19:47:50 +00002175 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002176 {
2177 status=MagickFalse;
2178 continue;
2179 }
cristybb503372010-05-27 20:51:26 +00002180 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002181 {
cristy4c08aed2011-07-01 19:47:50 +00002182 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002183 qixel;
2184
2185 PixelPacket
2186 pixel;
2187
2188 register double
cristyc47d1f82009-11-26 01:44:43 +00002189 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00002190
cristybb503372010-05-27 20:51:26 +00002191 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002192 i;
2193
cristy3ed852e2009-09-05 21:47:34 +00002194 k=kernel;
cristyddd82202009-11-03 20:14:50 +00002195 qixel=bias;
cristyed231572011-07-14 02:18:59 +00002196 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002197 {
cristybb503372010-05-27 20:51:26 +00002198 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002199 {
2200 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2201 offset[i].y,&pixel,exception);
2202 qixel.red+=(*k)*pixel.red;
2203 qixel.green+=(*k)*pixel.green;
2204 qixel.blue+=(*k)*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002205 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002206 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002207 qixel.black+=(*k)*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002208 k++;
2209 }
cristyed231572011-07-14 02:18:59 +00002210 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002211 SetPixelRed(blur_image,
2212 ClampToQuantum(qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002213 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002214 SetPixelGreen(blur_image,
2215 ClampToQuantum(qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002216 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002217 SetPixelBlue(blur_image,
2218 ClampToQuantum(qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002219 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002220 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002221 SetPixelBlack(blur_image,
2222 ClampToQuantum(qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002223 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002224 SetPixelAlpha(blur_image,
2225 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002226 }
2227 else
2228 {
2229 MagickRealType
2230 alpha,
2231 gamma;
2232
2233 alpha=0.0;
2234 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00002235 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002236 {
2237 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2238 offset[i].y,&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00002239 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00002240 qixel.red+=(*k)*alpha*pixel.red;
2241 qixel.green+=(*k)*alpha*pixel.green;
2242 qixel.blue+=(*k)*alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002243 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002244 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002245 qixel.black+=(*k)*alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002246 gamma+=(*k)*alpha;
2247 k++;
2248 }
2249 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00002250 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002251 SetPixelRed(blur_image,
2252 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002253 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002254 SetPixelGreen(blur_image,
2255 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002256 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002257 SetPixelBlue(blur_image,
2258 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002259 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002260 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002261 SetPixelBlack(blur_image,
2262 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002263 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002264 SetPixelAlpha(blur_image,
2265 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002266 }
cristyed231572011-07-14 02:18:59 +00002267 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002268 }
2269 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2270 status=MagickFalse;
2271 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2272 {
2273 MagickBooleanType
2274 proceed;
2275
cristyb557a152011-02-22 12:14:30 +00002276#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002277 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002278#endif
2279 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2280 if (proceed == MagickFalse)
2281 status=MagickFalse;
2282 }
2283 }
2284 blur_view=DestroyCacheView(blur_view);
2285 image_view=DestroyCacheView(image_view);
2286 kernel=(double *) RelinquishMagickMemory(kernel);
2287 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2288 if (status == MagickFalse)
2289 blur_image=DestroyImage(blur_image);
2290 return(blur_image);
2291}
2292
2293/*
2294%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2295% %
2296% %
2297% %
2298% P r e v i e w I m a g e %
2299% %
2300% %
2301% %
2302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2303%
2304% PreviewImage() tiles 9 thumbnails of the specified image with an image
2305% processing operation applied with varying parameters. This may be helpful
2306% pin-pointing an appropriate parameter for a particular image processing
2307% operation.
2308%
2309% The format of the PreviewImages method is:
2310%
2311% Image *PreviewImages(const Image *image,const PreviewType preview,
2312% ExceptionInfo *exception)
2313%
2314% A description of each parameter follows:
2315%
2316% o image: the image.
2317%
2318% o preview: the image processing operation.
2319%
2320% o exception: return any errors or warnings in this structure.
2321%
2322*/
2323MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2324 ExceptionInfo *exception)
2325{
2326#define NumberTiles 9
2327#define PreviewImageTag "Preview/Image"
2328#define DefaultPreviewGeometry "204x204+10+10"
2329
2330 char
2331 factor[MaxTextExtent],
2332 label[MaxTextExtent];
2333
2334 double
2335 degrees,
2336 gamma,
2337 percentage,
2338 radius,
2339 sigma,
2340 threshold;
2341
2342 Image
2343 *images,
2344 *montage_image,
2345 *preview_image,
2346 *thumbnail;
2347
2348 ImageInfo
2349 *preview_info;
2350
cristy3ed852e2009-09-05 21:47:34 +00002351 MagickBooleanType
2352 proceed;
2353
2354 MontageInfo
2355 *montage_info;
2356
2357 QuantizeInfo
2358 quantize_info;
2359
2360 RectangleInfo
2361 geometry;
2362
cristybb503372010-05-27 20:51:26 +00002363 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002364 i,
2365 x;
2366
cristybb503372010-05-27 20:51:26 +00002367 size_t
cristy3ed852e2009-09-05 21:47:34 +00002368 colors;
2369
cristy117ff172010-08-15 21:35:32 +00002370 ssize_t
2371 y;
2372
cristy3ed852e2009-09-05 21:47:34 +00002373 /*
2374 Open output image file.
2375 */
2376 assert(image != (Image *) NULL);
2377 assert(image->signature == MagickSignature);
2378 if (image->debug != MagickFalse)
2379 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2380 colors=2;
2381 degrees=0.0;
2382 gamma=(-0.2f);
2383 preview_info=AcquireImageInfo();
2384 SetGeometry(image,&geometry);
2385 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2386 &geometry.width,&geometry.height);
2387 images=NewImageList();
2388 percentage=12.5;
2389 GetQuantizeInfo(&quantize_info);
2390 radius=0.0;
2391 sigma=1.0;
2392 threshold=0.0;
2393 x=0;
2394 y=0;
2395 for (i=0; i < NumberTiles; i++)
2396 {
2397 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2398 if (thumbnail == (Image *) NULL)
2399 break;
2400 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2401 (void *) NULL);
2402 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
2403 if (i == (NumberTiles/2))
2404 {
2405 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
2406 AppendImageToList(&images,thumbnail);
2407 continue;
2408 }
2409 switch (preview)
2410 {
2411 case RotatePreview:
2412 {
2413 degrees+=45.0;
2414 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002415 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002416 break;
2417 }
2418 case ShearPreview:
2419 {
2420 degrees+=5.0;
2421 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002422 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002423 degrees,2.0*degrees);
2424 break;
2425 }
2426 case RollPreview:
2427 {
cristybb503372010-05-27 20:51:26 +00002428 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2429 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002430 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002431 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002432 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002433 break;
2434 }
2435 case HuePreview:
2436 {
2437 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2438 if (preview_image == (Image *) NULL)
2439 break;
cristyb51dff52011-05-19 16:55:47 +00002440 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002441 2.0*percentage);
2442 (void) ModulateImage(preview_image,factor);
cristyb51dff52011-05-19 16:55:47 +00002443 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002444 break;
2445 }
2446 case SaturationPreview:
2447 {
2448 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2449 if (preview_image == (Image *) NULL)
2450 break;
cristyb51dff52011-05-19 16:55:47 +00002451 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002452 2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00002453 (void) ModulateImage(preview_image,factor);
cristyb51dff52011-05-19 16:55:47 +00002454 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002455 break;
2456 }
2457 case BrightnessPreview:
2458 {
2459 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2460 if (preview_image == (Image *) NULL)
2461 break;
cristyb51dff52011-05-19 16:55:47 +00002462 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00002463 (void) ModulateImage(preview_image,factor);
cristyb51dff52011-05-19 16:55:47 +00002464 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002465 break;
2466 }
2467 case GammaPreview:
2468 default:
2469 {
2470 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2471 if (preview_image == (Image *) NULL)
2472 break;
2473 gamma+=0.4f;
cristy50fbc382011-07-07 02:19:17 +00002474 (void) GammaImage(preview_image,gamma);
cristyb51dff52011-05-19 16:55:47 +00002475 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002476 break;
2477 }
2478 case SpiffPreview:
2479 {
2480 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2481 if (preview_image != (Image *) NULL)
2482 for (x=0; x < i; x++)
2483 (void) ContrastImage(preview_image,MagickTrue);
cristyb51dff52011-05-19 16:55:47 +00002484 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002485 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002486 break;
2487 }
2488 case DullPreview:
2489 {
2490 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2491 if (preview_image == (Image *) NULL)
2492 break;
2493 for (x=0; x < i; x++)
2494 (void) ContrastImage(preview_image,MagickFalse);
cristyb51dff52011-05-19 16:55:47 +00002495 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002496 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002497 break;
2498 }
2499 case GrayscalePreview:
2500 {
2501 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2502 if (preview_image == (Image *) NULL)
2503 break;
2504 colors<<=1;
2505 quantize_info.number_colors=colors;
2506 quantize_info.colorspace=GRAYColorspace;
2507 (void) QuantizeImage(&quantize_info,preview_image);
cristyb51dff52011-05-19 16:55:47 +00002508 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002509 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002510 break;
2511 }
2512 case QuantizePreview:
2513 {
2514 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2515 if (preview_image == (Image *) NULL)
2516 break;
2517 colors<<=1;
2518 quantize_info.number_colors=colors;
2519 (void) QuantizeImage(&quantize_info,preview_image);
cristyb51dff52011-05-19 16:55:47 +00002520 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002521 colors);
cristy3ed852e2009-09-05 21:47:34 +00002522 break;
2523 }
2524 case DespecklePreview:
2525 {
2526 for (x=0; x < (i-1); x++)
2527 {
2528 preview_image=DespeckleImage(thumbnail,exception);
2529 if (preview_image == (Image *) NULL)
2530 break;
2531 thumbnail=DestroyImage(thumbnail);
2532 thumbnail=preview_image;
2533 }
2534 preview_image=DespeckleImage(thumbnail,exception);
2535 if (preview_image == (Image *) NULL)
2536 break;
cristyb51dff52011-05-19 16:55:47 +00002537 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002538 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002539 break;
2540 }
2541 case ReduceNoisePreview:
2542 {
cristy95c38342011-03-18 22:39:51 +00002543 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2544 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002545 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002546 break;
2547 }
2548 case AddNoisePreview:
2549 {
2550 switch ((int) i)
2551 {
2552 case 0:
2553 {
2554 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2555 break;
2556 }
2557 case 1:
2558 {
2559 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2560 break;
2561 }
2562 case 2:
2563 {
2564 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2565 break;
2566 }
2567 case 3:
2568 {
2569 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2570 break;
2571 }
2572 case 4:
2573 {
2574 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2575 break;
2576 }
2577 case 5:
2578 {
2579 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2580 break;
2581 }
2582 default:
2583 {
2584 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2585 break;
2586 }
2587 }
cristyd76c51e2011-03-26 00:21:26 +00002588 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2589 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002590 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002591 break;
2592 }
2593 case SharpenPreview:
2594 {
2595 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002596 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002597 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002598 break;
2599 }
2600 case BlurPreview:
2601 {
2602 preview_image=BlurImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002603 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002604 sigma);
2605 break;
2606 }
2607 case ThresholdPreview:
2608 {
2609 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2610 if (preview_image == (Image *) NULL)
2611 break;
2612 (void) BilevelImage(thumbnail,
2613 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
cristyb51dff52011-05-19 16:55:47 +00002614 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002615 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2616 break;
2617 }
2618 case EdgeDetectPreview:
2619 {
2620 preview_image=EdgeImage(thumbnail,radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002621 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002622 break;
2623 }
2624 case SpreadPreview:
2625 {
2626 preview_image=SpreadImage(thumbnail,radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002627 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002628 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002629 break;
2630 }
2631 case SolarizePreview:
2632 {
2633 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2634 if (preview_image == (Image *) NULL)
2635 break;
2636 (void) SolarizeImage(preview_image,(double) QuantumRange*
2637 percentage/100.0);
cristyb51dff52011-05-19 16:55:47 +00002638 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002639 (QuantumRange*percentage)/100.0);
2640 break;
2641 }
2642 case ShadePreview:
2643 {
2644 degrees+=10.0;
2645 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2646 exception);
cristyb51dff52011-05-19 16:55:47 +00002647 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002648 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002649 break;
2650 }
2651 case RaisePreview:
2652 {
2653 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2654 if (preview_image == (Image *) NULL)
2655 break;
cristybb503372010-05-27 20:51:26 +00002656 geometry.width=(size_t) (2*i+2);
2657 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002658 geometry.x=i/2;
2659 geometry.y=i/2;
2660 (void) RaiseImage(preview_image,&geometry,MagickTrue);
cristyb51dff52011-05-19 16:55:47 +00002661 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002662 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002663 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002664 break;
2665 }
2666 case SegmentPreview:
2667 {
2668 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2669 if (preview_image == (Image *) NULL)
2670 break;
2671 threshold+=0.4f;
2672 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
2673 threshold);
cristyb51dff52011-05-19 16:55:47 +00002674 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002675 threshold,threshold);
2676 break;
2677 }
2678 case SwirlPreview:
2679 {
2680 preview_image=SwirlImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002681 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002682 degrees+=45.0;
2683 break;
2684 }
2685 case ImplodePreview:
2686 {
2687 degrees+=0.1f;
2688 preview_image=ImplodeImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002689 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002690 break;
2691 }
2692 case WavePreview:
2693 {
2694 degrees+=5.0f;
2695 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002696 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002697 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002698 break;
2699 }
2700 case OilPaintPreview:
2701 {
2702 preview_image=OilPaintImage(thumbnail,(double) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002703 (void) FormatLocaleString(label,MaxTextExtent,"paint %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002704 break;
2705 }
2706 case CharcoalDrawingPreview:
2707 {
2708 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2709 exception);
cristyb51dff52011-05-19 16:55:47 +00002710 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002711 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002712 break;
2713 }
2714 case JPEGPreview:
2715 {
2716 char
2717 filename[MaxTextExtent];
2718
2719 int
2720 file;
2721
2722 MagickBooleanType
2723 status;
2724
2725 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2726 if (preview_image == (Image *) NULL)
2727 break;
cristybb503372010-05-27 20:51:26 +00002728 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002729 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002730 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002731 file=AcquireUniqueFileResource(filename);
2732 if (file != -1)
2733 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002734 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002735 "jpeg:%s",filename);
2736 status=WriteImage(preview_info,preview_image);
2737 if (status != MagickFalse)
2738 {
2739 Image
2740 *quality_image;
2741
2742 (void) CopyMagickString(preview_info->filename,
2743 preview_image->filename,MaxTextExtent);
2744 quality_image=ReadImage(preview_info,exception);
2745 if (quality_image != (Image *) NULL)
2746 {
2747 preview_image=DestroyImage(preview_image);
2748 preview_image=quality_image;
2749 }
2750 }
2751 (void) RelinquishUniqueFileResource(preview_image->filename);
2752 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002753 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002754 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2755 1024.0/1024.0);
2756 else
2757 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002758 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002759 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002760 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002761 else
cristyb51dff52011-05-19 16:55:47 +00002762 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002763 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002764 break;
2765 }
2766 }
2767 thumbnail=DestroyImage(thumbnail);
2768 percentage+=12.5;
2769 radius+=0.5;
2770 sigma+=0.25;
2771 if (preview_image == (Image *) NULL)
2772 break;
2773 (void) DeleteImageProperty(preview_image,"label");
2774 (void) SetImageProperty(preview_image,"label",label);
2775 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002776 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2777 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002778 if (proceed == MagickFalse)
2779 break;
2780 }
2781 if (images == (Image *) NULL)
2782 {
2783 preview_info=DestroyImageInfo(preview_info);
2784 return((Image *) NULL);
2785 }
2786 /*
2787 Create the montage.
2788 */
2789 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2790 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2791 montage_info->shadow=MagickTrue;
2792 (void) CloneString(&montage_info->tile,"3x3");
2793 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2794 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2795 montage_image=MontageImages(images,montage_info,exception);
2796 montage_info=DestroyMontageInfo(montage_info);
2797 images=DestroyImageList(images);
2798 if (montage_image == (Image *) NULL)
2799 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2800 if (montage_image->montage != (char *) NULL)
2801 {
2802 /*
2803 Free image directory.
2804 */
2805 montage_image->montage=(char *) RelinquishMagickMemory(
2806 montage_image->montage);
2807 if (image->directory != (char *) NULL)
2808 montage_image->directory=(char *) RelinquishMagickMemory(
2809 montage_image->directory);
2810 }
2811 preview_info=DestroyImageInfo(preview_info);
2812 return(montage_image);
2813}
2814
2815/*
2816%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2817% %
2818% %
2819% %
2820% R a d i a l B l u r I m a g e %
2821% %
2822% %
2823% %
2824%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2825%
2826% RadialBlurImage() applies a radial blur to the image.
2827%
2828% Andrew Protano contributed this effect.
2829%
2830% The format of the RadialBlurImage method is:
2831%
2832% Image *RadialBlurImage(const Image *image,const double angle,
2833% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002834%
2835% A description of each parameter follows:
2836%
2837% o image: the image.
2838%
cristy3ed852e2009-09-05 21:47:34 +00002839% o angle: the angle of the radial blur.
2840%
2841% o exception: return any errors or warnings in this structure.
2842%
2843*/
cristyf4ad9df2011-07-08 16:49:03 +00002844MagickExport Image *RadialBlurImage(const Image *image,
2845 const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002846{
cristyc4c8d132010-01-07 01:58:38 +00002847 CacheView
2848 *blur_view,
2849 *image_view;
2850
cristy3ed852e2009-09-05 21:47:34 +00002851 Image
2852 *blur_image;
2853
cristy3ed852e2009-09-05 21:47:34 +00002854 MagickBooleanType
2855 status;
2856
cristybb503372010-05-27 20:51:26 +00002857 MagickOffsetType
2858 progress;
2859
cristy4c08aed2011-07-01 19:47:50 +00002860 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002861 bias;
cristy3ed852e2009-09-05 21:47:34 +00002862
2863 MagickRealType
2864 blur_radius,
2865 *cos_theta,
2866 offset,
2867 *sin_theta,
2868 theta;
2869
2870 PointInfo
2871 blur_center;
2872
cristybb503372010-05-27 20:51:26 +00002873 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002874 i;
2875
cristybb503372010-05-27 20:51:26 +00002876 size_t
cristy3ed852e2009-09-05 21:47:34 +00002877 n;
2878
cristybb503372010-05-27 20:51:26 +00002879 ssize_t
2880 y;
2881
cristy3ed852e2009-09-05 21:47:34 +00002882 /*
2883 Allocate blur image.
2884 */
2885 assert(image != (Image *) NULL);
2886 assert(image->signature == MagickSignature);
2887 if (image->debug != MagickFalse)
2888 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2889 assert(exception != (ExceptionInfo *) NULL);
2890 assert(exception->signature == MagickSignature);
2891 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2892 if (blur_image == (Image *) NULL)
2893 return((Image *) NULL);
2894 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
2895 {
2896 InheritException(exception,&blur_image->exception);
2897 blur_image=DestroyImage(blur_image);
2898 return((Image *) NULL);
2899 }
2900 blur_center.x=(double) image->columns/2.0;
2901 blur_center.y=(double) image->rows/2.0;
2902 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002903 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002904 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2905 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2906 sizeof(*cos_theta));
2907 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2908 sizeof(*sin_theta));
2909 if ((cos_theta == (MagickRealType *) NULL) ||
2910 (sin_theta == (MagickRealType *) NULL))
2911 {
2912 blur_image=DestroyImage(blur_image);
2913 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2914 }
2915 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002916 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002917 {
2918 cos_theta[i]=cos((double) (theta*i-offset));
2919 sin_theta[i]=sin((double) (theta*i-offset));
2920 }
2921 /*
2922 Radial blur image.
2923 */
2924 status=MagickTrue;
2925 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002926 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002927 image_view=AcquireCacheView(image);
2928 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002929#if defined(MAGICKCORE_OPENMP_SUPPORT)
2930 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002931#endif
cristybb503372010-05-27 20:51:26 +00002932 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002933 {
cristy4c08aed2011-07-01 19:47:50 +00002934 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002935 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002936
cristy117ff172010-08-15 21:35:32 +00002937 register ssize_t
2938 x;
2939
cristy3ed852e2009-09-05 21:47:34 +00002940 if (status == MagickFalse)
2941 continue;
2942 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2943 exception);
cristy4c08aed2011-07-01 19:47:50 +00002944 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002945 {
2946 status=MagickFalse;
2947 continue;
2948 }
cristybb503372010-05-27 20:51:26 +00002949 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002950 {
cristy4c08aed2011-07-01 19:47:50 +00002951 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002952 qixel;
2953
2954 MagickRealType
2955 normalize,
2956 radius;
2957
2958 PixelPacket
2959 pixel;
2960
2961 PointInfo
2962 center;
2963
cristybb503372010-05-27 20:51:26 +00002964 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002965 i;
2966
cristybb503372010-05-27 20:51:26 +00002967 size_t
cristy3ed852e2009-09-05 21:47:34 +00002968 step;
2969
2970 center.x=(double) x-blur_center.x;
2971 center.y=(double) y-blur_center.y;
2972 radius=hypot((double) center.x,center.y);
2973 if (radius == 0)
2974 step=1;
2975 else
2976 {
cristybb503372010-05-27 20:51:26 +00002977 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002978 if (step == 0)
2979 step=1;
2980 else
2981 if (step >= n)
2982 step=n-1;
2983 }
2984 normalize=0.0;
cristyddd82202009-11-03 20:14:50 +00002985 qixel=bias;
cristyed231572011-07-14 02:18:59 +00002986 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002987 {
cristyeaedf062010-05-29 22:36:02 +00002988 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00002989 {
cristyeaedf062010-05-29 22:36:02 +00002990 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
2991 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
2992 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
2993 cos_theta[i]+0.5),&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002994 qixel.red+=pixel.red;
2995 qixel.green+=pixel.green;
2996 qixel.blue+=pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00002997 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002998 qixel.black+=pixel.black;
2999 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00003000 normalize+=1.0;
3001 }
3002 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3003 normalize);
cristyed231572011-07-14 02:18:59 +00003004 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003005 SetPixelRed(blur_image,
3006 ClampToQuantum(normalize*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00003007 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003008 SetPixelGreen(blur_image,
3009 ClampToQuantum(normalize*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00003010 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003011 SetPixelBlue(blur_image,
3012 ClampToQuantum(normalize*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003013 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003014 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003015 SetPixelBlack(blur_image,
3016 ClampToQuantum(normalize*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003017 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003018 SetPixelAlpha(blur_image,
3019 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003020 }
3021 else
3022 {
3023 MagickRealType
3024 alpha,
3025 gamma;
3026
3027 alpha=1.0;
3028 gamma=0.0;
cristyeaedf062010-05-29 22:36:02 +00003029 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00003030 {
cristyeaedf062010-05-29 22:36:02 +00003031 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3032 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3033 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3034 cos_theta[i]+0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003035 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00003036 qixel.red+=alpha*pixel.red;
3037 qixel.green+=alpha*pixel.green;
3038 qixel.blue+=alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00003039 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00003040 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00003041 qixel.black+=alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00003042 gamma+=alpha;
3043 normalize+=1.0;
3044 }
3045 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3046 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3047 normalize);
cristyed231572011-07-14 02:18:59 +00003048 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003049 SetPixelRed(blur_image,
3050 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00003051 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003052 SetPixelGreen(blur_image,
3053 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00003054 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003055 SetPixelBlue(blur_image,
3056 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003057 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003058 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003059 SetPixelBlack(blur_image,
3060 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003061 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003062 SetPixelAlpha(blur_image,
3063 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003064 }
cristyed231572011-07-14 02:18:59 +00003065 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003066 }
3067 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3068 status=MagickFalse;
3069 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3070 {
3071 MagickBooleanType
3072 proceed;
3073
cristyb5d5f722009-11-04 03:03:49 +00003074#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003075 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003076#endif
3077 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3078 if (proceed == MagickFalse)
3079 status=MagickFalse;
3080 }
3081 }
3082 blur_view=DestroyCacheView(blur_view);
3083 image_view=DestroyCacheView(image_view);
3084 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3085 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3086 if (status == MagickFalse)
3087 blur_image=DestroyImage(blur_image);
3088 return(blur_image);
3089}
3090
3091/*
3092%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3093% %
3094% %
3095% %
cristy3ed852e2009-09-05 21:47:34 +00003096% S e l e c t i v e B l u r I m a g e %
3097% %
3098% %
3099% %
3100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3101%
3102% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3103% It is similar to the unsharpen mask that sharpens everything with contrast
3104% above a certain threshold.
3105%
3106% The format of the SelectiveBlurImage method is:
3107%
3108% Image *SelectiveBlurImage(const Image *image,const double radius,
3109% const double sigma,const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003110%
3111% A description of each parameter follows:
3112%
3113% o image: the image.
3114%
cristy3ed852e2009-09-05 21:47:34 +00003115% o radius: the radius of the Gaussian, in pixels, not counting the center
3116% pixel.
3117%
3118% o sigma: the standard deviation of the Gaussian, in pixels.
3119%
3120% o threshold: only pixels within this contrast threshold are included
3121% in the blur operation.
3122%
3123% o exception: return any errors or warnings in this structure.
3124%
3125*/
cristyf4ad9df2011-07-08 16:49:03 +00003126MagickExport Image *SelectiveBlurImage(const Image *image,
3127 const double radius,const double sigma,const double threshold,
3128 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003129{
3130#define SelectiveBlurImageTag "SelectiveBlur/Image"
3131
cristy47e00502009-12-17 19:19:57 +00003132 CacheView
3133 *blur_view,
3134 *image_view;
3135
cristy3ed852e2009-09-05 21:47:34 +00003136 double
cristy3ed852e2009-09-05 21:47:34 +00003137 *kernel;
3138
3139 Image
3140 *blur_image;
3141
cristy3ed852e2009-09-05 21:47:34 +00003142 MagickBooleanType
3143 status;
3144
cristybb503372010-05-27 20:51:26 +00003145 MagickOffsetType
3146 progress;
3147
cristy4c08aed2011-07-01 19:47:50 +00003148 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003149 bias;
3150
cristybb503372010-05-27 20:51:26 +00003151 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003152 i;
cristy3ed852e2009-09-05 21:47:34 +00003153
cristybb503372010-05-27 20:51:26 +00003154 size_t
cristy3ed852e2009-09-05 21:47:34 +00003155 width;
3156
cristybb503372010-05-27 20:51:26 +00003157 ssize_t
3158 j,
3159 u,
3160 v,
3161 y;
3162
cristy3ed852e2009-09-05 21:47:34 +00003163 /*
3164 Initialize blur image attributes.
3165 */
3166 assert(image != (Image *) NULL);
3167 assert(image->signature == MagickSignature);
3168 if (image->debug != MagickFalse)
3169 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3170 assert(exception != (ExceptionInfo *) NULL);
3171 assert(exception->signature == MagickSignature);
3172 width=GetOptimalKernelWidth1D(radius,sigma);
3173 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3174 if (kernel == (double *) NULL)
3175 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003176 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003177 i=0;
cristy47e00502009-12-17 19:19:57 +00003178 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003179 {
cristy47e00502009-12-17 19:19:57 +00003180 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003181 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3182 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003183 }
3184 if (image->debug != MagickFalse)
3185 {
3186 char
3187 format[MaxTextExtent],
3188 *message;
3189
cristy117ff172010-08-15 21:35:32 +00003190 register const double
3191 *k;
3192
cristybb503372010-05-27 20:51:26 +00003193 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003194 u,
3195 v;
3196
cristy3ed852e2009-09-05 21:47:34 +00003197 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003198 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3199 width);
cristy3ed852e2009-09-05 21:47:34 +00003200 message=AcquireString("");
3201 k=kernel;
cristybb503372010-05-27 20:51:26 +00003202 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003203 {
3204 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003205 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003206 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003207 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003208 {
cristyb51dff52011-05-19 16:55:47 +00003209 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003210 (void) ConcatenateString(&message,format);
3211 }
3212 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3213 }
3214 message=DestroyString(message);
3215 }
3216 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3217 if (blur_image == (Image *) NULL)
3218 return((Image *) NULL);
3219 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3220 {
3221 InheritException(exception,&blur_image->exception);
3222 blur_image=DestroyImage(blur_image);
3223 return((Image *) NULL);
3224 }
3225 /*
3226 Threshold blur image.
3227 */
3228 status=MagickTrue;
3229 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003230 GetPixelInfo(image,&bias);
3231 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003232 image_view=AcquireCacheView(image);
3233 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003234#if defined(MAGICKCORE_OPENMP_SUPPORT)
3235 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003236#endif
cristybb503372010-05-27 20:51:26 +00003237 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003238 {
cristy4c08aed2011-07-01 19:47:50 +00003239 double
3240 contrast;
3241
cristy3ed852e2009-09-05 21:47:34 +00003242 MagickBooleanType
3243 sync;
3244
3245 MagickRealType
3246 gamma;
3247
cristy4c08aed2011-07-01 19:47:50 +00003248 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003249 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003250
cristy4c08aed2011-07-01 19:47:50 +00003251 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003252 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003253
cristy117ff172010-08-15 21:35:32 +00003254 register ssize_t
3255 x;
3256
cristy3ed852e2009-09-05 21:47:34 +00003257 if (status == MagickFalse)
3258 continue;
cristy117ff172010-08-15 21:35:32 +00003259 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3260 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00003261 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3262 exception);
cristy4c08aed2011-07-01 19:47:50 +00003263 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003264 {
3265 status=MagickFalse;
3266 continue;
3267 }
cristybb503372010-05-27 20:51:26 +00003268 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003269 {
cristy4c08aed2011-07-01 19:47:50 +00003270 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003271 pixel;
3272
3273 register const double
cristyc47d1f82009-11-26 01:44:43 +00003274 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00003275
cristybb503372010-05-27 20:51:26 +00003276 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003277 u;
3278
cristy117ff172010-08-15 21:35:32 +00003279 ssize_t
3280 j,
3281 v;
3282
cristyddd82202009-11-03 20:14:50 +00003283 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00003284 k=kernel;
3285 gamma=0.0;
3286 j=0;
cristyed231572011-07-14 02:18:59 +00003287 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003288 {
cristybb503372010-05-27 20:51:26 +00003289 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003290 {
cristybb503372010-05-27 20:51:26 +00003291 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003292 {
cristyed231572011-07-14 02:18:59 +00003293 contrast=GetPixelIntensity(image,p+(u+j)*GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003294 (double) GetPixelIntensity(blur_image,q);
3295 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003296 {
cristy4c08aed2011-07-01 19:47:50 +00003297 pixel.red+=(*k)*
cristyed231572011-07-14 02:18:59 +00003298 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003299 pixel.green+=(*k)*
cristyed231572011-07-14 02:18:59 +00003300 GetPixelGreen(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003301 pixel.blue+=(*k)*
cristyed231572011-07-14 02:18:59 +00003302 GetPixelBlue(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003303 if (image->colorspace == CMYKColorspace)
3304 pixel.black+=(*k)*
cristyed231572011-07-14 02:18:59 +00003305 GetPixelBlack(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003306 gamma+=(*k);
3307 k++;
3308 }
3309 }
cristyd99b0962010-05-29 23:14:26 +00003310 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003311 }
3312 if (gamma != 0.0)
3313 {
3314 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003315 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003316 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003317 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003318 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003319 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003320 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003321 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003322 (image->colorspace == CMYKColorspace))
3323 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003324 }
cristyed231572011-07-14 02:18:59 +00003325 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003326 {
3327 gamma=0.0;
3328 j=0;
cristybb503372010-05-27 20:51:26 +00003329 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003330 {
cristybb503372010-05-27 20:51:26 +00003331 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003332 {
cristy4c08aed2011-07-01 19:47:50 +00003333 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003334 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003335 GetPixelIntensity(blur_image,q);
3336 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003337 {
cristy4c08aed2011-07-01 19:47:50 +00003338 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003339 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003340 gamma+=(*k);
3341 k++;
3342 }
3343 }
cristyeaedf062010-05-29 22:36:02 +00003344 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003345 }
3346 if (gamma != 0.0)
3347 {
3348 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3349 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003350 SetPixelAlpha(blur_image,ClampToQuantum(gamma*pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003351 }
3352 }
3353 }
3354 else
3355 {
3356 MagickRealType
3357 alpha;
3358
cristybb503372010-05-27 20:51:26 +00003359 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003360 {
cristybb503372010-05-27 20:51:26 +00003361 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003362 {
cristy4c08aed2011-07-01 19:47:50 +00003363 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003364 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003365 GetPixelIntensity(blur_image,q);
3366 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003367 {
cristy4c08aed2011-07-01 19:47:50 +00003368 alpha=(MagickRealType) (QuantumScale*
cristyed231572011-07-14 02:18:59 +00003369 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image)));
cristy4c08aed2011-07-01 19:47:50 +00003370 pixel.red+=(*k)*alpha*
cristyed231572011-07-14 02:18:59 +00003371 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003372 pixel.green+=(*k)*alpha*GetPixelGreen(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003373 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003374 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003375 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003376 pixel.alpha+=(*k)*GetPixelAlpha(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003377 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003378 if (image->colorspace == CMYKColorspace)
3379 pixel.black+=(*k)*GetPixelBlack(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003380 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003381 gamma+=(*k)*alpha;
3382 k++;
3383 }
3384 }
cristyeaedf062010-05-29 22:36:02 +00003385 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003386 }
3387 if (gamma != 0.0)
3388 {
3389 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003390 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003391 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003392 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003393 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003394 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003395 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003396 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003397 (image->colorspace == CMYKColorspace))
3398 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003399 }
cristyed231572011-07-14 02:18:59 +00003400 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003401 {
3402 gamma=0.0;
3403 j=0;
cristybb503372010-05-27 20:51:26 +00003404 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003405 {
cristybb503372010-05-27 20:51:26 +00003406 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003407 {
cristy4c08aed2011-07-01 19:47:50 +00003408 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003409 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003410 GetPixelIntensity(blur_image,q);
3411 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003412 {
cristy4c08aed2011-07-01 19:47:50 +00003413 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003414 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003415 gamma+=(*k);
3416 k++;
3417 }
3418 }
cristyeaedf062010-05-29 22:36:02 +00003419 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003420 }
3421 if (gamma != 0.0)
3422 {
3423 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3424 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003425 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003426 }
3427 }
3428 }
cristyed231572011-07-14 02:18:59 +00003429 p+=GetPixelChannels(image);
3430 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003431 }
3432 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3433 if (sync == MagickFalse)
3434 status=MagickFalse;
3435 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3436 {
3437 MagickBooleanType
3438 proceed;
3439
cristyb5d5f722009-11-04 03:03:49 +00003440#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003441 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003442#endif
3443 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3444 image->rows);
3445 if (proceed == MagickFalse)
3446 status=MagickFalse;
3447 }
3448 }
3449 blur_image->type=image->type;
3450 blur_view=DestroyCacheView(blur_view);
3451 image_view=DestroyCacheView(image_view);
3452 kernel=(double *) RelinquishMagickMemory(kernel);
3453 if (status == MagickFalse)
3454 blur_image=DestroyImage(blur_image);
3455 return(blur_image);
3456}
3457
3458/*
3459%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3460% %
3461% %
3462% %
3463% S h a d e I m a g e %
3464% %
3465% %
3466% %
3467%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3468%
3469% ShadeImage() shines a distant light on an image to create a
3470% three-dimensional effect. You control the positioning of the light with
3471% azimuth and elevation; azimuth is measured in degrees off the x axis
3472% and elevation is measured in pixels above the Z axis.
3473%
3474% The format of the ShadeImage method is:
3475%
3476% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3477% const double azimuth,const double elevation,ExceptionInfo *exception)
3478%
3479% A description of each parameter follows:
3480%
3481% o image: the image.
3482%
3483% o gray: A value other than zero shades the intensity of each pixel.
3484%
3485% o azimuth, elevation: Define the light source direction.
3486%
3487% o exception: return any errors or warnings in this structure.
3488%
3489*/
3490MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3491 const double azimuth,const double elevation,ExceptionInfo *exception)
3492{
3493#define ShadeImageTag "Shade/Image"
3494
cristyc4c8d132010-01-07 01:58:38 +00003495 CacheView
3496 *image_view,
3497 *shade_view;
3498
cristy3ed852e2009-09-05 21:47:34 +00003499 Image
3500 *shade_image;
3501
cristy3ed852e2009-09-05 21:47:34 +00003502 MagickBooleanType
3503 status;
3504
cristybb503372010-05-27 20:51:26 +00003505 MagickOffsetType
3506 progress;
3507
cristy3ed852e2009-09-05 21:47:34 +00003508 PrimaryInfo
3509 light;
3510
cristybb503372010-05-27 20:51:26 +00003511 ssize_t
3512 y;
3513
cristy3ed852e2009-09-05 21:47:34 +00003514 /*
3515 Initialize shaded image attributes.
3516 */
3517 assert(image != (const Image *) NULL);
3518 assert(image->signature == MagickSignature);
3519 if (image->debug != MagickFalse)
3520 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3521 assert(exception != (ExceptionInfo *) NULL);
3522 assert(exception->signature == MagickSignature);
3523 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3524 if (shade_image == (Image *) NULL)
3525 return((Image *) NULL);
3526 if (SetImageStorageClass(shade_image,DirectClass) == MagickFalse)
3527 {
3528 InheritException(exception,&shade_image->exception);
3529 shade_image=DestroyImage(shade_image);
3530 return((Image *) NULL);
3531 }
3532 /*
3533 Compute the light vector.
3534 */
3535 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3536 cos(DegreesToRadians(elevation));
3537 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3538 cos(DegreesToRadians(elevation));
3539 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3540 /*
3541 Shade image.
3542 */
3543 status=MagickTrue;
3544 progress=0;
3545 image_view=AcquireCacheView(image);
3546 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003547#if defined(MAGICKCORE_OPENMP_SUPPORT)
3548 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003549#endif
cristybb503372010-05-27 20:51:26 +00003550 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003551 {
3552 MagickRealType
3553 distance,
3554 normal_distance,
3555 shade;
3556
3557 PrimaryInfo
3558 normal;
3559
cristy4c08aed2011-07-01 19:47:50 +00003560 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003561 *restrict p,
3562 *restrict s0,
3563 *restrict s1,
3564 *restrict s2;
cristy3ed852e2009-09-05 21:47:34 +00003565
cristy4c08aed2011-07-01 19:47:50 +00003566 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003567 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003568
cristy117ff172010-08-15 21:35:32 +00003569 register ssize_t
3570 x;
3571
cristy3ed852e2009-09-05 21:47:34 +00003572 if (status == MagickFalse)
3573 continue;
3574 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3575 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3576 exception);
cristy4c08aed2011-07-01 19:47:50 +00003577 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003578 {
3579 status=MagickFalse;
3580 continue;
3581 }
3582 /*
3583 Shade this row of pixels.
3584 */
3585 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristyed231572011-07-14 02:18:59 +00003586 s0=p+GetPixelChannels(image);
3587 s1=s0+(image->columns+2)*GetPixelChannels(image);
3588 s2=s1+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003589 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003590 {
3591 /*
3592 Determine the surface normal and compute shading.
3593 */
cristyed231572011-07-14 02:18:59 +00003594 normal.x=(double) (GetPixelIntensity(image,s0-GetPixelChannels(image))+
3595 GetPixelIntensity(image,s1-GetPixelChannels(image))+
3596 GetPixelIntensity(image,s2-GetPixelChannels(image))-
3597 GetPixelIntensity(image,s0+GetPixelChannels(image))-
3598 GetPixelIntensity(image,s1+GetPixelChannels(image))-
3599 GetPixelIntensity(image,s2+GetPixelChannels(image)));
3600 normal.y=(double) (GetPixelIntensity(image,s2-GetPixelChannels(image))+
cristy4c08aed2011-07-01 19:47:50 +00003601 GetPixelIntensity(image,s2)+
cristyed231572011-07-14 02:18:59 +00003602 GetPixelIntensity(image,s2+GetPixelChannels(image))-
3603 GetPixelIntensity(image,s0-GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003604 GetPixelIntensity(image,s0)-
cristyed231572011-07-14 02:18:59 +00003605 GetPixelIntensity(image,s0+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003606 if ((normal.x == 0.0) && (normal.y == 0.0))
3607 shade=light.z;
3608 else
3609 {
3610 shade=0.0;
3611 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3612 if (distance > MagickEpsilon)
3613 {
3614 normal_distance=
3615 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3616 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3617 shade=distance/sqrt((double) normal_distance);
3618 }
3619 }
3620 if (gray != MagickFalse)
3621 {
cristy4c08aed2011-07-01 19:47:50 +00003622 SetPixelRed(shade_image,ClampToQuantum(shade),q);
3623 SetPixelGreen(shade_image,ClampToQuantum(shade),q);
3624 SetPixelBlue(shade_image,ClampToQuantum(shade),q);
cristy3ed852e2009-09-05 21:47:34 +00003625 }
3626 else
3627 {
cristy4c08aed2011-07-01 19:47:50 +00003628 SetPixelRed(shade_image,ClampToQuantum(QuantumScale*shade*
3629 GetPixelRed(image,s1)),q);
3630 SetPixelGreen(shade_image,ClampToQuantum(QuantumScale*shade*
3631 GetPixelGreen(image,s1)),q);
3632 SetPixelBlue(shade_image,ClampToQuantum(QuantumScale*shade*
3633 GetPixelBlue(image,s1)),q);
cristy3ed852e2009-09-05 21:47:34 +00003634 }
cristy4c08aed2011-07-01 19:47:50 +00003635 SetPixelAlpha(shade_image,GetPixelAlpha(image,s1),q);
cristyed231572011-07-14 02:18:59 +00003636 s0+=GetPixelChannels(image);
3637 s1+=GetPixelChannels(image);
3638 s2+=GetPixelChannels(image);
3639 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003640 }
3641 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3642 status=MagickFalse;
3643 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3644 {
3645 MagickBooleanType
3646 proceed;
3647
cristyb5d5f722009-11-04 03:03:49 +00003648#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003649 #pragma omp critical (MagickCore_ShadeImage)
3650#endif
3651 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3652 if (proceed == MagickFalse)
3653 status=MagickFalse;
3654 }
3655 }
3656 shade_view=DestroyCacheView(shade_view);
3657 image_view=DestroyCacheView(image_view);
3658 if (status == MagickFalse)
3659 shade_image=DestroyImage(shade_image);
3660 return(shade_image);
3661}
3662
3663/*
3664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3665% %
3666% %
3667% %
3668% S h a r p e n I m a g e %
3669% %
3670% %
3671% %
3672%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3673%
3674% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3675% operator of the given radius and standard deviation (sigma). For
3676% reasonable results, radius should be larger than sigma. Use a radius of 0
3677% and SharpenImage() selects a suitable radius for you.
3678%
3679% Using a separable kernel would be faster, but the negative weights cancel
3680% out on the corners of the kernel producing often undesirable ringing in the
3681% filtered result; this can be avoided by using a 2D gaussian shaped image
3682% sharpening kernel instead.
3683%
3684% The format of the SharpenImage method is:
3685%
3686% Image *SharpenImage(const Image *image,const double radius,
3687% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003688%
3689% A description of each parameter follows:
3690%
3691% o image: the image.
3692%
cristy3ed852e2009-09-05 21:47:34 +00003693% o radius: the radius of the Gaussian, in pixels, not counting the center
3694% pixel.
3695%
3696% o sigma: the standard deviation of the Laplacian, in pixels.
3697%
3698% o exception: return any errors or warnings in this structure.
3699%
3700*/
cristy3ed852e2009-09-05 21:47:34 +00003701MagickExport Image *SharpenImage(const Image *image,const double radius,
3702 const double sigma,ExceptionInfo *exception)
3703{
cristy3ed852e2009-09-05 21:47:34 +00003704 double
cristy47e00502009-12-17 19:19:57 +00003705 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003706
3707 Image
3708 *sharp_image;
3709
cristy41cbe682011-07-15 19:12:37 +00003710 KernelInfo
3711 *kernel_info;
3712
cristybb503372010-05-27 20:51:26 +00003713 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003714 i;
3715
cristybb503372010-05-27 20:51:26 +00003716 size_t
cristy3ed852e2009-09-05 21:47:34 +00003717 width;
3718
cristy117ff172010-08-15 21:35:32 +00003719 ssize_t
3720 j,
3721 u,
3722 v;
3723
cristy3ed852e2009-09-05 21:47:34 +00003724 assert(image != (const Image *) NULL);
3725 assert(image->signature == MagickSignature);
3726 if (image->debug != MagickFalse)
3727 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3728 assert(exception != (ExceptionInfo *) NULL);
3729 assert(exception->signature == MagickSignature);
3730 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003731 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003732 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003733 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003734 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3735 kernel_info->width=width;
3736 kernel_info->height=width;
3737 kernel_info->signature=MagickSignature;
3738 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
3739 kernel_info->width*sizeof(*kernel_info->values));
3740 if (kernel_info->values == (double *) NULL)
3741 {
3742 kernel_info=DestroyKernelInfo(kernel_info);
3743 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3744 }
cristy3ed852e2009-09-05 21:47:34 +00003745 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003746 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003747 i=0;
3748 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003749 {
cristy47e00502009-12-17 19:19:57 +00003750 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003751 {
cristy41cbe682011-07-15 19:12:37 +00003752 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3753 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3754 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003755 i++;
3756 }
3757 }
cristy41cbe682011-07-15 19:12:37 +00003758 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003759 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003760 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003761 return(sharp_image);
3762}
3763
3764/*
3765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3766% %
3767% %
3768% %
3769% S p r e a d I m a g e %
3770% %
3771% %
3772% %
3773%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3774%
3775% SpreadImage() is a special effects method that randomly displaces each
3776% pixel in a block defined by the radius parameter.
3777%
3778% The format of the SpreadImage method is:
3779%
3780% Image *SpreadImage(const Image *image,const double radius,
3781% ExceptionInfo *exception)
3782%
3783% A description of each parameter follows:
3784%
3785% o image: the image.
3786%
3787% o radius: Choose a random pixel in a neighborhood of this extent.
3788%
3789% o exception: return any errors or warnings in this structure.
3790%
3791*/
3792MagickExport Image *SpreadImage(const Image *image,const double radius,
3793 ExceptionInfo *exception)
3794{
3795#define SpreadImageTag "Spread/Image"
3796
cristyfa112112010-01-04 17:48:07 +00003797 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003798 *image_view,
3799 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003800
cristy3ed852e2009-09-05 21:47:34 +00003801 Image
3802 *spread_image;
3803
cristy3ed852e2009-09-05 21:47:34 +00003804 MagickBooleanType
3805 status;
3806
cristybb503372010-05-27 20:51:26 +00003807 MagickOffsetType
3808 progress;
3809
cristy4c08aed2011-07-01 19:47:50 +00003810 PixelInfo
cristyddd82202009-11-03 20:14:50 +00003811 bias;
cristy3ed852e2009-09-05 21:47:34 +00003812
3813 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003814 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003815
cristybb503372010-05-27 20:51:26 +00003816 size_t
cristy3ed852e2009-09-05 21:47:34 +00003817 width;
3818
cristybb503372010-05-27 20:51:26 +00003819 ssize_t
3820 y;
3821
cristy3ed852e2009-09-05 21:47:34 +00003822 /*
3823 Initialize spread image attributes.
3824 */
3825 assert(image != (Image *) NULL);
3826 assert(image->signature == MagickSignature);
3827 if (image->debug != MagickFalse)
3828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3829 assert(exception != (ExceptionInfo *) NULL);
3830 assert(exception->signature == MagickSignature);
3831 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3832 exception);
3833 if (spread_image == (Image *) NULL)
3834 return((Image *) NULL);
3835 if (SetImageStorageClass(spread_image,DirectClass) == MagickFalse)
3836 {
3837 InheritException(exception,&spread_image->exception);
3838 spread_image=DestroyImage(spread_image);
3839 return((Image *) NULL);
3840 }
3841 /*
3842 Spread image.
3843 */
3844 status=MagickTrue;
3845 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003846 GetPixelInfo(spread_image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003847 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003848 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003849 image_view=AcquireCacheView(image);
3850 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003851#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00003852 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00003853#endif
cristybb503372010-05-27 20:51:26 +00003854 for (y=0; y < (ssize_t) spread_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003855 {
cristy5c9e6f22010-09-17 17:31:01 +00003856 const int
3857 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003858
cristy4c08aed2011-07-01 19:47:50 +00003859 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003860 pixel;
3861
cristy4c08aed2011-07-01 19:47:50 +00003862 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003863 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003864
cristy117ff172010-08-15 21:35:32 +00003865 register ssize_t
3866 x;
3867
cristy3ed852e2009-09-05 21:47:34 +00003868 if (status == MagickFalse)
3869 continue;
cristy9f7e7cb2011-03-26 00:49:57 +00003870 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003871 exception);
cristy4c08aed2011-07-01 19:47:50 +00003872 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003873 {
3874 status=MagickFalse;
3875 continue;
3876 }
cristyddd82202009-11-03 20:14:50 +00003877 pixel=bias;
cristybb503372010-05-27 20:51:26 +00003878 for (x=0; x < (ssize_t) spread_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003879 {
cristy4c08aed2011-07-01 19:47:50 +00003880 (void) InterpolatePixelInfo(image,image_view,
cristy8a7c3e82011-03-26 02:10:53 +00003881 UndefinedInterpolatePixel,(double) x+width*(GetPseudoRandomValue(
3882 random_info[id])-0.5),(double) y+width*(GetPseudoRandomValue(
3883 random_info[id])-0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003884 SetPixelPixelInfo(spread_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00003885 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003886 }
cristy9f7e7cb2011-03-26 00:49:57 +00003887 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003888 status=MagickFalse;
3889 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3890 {
3891 MagickBooleanType
3892 proceed;
3893
cristyb557a152011-02-22 12:14:30 +00003894#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003895 #pragma omp critical (MagickCore_SpreadImage)
3896#endif
3897 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3898 if (proceed == MagickFalse)
3899 status=MagickFalse;
3900 }
3901 }
cristy9f7e7cb2011-03-26 00:49:57 +00003902 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003903 image_view=DestroyCacheView(image_view);
3904 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003905 return(spread_image);
3906}
3907
3908/*
3909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3910% %
3911% %
3912% %
cristy0834d642011-03-18 18:26:08 +00003913% S t a t i s t i c I m a g e %
3914% %
3915% %
3916% %
3917%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3918%
3919% StatisticImage() makes each pixel the min / max / median / mode / etc. of
cristy8d752042011-03-19 01:00:36 +00003920% the neighborhood of the specified width and height.
cristy0834d642011-03-18 18:26:08 +00003921%
3922% The format of the StatisticImage method is:
3923%
3924% Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00003925% const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00003926%
3927% A description of each parameter follows:
3928%
3929% o image: the image.
3930%
cristy0834d642011-03-18 18:26:08 +00003931% o type: the statistic type (median, mode, etc.).
3932%
cristy95c38342011-03-18 22:39:51 +00003933% o width: the width of the pixel neighborhood.
3934%
3935% o height: the height of the pixel neighborhood.
cristy0834d642011-03-18 18:26:08 +00003936%
3937% o exception: return any errors or warnings in this structure.
3938%
3939*/
3940
cristy733678d2011-03-18 21:29:28 +00003941#define ListChannels 5
3942
3943typedef struct _ListNode
3944{
3945 size_t
3946 next[9],
3947 count,
3948 signature;
3949} ListNode;
3950
3951typedef struct _SkipList
3952{
3953 ssize_t
3954 level;
3955
3956 ListNode
3957 *nodes;
3958} SkipList;
3959
3960typedef struct _PixelList
3961{
3962 size_t
cristy6fc86bb2011-03-18 23:45:16 +00003963 length,
cristy733678d2011-03-18 21:29:28 +00003964 seed,
3965 signature;
3966
3967 SkipList
3968 lists[ListChannels];
3969} PixelList;
3970
3971static PixelList *DestroyPixelList(PixelList *pixel_list)
3972{
3973 register ssize_t
3974 i;
3975
3976 if (pixel_list == (PixelList *) NULL)
3977 return((PixelList *) NULL);
3978 for (i=0; i < ListChannels; i++)
3979 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
3980 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
3981 pixel_list->lists[i].nodes);
3982 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
3983 return(pixel_list);
3984}
3985
3986static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
3987{
3988 register ssize_t
3989 i;
3990
3991 assert(pixel_list != (PixelList **) NULL);
3992 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
3993 if (pixel_list[i] != (PixelList *) NULL)
3994 pixel_list[i]=DestroyPixelList(pixel_list[i]);
3995 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
3996 return(pixel_list);
3997}
3998
cristy6fc86bb2011-03-18 23:45:16 +00003999static PixelList *AcquirePixelList(const size_t width,const size_t height)
cristy733678d2011-03-18 21:29:28 +00004000{
4001 PixelList
4002 *pixel_list;
4003
4004 register ssize_t
4005 i;
4006
4007 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
4008 if (pixel_list == (PixelList *) NULL)
4009 return(pixel_list);
4010 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
cristy6fc86bb2011-03-18 23:45:16 +00004011 pixel_list->length=width*height;
cristy733678d2011-03-18 21:29:28 +00004012 for (i=0; i < ListChannels; i++)
4013 {
4014 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
4015 sizeof(*pixel_list->lists[i].nodes));
4016 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
4017 return(DestroyPixelList(pixel_list));
4018 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
4019 sizeof(*pixel_list->lists[i].nodes));
4020 }
4021 pixel_list->signature=MagickSignature;
4022 return(pixel_list);
4023}
4024
cristy6fc86bb2011-03-18 23:45:16 +00004025static PixelList **AcquirePixelListThreadSet(const size_t width,
4026 const size_t height)
cristy733678d2011-03-18 21:29:28 +00004027{
4028 PixelList
4029 **pixel_list;
4030
4031 register ssize_t
4032 i;
4033
4034 size_t
4035 number_threads;
4036
4037 number_threads=GetOpenMPMaximumThreads();
4038 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
4039 sizeof(*pixel_list));
4040 if (pixel_list == (PixelList **) NULL)
4041 return((PixelList **) NULL);
4042 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
4043 for (i=0; i < (ssize_t) number_threads; i++)
4044 {
cristy6fc86bb2011-03-18 23:45:16 +00004045 pixel_list[i]=AcquirePixelList(width,height);
cristy733678d2011-03-18 21:29:28 +00004046 if (pixel_list[i] == (PixelList *) NULL)
4047 return(DestroyPixelListThreadSet(pixel_list));
4048 }
4049 return(pixel_list);
4050}
4051
4052static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
4053 const size_t color)
4054{
4055 register SkipList
4056 *list;
4057
4058 register ssize_t
4059 level;
4060
4061 size_t
4062 search,
4063 update[9];
4064
4065 /*
4066 Initialize the node.
4067 */
4068 list=pixel_list->lists+channel;
4069 list->nodes[color].signature=pixel_list->signature;
4070 list->nodes[color].count=1;
4071 /*
4072 Determine where it belongs in the list.
4073 */
4074 search=65536UL;
4075 for (level=list->level; level >= 0; level--)
4076 {
4077 while (list->nodes[search].next[level] < color)
4078 search=list->nodes[search].next[level];
4079 update[level]=search;
4080 }
4081 /*
4082 Generate a pseudo-random level for this node.
4083 */
4084 for (level=0; ; level++)
4085 {
4086 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
4087 if ((pixel_list->seed & 0x300) != 0x300)
4088 break;
4089 }
4090 if (level > 8)
4091 level=8;
4092 if (level > (list->level+2))
4093 level=list->level+2;
4094 /*
4095 If we're raising the list's level, link back to the root node.
4096 */
4097 while (level > list->level)
4098 {
4099 list->level++;
4100 update[list->level]=65536UL;
4101 }
4102 /*
4103 Link the node into the skip-list.
4104 */
4105 do
4106 {
4107 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
4108 list->nodes[update[level]].next[level]=color;
cristy3cba8ca2011-03-19 01:29:12 +00004109 } while (level-- > 0);
cristy733678d2011-03-18 21:29:28 +00004110}
4111
cristy4c08aed2011-07-01 19:47:50 +00004112static PixelInfo GetMaximumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004113{
cristy4c08aed2011-07-01 19:47:50 +00004114 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004115 pixel;
4116
4117 register SkipList
4118 *list;
4119
4120 register ssize_t
4121 channel;
4122
4123 size_t
cristyd76c51e2011-03-26 00:21:26 +00004124 color,
4125 maximum;
cristy49f37242011-03-22 18:18:23 +00004126
4127 ssize_t
cristy6fc86bb2011-03-18 23:45:16 +00004128 count;
4129
4130 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004131 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004132
4133 /*
4134 Find the maximum value for each of the color.
4135 */
4136 for (channel=0; channel < 5; channel++)
4137 {
4138 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004139 color=65536L;
cristy6fc86bb2011-03-18 23:45:16 +00004140 count=0;
cristy49f37242011-03-22 18:18:23 +00004141 maximum=list->nodes[color].next[0];
cristy6fc86bb2011-03-18 23:45:16 +00004142 do
4143 {
4144 color=list->nodes[color].next[0];
cristy49f37242011-03-22 18:18:23 +00004145 if (color > maximum)
4146 maximum=color;
cristy6fc86bb2011-03-18 23:45:16 +00004147 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004148 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004149 channels[channel]=(unsigned short) maximum;
4150 }
cristy4c08aed2011-07-01 19:47:50 +00004151 GetPixelInfo((const Image *) NULL,&pixel);
cristy49f37242011-03-22 18:18:23 +00004152 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4153 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4154 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004155 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4156 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy49f37242011-03-22 18:18:23 +00004157 return(pixel);
4158}
4159
cristy4c08aed2011-07-01 19:47:50 +00004160static PixelInfo GetMeanPixelList(PixelList *pixel_list)
cristy49f37242011-03-22 18:18:23 +00004161{
cristy4c08aed2011-07-01 19:47:50 +00004162 PixelInfo
cristy49f37242011-03-22 18:18:23 +00004163 pixel;
4164
cristy80a99a32011-03-30 01:30:23 +00004165 MagickRealType
4166 sum;
4167
cristy49f37242011-03-22 18:18:23 +00004168 register SkipList
4169 *list;
4170
4171 register ssize_t
4172 channel;
4173
4174 size_t
cristy80a99a32011-03-30 01:30:23 +00004175 color;
cristy49f37242011-03-22 18:18:23 +00004176
4177 ssize_t
4178 count;
4179
4180 unsigned short
4181 channels[ListChannels];
4182
4183 /*
4184 Find the mean value for each of the color.
4185 */
4186 for (channel=0; channel < 5; channel++)
4187 {
4188 list=pixel_list->lists+channel;
4189 color=65536L;
4190 count=0;
cristy80a99a32011-03-30 01:30:23 +00004191 sum=0.0;
cristy49f37242011-03-22 18:18:23 +00004192 do
4193 {
4194 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004195 sum+=(MagickRealType) list->nodes[color].count*color;
cristy49f37242011-03-22 18:18:23 +00004196 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004197 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004198 sum/=pixel_list->length;
4199 channels[channel]=(unsigned short) sum;
cristy6fc86bb2011-03-18 23:45:16 +00004200 }
cristy4c08aed2011-07-01 19:47:50 +00004201 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004202 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4203 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4204 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004205 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4206 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004207 return(pixel);
4208}
4209
cristy4c08aed2011-07-01 19:47:50 +00004210static PixelInfo GetMedianPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004211{
cristy4c08aed2011-07-01 19:47:50 +00004212 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004213 pixel;
4214
4215 register SkipList
4216 *list;
4217
4218 register ssize_t
4219 channel;
4220
4221 size_t
cristy49f37242011-03-22 18:18:23 +00004222 color;
4223
4224 ssize_t
cristy733678d2011-03-18 21:29:28 +00004225 count;
4226
4227 unsigned short
4228 channels[ListChannels];
4229
4230 /*
4231 Find the median value for each of the color.
4232 */
cristy733678d2011-03-18 21:29:28 +00004233 for (channel=0; channel < 5; channel++)
4234 {
4235 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004236 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004237 count=0;
4238 do
4239 {
4240 color=list->nodes[color].next[0];
4241 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004242 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy6fc86bb2011-03-18 23:45:16 +00004243 channels[channel]=(unsigned short) color;
4244 }
cristy4c08aed2011-07-01 19:47:50 +00004245 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004246 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4247 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4248 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004249 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4250 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004251 return(pixel);
4252}
4253
cristy4c08aed2011-07-01 19:47:50 +00004254static PixelInfo GetMinimumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004255{
cristy4c08aed2011-07-01 19:47:50 +00004256 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004257 pixel;
4258
4259 register SkipList
4260 *list;
4261
4262 register ssize_t
4263 channel;
4264
4265 size_t
cristyd76c51e2011-03-26 00:21:26 +00004266 color,
4267 minimum;
cristy6fc86bb2011-03-18 23:45:16 +00004268
cristy49f37242011-03-22 18:18:23 +00004269 ssize_t
4270 count;
4271
cristy6fc86bb2011-03-18 23:45:16 +00004272 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004273 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004274
4275 /*
4276 Find the minimum value for each of the color.
4277 */
4278 for (channel=0; channel < 5; channel++)
4279 {
4280 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004281 count=0;
cristy6fc86bb2011-03-18 23:45:16 +00004282 color=65536UL;
cristy49f37242011-03-22 18:18:23 +00004283 minimum=list->nodes[color].next[0];
4284 do
4285 {
4286 color=list->nodes[color].next[0];
4287 if (color < minimum)
4288 minimum=color;
4289 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004290 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004291 channels[channel]=(unsigned short) minimum;
cristy733678d2011-03-18 21:29:28 +00004292 }
cristy4c08aed2011-07-01 19:47:50 +00004293 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004294 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4295 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4296 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004297 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4298 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004299 return(pixel);
4300}
4301
cristy4c08aed2011-07-01 19:47:50 +00004302static PixelInfo GetModePixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004303{
cristy4c08aed2011-07-01 19:47:50 +00004304 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004305 pixel;
4306
4307 register SkipList
4308 *list;
4309
4310 register ssize_t
4311 channel;
4312
4313 size_t
4314 color,
cristy733678d2011-03-18 21:29:28 +00004315 max_count,
cristy6fc86bb2011-03-18 23:45:16 +00004316 mode;
cristy733678d2011-03-18 21:29:28 +00004317
cristy49f37242011-03-22 18:18:23 +00004318 ssize_t
4319 count;
4320
cristy733678d2011-03-18 21:29:28 +00004321 unsigned short
4322 channels[5];
4323
4324 /*
glennrp30d2dc62011-06-25 03:17:16 +00004325 Make each pixel the 'predominant color' of the specified neighborhood.
cristy733678d2011-03-18 21:29:28 +00004326 */
cristy733678d2011-03-18 21:29:28 +00004327 for (channel=0; channel < 5; channel++)
4328 {
4329 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004330 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004331 mode=color;
4332 max_count=list->nodes[mode].count;
4333 count=0;
4334 do
4335 {
4336 color=list->nodes[color].next[0];
4337 if (list->nodes[color].count > max_count)
4338 {
4339 mode=color;
4340 max_count=list->nodes[mode].count;
4341 }
4342 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004343 } while (count < (ssize_t) pixel_list->length);
cristy733678d2011-03-18 21:29:28 +00004344 channels[channel]=(unsigned short) mode;
4345 }
cristy4c08aed2011-07-01 19:47:50 +00004346 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004347 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4348 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4349 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004350 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4351 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004352 return(pixel);
4353}
4354
cristy4c08aed2011-07-01 19:47:50 +00004355static PixelInfo GetNonpeakPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004356{
cristy4c08aed2011-07-01 19:47:50 +00004357 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004358 pixel;
4359
4360 register SkipList
4361 *list;
4362
4363 register ssize_t
4364 channel;
4365
4366 size_t
cristy733678d2011-03-18 21:29:28 +00004367 color,
cristy733678d2011-03-18 21:29:28 +00004368 next,
4369 previous;
4370
cristy49f37242011-03-22 18:18:23 +00004371 ssize_t
4372 count;
4373
cristy733678d2011-03-18 21:29:28 +00004374 unsigned short
4375 channels[5];
4376
4377 /*
cristy49f37242011-03-22 18:18:23 +00004378 Finds the non peak value for each of the colors.
cristy733678d2011-03-18 21:29:28 +00004379 */
cristy733678d2011-03-18 21:29:28 +00004380 for (channel=0; channel < 5; channel++)
4381 {
4382 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004383 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004384 next=list->nodes[color].next[0];
4385 count=0;
4386 do
4387 {
4388 previous=color;
4389 color=next;
4390 next=list->nodes[color].next[0];
4391 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004392 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy733678d2011-03-18 21:29:28 +00004393 if ((previous == 65536UL) && (next != 65536UL))
4394 color=next;
4395 else
4396 if ((previous != 65536UL) && (next == 65536UL))
4397 color=previous;
4398 channels[channel]=(unsigned short) color;
4399 }
cristy4c08aed2011-07-01 19:47:50 +00004400 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004401 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4402 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4403 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004404 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4405 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy733678d2011-03-18 21:29:28 +00004406 return(pixel);
4407}
4408
cristy4c08aed2011-07-01 19:47:50 +00004409static PixelInfo GetStandardDeviationPixelList(PixelList *pixel_list)
cristy9a68cbb2011-03-29 00:51:23 +00004410{
cristy4c08aed2011-07-01 19:47:50 +00004411 PixelInfo
cristy9a68cbb2011-03-29 00:51:23 +00004412 pixel;
4413
cristy80a99a32011-03-30 01:30:23 +00004414 MagickRealType
4415 sum,
4416 sum_squared;
4417
cristy9a68cbb2011-03-29 00:51:23 +00004418 register SkipList
4419 *list;
4420
4421 register ssize_t
4422 channel;
4423
4424 size_t
cristy80a99a32011-03-30 01:30:23 +00004425 color;
cristy9a68cbb2011-03-29 00:51:23 +00004426
4427 ssize_t
4428 count;
4429
4430 unsigned short
4431 channels[ListChannels];
4432
4433 /*
cristy80a99a32011-03-30 01:30:23 +00004434 Find the standard-deviation value for each of the color.
cristy9a68cbb2011-03-29 00:51:23 +00004435 */
4436 for (channel=0; channel < 5; channel++)
4437 {
4438 list=pixel_list->lists+channel;
4439 color=65536L;
4440 count=0;
cristy80a99a32011-03-30 01:30:23 +00004441 sum=0.0;
4442 sum_squared=0.0;
cristy9a68cbb2011-03-29 00:51:23 +00004443 do
4444 {
cristy80a99a32011-03-30 01:30:23 +00004445 register ssize_t
4446 i;
4447
cristy9a68cbb2011-03-29 00:51:23 +00004448 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004449 sum+=(MagickRealType) list->nodes[color].count*color;
4450 for (i=0; i < (ssize_t) list->nodes[color].count; i++)
4451 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
cristy9a68cbb2011-03-29 00:51:23 +00004452 count+=list->nodes[color].count;
4453 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004454 sum/=pixel_list->length;
4455 sum_squared/=pixel_list->length;
4456 channels[channel]=(unsigned short) sqrt(sum_squared-(sum*sum));
cristy9a68cbb2011-03-29 00:51:23 +00004457 }
cristy4c08aed2011-07-01 19:47:50 +00004458 GetPixelInfo((const Image *) NULL,&pixel);
cristy9a68cbb2011-03-29 00:51:23 +00004459 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4460 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4461 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004462 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4463 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy9a68cbb2011-03-29 00:51:23 +00004464 return(pixel);
4465}
4466
cristy4c08aed2011-07-01 19:47:50 +00004467static inline void InsertPixelList(const Image *image,const Quantum *pixel,
4468 PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004469{
4470 size_t
4471 signature;
4472
4473 unsigned short
4474 index;
4475
cristy4c08aed2011-07-01 19:47:50 +00004476 index=ScaleQuantumToShort(GetPixelRed(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004477 signature=pixel_list->lists[0].nodes[index].signature;
4478 if (signature == pixel_list->signature)
4479 pixel_list->lists[0].nodes[index].count++;
4480 else
4481 AddNodePixelList(pixel_list,0,index);
cristy4c08aed2011-07-01 19:47:50 +00004482 index=ScaleQuantumToShort(GetPixelGreen(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004483 signature=pixel_list->lists[1].nodes[index].signature;
4484 if (signature == pixel_list->signature)
4485 pixel_list->lists[1].nodes[index].count++;
4486 else
4487 AddNodePixelList(pixel_list,1,index);
cristy4c08aed2011-07-01 19:47:50 +00004488 index=ScaleQuantumToShort(GetPixelBlue(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004489 signature=pixel_list->lists[2].nodes[index].signature;
4490 if (signature == pixel_list->signature)
4491 pixel_list->lists[2].nodes[index].count++;
4492 else
4493 AddNodePixelList(pixel_list,2,index);
cristy4c08aed2011-07-01 19:47:50 +00004494 index=ScaleQuantumToShort(GetPixelAlpha(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004495 signature=pixel_list->lists[3].nodes[index].signature;
4496 if (signature == pixel_list->signature)
4497 pixel_list->lists[3].nodes[index].count++;
4498 else
4499 AddNodePixelList(pixel_list,3,index);
4500 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004501 index=ScaleQuantumToShort(GetPixelBlack(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004502 signature=pixel_list->lists[4].nodes[index].signature;
4503 if (signature == pixel_list->signature)
4504 pixel_list->lists[4].nodes[index].count++;
4505 else
4506 AddNodePixelList(pixel_list,4,index);
4507}
4508
cristy80c99742011-04-04 14:46:39 +00004509static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
4510{
4511 if (x < 0)
4512 return(-x);
4513 return(x);
4514}
4515
cristy733678d2011-03-18 21:29:28 +00004516static void ResetPixelList(PixelList *pixel_list)
4517{
4518 int
4519 level;
4520
4521 register ListNode
4522 *root;
4523
4524 register SkipList
4525 *list;
4526
4527 register ssize_t
4528 channel;
4529
4530 /*
4531 Reset the skip-list.
4532 */
4533 for (channel=0; channel < 5; channel++)
4534 {
4535 list=pixel_list->lists+channel;
4536 root=list->nodes+65536UL;
4537 list->level=0;
4538 for (level=0; level < 9; level++)
4539 root->next[level]=65536UL;
4540 }
4541 pixel_list->seed=pixel_list->signature++;
4542}
4543
cristy0834d642011-03-18 18:26:08 +00004544MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00004545 const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00004546{
cristy3cba8ca2011-03-19 01:29:12 +00004547#define StatisticWidth \
cristyd76c51e2011-03-26 00:21:26 +00004548 (width == 0 ? GetOptimalKernelWidth2D((double) width,0.5) : width)
cristy3cba8ca2011-03-19 01:29:12 +00004549#define StatisticHeight \
cristyd76c51e2011-03-26 00:21:26 +00004550 (height == 0 ? GetOptimalKernelWidth2D((double) height,0.5) : height)
cristy0834d642011-03-18 18:26:08 +00004551#define StatisticImageTag "Statistic/Image"
4552
4553 CacheView
4554 *image_view,
4555 *statistic_view;
4556
4557 Image
4558 *statistic_image;
4559
4560 MagickBooleanType
4561 status;
4562
4563 MagickOffsetType
4564 progress;
4565
4566 PixelList
4567 **restrict pixel_list;
4568
cristy0834d642011-03-18 18:26:08 +00004569 ssize_t
4570 y;
4571
4572 /*
4573 Initialize statistics image attributes.
4574 */
4575 assert(image != (Image *) NULL);
4576 assert(image->signature == MagickSignature);
4577 if (image->debug != MagickFalse)
4578 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4579 assert(exception != (ExceptionInfo *) NULL);
4580 assert(exception->signature == MagickSignature);
cristy0834d642011-03-18 18:26:08 +00004581 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4582 exception);
4583 if (statistic_image == (Image *) NULL)
4584 return((Image *) NULL);
4585 if (SetImageStorageClass(statistic_image,DirectClass) == MagickFalse)
4586 {
4587 InheritException(exception,&statistic_image->exception);
4588 statistic_image=DestroyImage(statistic_image);
4589 return((Image *) NULL);
4590 }
cristy6fc86bb2011-03-18 23:45:16 +00004591 pixel_list=AcquirePixelListThreadSet(StatisticWidth,StatisticHeight);
cristy0834d642011-03-18 18:26:08 +00004592 if (pixel_list == (PixelList **) NULL)
4593 {
4594 statistic_image=DestroyImage(statistic_image);
4595 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4596 }
4597 /*
cristy8d752042011-03-19 01:00:36 +00004598 Make each pixel the min / max / median / mode / etc. of the neighborhood.
cristy0834d642011-03-18 18:26:08 +00004599 */
4600 status=MagickTrue;
4601 progress=0;
4602 image_view=AcquireCacheView(image);
4603 statistic_view=AcquireCacheView(statistic_image);
4604#if defined(MAGICKCORE_OPENMP_SUPPORT)
4605 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4606#endif
4607 for (y=0; y < (ssize_t) statistic_image->rows; y++)
4608 {
4609 const int
4610 id = GetOpenMPThreadId();
4611
cristy4c08aed2011-07-01 19:47:50 +00004612 register const Quantum
cristy0834d642011-03-18 18:26:08 +00004613 *restrict p;
4614
cristy4c08aed2011-07-01 19:47:50 +00004615 register Quantum
cristy0834d642011-03-18 18:26:08 +00004616 *restrict q;
4617
4618 register ssize_t
4619 x;
4620
4621 if (status == MagickFalse)
4622 continue;
cristy6fc86bb2011-03-18 23:45:16 +00004623 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) StatisticWidth/2L),y-
4624 (ssize_t) (StatisticHeight/2L),image->columns+StatisticWidth,
4625 StatisticHeight,exception);
cristy3cba8ca2011-03-19 01:29:12 +00004626 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004627 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy0834d642011-03-18 18:26:08 +00004628 {
4629 status=MagickFalse;
4630 continue;
4631 }
cristy0834d642011-03-18 18:26:08 +00004632 for (x=0; x < (ssize_t) statistic_image->columns; x++)
4633 {
cristy4c08aed2011-07-01 19:47:50 +00004634 PixelInfo
cristy0834d642011-03-18 18:26:08 +00004635 pixel;
4636
cristy4c08aed2011-07-01 19:47:50 +00004637 register const Quantum
cristy6e3026a2011-03-19 00:54:38 +00004638 *restrict r;
4639
cristy0834d642011-03-18 18:26:08 +00004640 register ssize_t
4641 u,
4642 v;
4643
4644 r=p;
cristy0834d642011-03-18 18:26:08 +00004645 ResetPixelList(pixel_list[id]);
cristy6e4c3292011-03-19 00:53:55 +00004646 for (v=0; v < (ssize_t) StatisticHeight; v++)
cristy0834d642011-03-18 18:26:08 +00004647 {
cristy6e4c3292011-03-19 00:53:55 +00004648 for (u=0; u < (ssize_t) StatisticWidth; u++)
cristyed231572011-07-14 02:18:59 +00004649 InsertPixelList(image,r+u*GetPixelChannels(image),pixel_list[id]);
4650 r+=(image->columns+StatisticWidth)*GetPixelChannels(image);
cristy0834d642011-03-18 18:26:08 +00004651 }
cristy4c08aed2011-07-01 19:47:50 +00004652 GetPixelInfo(image,&pixel);
cristy490408a2011-07-07 14:42:05 +00004653 SetPixelInfo(image,p+(StatisticWidth*StatisticHeight/2)*
cristyed231572011-07-14 02:18:59 +00004654 GetPixelChannels(image),&pixel);
cristy0834d642011-03-18 18:26:08 +00004655 switch (type)
4656 {
cristy80c99742011-04-04 14:46:39 +00004657 case GradientStatistic:
4658 {
cristy4c08aed2011-07-01 19:47:50 +00004659 PixelInfo
cristy80c99742011-04-04 14:46:39 +00004660 maximum,
4661 minimum;
4662
4663 minimum=GetMinimumPixelList(pixel_list[id]);
4664 maximum=GetMaximumPixelList(pixel_list[id]);
4665 pixel.red=MagickAbsoluteValue(maximum.red-minimum.red);
4666 pixel.green=MagickAbsoluteValue(maximum.green-minimum.green);
4667 pixel.blue=MagickAbsoluteValue(maximum.blue-minimum.blue);
cristy4c08aed2011-07-01 19:47:50 +00004668 pixel.alpha=MagickAbsoluteValue(maximum.alpha-minimum.alpha);
cristy80c99742011-04-04 14:46:39 +00004669 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004670 pixel.black=MagickAbsoluteValue(maximum.black-minimum.black);
cristy80c99742011-04-04 14:46:39 +00004671 break;
4672 }
cristy6fc86bb2011-03-18 23:45:16 +00004673 case MaximumStatistic:
4674 {
4675 pixel=GetMaximumPixelList(pixel_list[id]);
4676 break;
4677 }
cristy49f37242011-03-22 18:18:23 +00004678 case MeanStatistic:
4679 {
4680 pixel=GetMeanPixelList(pixel_list[id]);
4681 break;
4682 }
cristyf2ad14a2011-03-18 18:57:25 +00004683 case MedianStatistic:
cristy6fc86bb2011-03-18 23:45:16 +00004684 default:
cristyf2ad14a2011-03-18 18:57:25 +00004685 {
4686 pixel=GetMedianPixelList(pixel_list[id]);
4687 break;
4688 }
cristy6fc86bb2011-03-18 23:45:16 +00004689 case MinimumStatistic:
4690 {
4691 pixel=GetMinimumPixelList(pixel_list[id]);
4692 break;
4693 }
cristyf2ad14a2011-03-18 18:57:25 +00004694 case ModeStatistic:
4695 {
4696 pixel=GetModePixelList(pixel_list[id]);
4697 break;
4698 }
4699 case NonpeakStatistic:
4700 {
4701 pixel=GetNonpeakPixelList(pixel_list[id]);
4702 break;
4703 }
cristy9a68cbb2011-03-29 00:51:23 +00004704 case StandardDeviationStatistic:
4705 {
4706 pixel=GetStandardDeviationPixelList(pixel_list[id]);
4707 break;
4708 }
cristy0834d642011-03-18 18:26:08 +00004709 }
cristyed231572011-07-14 02:18:59 +00004710 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004711 SetPixelRed(statistic_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00004712 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004713 SetPixelGreen(statistic_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00004714 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004715 SetPixelBlue(statistic_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00004716 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy0834d642011-03-18 18:26:08 +00004717 (image->colorspace == CMYKColorspace))
cristy490408a2011-07-07 14:42:05 +00004718 SetPixelBlack(statistic_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +00004719 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00004720 (image->matte != MagickFalse))
cristy490408a2011-07-07 14:42:05 +00004721 SetPixelAlpha(statistic_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00004722 p+=GetPixelChannels(image);
4723 q+=GetPixelChannels(statistic_image);
cristy0834d642011-03-18 18:26:08 +00004724 }
4725 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
4726 status=MagickFalse;
4727 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4728 {
4729 MagickBooleanType
4730 proceed;
4731
4732#if defined(MAGICKCORE_OPENMP_SUPPORT)
4733 #pragma omp critical (MagickCore_StatisticImage)
4734#endif
4735 proceed=SetImageProgress(image,StatisticImageTag,progress++,
4736 image->rows);
4737 if (proceed == MagickFalse)
4738 status=MagickFalse;
4739 }
4740 }
4741 statistic_view=DestroyCacheView(statistic_view);
4742 image_view=DestroyCacheView(image_view);
4743 pixel_list=DestroyPixelListThreadSet(pixel_list);
4744 return(statistic_image);
4745}
4746
4747/*
4748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4749% %
4750% %
4751% %
cristy3ed852e2009-09-05 21:47:34 +00004752% U n s h a r p M a s k I m a g e %
4753% %
4754% %
4755% %
4756%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4757%
4758% UnsharpMaskImage() sharpens one or more image channels. We convolve the
4759% image with a Gaussian operator of the given radius and standard deviation
4760% (sigma). For reasonable results, radius should be larger than sigma. Use a
4761% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
4762%
4763% The format of the UnsharpMaskImage method is:
4764%
4765% Image *UnsharpMaskImage(const Image *image,const double radius,
4766% const double sigma,const double amount,const double threshold,
4767% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004768%
4769% A description of each parameter follows:
4770%
4771% o image: the image.
4772%
cristy3ed852e2009-09-05 21:47:34 +00004773% o radius: the radius of the Gaussian, in pixels, not counting the center
4774% pixel.
4775%
4776% o sigma: the standard deviation of the Gaussian, in pixels.
4777%
4778% o amount: the percentage of the difference between the original and the
4779% blur image that is added back into the original.
4780%
4781% o threshold: the threshold in pixels needed to apply the diffence amount.
4782%
4783% o exception: return any errors or warnings in this structure.
4784%
4785*/
cristyf4ad9df2011-07-08 16:49:03 +00004786MagickExport Image *UnsharpMaskImage(const Image *image,
4787 const double radius,const double sigma,const double amount,
4788 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004789{
4790#define SharpenImageTag "Sharpen/Image"
4791
cristyc4c8d132010-01-07 01:58:38 +00004792 CacheView
4793 *image_view,
4794 *unsharp_view;
4795
cristy3ed852e2009-09-05 21:47:34 +00004796 Image
4797 *unsharp_image;
4798
cristy3ed852e2009-09-05 21:47:34 +00004799 MagickBooleanType
4800 status;
4801
cristybb503372010-05-27 20:51:26 +00004802 MagickOffsetType
4803 progress;
4804
cristy4c08aed2011-07-01 19:47:50 +00004805 PixelInfo
cristyddd82202009-11-03 20:14:50 +00004806 bias;
cristy3ed852e2009-09-05 21:47:34 +00004807
4808 MagickRealType
4809 quantum_threshold;
4810
cristybb503372010-05-27 20:51:26 +00004811 ssize_t
4812 y;
4813
cristy3ed852e2009-09-05 21:47:34 +00004814 assert(image != (const Image *) NULL);
4815 assert(image->signature == MagickSignature);
4816 if (image->debug != MagickFalse)
4817 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4818 assert(exception != (ExceptionInfo *) NULL);
cristyf4ad9df2011-07-08 16:49:03 +00004819 unsharp_image=BlurImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004820 if (unsharp_image == (Image *) NULL)
4821 return((Image *) NULL);
4822 quantum_threshold=(MagickRealType) QuantumRange*threshold;
4823 /*
4824 Unsharp-mask image.
4825 */
4826 status=MagickTrue;
4827 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00004828 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00004829 image_view=AcquireCacheView(image);
4830 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00004831#if defined(MAGICKCORE_OPENMP_SUPPORT)
4832 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004833#endif
cristybb503372010-05-27 20:51:26 +00004834 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004835 {
cristy4c08aed2011-07-01 19:47:50 +00004836 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004837 pixel;
4838
cristy4c08aed2011-07-01 19:47:50 +00004839 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004840 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004841
cristy4c08aed2011-07-01 19:47:50 +00004842 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004843 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004844
cristy117ff172010-08-15 21:35:32 +00004845 register ssize_t
4846 x;
4847
cristy3ed852e2009-09-05 21:47:34 +00004848 if (status == MagickFalse)
4849 continue;
4850 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4851 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
4852 exception);
cristy4c08aed2011-07-01 19:47:50 +00004853 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004854 {
4855 status=MagickFalse;
4856 continue;
4857 }
cristyddd82202009-11-03 20:14:50 +00004858 pixel=bias;
cristybb503372010-05-27 20:51:26 +00004859 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004860 {
cristyed231572011-07-14 02:18:59 +00004861 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004862 {
cristy4c08aed2011-07-01 19:47:50 +00004863 pixel.red=GetPixelRed(image,p)-(MagickRealType) GetPixelRed(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004864 if (fabs(2.0*pixel.red) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004865 pixel.red=(MagickRealType) GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004866 else
cristy4c08aed2011-07-01 19:47:50 +00004867 pixel.red=(MagickRealType) GetPixelRed(image,p)+(pixel.red*amount);
4868 SetPixelRed(unsharp_image,ClampToQuantum(pixel.red),q);
cristy3ed852e2009-09-05 21:47:34 +00004869 }
cristyed231572011-07-14 02:18:59 +00004870 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004871 {
cristy4c08aed2011-07-01 19:47:50 +00004872 pixel.green=GetPixelGreen(image,p)-
4873 (MagickRealType) GetPixelGreen(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004874 if (fabs(2.0*pixel.green) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004875 pixel.green=(MagickRealType)
4876 GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004877 else
cristy4c08aed2011-07-01 19:47:50 +00004878 pixel.green=(MagickRealType)
4879 GetPixelGreen(image,p)+
4880 (pixel.green*amount);
4881 SetPixelGreen(unsharp_image,
4882 ClampToQuantum(pixel.green),q);
cristy3ed852e2009-09-05 21:47:34 +00004883 }
cristyed231572011-07-14 02:18:59 +00004884 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004885 {
cristy4c08aed2011-07-01 19:47:50 +00004886 pixel.blue=GetPixelBlue(image,p)-
4887 (MagickRealType) GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00004888 if (fabs(2.0*pixel.blue) < quantum_threshold)
cristy4c08aed2011-07-01 19:47:50 +00004889 pixel.blue=(MagickRealType)
4890 GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004891 else
cristy4c08aed2011-07-01 19:47:50 +00004892 pixel.blue=(MagickRealType)
4893 GetPixelBlue(image,p)+(pixel.blue*amount);
4894 SetPixelBlue(unsharp_image,
4895 ClampToQuantum(pixel.blue),q);
cristy3ed852e2009-09-05 21:47:34 +00004896 }
cristyed231572011-07-14 02:18:59 +00004897 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00004898 (image->colorspace == CMYKColorspace))
4899 {
cristy4c08aed2011-07-01 19:47:50 +00004900 pixel.black=GetPixelBlack(image,p)-
4901 (MagickRealType) GetPixelBlack(image,q);
4902 if (fabs(2.0*pixel.black) < quantum_threshold)
4903 pixel.black=(MagickRealType)
4904 GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004905 else
cristy4c08aed2011-07-01 19:47:50 +00004906 pixel.black=(MagickRealType)
4907 GetPixelBlack(image,p)+(pixel.black*
4908 amount);
4909 SetPixelBlack(unsharp_image,
4910 ClampToQuantum(pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00004911 }
cristyed231572011-07-14 02:18:59 +00004912 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004913 {
4914 pixel.alpha=GetPixelAlpha(image,p)-
4915 (MagickRealType) GetPixelAlpha(image,q);
4916 if (fabs(2.0*pixel.alpha) < quantum_threshold)
4917 pixel.alpha=(MagickRealType)
4918 GetPixelAlpha(image,p);
4919 else
4920 pixel.alpha=GetPixelAlpha(image,p)+
4921 (pixel.alpha*amount);
4922 SetPixelAlpha(unsharp_image,
4923 ClampToQuantum(pixel.alpha),q);
4924 }
cristyed231572011-07-14 02:18:59 +00004925 p+=GetPixelChannels(image);
4926 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00004927 }
4928 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
4929 status=MagickFalse;
4930 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4931 {
4932 MagickBooleanType
4933 proceed;
4934
cristyb5d5f722009-11-04 03:03:49 +00004935#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00004936 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00004937#endif
4938 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4939 if (proceed == MagickFalse)
4940 status=MagickFalse;
4941 }
4942 }
4943 unsharp_image->type=image->type;
4944 unsharp_view=DestroyCacheView(unsharp_view);
4945 image_view=DestroyCacheView(image_view);
4946 if (status == MagickFalse)
4947 unsharp_image=DestroyImage(unsharp_image);
4948 return(unsharp_image);
4949}