blob: 3fdfab1a2c72b1a2220c3e874dbd7bf9d6266717 [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"
cristy8ea81222011-09-04 10:33:32 +000059#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000060#include "MagickCore/geometry.h"
61#include "MagickCore/image-private.h"
62#include "MagickCore/list.h"
63#include "MagickCore/log.h"
64#include "MagickCore/memory_.h"
65#include "MagickCore/monitor.h"
66#include "MagickCore/monitor-private.h"
67#include "MagickCore/montage.h"
68#include "MagickCore/morphology.h"
69#include "MagickCore/paint.h"
70#include "MagickCore/pixel-accessor.h"
71#include "MagickCore/property.h"
72#include "MagickCore/quantize.h"
73#include "MagickCore/quantum.h"
74#include "MagickCore/quantum-private.h"
75#include "MagickCore/random_.h"
76#include "MagickCore/random-private.h"
77#include "MagickCore/resample.h"
78#include "MagickCore/resample-private.h"
79#include "MagickCore/resize.h"
80#include "MagickCore/resource_.h"
81#include "MagickCore/segment.h"
82#include "MagickCore/shear.h"
83#include "MagickCore/signature-private.h"
84#include "MagickCore/string_.h"
85#include "MagickCore/thread-private.h"
86#include "MagickCore/transform.h"
87#include "MagickCore/threshold.h"
cristy3ed852e2009-09-05 21:47:34 +000088
89/*
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91% %
92% %
93% %
94% A d a p t i v e B l u r I m a g e %
95% %
96% %
97% %
98%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99%
100% AdaptiveBlurImage() adaptively blurs the image by blurring less
101% intensely near image edges and more intensely far from edges. We blur the
102% image with a Gaussian operator of the given radius and standard deviation
103% (sigma). For reasonable results, radius should be larger than sigma. Use a
104% radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
105%
106% The format of the AdaptiveBlurImage method is:
107%
108% Image *AdaptiveBlurImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000109% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000110%
111% A description of each parameter follows:
112%
113% o image: the image.
114%
cristy3ed852e2009-09-05 21:47:34 +0000115% o radius: the radius of the Gaussian, in pixels, not counting the center
116% pixel.
117%
118% o sigma: the standard deviation of the Laplacian, in pixels.
119%
cristy4c11c2b2011-09-05 20:17:07 +0000120% o bias: the bias.
121%
cristy3ed852e2009-09-05 21:47:34 +0000122% o exception: return any errors or warnings in this structure.
123%
124*/
125
cristyf89cb1d2011-07-07 01:24:37 +0000126MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
cristy051718b2011-08-28 22:49:25 +0000127 const char *levels,ExceptionInfo *exception)
cristyf89cb1d2011-07-07 01:24:37 +0000128{
129 double
130 black_point,
131 gamma,
132 white_point;
133
134 GeometryInfo
135 geometry_info;
136
137 MagickBooleanType
138 status;
139
140 MagickStatusType
141 flags;
142
143 /*
144 Parse levels.
145 */
146 if (levels == (char *) NULL)
147 return(MagickFalse);
148 flags=ParseGeometry(levels,&geometry_info);
149 black_point=geometry_info.rho;
150 white_point=(double) QuantumRange;
151 if ((flags & SigmaValue) != 0)
152 white_point=geometry_info.sigma;
153 gamma=1.0;
154 if ((flags & XiValue) != 0)
155 gamma=geometry_info.xi;
156 if ((flags & PercentValue) != 0)
157 {
158 black_point*=(double) image->columns*image->rows/100.0;
159 white_point*=(double) image->columns*image->rows/100.0;
160 }
161 if ((flags & SigmaValue) == 0)
162 white_point=(double) QuantumRange-black_point;
163 if ((flags & AspectValue ) == 0)
cristy7c0a0a42011-08-23 17:57:25 +0000164 status=LevelImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000165 else
cristy7c0a0a42011-08-23 17:57:25 +0000166 status=LevelizeImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000167 return(status);
168}
169
cristyf4ad9df2011-07-08 16:49:03 +0000170MagickExport Image *AdaptiveBlurImage(const Image *image,
cristy4c11c2b2011-09-05 20:17:07 +0000171 const double radius,const double sigma,const double bias,
172 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000173{
174#define AdaptiveBlurImageTag "Convolve/Image"
175#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
176
cristyc4c8d132010-01-07 01:58:38 +0000177 CacheView
178 *blur_view,
179 *edge_view,
180 *image_view;
181
cristy3ed852e2009-09-05 21:47:34 +0000182 double
cristy47e00502009-12-17 19:19:57 +0000183 **kernel,
184 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000185
186 Image
187 *blur_image,
188 *edge_image,
189 *gaussian_image;
190
cristy3ed852e2009-09-05 21:47:34 +0000191 MagickBooleanType
192 status;
193
cristybb503372010-05-27 20:51:26 +0000194 MagickOffsetType
195 progress;
196
cristybb503372010-05-27 20:51:26 +0000197 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000198 i;
cristy3ed852e2009-09-05 21:47:34 +0000199
cristybb503372010-05-27 20:51:26 +0000200 size_t
cristy3ed852e2009-09-05 21:47:34 +0000201 width;
202
cristybb503372010-05-27 20:51:26 +0000203 ssize_t
204 j,
205 k,
206 u,
207 v,
208 y;
209
cristy3ed852e2009-09-05 21:47:34 +0000210 assert(image != (const Image *) NULL);
211 assert(image->signature == MagickSignature);
212 if (image->debug != MagickFalse)
213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
214 assert(exception != (ExceptionInfo *) NULL);
215 assert(exception->signature == MagickSignature);
216 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
217 if (blur_image == (Image *) NULL)
218 return((Image *) NULL);
219 if (fabs(sigma) <= MagickEpsilon)
220 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000221 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000222 {
cristy3ed852e2009-09-05 21:47:34 +0000223 blur_image=DestroyImage(blur_image);
224 return((Image *) NULL);
225 }
226 /*
227 Edge detect the image brighness channel, level, blur, and level again.
228 */
cristy8ae632d2011-09-05 17:29:53 +0000229 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000230 if (edge_image == (Image *) NULL)
231 {
232 blur_image=DestroyImage(blur_image);
233 return((Image *) NULL);
234 }
cristy051718b2011-08-28 22:49:25 +0000235 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristy05c0c9a2011-09-05 23:16:13 +0000236 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +0000237 if (gaussian_image != (Image *) NULL)
238 {
239 edge_image=DestroyImage(edge_image);
240 edge_image=gaussian_image;
241 }
cristy051718b2011-08-28 22:49:25 +0000242 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
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;
cristy3ed852e2009-09-05 21:47:34 +0000294 image_view=AcquireCacheView(image);
295 edge_view=AcquireCacheView(edge_image);
296 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000297#if defined(MAGICKCORE_OPENMP_SUPPORT)
298 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000299#endif
cristybb503372010-05-27 20:51:26 +0000300 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000301 {
cristy4c08aed2011-07-01 19:47:50 +0000302 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000303 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000304
cristy4c08aed2011-07-01 19:47:50 +0000305 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000306 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000307
cristy117ff172010-08-15 21:35:32 +0000308 register ssize_t
309 x;
310
cristy3ed852e2009-09-05 21:47:34 +0000311 if (status == MagickFalse)
312 continue;
313 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
314 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
315 exception);
cristyacd2ed22011-08-30 01:44:23 +0000316 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000317 {
318 status=MagickFalse;
319 continue;
320 }
cristybb503372010-05-27 20:51:26 +0000321 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000322 {
cristy4c11c2b2011-09-05 20:17:07 +0000323 register const Quantum
324 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000325
cristybb503372010-05-27 20:51:26 +0000326 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000327 i;
cristy3ed852e2009-09-05 21:47:34 +0000328
cristy4c11c2b2011-09-05 20:17:07 +0000329 ssize_t
330 center,
331 j;
332
333 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000334 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000335 if (j < 0)
336 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000337 else
cristy4c11c2b2011-09-05 20:17:07 +0000338 if (j > (ssize_t) width)
339 j=(ssize_t) width;
340 if ((j & 0x01) != 0)
341 j--;
342 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
343 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000344 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000345 break;
cristy4c11c2b2011-09-05 20:17:07 +0000346 center=(ssize_t) GetPixelChannels(image)*(width-j)*
347 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2);
348 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000349 {
cristy4c11c2b2011-09-05 20:17:07 +0000350 MagickRealType
351 alpha,
352 gamma,
353 pixel;
354
355 PixelChannel
356 channel;
357
358 PixelTrait
359 blur_traits,
360 traits;
361
362 register const double
363 *restrict k;
364
365 register const Quantum
366 *restrict pixels;
367
368 register ssize_t
369 u;
370
371 ssize_t
372 v;
373
374 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
375 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
376 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
377 if ((traits == UndefinedPixelTrait) ||
378 (blur_traits == UndefinedPixelTrait))
379 continue;
380 if ((blur_traits & CopyPixelTrait) != 0)
381 {
382 q[channel]=p[center+i];
383 continue;
384 }
385 k=kernel[j];
386 pixels=p;
387 pixel=bias;
388 gamma=0.0;
389 if ((blur_traits & BlendPixelTrait) == 0)
390 {
391 /*
392 No alpha blending.
393 */
394 for (v=0; v < (ssize_t) (width-j); v++)
395 {
396 for (u=0; u < (ssize_t) (width-j); u++)
397 {
398 pixel+=(*k)*pixels[i];
399 gamma+=(*k);
400 k++;
401 pixels+=GetPixelChannels(image);
402 }
403 }
404 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
405 q[channel]=ClampToQuantum(gamma*pixel);
406 continue;
407 }
408 /*
409 Alpha blending.
410 */
411 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000412 {
cristy4c11c2b2011-09-05 20:17:07 +0000413 for (u=0; u < (ssize_t) (width-j); u++)
414 {
415 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
416 pixel+=(*k)*alpha*pixels[i];
417 gamma+=(*k)*alpha;
418 k++;
419 pixels+=GetPixelChannels(image);
420 }
cristy3ed852e2009-09-05 21:47:34 +0000421 }
cristy4c11c2b2011-09-05 20:17:07 +0000422 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
423 q[channel]=ClampToQuantum(gamma*pixel);
cristy3ed852e2009-09-05 21:47:34 +0000424 }
cristyed231572011-07-14 02:18:59 +0000425 q+=GetPixelChannels(blur_image);
426 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000427 }
428 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
429 status=MagickFalse;
430 if (image->progress_monitor != (MagickProgressMonitor) NULL)
431 {
432 MagickBooleanType
433 proceed;
434
cristyb5d5f722009-11-04 03:03:49 +0000435#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8ae632d2011-09-05 17:29:53 +0000436 #pragma omp critical (MagickCore_AdaptiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +0000437#endif
438 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
439 image->rows);
440 if (proceed == MagickFalse)
441 status=MagickFalse;
442 }
443 }
444 blur_image->type=image->type;
445 blur_view=DestroyCacheView(blur_view);
446 edge_view=DestroyCacheView(edge_view);
447 image_view=DestroyCacheView(image_view);
448 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000449 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000450 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
451 kernel=(double **) RelinquishMagickMemory(kernel);
452 if (status == MagickFalse)
453 blur_image=DestroyImage(blur_image);
454 return(blur_image);
455}
456
457/*
458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459% %
460% %
461% %
462% A d a p t i v e S h a r p e n I m a g e %
463% %
464% %
465% %
466%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467%
468% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
469% intensely near image edges and less intensely far from edges. We sharpen the
470% image with a Gaussian operator of the given radius and standard deviation
471% (sigma). For reasonable results, radius should be larger than sigma. Use a
472% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
473%
474% The format of the AdaptiveSharpenImage method is:
475%
476% Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000477% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000478%
479% A description of each parameter follows:
480%
481% o image: the image.
482%
cristy3ed852e2009-09-05 21:47:34 +0000483% o radius: the radius of the Gaussian, in pixels, not counting the center
484% pixel.
485%
486% o sigma: the standard deviation of the Laplacian, in pixels.
487%
cristy4c11c2b2011-09-05 20:17:07 +0000488% o bias: the bias.
489%
cristy3ed852e2009-09-05 21:47:34 +0000490% o exception: return any errors or warnings in this structure.
491%
492*/
cristy3ed852e2009-09-05 21:47:34 +0000493MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000494 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000495{
cristy3ed852e2009-09-05 21:47:34 +0000496#define AdaptiveSharpenImageTag "Convolve/Image"
497#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
498
cristyc4c8d132010-01-07 01:58:38 +0000499 CacheView
500 *sharp_view,
501 *edge_view,
502 *image_view;
503
cristy3ed852e2009-09-05 21:47:34 +0000504 double
cristy47e00502009-12-17 19:19:57 +0000505 **kernel,
506 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000507
508 Image
509 *sharp_image,
510 *edge_image,
511 *gaussian_image;
512
cristy3ed852e2009-09-05 21:47:34 +0000513 MagickBooleanType
514 status;
515
cristybb503372010-05-27 20:51:26 +0000516 MagickOffsetType
517 progress;
518
cristybb503372010-05-27 20:51:26 +0000519 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000520 i;
cristy3ed852e2009-09-05 21:47:34 +0000521
cristybb503372010-05-27 20:51:26 +0000522 size_t
cristy3ed852e2009-09-05 21:47:34 +0000523 width;
524
cristybb503372010-05-27 20:51:26 +0000525 ssize_t
526 j,
527 k,
528 u,
529 v,
530 y;
531
cristy3ed852e2009-09-05 21:47:34 +0000532 assert(image != (const Image *) NULL);
533 assert(image->signature == MagickSignature);
534 if (image->debug != MagickFalse)
535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
536 assert(exception != (ExceptionInfo *) NULL);
537 assert(exception->signature == MagickSignature);
538 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
539 if (sharp_image == (Image *) NULL)
540 return((Image *) NULL);
541 if (fabs(sigma) <= MagickEpsilon)
542 return(sharp_image);
cristy574cc262011-08-05 01:23:58 +0000543 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000544 {
cristy3ed852e2009-09-05 21:47:34 +0000545 sharp_image=DestroyImage(sharp_image);
546 return((Image *) NULL);
547 }
548 /*
549 Edge detect the image brighness channel, level, sharp, and level again.
550 */
cristy8ae632d2011-09-05 17:29:53 +0000551 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000552 if (edge_image == (Image *) NULL)
553 {
554 sharp_image=DestroyImage(sharp_image);
555 return((Image *) NULL);
556 }
cristy051718b2011-08-28 22:49:25 +0000557 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristy05c0c9a2011-09-05 23:16:13 +0000558 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +0000559 if (gaussian_image != (Image *) NULL)
560 {
561 edge_image=DestroyImage(edge_image);
562 edge_image=gaussian_image;
563 }
cristy051718b2011-08-28 22:49:25 +0000564 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000565 /*
566 Create a set of kernels from maximum (radius,sigma) to minimum.
567 */
568 width=GetOptimalKernelWidth2D(radius,sigma);
569 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
570 if (kernel == (double **) NULL)
571 {
572 edge_image=DestroyImage(edge_image);
573 sharp_image=DestroyImage(sharp_image);
574 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
575 }
576 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000577 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000578 {
579 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
580 sizeof(**kernel));
581 if (kernel[i] == (double *) NULL)
582 break;
cristy47e00502009-12-17 19:19:57 +0000583 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000584 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000585 k=0;
586 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000587 {
cristy47e00502009-12-17 19:19:57 +0000588 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000589 {
cristy4205a3c2010-09-12 20:19:59 +0000590 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
591 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000592 normalize+=kernel[i][k];
593 k++;
cristy3ed852e2009-09-05 21:47:34 +0000594 }
595 }
cristy3ed852e2009-09-05 21:47:34 +0000596 if (fabs(normalize) <= MagickEpsilon)
597 normalize=1.0;
598 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000599 for (k=0; k < (j*j); k++)
600 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000601 }
cristybb503372010-05-27 20:51:26 +0000602 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000603 {
604 for (i-=2; i >= 0; i-=2)
605 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
606 kernel=(double **) RelinquishMagickMemory(kernel);
607 edge_image=DestroyImage(edge_image);
608 sharp_image=DestroyImage(sharp_image);
609 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
610 }
611 /*
612 Adaptively sharpen image.
613 */
614 status=MagickTrue;
615 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000616 image_view=AcquireCacheView(image);
617 edge_view=AcquireCacheView(edge_image);
618 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000619#if defined(MAGICKCORE_OPENMP_SUPPORT)
620 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000621#endif
cristybb503372010-05-27 20:51:26 +0000622 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000623 {
cristy4c08aed2011-07-01 19:47:50 +0000624 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000625 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000626
cristy4c08aed2011-07-01 19:47:50 +0000627 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000628 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000629
cristy117ff172010-08-15 21:35:32 +0000630 register ssize_t
631 x;
632
cristy3ed852e2009-09-05 21:47:34 +0000633 if (status == MagickFalse)
634 continue;
635 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
636 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
637 exception);
cristy4c08aed2011-07-01 19:47:50 +0000638 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000639 {
640 status=MagickFalse;
641 continue;
642 }
cristybb503372010-05-27 20:51:26 +0000643 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000644 {
cristy4c11c2b2011-09-05 20:17:07 +0000645 register const Quantum
646 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000647
cristybb503372010-05-27 20:51:26 +0000648 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000649 i;
cristy3ed852e2009-09-05 21:47:34 +0000650
cristy4c11c2b2011-09-05 20:17:07 +0000651 ssize_t
652 center,
653 j;
654
655 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000656 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000657 if (j < 0)
658 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000659 else
cristy4c11c2b2011-09-05 20:17:07 +0000660 if (j > (ssize_t) width)
661 j=(ssize_t) width;
662 if ((j & 0x01) != 0)
663 j--;
664 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
665 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000666 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000667 break;
cristy4c11c2b2011-09-05 20:17:07 +0000668 center=(ssize_t) GetPixelChannels(image)*(width-j)*
669 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2);
670 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000671 {
cristy4c11c2b2011-09-05 20:17:07 +0000672 MagickRealType
673 alpha,
674 gamma,
675 pixel;
676
677 PixelChannel
678 channel;
679
680 PixelTrait
681 sharp_traits,
682 traits;
683
684 register const double
685 *restrict k;
686
687 register const Quantum
688 *restrict pixels;
689
690 register ssize_t
691 u;
692
693 ssize_t
694 v;
695
696 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
697 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
698 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
699 if ((traits == UndefinedPixelTrait) ||
700 (sharp_traits == UndefinedPixelTrait))
701 continue;
702 if ((sharp_traits & CopyPixelTrait) != 0)
703 {
704 q[channel]=p[center+i];
705 continue;
706 }
707 k=kernel[j];
708 pixels=p;
709 pixel=bias;
710 gamma=0.0;
711 if ((sharp_traits & BlendPixelTrait) == 0)
712 {
713 /*
714 No alpha blending.
715 */
716 for (v=0; v < (ssize_t) (width-j); v++)
717 {
718 for (u=0; u < (ssize_t) (width-j); u++)
719 {
720 pixel+=(*k)*pixels[i];
721 gamma+=(*k);
722 k++;
723 pixels+=GetPixelChannels(image);
724 }
725 }
726 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
727 q[channel]=ClampToQuantum(gamma*pixel);
728 continue;
729 }
730 /*
731 Alpha blending.
732 */
733 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000734 {
cristy4c11c2b2011-09-05 20:17:07 +0000735 for (u=0; u < (ssize_t) (width-j); u++)
736 {
737 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
738 pixel+=(*k)*alpha*pixels[i];
739 gamma+=(*k)*alpha;
740 k++;
741 pixels+=GetPixelChannels(image);
742 }
cristy3ed852e2009-09-05 21:47:34 +0000743 }
cristy4c11c2b2011-09-05 20:17:07 +0000744 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
745 q[channel]=ClampToQuantum(gamma*pixel);
cristy3ed852e2009-09-05 21:47:34 +0000746 }
cristyed231572011-07-14 02:18:59 +0000747 q+=GetPixelChannels(sharp_image);
748 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000749 }
750 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
751 status=MagickFalse;
752 if (image->progress_monitor != (MagickProgressMonitor) NULL)
753 {
754 MagickBooleanType
755 proceed;
756
cristyb5d5f722009-11-04 03:03:49 +0000757#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +0000758 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000759#endif
760 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
761 image->rows);
762 if (proceed == MagickFalse)
763 status=MagickFalse;
764 }
765 }
766 sharp_image->type=image->type;
767 sharp_view=DestroyCacheView(sharp_view);
768 edge_view=DestroyCacheView(edge_view);
769 image_view=DestroyCacheView(image_view);
770 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000771 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000772 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
773 kernel=(double **) RelinquishMagickMemory(kernel);
774 if (status == MagickFalse)
775 sharp_image=DestroyImage(sharp_image);
776 return(sharp_image);
777}
778
779/*
780%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
781% %
782% %
783% %
784% B l u r I m a g e %
785% %
786% %
787% %
788%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
789%
790% BlurImage() blurs an image. We convolve the image with a Gaussian operator
791% of the given radius and standard deviation (sigma). For reasonable results,
792% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
793% selects a suitable radius for you.
794%
795% BlurImage() differs from GaussianBlurImage() in that it uses a separable
796% kernel which is faster but mathematically equivalent to the non-separable
797% kernel.
798%
799% The format of the BlurImage method is:
800%
801% Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000802% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000803%
804% A description of each parameter follows:
805%
806% o image: the image.
807%
cristy3ed852e2009-09-05 21:47:34 +0000808% o radius: the radius of the Gaussian, in pixels, not counting the center
809% pixel.
810%
811% o sigma: the standard deviation of the Gaussian, in pixels.
812%
cristy05c0c9a2011-09-05 23:16:13 +0000813% o bias: the bias.
814%
cristy3ed852e2009-09-05 21:47:34 +0000815% o exception: return any errors or warnings in this structure.
816%
817*/
818
cristybb503372010-05-27 20:51:26 +0000819static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000820{
cristy3ed852e2009-09-05 21:47:34 +0000821 double
cristy47e00502009-12-17 19:19:57 +0000822 *kernel,
823 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000824
cristy117ff172010-08-15 21:35:32 +0000825 register ssize_t
826 i;
827
cristybb503372010-05-27 20:51:26 +0000828 ssize_t
cristy47e00502009-12-17 19:19:57 +0000829 j,
830 k;
cristy3ed852e2009-09-05 21:47:34 +0000831
cristy3ed852e2009-09-05 21:47:34 +0000832 /*
833 Generate a 1-D convolution kernel.
834 */
835 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
836 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
837 if (kernel == (double *) NULL)
838 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000839 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000840 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000841 i=0;
842 for (k=(-j); k <= j; k++)
843 {
cristy4205a3c2010-09-12 20:19:59 +0000844 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
845 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000846 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000847 i++;
848 }
cristybb503372010-05-27 20:51:26 +0000849 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000850 kernel[i]/=normalize;
851 return(kernel);
852}
853
cristyf4ad9df2011-07-08 16:49:03 +0000854MagickExport Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000855 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000856{
857#define BlurImageTag "Blur/Image"
858
cristyc4c8d132010-01-07 01:58:38 +0000859 CacheView
860 *blur_view,
861 *image_view;
862
cristy3ed852e2009-09-05 21:47:34 +0000863 double
864 *kernel;
865
866 Image
867 *blur_image;
868
cristy3ed852e2009-09-05 21:47:34 +0000869 MagickBooleanType
870 status;
871
cristybb503372010-05-27 20:51:26 +0000872 MagickOffsetType
873 progress;
874
cristybb503372010-05-27 20:51:26 +0000875 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000876 i;
877
cristybb503372010-05-27 20:51:26 +0000878 size_t
cristy3ed852e2009-09-05 21:47:34 +0000879 width;
880
cristybb503372010-05-27 20:51:26 +0000881 ssize_t
882 x,
883 y;
884
cristy3ed852e2009-09-05 21:47:34 +0000885 /*
886 Initialize blur image attributes.
887 */
888 assert(image != (Image *) NULL);
889 assert(image->signature == MagickSignature);
890 if (image->debug != MagickFalse)
891 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
892 assert(exception != (ExceptionInfo *) NULL);
893 assert(exception->signature == MagickSignature);
cristyd25c77e2011-09-06 00:10:24 +0000894 blur_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000895 if (blur_image == (Image *) NULL)
896 return((Image *) NULL);
897 if (fabs(sigma) <= MagickEpsilon)
898 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000899 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000900 {
cristy3ed852e2009-09-05 21:47:34 +0000901 blur_image=DestroyImage(blur_image);
902 return((Image *) NULL);
903 }
904 width=GetOptimalKernelWidth1D(radius,sigma);
905 kernel=GetBlurKernel(width,sigma);
906 if (kernel == (double *) NULL)
907 {
908 blur_image=DestroyImage(blur_image);
909 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
910 }
911 if (image->debug != MagickFalse)
912 {
913 char
914 format[MaxTextExtent],
915 *message;
916
917 register const double
918 *k;
919
920 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000921 " BlurImage with %.20g kernel:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000922 message=AcquireString("");
923 k=kernel;
cristybb503372010-05-27 20:51:26 +0000924 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000925 {
926 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000927 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000928 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000929 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000930 (void) ConcatenateString(&message,format);
931 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
932 }
933 message=DestroyString(message);
934 }
935 /*
936 Blur rows.
937 */
938 status=MagickTrue;
939 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000940 image_view=AcquireCacheView(image);
941 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000942#if defined(MAGICKCORE_OPENMP_SUPPORT)
943 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000944#endif
cristyd25c77e2011-09-06 00:10:24 +0000945 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000946 {
cristy4c08aed2011-07-01 19:47:50 +0000947 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000948 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000949
cristy4c08aed2011-07-01 19:47:50 +0000950 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000951 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000952
cristy117ff172010-08-15 21:35:32 +0000953 register ssize_t
954 x;
955
cristy3ed852e2009-09-05 21:47:34 +0000956 if (status == MagickFalse)
957 continue;
cristy117ff172010-08-15 21:35:32 +0000958 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
959 image->columns+width,1,exception);
cristy3ed852e2009-09-05 21:47:34 +0000960 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
961 exception);
cristy4c08aed2011-07-01 19:47:50 +0000962 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000963 {
964 status=MagickFalse;
965 continue;
966 }
cristyd25c77e2011-09-06 00:10:24 +0000967 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000968 {
cristyd25c77e2011-09-06 00:10:24 +0000969 PixelInfo
970 pixel;
971
972 register const double
973 *restrict k;
974
975 register const Quantum
976 *restrict kernel_pixels;
977
cristybb503372010-05-27 20:51:26 +0000978 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000979 i;
980
cristyd25c77e2011-09-06 00:10:24 +0000981 pixel.red=bias;
982 pixel.green=bias;
983 pixel.blue=bias;
984 pixel.black=bias;
985 pixel.alpha=bias;
986 k=kernel;
987 kernel_pixels=p;
988 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
989 (image->matte == MagickFalse))
cristy05c0c9a2011-09-05 23:16:13 +0000990 {
cristyd25c77e2011-09-06 00:10:24 +0000991 for (i=0; i < (ssize_t) width; i++)
992 {
993 pixel.red+=(*k)*GetPixelRed(image,kernel_pixels);
994 pixel.green+=(*k)*GetPixelGreen(image,kernel_pixels);
995 pixel.blue+=(*k)*GetPixelBlue(image,kernel_pixels);
996 if (image->colorspace == CMYKColorspace)
997 pixel.black+=(*k)*GetPixelBlack(image,kernel_pixels);
998 k++;
999 kernel_pixels+=GetPixelChannels(image);
1000 }
1001 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1002 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
1003 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1004 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
1005 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1006 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
1007 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1008 (blur_image->colorspace == CMYKColorspace))
1009 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
1010 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1011 {
1012 k=kernel;
1013 kernel_pixels=p;
1014 for (i=0; i < (ssize_t) width; i++)
1015 {
1016 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
1017 k++;
1018 kernel_pixels+=GetPixelChannels(image);
1019 }
1020 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1021 }
cristy3ed852e2009-09-05 21:47:34 +00001022 }
cristyd25c77e2011-09-06 00:10:24 +00001023 else
1024 {
1025 MagickRealType
1026 alpha,
1027 gamma;
1028
1029 gamma=0.0;
1030 for (i=0; i < (ssize_t) width; i++)
1031 {
1032 alpha=(MagickRealType) (QuantumScale*
1033 GetPixelAlpha(image,kernel_pixels));
1034 pixel.red+=(*k)*alpha*GetPixelRed(image,kernel_pixels);
1035 pixel.green+=(*k)*alpha*GetPixelGreen(image,kernel_pixels);
1036 pixel.blue+=(*k)*alpha*GetPixelBlue(image,kernel_pixels);
1037 if (image->colorspace == CMYKColorspace)
1038 pixel.black+=(*k)*alpha*GetPixelBlack(image,kernel_pixels);
1039 gamma+=(*k)*alpha;
1040 k++;
1041 kernel_pixels+=GetPixelChannels(image);
1042 }
1043 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1044 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1045 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
1046 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1047 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
1048 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1049 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
1050 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1051 (blur_image->colorspace == CMYKColorspace))
1052 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
1053 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1054 {
1055 k=kernel;
1056 kernel_pixels=p;
1057 for (i=0; i < (ssize_t) width; i++)
1058 {
1059 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
1060 k++;
1061 kernel_pixels+=GetPixelChannels(image);
1062 }
1063 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1064 }
1065 }
cristyed231572011-07-14 02:18:59 +00001066 p+=GetPixelChannels(image);
1067 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001068 }
1069 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1070 status=MagickFalse;
1071 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1072 {
1073 MagickBooleanType
1074 proceed;
1075
cristyb5d5f722009-11-04 03:03:49 +00001076#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001077 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001078#endif
1079 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1080 blur_image->columns);
1081 if (proceed == MagickFalse)
1082 status=MagickFalse;
1083 }
1084 }
1085 blur_view=DestroyCacheView(blur_view);
1086 image_view=DestroyCacheView(image_view);
1087 /*
1088 Blur columns.
1089 */
1090 image_view=AcquireCacheView(blur_image);
1091 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001092#if defined(MAGICKCORE_OPENMP_SUPPORT)
1093 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001094#endif
cristyd25c77e2011-09-06 00:10:24 +00001095 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001096 {
cristy4c08aed2011-07-01 19:47:50 +00001097 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001098 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001099
cristy4c08aed2011-07-01 19:47:50 +00001100 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001101 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001102
cristy117ff172010-08-15 21:35:32 +00001103 register ssize_t
1104 y;
1105
cristy3ed852e2009-09-05 21:47:34 +00001106 if (status == MagickFalse)
1107 continue;
cristy117ff172010-08-15 21:35:32 +00001108 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1109 image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001110 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001111 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001112 {
1113 status=MagickFalse;
1114 continue;
1115 }
cristyd25c77e2011-09-06 00:10:24 +00001116 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001117 {
cristyd25c77e2011-09-06 00:10:24 +00001118 PixelInfo
1119 pixel;
1120
1121 register const double
1122 *restrict k;
1123
1124 register const Quantum
1125 *restrict kernel_pixels;
1126
cristybb503372010-05-27 20:51:26 +00001127 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001128 i;
1129
cristyd25c77e2011-09-06 00:10:24 +00001130 pixel.red=bias;
1131 pixel.green=bias;
1132 pixel.blue=bias;
1133 pixel.black=bias;
1134 pixel.alpha=bias;
1135 k=kernel;
1136 kernel_pixels=p;
1137 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
1138 (blur_image->matte == MagickFalse))
cristy05c0c9a2011-09-05 23:16:13 +00001139 {
cristyd25c77e2011-09-06 00:10:24 +00001140 for (i=0; i < (ssize_t) width; i++)
1141 {
1142 pixel.red+=(*k)*GetPixelRed(blur_image,kernel_pixels);
1143 pixel.green+=(*k)*GetPixelGreen(blur_image,kernel_pixels);
1144 pixel.blue+=(*k)*GetPixelBlue(blur_image,kernel_pixels);
1145 if (blur_image->colorspace == CMYKColorspace)
1146 pixel.black+=(*k)*GetPixelBlack(blur_image,kernel_pixels);
1147 k++;
1148 kernel_pixels+=GetPixelChannels(blur_image);
1149 }
1150 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1151 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
1152 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1153 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
1154 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1155 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
1156 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1157 (blur_image->colorspace == CMYKColorspace))
1158 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
1159 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1160 {
1161 k=kernel;
1162 kernel_pixels=p;
1163 for (i=0; i < (ssize_t) width; i++)
1164 {
1165 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
1166 k++;
1167 kernel_pixels+=GetPixelChannels(blur_image);
1168 }
1169 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1170 }
cristy3ed852e2009-09-05 21:47:34 +00001171 }
cristyd25c77e2011-09-06 00:10:24 +00001172 else
1173 {
1174 MagickRealType
1175 alpha,
1176 gamma;
1177
1178 gamma=0.0;
1179 for (i=0; i < (ssize_t) width; i++)
1180 {
1181 alpha=(MagickRealType) (QuantumScale*
1182 GetPixelAlpha(blur_image,kernel_pixels));
1183 pixel.red+=(*k)*alpha*GetPixelRed(blur_image,kernel_pixels);
1184 pixel.green+=(*k)*alpha*GetPixelGreen(blur_image,kernel_pixels);
1185 pixel.blue+=(*k)*alpha*GetPixelBlue(blur_image,kernel_pixels);
1186 if (blur_image->colorspace == CMYKColorspace)
1187 pixel.black+=(*k)*alpha*GetPixelBlack(blur_image,kernel_pixels);
1188 gamma+=(*k)*alpha;
1189 k++;
1190 kernel_pixels+=GetPixelChannels(blur_image);
1191 }
1192 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1193 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1194 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
1195 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1196 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
1197 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1198 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
1199 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1200 (blur_image->colorspace == CMYKColorspace))
1201 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
1202 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1203 {
1204 k=kernel;
1205 kernel_pixels=p;
1206 for (i=0; i < (ssize_t) width; i++)
1207 {
1208 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
1209 k++;
1210 kernel_pixels+=GetPixelChannels(blur_image);
1211 }
1212 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1213 }
1214 }
1215 p+=GetPixelChannels(blur_image);
cristyed231572011-07-14 02:18:59 +00001216 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001217 }
1218 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1219 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001220 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001221 {
1222 MagickBooleanType
1223 proceed;
1224
cristyb5d5f722009-11-04 03:03:49 +00001225#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001226 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001227#endif
cristy4c08aed2011-07-01 19:47:50 +00001228 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1229 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001230 if (proceed == MagickFalse)
1231 status=MagickFalse;
1232 }
1233 }
1234 blur_view=DestroyCacheView(blur_view);
1235 image_view=DestroyCacheView(image_view);
1236 kernel=(double *) RelinquishMagickMemory(kernel);
1237 if (status == MagickFalse)
1238 blur_image=DestroyImage(blur_image);
1239 blur_image->type=image->type;
1240 return(blur_image);
1241}
1242
1243/*
1244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245% %
1246% %
1247% %
cristyfccdab92009-11-30 16:43:57 +00001248% C o n v o l v e I m a g e %
1249% %
1250% %
1251% %
1252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253%
1254% ConvolveImage() applies a custom convolution kernel to the image.
1255%
1256% The format of the ConvolveImage method is:
1257%
cristy5e6be1e2011-07-16 01:23:39 +00001258% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1259% ExceptionInfo *exception)
1260%
cristyfccdab92009-11-30 16:43:57 +00001261% A description of each parameter follows:
1262%
1263% o image: the image.
1264%
cristy5e6be1e2011-07-16 01:23:39 +00001265% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001266%
1267% o exception: return any errors or warnings in this structure.
1268%
1269*/
cristy5e6be1e2011-07-16 01:23:39 +00001270MagickExport Image *ConvolveImage(const Image *image,
1271 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001272{
cristyfccdab92009-11-30 16:43:57 +00001273#define ConvolveImageTag "Convolve/Image"
1274
cristyc4c8d132010-01-07 01:58:38 +00001275 CacheView
1276 *convolve_view,
cristy105ba3c2011-07-18 02:28:38 +00001277 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00001278
cristyfccdab92009-11-30 16:43:57 +00001279 Image
1280 *convolve_image;
1281
cristyfccdab92009-11-30 16:43:57 +00001282 MagickBooleanType
1283 status;
1284
cristybb503372010-05-27 20:51:26 +00001285 MagickOffsetType
1286 progress;
1287
cristybb503372010-05-27 20:51:26 +00001288 ssize_t
cristy574cc262011-08-05 01:23:58 +00001289 center,
cristybb503372010-05-27 20:51:26 +00001290 y;
1291
cristyfccdab92009-11-30 16:43:57 +00001292 /*
1293 Initialize convolve image attributes.
1294 */
1295 assert(image != (Image *) NULL);
1296 assert(image->signature == MagickSignature);
1297 if (image->debug != MagickFalse)
1298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1299 assert(exception != (ExceptionInfo *) NULL);
1300 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001301 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001302 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001303 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1304 exception);
cristyfccdab92009-11-30 16:43:57 +00001305 if (convolve_image == (Image *) NULL)
1306 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001307 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001308 {
cristyfccdab92009-11-30 16:43:57 +00001309 convolve_image=DestroyImage(convolve_image);
1310 return((Image *) NULL);
1311 }
1312 if (image->debug != MagickFalse)
1313 {
1314 char
1315 format[MaxTextExtent],
1316 *message;
1317
cristy117ff172010-08-15 21:35:32 +00001318 register const double
1319 *k;
1320
cristy4e154852011-07-14 13:28:53 +00001321 register ssize_t
1322 u;
1323
cristybb503372010-05-27 20:51:26 +00001324 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001325 v;
1326
cristyfccdab92009-11-30 16:43:57 +00001327 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001328 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1329 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001330 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001331 k=kernel_info->values;
1332 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001333 {
1334 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001335 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001336 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001337 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001338 {
cristyb51dff52011-05-19 16:55:47 +00001339 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001340 (void) ConcatenateString(&message,format);
1341 }
1342 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1343 }
1344 message=DestroyString(message);
1345 }
1346 /*
cristyfccdab92009-11-30 16:43:57 +00001347 Convolve image.
1348 */
cristy574cc262011-08-05 01:23:58 +00001349 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
1350 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2);
cristyfccdab92009-11-30 16:43:57 +00001351 status=MagickTrue;
1352 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001353 image_view=AcquireCacheView(image);
1354 convolve_view=AcquireCacheView(convolve_image);
1355#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy175653e2011-07-10 23:13:34 +00001356 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001357#endif
cristybb503372010-05-27 20:51:26 +00001358 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001359 {
cristy4c08aed2011-07-01 19:47:50 +00001360 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001361 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001362
cristy4c08aed2011-07-01 19:47:50 +00001363 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001364 *restrict q;
1365
cristy117ff172010-08-15 21:35:32 +00001366 register ssize_t
1367 x;
1368
cristyfccdab92009-11-30 16:43:57 +00001369 if (status == MagickFalse)
1370 continue;
cristy105ba3c2011-07-18 02:28:38 +00001371 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1372 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1373 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001374 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001375 exception);
cristy105ba3c2011-07-18 02:28:38 +00001376 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001377 {
1378 status=MagickFalse;
1379 continue;
1380 }
cristybb503372010-05-27 20:51:26 +00001381 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001382 {
cristybb503372010-05-27 20:51:26 +00001383 register ssize_t
cristyed231572011-07-14 02:18:59 +00001384 i;
cristyfccdab92009-11-30 16:43:57 +00001385
cristya30d9ba2011-07-23 21:00:48 +00001386 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001387 {
cristyed231572011-07-14 02:18:59 +00001388 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001389 alpha,
1390 gamma,
cristyed231572011-07-14 02:18:59 +00001391 pixel;
1392
1393 PixelChannel
1394 channel;
1395
1396 PixelTrait
1397 convolve_traits,
1398 traits;
1399
1400 register const double
1401 *restrict k;
1402
1403 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001404 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001405
1406 register ssize_t
1407 u;
1408
1409 ssize_t
1410 v;
1411
cristy30301712011-07-18 15:06:51 +00001412 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristy30301712011-07-18 15:06:51 +00001413 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
cristy4e154852011-07-14 13:28:53 +00001414 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001415 if ((traits == UndefinedPixelTrait) ||
1416 (convolve_traits == UndefinedPixelTrait))
cristy4e154852011-07-14 13:28:53 +00001417 continue;
1418 if ((convolve_traits & CopyPixelTrait) != 0)
1419 {
cristyf7dc44c2011-07-20 14:41:15 +00001420 q[channel]=p[center+i];
cristy4e154852011-07-14 13:28:53 +00001421 continue;
1422 }
cristy5e6be1e2011-07-16 01:23:39 +00001423 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001424 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001425 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001426 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001427 {
cristyed231572011-07-14 02:18:59 +00001428 /*
cristy4e154852011-07-14 13:28:53 +00001429 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001430 */
cristyeb52cde2011-07-17 01:52:52 +00001431 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001432 {
cristyeb52cde2011-07-17 01:52:52 +00001433 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001434 {
cristyeb52cde2011-07-17 01:52:52 +00001435 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001436 k++;
cristya30d9ba2011-07-23 21:00:48 +00001437 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001438 }
cristya30d9ba2011-07-23 21:00:48 +00001439 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001440 }
cristyf7dc44c2011-07-20 14:41:15 +00001441 q[channel]=ClampToQuantum(pixel);
cristy4e154852011-07-14 13:28:53 +00001442 continue;
cristyed231572011-07-14 02:18:59 +00001443 }
cristy4e154852011-07-14 13:28:53 +00001444 /*
1445 Alpha blending.
1446 */
1447 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001448 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001449 {
cristyeb52cde2011-07-17 01:52:52 +00001450 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001451 {
cristyeb52cde2011-07-17 01:52:52 +00001452 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1453 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001454 gamma+=(*k)*alpha;
1455 k++;
cristya30d9ba2011-07-23 21:00:48 +00001456 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001457 }
cristya30d9ba2011-07-23 21:00:48 +00001458 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001459 }
cristy1ce96d02011-07-14 17:57:24 +00001460 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristye7a41c92011-07-20 21:31:01 +00001461 q[channel]=ClampToQuantum(gamma*pixel);
cristyed231572011-07-14 02:18:59 +00001462 }
cristya30d9ba2011-07-23 21:00:48 +00001463 p+=GetPixelChannels(image);
1464 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001465 }
cristyed231572011-07-14 02:18:59 +00001466 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001467 status=MagickFalse;
1468 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1469 {
1470 MagickBooleanType
1471 proceed;
1472
1473#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001474 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001475#endif
1476 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1477 if (proceed == MagickFalse)
1478 status=MagickFalse;
1479 }
1480 }
1481 convolve_image->type=image->type;
1482 convolve_view=DestroyCacheView(convolve_view);
1483 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001484 if (status == MagickFalse)
1485 convolve_image=DestroyImage(convolve_image);
1486 return(convolve_image);
1487}
1488
1489/*
1490%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1491% %
1492% %
1493% %
cristy3ed852e2009-09-05 21:47:34 +00001494% D e s p e c k l e I m a g e %
1495% %
1496% %
1497% %
1498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1499%
1500% DespeckleImage() reduces the speckle noise in an image while perserving the
1501% edges of the original image.
1502%
1503% The format of the DespeckleImage method is:
1504%
1505% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1506%
1507% A description of each parameter follows:
1508%
1509% o image: the image.
1510%
1511% o exception: return any errors or warnings in this structure.
1512%
1513*/
1514
cristybb503372010-05-27 20:51:26 +00001515static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1516 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
cristy3ed852e2009-09-05 21:47:34 +00001517 const int polarity)
1518{
cristy3ed852e2009-09-05 21:47:34 +00001519 MagickRealType
1520 v;
1521
cristy3ed852e2009-09-05 21:47:34 +00001522 register Quantum
1523 *p,
1524 *q,
1525 *r,
1526 *s;
1527
cristy117ff172010-08-15 21:35:32 +00001528 register ssize_t
1529 x;
1530
1531 ssize_t
1532 y;
1533
cristy3ed852e2009-09-05 21:47:34 +00001534 assert(f != (Quantum *) NULL);
1535 assert(g != (Quantum *) NULL);
1536 p=f+(columns+2);
1537 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001538 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1539 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001540 {
1541 p++;
1542 q++;
1543 r++;
1544 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001545 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001546 {
1547 v=(MagickRealType) (*p);
1548 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1549 v+=ScaleCharToQuantum(1);
1550 *q=(Quantum) v;
1551 p++;
1552 q++;
1553 r++;
1554 }
1555 else
cristybb503372010-05-27 20:51:26 +00001556 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001557 {
1558 v=(MagickRealType) (*p);
1559 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
cristybb503372010-05-27 20:51:26 +00001560 v-=(ssize_t) ScaleCharToQuantum(1);
cristy3ed852e2009-09-05 21:47:34 +00001561 *q=(Quantum) v;
1562 p++;
1563 q++;
1564 r++;
1565 }
1566 p++;
1567 q++;
1568 r++;
1569 }
1570 p=f+(columns+2);
1571 q=g+(columns+2);
cristybb503372010-05-27 20:51:26 +00001572 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1573 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1574 for (y=0; y < (ssize_t) rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001575 {
1576 p++;
1577 q++;
1578 r++;
1579 s++;
1580 if (polarity > 0)
cristybb503372010-05-27 20:51:26 +00001581 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001582 {
1583 v=(MagickRealType) (*q);
1584 if (((MagickRealType) *s >=
1585 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1586 ((MagickRealType) *r > v))
1587 v+=ScaleCharToQuantum(1);
1588 *p=(Quantum) v;
1589 p++;
1590 q++;
1591 r++;
1592 s++;
1593 }
1594 else
cristybb503372010-05-27 20:51:26 +00001595 for (x=(ssize_t) columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001596 {
1597 v=(MagickRealType) (*q);
1598 if (((MagickRealType) *s <=
1599 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1600 ((MagickRealType) *r < v))
1601 v-=(MagickRealType) ScaleCharToQuantum(1);
1602 *p=(Quantum) v;
1603 p++;
1604 q++;
1605 r++;
1606 s++;
1607 }
1608 p++;
1609 q++;
1610 r++;
1611 s++;
1612 }
1613}
1614
1615MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1616{
1617#define DespeckleImageTag "Despeckle/Image"
1618
cristy2407fc22009-09-11 00:55:25 +00001619 CacheView
1620 *despeckle_view,
1621 *image_view;
1622
cristy3ed852e2009-09-05 21:47:34 +00001623 Image
1624 *despeckle_image;
1625
cristy3ed852e2009-09-05 21:47:34 +00001626 MagickBooleanType
1627 status;
1628
cristya58c3172011-02-19 19:23:11 +00001629 register ssize_t
1630 i;
1631
cristy3ed852e2009-09-05 21:47:34 +00001632 Quantum
cristy65b9f392011-02-22 14:22:54 +00001633 *restrict buffers,
1634 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +00001635
1636 size_t
cristya58c3172011-02-19 19:23:11 +00001637 length,
1638 number_channels;
cristy117ff172010-08-15 21:35:32 +00001639
cristybb503372010-05-27 20:51:26 +00001640 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001641 X[4] = {0, 1, 1,-1},
1642 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001643
cristy3ed852e2009-09-05 21:47:34 +00001644 /*
1645 Allocate despeckled image.
1646 */
1647 assert(image != (const Image *) NULL);
1648 assert(image->signature == MagickSignature);
1649 if (image->debug != MagickFalse)
1650 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1651 assert(exception != (ExceptionInfo *) NULL);
1652 assert(exception->signature == MagickSignature);
1653 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1654 exception);
1655 if (despeckle_image == (Image *) NULL)
1656 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001657 if (SetImageStorageClass(despeckle_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001658 {
cristy3ed852e2009-09-05 21:47:34 +00001659 despeckle_image=DestroyImage(despeckle_image);
1660 return((Image *) NULL);
1661 }
1662 /*
1663 Allocate image buffers.
1664 */
1665 length=(size_t) ((image->columns+2)*(image->rows+2));
cristy65b9f392011-02-22 14:22:54 +00001666 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1667 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1668 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001669 {
cristy65b9f392011-02-22 14:22:54 +00001670 if (buffers != (Quantum *) NULL)
1671 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1672 if (pixels != (Quantum *) NULL)
1673 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001674 despeckle_image=DestroyImage(despeckle_image);
1675 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1676 }
1677 /*
1678 Reduce speckle in the image.
1679 */
1680 status=MagickTrue;
cristy109695a2011-02-19 19:38:14 +00001681 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
cristy3ed852e2009-09-05 21:47:34 +00001682 image_view=AcquireCacheView(image);
1683 despeckle_view=AcquireCacheView(despeckle_image);
cristy8df3d002011-02-19 19:40:59 +00001684 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001685 {
cristy3ed852e2009-09-05 21:47:34 +00001686 register Quantum
1687 *buffer,
1688 *pixel;
1689
cristyc1488b52011-02-19 18:54:15 +00001690 register ssize_t
cristya58c3172011-02-19 19:23:11 +00001691 k,
cristyc1488b52011-02-19 18:54:15 +00001692 x;
1693
cristy117ff172010-08-15 21:35:32 +00001694 ssize_t
1695 j,
1696 y;
1697
cristy3ed852e2009-09-05 21:47:34 +00001698 if (status == MagickFalse)
1699 continue;
cristy65b9f392011-02-22 14:22:54 +00001700 pixel=pixels;
cristy3ed852e2009-09-05 21:47:34 +00001701 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
cristy65b9f392011-02-22 14:22:54 +00001702 buffer=buffers;
cristybb503372010-05-27 20:51:26 +00001703 j=(ssize_t) image->columns+2;
1704 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001705 {
cristy4c08aed2011-07-01 19:47:50 +00001706 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001707 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001708
1709 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001710 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001711 break;
1712 j++;
cristybb503372010-05-27 20:51:26 +00001713 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001714 {
cristya58c3172011-02-19 19:23:11 +00001715 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001716 {
cristy4c08aed2011-07-01 19:47:50 +00001717 case 0: pixel[j]=GetPixelRed(image,p); break;
1718 case 1: pixel[j]=GetPixelGreen(image,p); break;
1719 case 2: pixel[j]=GetPixelBlue(image,p); break;
1720 case 3: pixel[j]=GetPixelAlpha(image,p); break;
1721 case 4: pixel[j]=GetPixelBlack(image,p); break;
cristy3ed852e2009-09-05 21:47:34 +00001722 default: break;
1723 }
cristyed231572011-07-14 02:18:59 +00001724 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001725 j++;
1726 }
1727 j++;
1728 }
cristy3ed852e2009-09-05 21:47:34 +00001729 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
cristya58c3172011-02-19 19:23:11 +00001730 for (k=0; k < 4; k++)
cristy3ed852e2009-09-05 21:47:34 +00001731 {
cristya58c3172011-02-19 19:23:11 +00001732 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1733 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1734 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1735 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
cristy3ed852e2009-09-05 21:47:34 +00001736 }
cristybb503372010-05-27 20:51:26 +00001737 j=(ssize_t) image->columns+2;
1738 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001739 {
1740 MagickBooleanType
1741 sync;
1742
cristy4c08aed2011-07-01 19:47:50 +00001743 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001744 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001745
1746 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1747 1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001748 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001749 break;
1750 j++;
cristybb503372010-05-27 20:51:26 +00001751 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001752 {
cristya58c3172011-02-19 19:23:11 +00001753 switch (i)
cristy3ed852e2009-09-05 21:47:34 +00001754 {
cristy4c08aed2011-07-01 19:47:50 +00001755 case 0: SetPixelRed(despeckle_image,pixel[j],q); break;
1756 case 1: SetPixelGreen(despeckle_image,pixel[j],q); break;
1757 case 2: SetPixelBlue(despeckle_image,pixel[j],q); break;
1758 case 3: SetPixelAlpha(despeckle_image,pixel[j],q); break;
1759 case 4: SetPixelBlack(despeckle_image,pixel[j],q); break;
cristy3ed852e2009-09-05 21:47:34 +00001760 default: break;
1761 }
cristyed231572011-07-14 02:18:59 +00001762 q+=GetPixelChannels(despeckle_image);
cristy3ed852e2009-09-05 21:47:34 +00001763 j++;
1764 }
1765 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1766 if (sync == MagickFalse)
1767 {
1768 status=MagickFalse;
1769 break;
1770 }
1771 j++;
1772 }
1773 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1774 {
1775 MagickBooleanType
1776 proceed;
1777
cristya58c3172011-02-19 19:23:11 +00001778 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1779 number_channels);
cristy3ed852e2009-09-05 21:47:34 +00001780 if (proceed == MagickFalse)
1781 status=MagickFalse;
1782 }
1783 }
1784 despeckle_view=DestroyCacheView(despeckle_view);
1785 image_view=DestroyCacheView(image_view);
cristy65b9f392011-02-22 14:22:54 +00001786 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1787 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001788 despeckle_image->type=image->type;
1789 if (status == MagickFalse)
1790 despeckle_image=DestroyImage(despeckle_image);
1791 return(despeckle_image);
1792}
1793
1794/*
1795%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1796% %
1797% %
1798% %
1799% E d g e I m a g e %
1800% %
1801% %
1802% %
1803%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1804%
1805% EdgeImage() finds edges in an image. Radius defines the radius of the
1806% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1807% radius for you.
1808%
1809% The format of the EdgeImage method is:
1810%
1811% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001812% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001813%
1814% A description of each parameter follows:
1815%
1816% o image: the image.
1817%
1818% o radius: the radius of the pixel neighborhood.
1819%
cristy8ae632d2011-09-05 17:29:53 +00001820% o sigma: the standard deviation of the Gaussian, in pixels.
1821%
cristy3ed852e2009-09-05 21:47:34 +00001822% o exception: return any errors or warnings in this structure.
1823%
1824*/
1825MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001826 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001827{
1828 Image
1829 *edge_image;
1830
cristy41cbe682011-07-15 19:12:37 +00001831 KernelInfo
1832 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001833
cristybb503372010-05-27 20:51:26 +00001834 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001835 i;
1836
cristybb503372010-05-27 20:51:26 +00001837 size_t
cristy3ed852e2009-09-05 21:47:34 +00001838 width;
1839
cristy41cbe682011-07-15 19:12:37 +00001840 ssize_t
1841 j,
1842 u,
1843 v;
1844
cristy3ed852e2009-09-05 21:47:34 +00001845 assert(image != (const Image *) NULL);
1846 assert(image->signature == MagickSignature);
1847 if (image->debug != MagickFalse)
1848 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1849 assert(exception != (ExceptionInfo *) NULL);
1850 assert(exception->signature == MagickSignature);
cristy8ae632d2011-09-05 17:29:53 +00001851 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001852 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001853 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001854 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001855 kernel_info->width=width;
1856 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001857 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1858 kernel_info->width*sizeof(*kernel_info->values));
1859 if (kernel_info->values == (double *) NULL)
1860 {
1861 kernel_info=DestroyKernelInfo(kernel_info);
1862 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1863 }
1864 j=(ssize_t) kernel_info->width/2;
1865 i=0;
1866 for (v=(-j); v <= j; v++)
1867 {
1868 for (u=(-j); u <= j; u++)
1869 {
1870 kernel_info->values[i]=(-1.0);
1871 i++;
1872 }
1873 }
1874 kernel_info->values[i/2]=(double) (width*width-1.0);
cristy0a922382011-07-16 15:30:34 +00001875 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001876 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001877 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001878 return(edge_image);
1879}
1880
1881/*
1882%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1883% %
1884% %
1885% %
1886% E m b o s s I m a g e %
1887% %
1888% %
1889% %
1890%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1891%
1892% EmbossImage() returns a grayscale image with a three-dimensional effect.
1893% We convolve the image with a Gaussian operator of the given radius and
1894% standard deviation (sigma). For reasonable results, radius should be
1895% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1896% radius for you.
1897%
1898% The format of the EmbossImage method is:
1899%
1900% Image *EmbossImage(const Image *image,const double radius,
1901% const double sigma,ExceptionInfo *exception)
1902%
1903% A description of each parameter follows:
1904%
1905% o image: the image.
1906%
1907% o radius: the radius of the pixel neighborhood.
1908%
1909% o sigma: the standard deviation of the Gaussian, in pixels.
1910%
1911% o exception: return any errors or warnings in this structure.
1912%
1913*/
1914MagickExport Image *EmbossImage(const Image *image,const double radius,
1915 const double sigma,ExceptionInfo *exception)
1916{
cristy3ed852e2009-09-05 21:47:34 +00001917 Image
1918 *emboss_image;
1919
cristy41cbe682011-07-15 19:12:37 +00001920 KernelInfo
1921 *kernel_info;
1922
cristybb503372010-05-27 20:51:26 +00001923 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001924 i;
1925
cristybb503372010-05-27 20:51:26 +00001926 size_t
cristy3ed852e2009-09-05 21:47:34 +00001927 width;
1928
cristy117ff172010-08-15 21:35:32 +00001929 ssize_t
1930 j,
1931 k,
1932 u,
1933 v;
1934
cristy41cbe682011-07-15 19:12:37 +00001935 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001936 assert(image->signature == MagickSignature);
1937 if (image->debug != MagickFalse)
1938 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1939 assert(exception != (ExceptionInfo *) NULL);
1940 assert(exception->signature == MagickSignature);
1941 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001942 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001943 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001944 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001945 kernel_info->width=width;
1946 kernel_info->height=width;
cristy41cbe682011-07-15 19:12:37 +00001947 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1948 kernel_info->width*sizeof(*kernel_info->values));
1949 if (kernel_info->values == (double *) NULL)
1950 {
1951 kernel_info=DestroyKernelInfo(kernel_info);
1952 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1953 }
1954 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001955 k=j;
1956 i=0;
1957 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001958 {
cristy47e00502009-12-17 19:19:57 +00001959 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001960 {
cristy41cbe682011-07-15 19:12:37 +00001961 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001962 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001963 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001964 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001965 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001966 i++;
1967 }
cristy47e00502009-12-17 19:19:57 +00001968 k--;
cristy3ed852e2009-09-05 21:47:34 +00001969 }
cristy0a922382011-07-16 15:30:34 +00001970 kernel_info->bias=image->bias;
cristy5e6be1e2011-07-16 01:23:39 +00001971 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001972 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001973 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001974 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001975 return(emboss_image);
1976}
1977
1978/*
1979%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1980% %
1981% %
1982% %
1983% G a u s s i a n B l u r I m a g e %
1984% %
1985% %
1986% %
1987%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1988%
1989% GaussianBlurImage() blurs an image. We convolve the image with a
1990% Gaussian operator of the given radius and standard deviation (sigma).
1991% For reasonable results, the radius should be larger than sigma. Use a
1992% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1993%
1994% The format of the GaussianBlurImage method is:
1995%
1996% Image *GaussianBlurImage(const Image *image,onst double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001997% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001998%
1999% A description of each parameter follows:
2000%
2001% o image: the image.
2002%
cristy3ed852e2009-09-05 21:47:34 +00002003% o radius: the radius of the Gaussian, in pixels, not counting the center
2004% pixel.
2005%
2006% o sigma: the standard deviation of the Gaussian, in pixels.
2007%
cristy05c0c9a2011-09-05 23:16:13 +00002008% o bias: the bias.
2009%
cristy3ed852e2009-09-05 21:47:34 +00002010% o exception: return any errors or warnings in this structure.
2011%
2012*/
cristy41cbe682011-07-15 19:12:37 +00002013MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00002014 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002015{
cristy3ed852e2009-09-05 21:47:34 +00002016 Image
2017 *blur_image;
2018
cristy41cbe682011-07-15 19:12:37 +00002019 KernelInfo
2020 *kernel_info;
2021
cristybb503372010-05-27 20:51:26 +00002022 register ssize_t
cristy47e00502009-12-17 19:19:57 +00002023 i;
2024
cristybb503372010-05-27 20:51:26 +00002025 size_t
cristy3ed852e2009-09-05 21:47:34 +00002026 width;
2027
cristy117ff172010-08-15 21:35:32 +00002028 ssize_t
2029 j,
2030 u,
2031 v;
2032
cristy3ed852e2009-09-05 21:47:34 +00002033 assert(image != (const Image *) NULL);
2034 assert(image->signature == MagickSignature);
2035 if (image->debug != MagickFalse)
2036 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2037 assert(exception != (ExceptionInfo *) NULL);
2038 assert(exception->signature == MagickSignature);
2039 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00002040 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00002041 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002042 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00002043 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
2044 kernel_info->width=width;
2045 kernel_info->height=width;
cristy05c0c9a2011-09-05 23:16:13 +00002046 kernel_info->bias=bias;
cristy41cbe682011-07-15 19:12:37 +00002047 kernel_info->signature=MagickSignature;
2048 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
2049 kernel_info->width*sizeof(*kernel_info->values));
2050 if (kernel_info->values == (double *) NULL)
2051 {
2052 kernel_info=DestroyKernelInfo(kernel_info);
2053 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2054 }
2055 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00002056 i=0;
cristy47e00502009-12-17 19:19:57 +00002057 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00002058 {
cristy47e00502009-12-17 19:19:57 +00002059 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00002060 {
2061 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
2062 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2063 i++;
2064 }
cristy3ed852e2009-09-05 21:47:34 +00002065 }
cristy5e6be1e2011-07-16 01:23:39 +00002066 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00002067 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00002068 return(blur_image);
2069}
2070
2071/*
2072%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2073% %
2074% %
2075% %
cristy3ed852e2009-09-05 21:47:34 +00002076% M o t i o n B l u r I m a g e %
2077% %
2078% %
2079% %
2080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2081%
2082% MotionBlurImage() simulates motion blur. We convolve the image with a
2083% Gaussian operator of the given radius and standard deviation (sigma).
2084% For reasonable results, radius should be larger than sigma. Use a
2085% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2086% Angle gives the angle of the blurring motion.
2087%
2088% Andrew Protano contributed this effect.
2089%
2090% The format of the MotionBlurImage method is:
2091%
2092% Image *MotionBlurImage(const Image *image,const double radius,
2093% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002094%
2095% A description of each parameter follows:
2096%
2097% o image: the image.
2098%
cristy3ed852e2009-09-05 21:47:34 +00002099% o radius: the radius of the Gaussian, in pixels, not counting
2100% the center pixel.
2101%
2102% o sigma: the standard deviation of the Gaussian, in pixels.
2103%
cristycee97112010-05-28 00:44:52 +00002104% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002105%
2106% o exception: return any errors or warnings in this structure.
2107%
2108*/
2109
cristybb503372010-05-27 20:51:26 +00002110static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002111{
cristy3ed852e2009-09-05 21:47:34 +00002112 double
cristy47e00502009-12-17 19:19:57 +00002113 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002114 normalize;
2115
cristybb503372010-05-27 20:51:26 +00002116 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002117 i;
2118
2119 /*
cristy47e00502009-12-17 19:19:57 +00002120 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002121 */
2122 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2123 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2124 if (kernel == (double *) NULL)
2125 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002126 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002127 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002128 {
cristy4205a3c2010-09-12 20:19:59 +00002129 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2130 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002131 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002132 }
cristybb503372010-05-27 20:51:26 +00002133 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002134 kernel[i]/=normalize;
2135 return(kernel);
2136}
2137
cristyf4ad9df2011-07-08 16:49:03 +00002138MagickExport Image *MotionBlurImage(const Image *image,
2139 const double radius,const double sigma,const double angle,
2140 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002141{
cristyc4c8d132010-01-07 01:58:38 +00002142 CacheView
2143 *blur_view,
2144 *image_view;
2145
cristy3ed852e2009-09-05 21:47:34 +00002146 double
2147 *kernel;
2148
2149 Image
2150 *blur_image;
2151
cristy3ed852e2009-09-05 21:47:34 +00002152 MagickBooleanType
2153 status;
2154
cristybb503372010-05-27 20:51:26 +00002155 MagickOffsetType
2156 progress;
2157
cristy4c08aed2011-07-01 19:47:50 +00002158 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002159 bias;
cristy3ed852e2009-09-05 21:47:34 +00002160
2161 OffsetInfo
2162 *offset;
2163
2164 PointInfo
2165 point;
2166
cristybb503372010-05-27 20:51:26 +00002167 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002168 i;
2169
cristybb503372010-05-27 20:51:26 +00002170 size_t
cristy3ed852e2009-09-05 21:47:34 +00002171 width;
2172
cristybb503372010-05-27 20:51:26 +00002173 ssize_t
2174 y;
2175
cristy3ed852e2009-09-05 21:47:34 +00002176 assert(image != (Image *) NULL);
2177 assert(image->signature == MagickSignature);
2178 if (image->debug != MagickFalse)
2179 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2180 assert(exception != (ExceptionInfo *) NULL);
2181 width=GetOptimalKernelWidth1D(radius,sigma);
2182 kernel=GetMotionBlurKernel(width,sigma);
2183 if (kernel == (double *) NULL)
2184 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2185 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2186 if (offset == (OffsetInfo *) NULL)
2187 {
2188 kernel=(double *) RelinquishMagickMemory(kernel);
2189 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2190 }
2191 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2192 if (blur_image == (Image *) NULL)
2193 {
2194 kernel=(double *) RelinquishMagickMemory(kernel);
2195 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2196 return((Image *) NULL);
2197 }
cristy574cc262011-08-05 01:23:58 +00002198 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002199 {
2200 kernel=(double *) RelinquishMagickMemory(kernel);
2201 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002202 blur_image=DestroyImage(blur_image);
2203 return((Image *) NULL);
2204 }
2205 point.x=(double) width*sin(DegreesToRadians(angle));
2206 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002207 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002208 {
cristybb503372010-05-27 20:51:26 +00002209 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2210 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002211 }
2212 /*
2213 Motion blur image.
2214 */
2215 status=MagickTrue;
2216 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002217 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002218 image_view=AcquireCacheView(image);
2219 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002220#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00002221 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00002222#endif
cristybb503372010-05-27 20:51:26 +00002223 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002224 {
cristy4c08aed2011-07-01 19:47:50 +00002225 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002226 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002227
cristy117ff172010-08-15 21:35:32 +00002228 register ssize_t
2229 x;
2230
cristy3ed852e2009-09-05 21:47:34 +00002231 if (status == MagickFalse)
2232 continue;
2233 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2234 exception);
cristyacd2ed22011-08-30 01:44:23 +00002235 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002236 {
2237 status=MagickFalse;
2238 continue;
2239 }
cristybb503372010-05-27 20:51:26 +00002240 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002241 {
cristy4c08aed2011-07-01 19:47:50 +00002242 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002243 qixel;
2244
2245 PixelPacket
2246 pixel;
2247
2248 register double
cristyc47d1f82009-11-26 01:44:43 +00002249 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00002250
cristybb503372010-05-27 20:51:26 +00002251 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002252 i;
2253
cristy3ed852e2009-09-05 21:47:34 +00002254 k=kernel;
cristyddd82202009-11-03 20:14:50 +00002255 qixel=bias;
cristyed231572011-07-14 02:18:59 +00002256 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002257 {
cristybb503372010-05-27 20:51:26 +00002258 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002259 {
2260 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2261 offset[i].y,&pixel,exception);
2262 qixel.red+=(*k)*pixel.red;
2263 qixel.green+=(*k)*pixel.green;
2264 qixel.blue+=(*k)*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002265 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002266 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002267 qixel.black+=(*k)*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002268 k++;
2269 }
cristyed231572011-07-14 02:18:59 +00002270 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002271 SetPixelRed(blur_image,
2272 ClampToQuantum(qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002273 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002274 SetPixelGreen(blur_image,
2275 ClampToQuantum(qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002276 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002277 SetPixelBlue(blur_image,
2278 ClampToQuantum(qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002279 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002280 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002281 SetPixelBlack(blur_image,
2282 ClampToQuantum(qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002283 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002284 SetPixelAlpha(blur_image,
2285 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002286 }
2287 else
2288 {
2289 MagickRealType
2290 alpha,
2291 gamma;
2292
2293 alpha=0.0;
2294 gamma=0.0;
cristybb503372010-05-27 20:51:26 +00002295 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002296 {
2297 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2298 offset[i].y,&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00002299 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00002300 qixel.red+=(*k)*alpha*pixel.red;
2301 qixel.green+=(*k)*alpha*pixel.green;
2302 qixel.blue+=(*k)*alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00002303 qixel.alpha+=(*k)*pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002304 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002305 qixel.black+=(*k)*alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00002306 gamma+=(*k)*alpha;
2307 k++;
2308 }
2309 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00002310 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002311 SetPixelRed(blur_image,
2312 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00002313 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002314 SetPixelGreen(blur_image,
2315 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00002316 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002317 SetPixelBlue(blur_image,
2318 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002319 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002320 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00002321 SetPixelBlack(blur_image,
2322 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00002323 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002324 SetPixelAlpha(blur_image,
2325 ClampToQuantum(qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00002326 }
cristyed231572011-07-14 02:18:59 +00002327 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002328 }
2329 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2330 status=MagickFalse;
2331 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2332 {
2333 MagickBooleanType
2334 proceed;
2335
cristyb557a152011-02-22 12:14:30 +00002336#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002337 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002338#endif
2339 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2340 if (proceed == MagickFalse)
2341 status=MagickFalse;
2342 }
2343 }
2344 blur_view=DestroyCacheView(blur_view);
2345 image_view=DestroyCacheView(image_view);
2346 kernel=(double *) RelinquishMagickMemory(kernel);
2347 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2348 if (status == MagickFalse)
2349 blur_image=DestroyImage(blur_image);
2350 return(blur_image);
2351}
2352
2353/*
2354%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2355% %
2356% %
2357% %
2358% P r e v i e w I m a g e %
2359% %
2360% %
2361% %
2362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2363%
2364% PreviewImage() tiles 9 thumbnails of the specified image with an image
2365% processing operation applied with varying parameters. This may be helpful
2366% pin-pointing an appropriate parameter for a particular image processing
2367% operation.
2368%
2369% The format of the PreviewImages method is:
2370%
2371% Image *PreviewImages(const Image *image,const PreviewType preview,
2372% ExceptionInfo *exception)
2373%
2374% A description of each parameter follows:
2375%
2376% o image: the image.
2377%
2378% o preview: the image processing operation.
2379%
2380% o exception: return any errors or warnings in this structure.
2381%
2382*/
2383MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2384 ExceptionInfo *exception)
2385{
2386#define NumberTiles 9
2387#define PreviewImageTag "Preview/Image"
2388#define DefaultPreviewGeometry "204x204+10+10"
2389
2390 char
2391 factor[MaxTextExtent],
2392 label[MaxTextExtent];
2393
2394 double
2395 degrees,
2396 gamma,
2397 percentage,
2398 radius,
2399 sigma,
2400 threshold;
2401
2402 Image
2403 *images,
2404 *montage_image,
2405 *preview_image,
2406 *thumbnail;
2407
2408 ImageInfo
2409 *preview_info;
2410
cristy3ed852e2009-09-05 21:47:34 +00002411 MagickBooleanType
2412 proceed;
2413
2414 MontageInfo
2415 *montage_info;
2416
2417 QuantizeInfo
2418 quantize_info;
2419
2420 RectangleInfo
2421 geometry;
2422
cristybb503372010-05-27 20:51:26 +00002423 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002424 i,
2425 x;
2426
cristybb503372010-05-27 20:51:26 +00002427 size_t
cristy3ed852e2009-09-05 21:47:34 +00002428 colors;
2429
cristy117ff172010-08-15 21:35:32 +00002430 ssize_t
2431 y;
2432
cristy3ed852e2009-09-05 21:47:34 +00002433 /*
2434 Open output image file.
2435 */
2436 assert(image != (Image *) NULL);
2437 assert(image->signature == MagickSignature);
2438 if (image->debug != MagickFalse)
2439 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2440 colors=2;
2441 degrees=0.0;
2442 gamma=(-0.2f);
2443 preview_info=AcquireImageInfo();
2444 SetGeometry(image,&geometry);
2445 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2446 &geometry.width,&geometry.height);
2447 images=NewImageList();
2448 percentage=12.5;
2449 GetQuantizeInfo(&quantize_info);
2450 radius=0.0;
2451 sigma=1.0;
2452 threshold=0.0;
2453 x=0;
2454 y=0;
2455 for (i=0; i < NumberTiles; i++)
2456 {
2457 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2458 if (thumbnail == (Image *) NULL)
2459 break;
2460 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2461 (void *) NULL);
2462 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
2463 if (i == (NumberTiles/2))
2464 {
2465 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
2466 AppendImageToList(&images,thumbnail);
2467 continue;
2468 }
2469 switch (preview)
2470 {
2471 case RotatePreview:
2472 {
2473 degrees+=45.0;
2474 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002475 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002476 break;
2477 }
2478 case ShearPreview:
2479 {
2480 degrees+=5.0;
2481 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002482 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002483 degrees,2.0*degrees);
2484 break;
2485 }
2486 case RollPreview:
2487 {
cristybb503372010-05-27 20:51:26 +00002488 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2489 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002490 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002491 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002492 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002493 break;
2494 }
2495 case HuePreview:
2496 {
2497 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2498 if (preview_image == (Image *) NULL)
2499 break;
cristyb51dff52011-05-19 16:55:47 +00002500 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002501 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002502 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002503 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002504 break;
2505 }
2506 case SaturationPreview:
2507 {
2508 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2509 if (preview_image == (Image *) NULL)
2510 break;
cristyb51dff52011-05-19 16:55:47 +00002511 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002512 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002513 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002514 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002515 break;
2516 }
2517 case BrightnessPreview:
2518 {
2519 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2520 if (preview_image == (Image *) NULL)
2521 break;
cristyb51dff52011-05-19 16:55:47 +00002522 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002523 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002524 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002525 break;
2526 }
2527 case GammaPreview:
2528 default:
2529 {
2530 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2531 if (preview_image == (Image *) NULL)
2532 break;
2533 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002534 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002535 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002536 break;
2537 }
2538 case SpiffPreview:
2539 {
2540 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2541 if (preview_image != (Image *) NULL)
2542 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002543 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002544 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002545 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002546 break;
2547 }
2548 case DullPreview:
2549 {
2550 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2551 if (preview_image == (Image *) NULL)
2552 break;
2553 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002554 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002555 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002556 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002557 break;
2558 }
2559 case GrayscalePreview:
2560 {
2561 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2562 if (preview_image == (Image *) NULL)
2563 break;
2564 colors<<=1;
2565 quantize_info.number_colors=colors;
2566 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002567 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002568 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002569 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002570 break;
2571 }
2572 case QuantizePreview:
2573 {
2574 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2575 if (preview_image == (Image *) NULL)
2576 break;
2577 colors<<=1;
2578 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002579 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002580 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002581 colors);
cristy3ed852e2009-09-05 21:47:34 +00002582 break;
2583 }
2584 case DespecklePreview:
2585 {
2586 for (x=0; x < (i-1); x++)
2587 {
2588 preview_image=DespeckleImage(thumbnail,exception);
2589 if (preview_image == (Image *) NULL)
2590 break;
2591 thumbnail=DestroyImage(thumbnail);
2592 thumbnail=preview_image;
2593 }
2594 preview_image=DespeckleImage(thumbnail,exception);
2595 if (preview_image == (Image *) NULL)
2596 break;
cristyb51dff52011-05-19 16:55:47 +00002597 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002598 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002599 break;
2600 }
2601 case ReduceNoisePreview:
2602 {
cristy95c38342011-03-18 22:39:51 +00002603 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2604 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002605 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002606 break;
2607 }
2608 case AddNoisePreview:
2609 {
2610 switch ((int) i)
2611 {
2612 case 0:
2613 {
2614 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2615 break;
2616 }
2617 case 1:
2618 {
2619 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2620 break;
2621 }
2622 case 2:
2623 {
2624 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2625 break;
2626 }
2627 case 3:
2628 {
2629 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2630 break;
2631 }
2632 case 4:
2633 {
2634 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2635 break;
2636 }
2637 case 5:
2638 {
2639 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2640 break;
2641 }
2642 default:
2643 {
2644 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2645 break;
2646 }
2647 }
cristyd76c51e2011-03-26 00:21:26 +00002648 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2649 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002650 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002651 break;
2652 }
2653 case SharpenPreview:
2654 {
cristy05c0c9a2011-09-05 23:16:13 +00002655 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2656 exception);
cristyb51dff52011-05-19 16:55:47 +00002657 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002658 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002659 break;
2660 }
2661 case BlurPreview:
2662 {
cristy05c0c9a2011-09-05 23:16:13 +00002663 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002664 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002665 sigma);
2666 break;
2667 }
2668 case ThresholdPreview:
2669 {
2670 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2671 if (preview_image == (Image *) NULL)
2672 break;
2673 (void) BilevelImage(thumbnail,
2674 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
cristyb51dff52011-05-19 16:55:47 +00002675 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002676 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2677 break;
2678 }
2679 case EdgeDetectPreview:
2680 {
cristy8ae632d2011-09-05 17:29:53 +00002681 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002682 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002683 break;
2684 }
2685 case SpreadPreview:
2686 {
2687 preview_image=SpreadImage(thumbnail,radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002688 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002689 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002690 break;
2691 }
2692 case SolarizePreview:
2693 {
2694 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2695 if (preview_image == (Image *) NULL)
2696 break;
2697 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002698 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002699 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002700 (QuantumRange*percentage)/100.0);
2701 break;
2702 }
2703 case ShadePreview:
2704 {
2705 degrees+=10.0;
2706 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2707 exception);
cristyb51dff52011-05-19 16:55:47 +00002708 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002709 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002710 break;
2711 }
2712 case RaisePreview:
2713 {
2714 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2715 if (preview_image == (Image *) NULL)
2716 break;
cristybb503372010-05-27 20:51:26 +00002717 geometry.width=(size_t) (2*i+2);
2718 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002719 geometry.x=i/2;
2720 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002721 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002722 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002723 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002724 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002725 break;
2726 }
2727 case SegmentPreview:
2728 {
2729 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2730 if (preview_image == (Image *) NULL)
2731 break;
2732 threshold+=0.4f;
2733 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002734 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002735 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002736 threshold,threshold);
2737 break;
2738 }
2739 case SwirlPreview:
2740 {
2741 preview_image=SwirlImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002742 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002743 degrees+=45.0;
2744 break;
2745 }
2746 case ImplodePreview:
2747 {
2748 degrees+=0.1f;
2749 preview_image=ImplodeImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002750 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002751 break;
2752 }
2753 case WavePreview:
2754 {
2755 degrees+=5.0f;
2756 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002757 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002758 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002759 break;
2760 }
2761 case OilPaintPreview:
2762 {
cristy14973ba2011-08-27 23:48:07 +00002763 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2764 exception);
2765 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2766 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002767 break;
2768 }
2769 case CharcoalDrawingPreview:
2770 {
2771 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristy05c0c9a2011-09-05 23:16:13 +00002772 image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002773 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002774 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002775 break;
2776 }
2777 case JPEGPreview:
2778 {
2779 char
2780 filename[MaxTextExtent];
2781
2782 int
2783 file;
2784
2785 MagickBooleanType
2786 status;
2787
2788 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2789 if (preview_image == (Image *) NULL)
2790 break;
cristybb503372010-05-27 20:51:26 +00002791 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002792 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002793 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002794 file=AcquireUniqueFileResource(filename);
2795 if (file != -1)
2796 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002797 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002798 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002799 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002800 if (status != MagickFalse)
2801 {
2802 Image
2803 *quality_image;
2804
2805 (void) CopyMagickString(preview_info->filename,
2806 preview_image->filename,MaxTextExtent);
2807 quality_image=ReadImage(preview_info,exception);
2808 if (quality_image != (Image *) NULL)
2809 {
2810 preview_image=DestroyImage(preview_image);
2811 preview_image=quality_image;
2812 }
2813 }
2814 (void) RelinquishUniqueFileResource(preview_image->filename);
2815 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002816 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002817 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2818 1024.0/1024.0);
2819 else
2820 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002821 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002822 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002823 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002824 else
cristyb51dff52011-05-19 16:55:47 +00002825 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002826 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002827 break;
2828 }
2829 }
2830 thumbnail=DestroyImage(thumbnail);
2831 percentage+=12.5;
2832 radius+=0.5;
2833 sigma+=0.25;
2834 if (preview_image == (Image *) NULL)
2835 break;
2836 (void) DeleteImageProperty(preview_image,"label");
2837 (void) SetImageProperty(preview_image,"label",label);
2838 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002839 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2840 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002841 if (proceed == MagickFalse)
2842 break;
2843 }
2844 if (images == (Image *) NULL)
2845 {
2846 preview_info=DestroyImageInfo(preview_info);
2847 return((Image *) NULL);
2848 }
2849 /*
2850 Create the montage.
2851 */
2852 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2853 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2854 montage_info->shadow=MagickTrue;
2855 (void) CloneString(&montage_info->tile,"3x3");
2856 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2857 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2858 montage_image=MontageImages(images,montage_info,exception);
2859 montage_info=DestroyMontageInfo(montage_info);
2860 images=DestroyImageList(images);
2861 if (montage_image == (Image *) NULL)
2862 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2863 if (montage_image->montage != (char *) NULL)
2864 {
2865 /*
2866 Free image directory.
2867 */
2868 montage_image->montage=(char *) RelinquishMagickMemory(
2869 montage_image->montage);
2870 if (image->directory != (char *) NULL)
2871 montage_image->directory=(char *) RelinquishMagickMemory(
2872 montage_image->directory);
2873 }
2874 preview_info=DestroyImageInfo(preview_info);
2875 return(montage_image);
2876}
2877
2878/*
2879%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2880% %
2881% %
2882% %
2883% R a d i a l B l u r I m a g e %
2884% %
2885% %
2886% %
2887%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2888%
2889% RadialBlurImage() applies a radial blur to the image.
2890%
2891% Andrew Protano contributed this effect.
2892%
2893% The format of the RadialBlurImage method is:
2894%
2895% Image *RadialBlurImage(const Image *image,const double angle,
2896% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002897%
2898% A description of each parameter follows:
2899%
2900% o image: the image.
2901%
cristy3ed852e2009-09-05 21:47:34 +00002902% o angle: the angle of the radial blur.
2903%
2904% o exception: return any errors or warnings in this structure.
2905%
2906*/
cristyf4ad9df2011-07-08 16:49:03 +00002907MagickExport Image *RadialBlurImage(const Image *image,
2908 const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002909{
cristyc4c8d132010-01-07 01:58:38 +00002910 CacheView
2911 *blur_view,
2912 *image_view;
2913
cristy3ed852e2009-09-05 21:47:34 +00002914 Image
2915 *blur_image;
2916
cristy3ed852e2009-09-05 21:47:34 +00002917 MagickBooleanType
2918 status;
2919
cristybb503372010-05-27 20:51:26 +00002920 MagickOffsetType
2921 progress;
2922
cristy4c08aed2011-07-01 19:47:50 +00002923 PixelInfo
cristyddd82202009-11-03 20:14:50 +00002924 bias;
cristy3ed852e2009-09-05 21:47:34 +00002925
2926 MagickRealType
2927 blur_radius,
2928 *cos_theta,
2929 offset,
2930 *sin_theta,
2931 theta;
2932
2933 PointInfo
2934 blur_center;
2935
cristybb503372010-05-27 20:51:26 +00002936 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002937 i;
2938
cristybb503372010-05-27 20:51:26 +00002939 size_t
cristy3ed852e2009-09-05 21:47:34 +00002940 n;
2941
cristybb503372010-05-27 20:51:26 +00002942 ssize_t
2943 y;
2944
cristy3ed852e2009-09-05 21:47:34 +00002945 /*
2946 Allocate blur image.
2947 */
2948 assert(image != (Image *) NULL);
2949 assert(image->signature == MagickSignature);
2950 if (image->debug != MagickFalse)
2951 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2952 assert(exception != (ExceptionInfo *) NULL);
2953 assert(exception->signature == MagickSignature);
2954 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2955 if (blur_image == (Image *) NULL)
2956 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002957 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002958 {
cristy3ed852e2009-09-05 21:47:34 +00002959 blur_image=DestroyImage(blur_image);
2960 return((Image *) NULL);
2961 }
2962 blur_center.x=(double) image->columns/2.0;
2963 blur_center.y=(double) image->rows/2.0;
2964 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002965 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002966 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2967 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2968 sizeof(*cos_theta));
2969 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2970 sizeof(*sin_theta));
2971 if ((cos_theta == (MagickRealType *) NULL) ||
2972 (sin_theta == (MagickRealType *) NULL))
2973 {
2974 blur_image=DestroyImage(blur_image);
2975 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2976 }
2977 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002978 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002979 {
2980 cos_theta[i]=cos((double) (theta*i-offset));
2981 sin_theta[i]=sin((double) (theta*i-offset));
2982 }
2983 /*
2984 Radial blur image.
2985 */
2986 status=MagickTrue;
2987 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00002988 GetPixelInfo(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00002989 image_view=AcquireCacheView(image);
2990 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002991#if defined(MAGICKCORE_OPENMP_SUPPORT)
2992 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002993#endif
cristybb503372010-05-27 20:51:26 +00002994 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002995 {
cristy4c08aed2011-07-01 19:47:50 +00002996 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002997 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002998
cristy117ff172010-08-15 21:35:32 +00002999 register ssize_t
3000 x;
3001
cristy3ed852e2009-09-05 21:47:34 +00003002 if (status == MagickFalse)
3003 continue;
3004 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3005 exception);
cristyacd2ed22011-08-30 01:44:23 +00003006 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003007 {
3008 status=MagickFalse;
3009 continue;
3010 }
cristybb503372010-05-27 20:51:26 +00003011 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003012 {
cristy4c08aed2011-07-01 19:47:50 +00003013 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003014 qixel;
3015
3016 MagickRealType
3017 normalize,
3018 radius;
3019
3020 PixelPacket
3021 pixel;
3022
3023 PointInfo
3024 center;
3025
cristybb503372010-05-27 20:51:26 +00003026 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003027 i;
3028
cristybb503372010-05-27 20:51:26 +00003029 size_t
cristy3ed852e2009-09-05 21:47:34 +00003030 step;
3031
3032 center.x=(double) x-blur_center.x;
3033 center.y=(double) y-blur_center.y;
3034 radius=hypot((double) center.x,center.y);
3035 if (radius == 0)
3036 step=1;
3037 else
3038 {
cristybb503372010-05-27 20:51:26 +00003039 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00003040 if (step == 0)
3041 step=1;
3042 else
3043 if (step >= n)
3044 step=n-1;
3045 }
3046 normalize=0.0;
cristyddd82202009-11-03 20:14:50 +00003047 qixel=bias;
cristyed231572011-07-14 02:18:59 +00003048 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003049 {
cristyeaedf062010-05-29 22:36:02 +00003050 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00003051 {
cristyeaedf062010-05-29 22:36:02 +00003052 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3053 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3054 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3055 cos_theta[i]+0.5),&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003056 qixel.red+=pixel.red;
3057 qixel.green+=pixel.green;
3058 qixel.blue+=pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00003059 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00003060 qixel.black+=pixel.black;
3061 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00003062 normalize+=1.0;
3063 }
3064 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3065 normalize);
cristyed231572011-07-14 02:18:59 +00003066 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003067 SetPixelRed(blur_image,
3068 ClampToQuantum(normalize*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00003069 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003070 SetPixelGreen(blur_image,
3071 ClampToQuantum(normalize*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00003072 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003073 SetPixelBlue(blur_image,
3074 ClampToQuantum(normalize*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003075 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003076 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003077 SetPixelBlack(blur_image,
3078 ClampToQuantum(normalize*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003079 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003080 SetPixelAlpha(blur_image,
3081 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003082 }
3083 else
3084 {
3085 MagickRealType
3086 alpha,
3087 gamma;
3088
3089 alpha=1.0;
3090 gamma=0.0;
cristyeaedf062010-05-29 22:36:02 +00003091 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +00003092 {
cristyeaedf062010-05-29 22:36:02 +00003093 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3094 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3095 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3096 cos_theta[i]+0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003097 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00003098 qixel.red+=alpha*pixel.red;
3099 qixel.green+=alpha*pixel.green;
3100 qixel.blue+=alpha*pixel.blue;
cristy4c08aed2011-07-01 19:47:50 +00003101 qixel.alpha+=pixel.alpha;
cristy3ed852e2009-09-05 21:47:34 +00003102 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00003103 qixel.black+=alpha*pixel.black;
cristy3ed852e2009-09-05 21:47:34 +00003104 gamma+=alpha;
3105 normalize+=1.0;
3106 }
3107 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3108 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3109 normalize);
cristyed231572011-07-14 02:18:59 +00003110 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003111 SetPixelRed(blur_image,
3112 ClampToQuantum(gamma*qixel.red),q);
cristyed231572011-07-14 02:18:59 +00003113 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003114 SetPixelGreen(blur_image,
3115 ClampToQuantum(gamma*qixel.green),q);
cristyed231572011-07-14 02:18:59 +00003116 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003117 SetPixelBlue(blur_image,
3118 ClampToQuantum(gamma*qixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003119 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00003120 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +00003121 SetPixelBlack(blur_image,
3122 ClampToQuantum(gamma*qixel.black),q);
cristyed231572011-07-14 02:18:59 +00003123 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003124 SetPixelAlpha(blur_image,
3125 ClampToQuantum(normalize*qixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003126 }
cristyed231572011-07-14 02:18:59 +00003127 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003128 }
3129 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3130 status=MagickFalse;
3131 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3132 {
3133 MagickBooleanType
3134 proceed;
3135
cristyb5d5f722009-11-04 03:03:49 +00003136#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003137 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003138#endif
3139 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3140 if (proceed == MagickFalse)
3141 status=MagickFalse;
3142 }
3143 }
3144 blur_view=DestroyCacheView(blur_view);
3145 image_view=DestroyCacheView(image_view);
3146 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3147 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3148 if (status == MagickFalse)
3149 blur_image=DestroyImage(blur_image);
3150 return(blur_image);
3151}
3152
3153/*
3154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3155% %
3156% %
3157% %
cristy3ed852e2009-09-05 21:47:34 +00003158% S e l e c t i v e B l u r I m a g e %
3159% %
3160% %
3161% %
3162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3163%
3164% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3165% It is similar to the unsharpen mask that sharpens everything with contrast
3166% above a certain threshold.
3167%
3168% The format of the SelectiveBlurImage method is:
3169%
3170% Image *SelectiveBlurImage(const Image *image,const double radius,
3171% const double sigma,const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003172%
3173% A description of each parameter follows:
3174%
3175% o image: the image.
3176%
cristy3ed852e2009-09-05 21:47:34 +00003177% o radius: the radius of the Gaussian, in pixels, not counting the center
3178% pixel.
3179%
3180% o sigma: the standard deviation of the Gaussian, in pixels.
3181%
3182% o threshold: only pixels within this contrast threshold are included
3183% in the blur operation.
3184%
3185% o exception: return any errors or warnings in this structure.
3186%
3187*/
cristyf4ad9df2011-07-08 16:49:03 +00003188MagickExport Image *SelectiveBlurImage(const Image *image,
3189 const double radius,const double sigma,const double threshold,
3190 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003191{
3192#define SelectiveBlurImageTag "SelectiveBlur/Image"
3193
cristy47e00502009-12-17 19:19:57 +00003194 CacheView
3195 *blur_view,
3196 *image_view;
3197
cristy3ed852e2009-09-05 21:47:34 +00003198 double
cristy3ed852e2009-09-05 21:47:34 +00003199 *kernel;
3200
3201 Image
3202 *blur_image;
3203
cristy3ed852e2009-09-05 21:47:34 +00003204 MagickBooleanType
3205 status;
3206
cristybb503372010-05-27 20:51:26 +00003207 MagickOffsetType
3208 progress;
3209
cristy4c08aed2011-07-01 19:47:50 +00003210 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003211 bias;
3212
cristybb503372010-05-27 20:51:26 +00003213 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003214 i;
cristy3ed852e2009-09-05 21:47:34 +00003215
cristybb503372010-05-27 20:51:26 +00003216 size_t
cristy3ed852e2009-09-05 21:47:34 +00003217 width;
3218
cristybb503372010-05-27 20:51:26 +00003219 ssize_t
3220 j,
3221 u,
3222 v,
3223 y;
3224
cristy3ed852e2009-09-05 21:47:34 +00003225 /*
3226 Initialize blur image attributes.
3227 */
3228 assert(image != (Image *) NULL);
3229 assert(image->signature == MagickSignature);
3230 if (image->debug != MagickFalse)
3231 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3232 assert(exception != (ExceptionInfo *) NULL);
3233 assert(exception->signature == MagickSignature);
3234 width=GetOptimalKernelWidth1D(radius,sigma);
3235 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3236 if (kernel == (double *) NULL)
3237 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003238 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003239 i=0;
cristy47e00502009-12-17 19:19:57 +00003240 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003241 {
cristy47e00502009-12-17 19:19:57 +00003242 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003243 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3244 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003245 }
3246 if (image->debug != MagickFalse)
3247 {
3248 char
3249 format[MaxTextExtent],
3250 *message;
3251
cristy117ff172010-08-15 21:35:32 +00003252 register const double
3253 *k;
3254
cristybb503372010-05-27 20:51:26 +00003255 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003256 u,
3257 v;
3258
cristy3ed852e2009-09-05 21:47:34 +00003259 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003260 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3261 width);
cristy3ed852e2009-09-05 21:47:34 +00003262 message=AcquireString("");
3263 k=kernel;
cristybb503372010-05-27 20:51:26 +00003264 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003265 {
3266 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003267 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003268 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003269 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003270 {
cristyb51dff52011-05-19 16:55:47 +00003271 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003272 (void) ConcatenateString(&message,format);
3273 }
3274 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3275 }
3276 message=DestroyString(message);
3277 }
3278 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3279 if (blur_image == (Image *) NULL)
3280 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003281 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003282 {
cristy3ed852e2009-09-05 21:47:34 +00003283 blur_image=DestroyImage(blur_image);
3284 return((Image *) NULL);
3285 }
3286 /*
3287 Threshold blur image.
3288 */
3289 status=MagickTrue;
3290 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003291 GetPixelInfo(image,&bias);
3292 SetPixelInfoBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003293 image_view=AcquireCacheView(image);
3294 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003295#if defined(MAGICKCORE_OPENMP_SUPPORT)
3296 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003297#endif
cristybb503372010-05-27 20:51:26 +00003298 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003299 {
cristy4c08aed2011-07-01 19:47:50 +00003300 double
3301 contrast;
3302
cristy3ed852e2009-09-05 21:47:34 +00003303 MagickBooleanType
3304 sync;
3305
3306 MagickRealType
3307 gamma;
3308
cristy4c08aed2011-07-01 19:47:50 +00003309 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003310 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003311
cristy4c08aed2011-07-01 19:47:50 +00003312 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003313 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003314
cristy117ff172010-08-15 21:35:32 +00003315 register ssize_t
3316 x;
3317
cristy3ed852e2009-09-05 21:47:34 +00003318 if (status == MagickFalse)
3319 continue;
cristy117ff172010-08-15 21:35:32 +00003320 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3321 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00003322 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3323 exception);
cristy4c08aed2011-07-01 19:47:50 +00003324 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003325 {
3326 status=MagickFalse;
3327 continue;
3328 }
cristybb503372010-05-27 20:51:26 +00003329 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003330 {
cristy4c08aed2011-07-01 19:47:50 +00003331 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003332 pixel;
3333
3334 register const double
cristyc47d1f82009-11-26 01:44:43 +00003335 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00003336
cristybb503372010-05-27 20:51:26 +00003337 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003338 u;
3339
cristy117ff172010-08-15 21:35:32 +00003340 ssize_t
3341 j,
3342 v;
3343
cristyddd82202009-11-03 20:14:50 +00003344 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00003345 k=kernel;
3346 gamma=0.0;
3347 j=0;
cristyed231572011-07-14 02:18:59 +00003348 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003349 {
cristybb503372010-05-27 20:51:26 +00003350 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003351 {
cristybb503372010-05-27 20:51:26 +00003352 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003353 {
cristyed231572011-07-14 02:18:59 +00003354 contrast=GetPixelIntensity(image,p+(u+j)*GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003355 (double) GetPixelIntensity(blur_image,q);
3356 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003357 {
cristy4c08aed2011-07-01 19:47:50 +00003358 pixel.red+=(*k)*
cristyed231572011-07-14 02:18:59 +00003359 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003360 pixel.green+=(*k)*
cristyed231572011-07-14 02:18:59 +00003361 GetPixelGreen(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003362 pixel.blue+=(*k)*
cristyed231572011-07-14 02:18:59 +00003363 GetPixelBlue(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003364 if (image->colorspace == CMYKColorspace)
3365 pixel.black+=(*k)*
cristyed231572011-07-14 02:18:59 +00003366 GetPixelBlack(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003367 gamma+=(*k);
3368 k++;
3369 }
3370 }
cristyd99b0962010-05-29 23:14:26 +00003371 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003372 }
3373 if (gamma != 0.0)
3374 {
3375 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003376 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003377 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003378 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003379 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003380 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003381 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003382 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003383 (image->colorspace == CMYKColorspace))
3384 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003385 }
cristyed231572011-07-14 02:18:59 +00003386 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003387 {
3388 gamma=0.0;
3389 j=0;
cristybb503372010-05-27 20:51:26 +00003390 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003391 {
cristybb503372010-05-27 20:51:26 +00003392 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003393 {
cristy4c08aed2011-07-01 19:47:50 +00003394 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003395 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003396 GetPixelIntensity(blur_image,q);
3397 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003398 {
cristy4c08aed2011-07-01 19:47:50 +00003399 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003400 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003401 gamma+=(*k);
3402 k++;
3403 }
3404 }
cristyeaedf062010-05-29 22:36:02 +00003405 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003406 }
3407 if (gamma != 0.0)
3408 {
3409 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3410 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003411 SetPixelAlpha(blur_image,ClampToQuantum(gamma*pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003412 }
3413 }
3414 }
3415 else
3416 {
3417 MagickRealType
3418 alpha;
3419
cristybb503372010-05-27 20:51:26 +00003420 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003421 {
cristybb503372010-05-27 20:51:26 +00003422 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003423 {
cristy4c08aed2011-07-01 19:47:50 +00003424 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003425 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003426 GetPixelIntensity(blur_image,q);
3427 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003428 {
cristy4c08aed2011-07-01 19:47:50 +00003429 alpha=(MagickRealType) (QuantumScale*
cristyed231572011-07-14 02:18:59 +00003430 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image)));
cristy4c08aed2011-07-01 19:47:50 +00003431 pixel.red+=(*k)*alpha*
cristyed231572011-07-14 02:18:59 +00003432 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003433 pixel.green+=(*k)*alpha*GetPixelGreen(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003434 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003435 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003436 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003437 pixel.alpha+=(*k)*GetPixelAlpha(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003438 GetPixelChannels(image));
cristy4c08aed2011-07-01 19:47:50 +00003439 if (image->colorspace == CMYKColorspace)
3440 pixel.black+=(*k)*GetPixelBlack(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003441 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003442 gamma+=(*k)*alpha;
3443 k++;
3444 }
3445 }
cristyeaedf062010-05-29 22:36:02 +00003446 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003447 }
3448 if (gamma != 0.0)
3449 {
3450 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristyed231572011-07-14 02:18:59 +00003451 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003452 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
cristyed231572011-07-14 02:18:59 +00003453 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003454 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
cristyed231572011-07-14 02:18:59 +00003455 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00003456 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00003457 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00003458 (image->colorspace == CMYKColorspace))
3459 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
cristy3ed852e2009-09-05 21:47:34 +00003460 }
cristyed231572011-07-14 02:18:59 +00003461 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003462 {
3463 gamma=0.0;
3464 j=0;
cristybb503372010-05-27 20:51:26 +00003465 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003466 {
cristybb503372010-05-27 20:51:26 +00003467 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003468 {
cristy4c08aed2011-07-01 19:47:50 +00003469 contrast=GetPixelIntensity(image,p+(u+j)*
cristyed231572011-07-14 02:18:59 +00003470 GetPixelChannels(image))-(double)
cristy4c08aed2011-07-01 19:47:50 +00003471 GetPixelIntensity(blur_image,q);
3472 if (fabs(contrast) < threshold)
cristy3ed852e2009-09-05 21:47:34 +00003473 {
cristy4c08aed2011-07-01 19:47:50 +00003474 pixel.alpha+=(*k)*
cristyed231572011-07-14 02:18:59 +00003475 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00003476 gamma+=(*k);
3477 k++;
3478 }
3479 }
cristyeaedf062010-05-29 22:36:02 +00003480 j+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +00003481 }
3482 if (gamma != 0.0)
3483 {
3484 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3485 gamma);
cristy4c08aed2011-07-01 19:47:50 +00003486 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00003487 }
3488 }
3489 }
cristyed231572011-07-14 02:18:59 +00003490 p+=GetPixelChannels(image);
3491 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003492 }
3493 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3494 if (sync == MagickFalse)
3495 status=MagickFalse;
3496 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3497 {
3498 MagickBooleanType
3499 proceed;
3500
cristyb5d5f722009-11-04 03:03:49 +00003501#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003502 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003503#endif
3504 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3505 image->rows);
3506 if (proceed == MagickFalse)
3507 status=MagickFalse;
3508 }
3509 }
3510 blur_image->type=image->type;
3511 blur_view=DestroyCacheView(blur_view);
3512 image_view=DestroyCacheView(image_view);
3513 kernel=(double *) RelinquishMagickMemory(kernel);
3514 if (status == MagickFalse)
3515 blur_image=DestroyImage(blur_image);
3516 return(blur_image);
3517}
3518
3519/*
3520%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3521% %
3522% %
3523% %
3524% S h a d e I m a g e %
3525% %
3526% %
3527% %
3528%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3529%
3530% ShadeImage() shines a distant light on an image to create a
3531% three-dimensional effect. You control the positioning of the light with
3532% azimuth and elevation; azimuth is measured in degrees off the x axis
3533% and elevation is measured in pixels above the Z axis.
3534%
3535% The format of the ShadeImage method is:
3536%
3537% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3538% const double azimuth,const double elevation,ExceptionInfo *exception)
3539%
3540% A description of each parameter follows:
3541%
3542% o image: the image.
3543%
3544% o gray: A value other than zero shades the intensity of each pixel.
3545%
3546% o azimuth, elevation: Define the light source direction.
3547%
3548% o exception: return any errors or warnings in this structure.
3549%
3550*/
3551MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3552 const double azimuth,const double elevation,ExceptionInfo *exception)
3553{
3554#define ShadeImageTag "Shade/Image"
3555
cristyc4c8d132010-01-07 01:58:38 +00003556 CacheView
3557 *image_view,
3558 *shade_view;
3559
cristy3ed852e2009-09-05 21:47:34 +00003560 Image
3561 *shade_image;
3562
cristy3ed852e2009-09-05 21:47:34 +00003563 MagickBooleanType
3564 status;
3565
cristybb503372010-05-27 20:51:26 +00003566 MagickOffsetType
3567 progress;
3568
cristy3ed852e2009-09-05 21:47:34 +00003569 PrimaryInfo
3570 light;
3571
cristybb503372010-05-27 20:51:26 +00003572 ssize_t
3573 y;
3574
cristy3ed852e2009-09-05 21:47:34 +00003575 /*
3576 Initialize shaded image attributes.
3577 */
3578 assert(image != (const Image *) NULL);
3579 assert(image->signature == MagickSignature);
3580 if (image->debug != MagickFalse)
3581 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3582 assert(exception != (ExceptionInfo *) NULL);
3583 assert(exception->signature == MagickSignature);
3584 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3585 if (shade_image == (Image *) NULL)
3586 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003587 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003588 {
cristy3ed852e2009-09-05 21:47:34 +00003589 shade_image=DestroyImage(shade_image);
3590 return((Image *) NULL);
3591 }
3592 /*
3593 Compute the light vector.
3594 */
3595 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3596 cos(DegreesToRadians(elevation));
3597 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3598 cos(DegreesToRadians(elevation));
3599 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3600 /*
3601 Shade image.
3602 */
3603 status=MagickTrue;
3604 progress=0;
3605 image_view=AcquireCacheView(image);
3606 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003607#if defined(MAGICKCORE_OPENMP_SUPPORT)
3608 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003609#endif
cristybb503372010-05-27 20:51:26 +00003610 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003611 {
3612 MagickRealType
3613 distance,
3614 normal_distance,
3615 shade;
3616
3617 PrimaryInfo
3618 normal;
3619
cristy4c08aed2011-07-01 19:47:50 +00003620 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003621 *restrict p,
3622 *restrict s0,
3623 *restrict s1,
3624 *restrict s2;
cristy3ed852e2009-09-05 21:47:34 +00003625
cristy4c08aed2011-07-01 19:47:50 +00003626 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003627 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003628
cristy117ff172010-08-15 21:35:32 +00003629 register ssize_t
3630 x;
3631
cristy3ed852e2009-09-05 21:47:34 +00003632 if (status == MagickFalse)
3633 continue;
3634 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3635 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3636 exception);
cristy4c08aed2011-07-01 19:47:50 +00003637 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003638 {
3639 status=MagickFalse;
3640 continue;
3641 }
3642 /*
3643 Shade this row of pixels.
3644 */
3645 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristyed231572011-07-14 02:18:59 +00003646 s0=p+GetPixelChannels(image);
3647 s1=s0+(image->columns+2)*GetPixelChannels(image);
3648 s2=s1+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003649 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003650 {
3651 /*
3652 Determine the surface normal and compute shading.
3653 */
cristyed231572011-07-14 02:18:59 +00003654 normal.x=(double) (GetPixelIntensity(image,s0-GetPixelChannels(image))+
3655 GetPixelIntensity(image,s1-GetPixelChannels(image))+
3656 GetPixelIntensity(image,s2-GetPixelChannels(image))-
3657 GetPixelIntensity(image,s0+GetPixelChannels(image))-
3658 GetPixelIntensity(image,s1+GetPixelChannels(image))-
3659 GetPixelIntensity(image,s2+GetPixelChannels(image)));
3660 normal.y=(double) (GetPixelIntensity(image,s2-GetPixelChannels(image))+
cristy4c08aed2011-07-01 19:47:50 +00003661 GetPixelIntensity(image,s2)+
cristyed231572011-07-14 02:18:59 +00003662 GetPixelIntensity(image,s2+GetPixelChannels(image))-
3663 GetPixelIntensity(image,s0-GetPixelChannels(image))-
cristy4c08aed2011-07-01 19:47:50 +00003664 GetPixelIntensity(image,s0)-
cristyed231572011-07-14 02:18:59 +00003665 GetPixelIntensity(image,s0+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003666 if ((normal.x == 0.0) && (normal.y == 0.0))
3667 shade=light.z;
3668 else
3669 {
3670 shade=0.0;
3671 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3672 if (distance > MagickEpsilon)
3673 {
3674 normal_distance=
3675 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3676 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3677 shade=distance/sqrt((double) normal_distance);
3678 }
3679 }
3680 if (gray != MagickFalse)
3681 {
cristy4c08aed2011-07-01 19:47:50 +00003682 SetPixelRed(shade_image,ClampToQuantum(shade),q);
3683 SetPixelGreen(shade_image,ClampToQuantum(shade),q);
3684 SetPixelBlue(shade_image,ClampToQuantum(shade),q);
cristy3ed852e2009-09-05 21:47:34 +00003685 }
3686 else
3687 {
cristy4c08aed2011-07-01 19:47:50 +00003688 SetPixelRed(shade_image,ClampToQuantum(QuantumScale*shade*
3689 GetPixelRed(image,s1)),q);
3690 SetPixelGreen(shade_image,ClampToQuantum(QuantumScale*shade*
3691 GetPixelGreen(image,s1)),q);
3692 SetPixelBlue(shade_image,ClampToQuantum(QuantumScale*shade*
3693 GetPixelBlue(image,s1)),q);
cristy3ed852e2009-09-05 21:47:34 +00003694 }
cristy4c08aed2011-07-01 19:47:50 +00003695 SetPixelAlpha(shade_image,GetPixelAlpha(image,s1),q);
cristyed231572011-07-14 02:18:59 +00003696 s0+=GetPixelChannels(image);
3697 s1+=GetPixelChannels(image);
3698 s2+=GetPixelChannels(image);
3699 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003700 }
3701 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3702 status=MagickFalse;
3703 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3704 {
3705 MagickBooleanType
3706 proceed;
3707
cristyb5d5f722009-11-04 03:03:49 +00003708#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003709 #pragma omp critical (MagickCore_ShadeImage)
3710#endif
3711 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3712 if (proceed == MagickFalse)
3713 status=MagickFalse;
3714 }
3715 }
3716 shade_view=DestroyCacheView(shade_view);
3717 image_view=DestroyCacheView(image_view);
3718 if (status == MagickFalse)
3719 shade_image=DestroyImage(shade_image);
3720 return(shade_image);
3721}
3722
3723/*
3724%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3725% %
3726% %
3727% %
3728% S h a r p e n I m a g e %
3729% %
3730% %
3731% %
3732%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3733%
3734% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3735% operator of the given radius and standard deviation (sigma). For
3736% reasonable results, radius should be larger than sigma. Use a radius of 0
3737% and SharpenImage() selects a suitable radius for you.
3738%
3739% Using a separable kernel would be faster, but the negative weights cancel
3740% out on the corners of the kernel producing often undesirable ringing in the
3741% filtered result; this can be avoided by using a 2D gaussian shaped image
3742% sharpening kernel instead.
3743%
3744% The format of the SharpenImage method is:
3745%
3746% Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003747% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003748%
3749% A description of each parameter follows:
3750%
3751% o image: the image.
3752%
cristy3ed852e2009-09-05 21:47:34 +00003753% o radius: the radius of the Gaussian, in pixels, not counting the center
3754% pixel.
3755%
3756% o sigma: the standard deviation of the Laplacian, in pixels.
3757%
cristy05c0c9a2011-09-05 23:16:13 +00003758% o bias: bias.
3759%
cristy3ed852e2009-09-05 21:47:34 +00003760% o exception: return any errors or warnings in this structure.
3761%
3762*/
cristy3ed852e2009-09-05 21:47:34 +00003763MagickExport Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003764 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003765{
cristy3ed852e2009-09-05 21:47:34 +00003766 double
cristy47e00502009-12-17 19:19:57 +00003767 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003768
3769 Image
3770 *sharp_image;
3771
cristy41cbe682011-07-15 19:12:37 +00003772 KernelInfo
3773 *kernel_info;
3774
cristybb503372010-05-27 20:51:26 +00003775 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003776 i;
3777
cristybb503372010-05-27 20:51:26 +00003778 size_t
cristy3ed852e2009-09-05 21:47:34 +00003779 width;
3780
cristy117ff172010-08-15 21:35:32 +00003781 ssize_t
3782 j,
3783 u,
3784 v;
3785
cristy3ed852e2009-09-05 21:47:34 +00003786 assert(image != (const Image *) NULL);
3787 assert(image->signature == MagickSignature);
3788 if (image->debug != MagickFalse)
3789 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3790 assert(exception != (ExceptionInfo *) NULL);
3791 assert(exception->signature == MagickSignature);
3792 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003793 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003794 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003795 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003796 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3797 kernel_info->width=width;
3798 kernel_info->height=width;
cristy05c0c9a2011-09-05 23:16:13 +00003799 kernel_info->bias=bias;
cristy41cbe682011-07-15 19:12:37 +00003800 kernel_info->signature=MagickSignature;
3801 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
3802 kernel_info->width*sizeof(*kernel_info->values));
3803 if (kernel_info->values == (double *) NULL)
3804 {
3805 kernel_info=DestroyKernelInfo(kernel_info);
3806 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3807 }
cristy3ed852e2009-09-05 21:47:34 +00003808 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003809 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003810 i=0;
3811 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003812 {
cristy47e00502009-12-17 19:19:57 +00003813 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003814 {
cristy41cbe682011-07-15 19:12:37 +00003815 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3816 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3817 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003818 i++;
3819 }
3820 }
cristy41cbe682011-07-15 19:12:37 +00003821 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003822 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003823 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003824 return(sharp_image);
3825}
3826
3827/*
3828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3829% %
3830% %
3831% %
3832% S p r e a d I m a g e %
3833% %
3834% %
3835% %
3836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3837%
3838% SpreadImage() is a special effects method that randomly displaces each
3839% pixel in a block defined by the radius parameter.
3840%
3841% The format of the SpreadImage method is:
3842%
3843% Image *SpreadImage(const Image *image,const double radius,
3844% ExceptionInfo *exception)
3845%
3846% A description of each parameter follows:
3847%
3848% o image: the image.
3849%
3850% o radius: Choose a random pixel in a neighborhood of this extent.
3851%
3852% o exception: return any errors or warnings in this structure.
3853%
3854*/
3855MagickExport Image *SpreadImage(const Image *image,const double radius,
3856 ExceptionInfo *exception)
3857{
3858#define SpreadImageTag "Spread/Image"
3859
cristyfa112112010-01-04 17:48:07 +00003860 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003861 *image_view,
3862 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003863
cristy3ed852e2009-09-05 21:47:34 +00003864 Image
3865 *spread_image;
3866
cristy3ed852e2009-09-05 21:47:34 +00003867 MagickBooleanType
3868 status;
3869
cristybb503372010-05-27 20:51:26 +00003870 MagickOffsetType
3871 progress;
3872
cristy4c08aed2011-07-01 19:47:50 +00003873 PixelInfo
cristyddd82202009-11-03 20:14:50 +00003874 bias;
cristy3ed852e2009-09-05 21:47:34 +00003875
3876 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003877 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003878
cristybb503372010-05-27 20:51:26 +00003879 size_t
cristy3ed852e2009-09-05 21:47:34 +00003880 width;
3881
cristybb503372010-05-27 20:51:26 +00003882 ssize_t
3883 y;
3884
cristy3ed852e2009-09-05 21:47:34 +00003885 /*
3886 Initialize spread image attributes.
3887 */
3888 assert(image != (Image *) NULL);
3889 assert(image->signature == MagickSignature);
3890 if (image->debug != MagickFalse)
3891 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3892 assert(exception != (ExceptionInfo *) NULL);
3893 assert(exception->signature == MagickSignature);
3894 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3895 exception);
3896 if (spread_image == (Image *) NULL)
3897 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003898 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003899 {
cristy3ed852e2009-09-05 21:47:34 +00003900 spread_image=DestroyImage(spread_image);
3901 return((Image *) NULL);
3902 }
3903 /*
3904 Spread image.
3905 */
3906 status=MagickTrue;
3907 progress=0;
cristy4c08aed2011-07-01 19:47:50 +00003908 GetPixelInfo(spread_image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003909 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003910 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003911 image_view=AcquireCacheView(image);
3912 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003913#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00003914 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristy3ed852e2009-09-05 21:47:34 +00003915#endif
cristybb503372010-05-27 20:51:26 +00003916 for (y=0; y < (ssize_t) spread_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003917 {
cristy5c9e6f22010-09-17 17:31:01 +00003918 const int
3919 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003920
cristy4c08aed2011-07-01 19:47:50 +00003921 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003922 pixel;
3923
cristy4c08aed2011-07-01 19:47:50 +00003924 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003925 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003926
cristy117ff172010-08-15 21:35:32 +00003927 register ssize_t
3928 x;
3929
cristy3ed852e2009-09-05 21:47:34 +00003930 if (status == MagickFalse)
3931 continue;
cristy9f7e7cb2011-03-26 00:49:57 +00003932 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003933 exception);
cristyacd2ed22011-08-30 01:44:23 +00003934 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003935 {
3936 status=MagickFalse;
3937 continue;
3938 }
cristyddd82202009-11-03 20:14:50 +00003939 pixel=bias;
cristybb503372010-05-27 20:51:26 +00003940 for (x=0; x < (ssize_t) spread_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003941 {
cristy4c08aed2011-07-01 19:47:50 +00003942 (void) InterpolatePixelInfo(image,image_view,
cristy8a7c3e82011-03-26 02:10:53 +00003943 UndefinedInterpolatePixel,(double) x+width*(GetPseudoRandomValue(
3944 random_info[id])-0.5),(double) y+width*(GetPseudoRandomValue(
3945 random_info[id])-0.5),&pixel,exception);
cristy4c08aed2011-07-01 19:47:50 +00003946 SetPixelPixelInfo(spread_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00003947 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003948 }
cristy9f7e7cb2011-03-26 00:49:57 +00003949 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003950 status=MagickFalse;
3951 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3952 {
3953 MagickBooleanType
3954 proceed;
3955
cristyb557a152011-02-22 12:14:30 +00003956#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003957 #pragma omp critical (MagickCore_SpreadImage)
3958#endif
3959 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3960 if (proceed == MagickFalse)
3961 status=MagickFalse;
3962 }
3963 }
cristy9f7e7cb2011-03-26 00:49:57 +00003964 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003965 image_view=DestroyCacheView(image_view);
3966 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003967 return(spread_image);
3968}
3969
3970/*
3971%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3972% %
3973% %
3974% %
cristy0834d642011-03-18 18:26:08 +00003975% S t a t i s t i c I m a g e %
3976% %
3977% %
3978% %
3979%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3980%
3981% StatisticImage() makes each pixel the min / max / median / mode / etc. of
cristy8d752042011-03-19 01:00:36 +00003982% the neighborhood of the specified width and height.
cristy0834d642011-03-18 18:26:08 +00003983%
3984% The format of the StatisticImage method is:
3985%
3986% Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00003987% const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00003988%
3989% A description of each parameter follows:
3990%
3991% o image: the image.
3992%
cristy0834d642011-03-18 18:26:08 +00003993% o type: the statistic type (median, mode, etc.).
3994%
cristy95c38342011-03-18 22:39:51 +00003995% o width: the width of the pixel neighborhood.
3996%
3997% o height: the height of the pixel neighborhood.
cristy0834d642011-03-18 18:26:08 +00003998%
3999% o exception: return any errors or warnings in this structure.
4000%
4001*/
4002
cristy733678d2011-03-18 21:29:28 +00004003#define ListChannels 5
4004
4005typedef struct _ListNode
4006{
4007 size_t
4008 next[9],
4009 count,
4010 signature;
4011} ListNode;
4012
4013typedef struct _SkipList
4014{
4015 ssize_t
4016 level;
4017
4018 ListNode
4019 *nodes;
4020} SkipList;
4021
4022typedef struct _PixelList
4023{
4024 size_t
cristy6fc86bb2011-03-18 23:45:16 +00004025 length,
cristy733678d2011-03-18 21:29:28 +00004026 seed,
4027 signature;
4028
4029 SkipList
4030 lists[ListChannels];
4031} PixelList;
4032
4033static PixelList *DestroyPixelList(PixelList *pixel_list)
4034{
4035 register ssize_t
4036 i;
4037
4038 if (pixel_list == (PixelList *) NULL)
4039 return((PixelList *) NULL);
4040 for (i=0; i < ListChannels; i++)
4041 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
4042 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
4043 pixel_list->lists[i].nodes);
4044 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
4045 return(pixel_list);
4046}
4047
4048static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
4049{
4050 register ssize_t
4051 i;
4052
4053 assert(pixel_list != (PixelList **) NULL);
4054 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
4055 if (pixel_list[i] != (PixelList *) NULL)
4056 pixel_list[i]=DestroyPixelList(pixel_list[i]);
4057 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
4058 return(pixel_list);
4059}
4060
cristy6fc86bb2011-03-18 23:45:16 +00004061static PixelList *AcquirePixelList(const size_t width,const size_t height)
cristy733678d2011-03-18 21:29:28 +00004062{
4063 PixelList
4064 *pixel_list;
4065
4066 register ssize_t
4067 i;
4068
4069 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
4070 if (pixel_list == (PixelList *) NULL)
4071 return(pixel_list);
4072 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
cristy6fc86bb2011-03-18 23:45:16 +00004073 pixel_list->length=width*height;
cristy733678d2011-03-18 21:29:28 +00004074 for (i=0; i < ListChannels; i++)
4075 {
4076 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
4077 sizeof(*pixel_list->lists[i].nodes));
4078 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
4079 return(DestroyPixelList(pixel_list));
4080 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
4081 sizeof(*pixel_list->lists[i].nodes));
4082 }
4083 pixel_list->signature=MagickSignature;
4084 return(pixel_list);
4085}
4086
cristy6fc86bb2011-03-18 23:45:16 +00004087static PixelList **AcquirePixelListThreadSet(const size_t width,
4088 const size_t height)
cristy733678d2011-03-18 21:29:28 +00004089{
4090 PixelList
4091 **pixel_list;
4092
4093 register ssize_t
4094 i;
4095
4096 size_t
4097 number_threads;
4098
4099 number_threads=GetOpenMPMaximumThreads();
4100 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
4101 sizeof(*pixel_list));
4102 if (pixel_list == (PixelList **) NULL)
4103 return((PixelList **) NULL);
4104 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
4105 for (i=0; i < (ssize_t) number_threads; i++)
4106 {
cristy6fc86bb2011-03-18 23:45:16 +00004107 pixel_list[i]=AcquirePixelList(width,height);
cristy733678d2011-03-18 21:29:28 +00004108 if (pixel_list[i] == (PixelList *) NULL)
4109 return(DestroyPixelListThreadSet(pixel_list));
4110 }
4111 return(pixel_list);
4112}
4113
4114static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
4115 const size_t color)
4116{
4117 register SkipList
4118 *list;
4119
4120 register ssize_t
4121 level;
4122
4123 size_t
4124 search,
4125 update[9];
4126
4127 /*
4128 Initialize the node.
4129 */
4130 list=pixel_list->lists+channel;
4131 list->nodes[color].signature=pixel_list->signature;
4132 list->nodes[color].count=1;
4133 /*
4134 Determine where it belongs in the list.
4135 */
4136 search=65536UL;
4137 for (level=list->level; level >= 0; level--)
4138 {
4139 while (list->nodes[search].next[level] < color)
4140 search=list->nodes[search].next[level];
4141 update[level]=search;
4142 }
4143 /*
4144 Generate a pseudo-random level for this node.
4145 */
4146 for (level=0; ; level++)
4147 {
4148 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
4149 if ((pixel_list->seed & 0x300) != 0x300)
4150 break;
4151 }
4152 if (level > 8)
4153 level=8;
4154 if (level > (list->level+2))
4155 level=list->level+2;
4156 /*
4157 If we're raising the list's level, link back to the root node.
4158 */
4159 while (level > list->level)
4160 {
4161 list->level++;
4162 update[list->level]=65536UL;
4163 }
4164 /*
4165 Link the node into the skip-list.
4166 */
4167 do
4168 {
4169 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
4170 list->nodes[update[level]].next[level]=color;
cristy3cba8ca2011-03-19 01:29:12 +00004171 } while (level-- > 0);
cristy733678d2011-03-18 21:29:28 +00004172}
4173
cristy4c08aed2011-07-01 19:47:50 +00004174static PixelInfo GetMaximumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004175{
cristy4c08aed2011-07-01 19:47:50 +00004176 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004177 pixel;
4178
4179 register SkipList
4180 *list;
4181
4182 register ssize_t
4183 channel;
4184
4185 size_t
cristyd76c51e2011-03-26 00:21:26 +00004186 color,
4187 maximum;
cristy49f37242011-03-22 18:18:23 +00004188
4189 ssize_t
cristy6fc86bb2011-03-18 23:45:16 +00004190 count;
4191
4192 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004193 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004194
4195 /*
4196 Find the maximum value for each of the color.
4197 */
4198 for (channel=0; channel < 5; channel++)
4199 {
4200 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004201 color=65536L;
cristy6fc86bb2011-03-18 23:45:16 +00004202 count=0;
cristy49f37242011-03-22 18:18:23 +00004203 maximum=list->nodes[color].next[0];
cristy6fc86bb2011-03-18 23:45:16 +00004204 do
4205 {
4206 color=list->nodes[color].next[0];
cristy49f37242011-03-22 18:18:23 +00004207 if (color > maximum)
4208 maximum=color;
cristy6fc86bb2011-03-18 23:45:16 +00004209 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004210 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004211 channels[channel]=(unsigned short) maximum;
4212 }
cristy4c08aed2011-07-01 19:47:50 +00004213 GetPixelInfo((const Image *) NULL,&pixel);
cristy49f37242011-03-22 18:18:23 +00004214 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4215 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4216 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004217 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4218 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy49f37242011-03-22 18:18:23 +00004219 return(pixel);
4220}
4221
cristy4c08aed2011-07-01 19:47:50 +00004222static PixelInfo GetMeanPixelList(PixelList *pixel_list)
cristy49f37242011-03-22 18:18:23 +00004223{
cristy4c08aed2011-07-01 19:47:50 +00004224 PixelInfo
cristy49f37242011-03-22 18:18:23 +00004225 pixel;
4226
cristy80a99a32011-03-30 01:30:23 +00004227 MagickRealType
4228 sum;
4229
cristy49f37242011-03-22 18:18:23 +00004230 register SkipList
4231 *list;
4232
4233 register ssize_t
4234 channel;
4235
4236 size_t
cristy80a99a32011-03-30 01:30:23 +00004237 color;
cristy49f37242011-03-22 18:18:23 +00004238
4239 ssize_t
4240 count;
4241
4242 unsigned short
4243 channels[ListChannels];
4244
4245 /*
4246 Find the mean value for each of the color.
4247 */
4248 for (channel=0; channel < 5; channel++)
4249 {
4250 list=pixel_list->lists+channel;
4251 color=65536L;
4252 count=0;
cristy80a99a32011-03-30 01:30:23 +00004253 sum=0.0;
cristy49f37242011-03-22 18:18:23 +00004254 do
4255 {
4256 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004257 sum+=(MagickRealType) list->nodes[color].count*color;
cristy49f37242011-03-22 18:18:23 +00004258 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004259 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004260 sum/=pixel_list->length;
4261 channels[channel]=(unsigned short) sum;
cristy6fc86bb2011-03-18 23:45:16 +00004262 }
cristy4c08aed2011-07-01 19:47:50 +00004263 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004264 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4265 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4266 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004267 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4268 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004269 return(pixel);
4270}
4271
cristy4c08aed2011-07-01 19:47:50 +00004272static PixelInfo GetMedianPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004273{
cristy4c08aed2011-07-01 19:47:50 +00004274 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004275 pixel;
4276
4277 register SkipList
4278 *list;
4279
4280 register ssize_t
4281 channel;
4282
4283 size_t
cristy49f37242011-03-22 18:18:23 +00004284 color;
4285
4286 ssize_t
cristy733678d2011-03-18 21:29:28 +00004287 count;
4288
4289 unsigned short
4290 channels[ListChannels];
4291
4292 /*
4293 Find the median value for each of the color.
4294 */
cristy733678d2011-03-18 21:29:28 +00004295 for (channel=0; channel < 5; channel++)
4296 {
4297 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004298 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004299 count=0;
4300 do
4301 {
4302 color=list->nodes[color].next[0];
4303 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004304 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy6fc86bb2011-03-18 23:45:16 +00004305 channels[channel]=(unsigned short) color;
4306 }
cristy4c08aed2011-07-01 19:47:50 +00004307 GetPixelInfo((const Image *) NULL,&pixel);
cristy6fc86bb2011-03-18 23:45:16 +00004308 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4309 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4310 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004311 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4312 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy6fc86bb2011-03-18 23:45:16 +00004313 return(pixel);
4314}
4315
cristy4c08aed2011-07-01 19:47:50 +00004316static PixelInfo GetMinimumPixelList(PixelList *pixel_list)
cristy6fc86bb2011-03-18 23:45:16 +00004317{
cristy4c08aed2011-07-01 19:47:50 +00004318 PixelInfo
cristy6fc86bb2011-03-18 23:45:16 +00004319 pixel;
4320
4321 register SkipList
4322 *list;
4323
4324 register ssize_t
4325 channel;
4326
4327 size_t
cristyd76c51e2011-03-26 00:21:26 +00004328 color,
4329 minimum;
cristy6fc86bb2011-03-18 23:45:16 +00004330
cristy49f37242011-03-22 18:18:23 +00004331 ssize_t
4332 count;
4333
cristy6fc86bb2011-03-18 23:45:16 +00004334 unsigned short
cristyd76c51e2011-03-26 00:21:26 +00004335 channels[ListChannels];
cristy6fc86bb2011-03-18 23:45:16 +00004336
4337 /*
4338 Find the minimum value for each of the color.
4339 */
4340 for (channel=0; channel < 5; channel++)
4341 {
4342 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004343 count=0;
cristy6fc86bb2011-03-18 23:45:16 +00004344 color=65536UL;
cristy49f37242011-03-22 18:18:23 +00004345 minimum=list->nodes[color].next[0];
4346 do
4347 {
4348 color=list->nodes[color].next[0];
4349 if (color < minimum)
4350 minimum=color;
4351 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004352 } while (count < (ssize_t) pixel_list->length);
cristy49f37242011-03-22 18:18:23 +00004353 channels[channel]=(unsigned short) minimum;
cristy733678d2011-03-18 21:29:28 +00004354 }
cristy4c08aed2011-07-01 19:47:50 +00004355 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004356 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4357 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4358 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004359 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4360 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004361 return(pixel);
4362}
4363
cristy4c08aed2011-07-01 19:47:50 +00004364static PixelInfo GetModePixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004365{
cristy4c08aed2011-07-01 19:47:50 +00004366 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004367 pixel;
4368
4369 register SkipList
4370 *list;
4371
4372 register ssize_t
4373 channel;
4374
4375 size_t
4376 color,
cristy733678d2011-03-18 21:29:28 +00004377 max_count,
cristy6fc86bb2011-03-18 23:45:16 +00004378 mode;
cristy733678d2011-03-18 21:29:28 +00004379
cristy49f37242011-03-22 18:18:23 +00004380 ssize_t
4381 count;
4382
cristy733678d2011-03-18 21:29:28 +00004383 unsigned short
4384 channels[5];
4385
4386 /*
glennrp30d2dc62011-06-25 03:17:16 +00004387 Make each pixel the 'predominant color' of the specified neighborhood.
cristy733678d2011-03-18 21:29:28 +00004388 */
cristy733678d2011-03-18 21:29:28 +00004389 for (channel=0; channel < 5; channel++)
4390 {
4391 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004392 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004393 mode=color;
4394 max_count=list->nodes[mode].count;
4395 count=0;
4396 do
4397 {
4398 color=list->nodes[color].next[0];
4399 if (list->nodes[color].count > max_count)
4400 {
4401 mode=color;
4402 max_count=list->nodes[mode].count;
4403 }
4404 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004405 } while (count < (ssize_t) pixel_list->length);
cristy733678d2011-03-18 21:29:28 +00004406 channels[channel]=(unsigned short) mode;
4407 }
cristy4c08aed2011-07-01 19:47:50 +00004408 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004409 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4410 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4411 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004412 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4413 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
cristy733678d2011-03-18 21:29:28 +00004414 return(pixel);
4415}
4416
cristy4c08aed2011-07-01 19:47:50 +00004417static PixelInfo GetNonpeakPixelList(PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004418{
cristy4c08aed2011-07-01 19:47:50 +00004419 PixelInfo
cristy733678d2011-03-18 21:29:28 +00004420 pixel;
4421
4422 register SkipList
4423 *list;
4424
4425 register ssize_t
4426 channel;
4427
4428 size_t
cristy733678d2011-03-18 21:29:28 +00004429 color,
cristy733678d2011-03-18 21:29:28 +00004430 next,
4431 previous;
4432
cristy49f37242011-03-22 18:18:23 +00004433 ssize_t
4434 count;
4435
cristy733678d2011-03-18 21:29:28 +00004436 unsigned short
4437 channels[5];
4438
4439 /*
cristy49f37242011-03-22 18:18:23 +00004440 Finds the non peak value for each of the colors.
cristy733678d2011-03-18 21:29:28 +00004441 */
cristy733678d2011-03-18 21:29:28 +00004442 for (channel=0; channel < 5; channel++)
4443 {
4444 list=pixel_list->lists+channel;
cristy49f37242011-03-22 18:18:23 +00004445 color=65536L;
cristy733678d2011-03-18 21:29:28 +00004446 next=list->nodes[color].next[0];
4447 count=0;
4448 do
4449 {
4450 previous=color;
4451 color=next;
4452 next=list->nodes[color].next[0];
4453 count+=list->nodes[color].count;
cristyd76c51e2011-03-26 00:21:26 +00004454 } while (count <= (ssize_t) (pixel_list->length >> 1));
cristy733678d2011-03-18 21:29:28 +00004455 if ((previous == 65536UL) && (next != 65536UL))
4456 color=next;
4457 else
4458 if ((previous != 65536UL) && (next == 65536UL))
4459 color=previous;
4460 channels[channel]=(unsigned short) color;
4461 }
cristy4c08aed2011-07-01 19:47:50 +00004462 GetPixelInfo((const Image *) NULL,&pixel);
cristy733678d2011-03-18 21:29:28 +00004463 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4464 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4465 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004466 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4467 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy733678d2011-03-18 21:29:28 +00004468 return(pixel);
4469}
4470
cristy4c08aed2011-07-01 19:47:50 +00004471static PixelInfo GetStandardDeviationPixelList(PixelList *pixel_list)
cristy9a68cbb2011-03-29 00:51:23 +00004472{
cristy4c08aed2011-07-01 19:47:50 +00004473 PixelInfo
cristy9a68cbb2011-03-29 00:51:23 +00004474 pixel;
4475
cristy80a99a32011-03-30 01:30:23 +00004476 MagickRealType
4477 sum,
4478 sum_squared;
4479
cristy9a68cbb2011-03-29 00:51:23 +00004480 register SkipList
4481 *list;
4482
4483 register ssize_t
4484 channel;
4485
4486 size_t
cristy80a99a32011-03-30 01:30:23 +00004487 color;
cristy9a68cbb2011-03-29 00:51:23 +00004488
4489 ssize_t
4490 count;
4491
4492 unsigned short
4493 channels[ListChannels];
4494
4495 /*
cristy80a99a32011-03-30 01:30:23 +00004496 Find the standard-deviation value for each of the color.
cristy9a68cbb2011-03-29 00:51:23 +00004497 */
4498 for (channel=0; channel < 5; channel++)
4499 {
4500 list=pixel_list->lists+channel;
4501 color=65536L;
4502 count=0;
cristy80a99a32011-03-30 01:30:23 +00004503 sum=0.0;
4504 sum_squared=0.0;
cristy9a68cbb2011-03-29 00:51:23 +00004505 do
4506 {
cristy80a99a32011-03-30 01:30:23 +00004507 register ssize_t
4508 i;
4509
cristy9a68cbb2011-03-29 00:51:23 +00004510 color=list->nodes[color].next[0];
cristy80a99a32011-03-30 01:30:23 +00004511 sum+=(MagickRealType) list->nodes[color].count*color;
4512 for (i=0; i < (ssize_t) list->nodes[color].count; i++)
4513 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
cristy9a68cbb2011-03-29 00:51:23 +00004514 count+=list->nodes[color].count;
4515 } while (count < (ssize_t) pixel_list->length);
cristy80a99a32011-03-30 01:30:23 +00004516 sum/=pixel_list->length;
4517 sum_squared/=pixel_list->length;
4518 channels[channel]=(unsigned short) sqrt(sum_squared-(sum*sum));
cristy9a68cbb2011-03-29 00:51:23 +00004519 }
cristy4c08aed2011-07-01 19:47:50 +00004520 GetPixelInfo((const Image *) NULL,&pixel);
cristy9a68cbb2011-03-29 00:51:23 +00004521 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4522 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4523 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
cristy4c08aed2011-07-01 19:47:50 +00004524 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4525 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
cristy9a68cbb2011-03-29 00:51:23 +00004526 return(pixel);
4527}
4528
cristy4c08aed2011-07-01 19:47:50 +00004529static inline void InsertPixelList(const Image *image,const Quantum *pixel,
4530 PixelList *pixel_list)
cristy733678d2011-03-18 21:29:28 +00004531{
4532 size_t
4533 signature;
4534
4535 unsigned short
4536 index;
4537
cristy4c08aed2011-07-01 19:47:50 +00004538 index=ScaleQuantumToShort(GetPixelRed(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004539 signature=pixel_list->lists[0].nodes[index].signature;
4540 if (signature == pixel_list->signature)
4541 pixel_list->lists[0].nodes[index].count++;
4542 else
4543 AddNodePixelList(pixel_list,0,index);
cristy4c08aed2011-07-01 19:47:50 +00004544 index=ScaleQuantumToShort(GetPixelGreen(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004545 signature=pixel_list->lists[1].nodes[index].signature;
4546 if (signature == pixel_list->signature)
4547 pixel_list->lists[1].nodes[index].count++;
4548 else
4549 AddNodePixelList(pixel_list,1,index);
cristy4c08aed2011-07-01 19:47:50 +00004550 index=ScaleQuantumToShort(GetPixelBlue(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004551 signature=pixel_list->lists[2].nodes[index].signature;
4552 if (signature == pixel_list->signature)
4553 pixel_list->lists[2].nodes[index].count++;
4554 else
4555 AddNodePixelList(pixel_list,2,index);
cristy4c08aed2011-07-01 19:47:50 +00004556 index=ScaleQuantumToShort(GetPixelAlpha(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004557 signature=pixel_list->lists[3].nodes[index].signature;
4558 if (signature == pixel_list->signature)
4559 pixel_list->lists[3].nodes[index].count++;
4560 else
4561 AddNodePixelList(pixel_list,3,index);
4562 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004563 index=ScaleQuantumToShort(GetPixelBlack(image,pixel));
cristy733678d2011-03-18 21:29:28 +00004564 signature=pixel_list->lists[4].nodes[index].signature;
4565 if (signature == pixel_list->signature)
4566 pixel_list->lists[4].nodes[index].count++;
4567 else
4568 AddNodePixelList(pixel_list,4,index);
4569}
4570
cristy80c99742011-04-04 14:46:39 +00004571static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
4572{
4573 if (x < 0)
4574 return(-x);
4575 return(x);
4576}
4577
cristy733678d2011-03-18 21:29:28 +00004578static void ResetPixelList(PixelList *pixel_list)
4579{
4580 int
4581 level;
4582
4583 register ListNode
4584 *root;
4585
4586 register SkipList
4587 *list;
4588
4589 register ssize_t
4590 channel;
4591
4592 /*
4593 Reset the skip-list.
4594 */
4595 for (channel=0; channel < 5; channel++)
4596 {
4597 list=pixel_list->lists+channel;
4598 root=list->nodes+65536UL;
4599 list->level=0;
4600 for (level=0; level < 9; level++)
4601 root->next[level]=65536UL;
4602 }
4603 pixel_list->seed=pixel_list->signature++;
4604}
4605
cristy0834d642011-03-18 18:26:08 +00004606MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
cristy95c38342011-03-18 22:39:51 +00004607 const size_t width,const size_t height,ExceptionInfo *exception)
cristy0834d642011-03-18 18:26:08 +00004608{
cristy3cba8ca2011-03-19 01:29:12 +00004609#define StatisticWidth \
cristyd76c51e2011-03-26 00:21:26 +00004610 (width == 0 ? GetOptimalKernelWidth2D((double) width,0.5) : width)
cristy3cba8ca2011-03-19 01:29:12 +00004611#define StatisticHeight \
cristyd76c51e2011-03-26 00:21:26 +00004612 (height == 0 ? GetOptimalKernelWidth2D((double) height,0.5) : height)
cristy0834d642011-03-18 18:26:08 +00004613#define StatisticImageTag "Statistic/Image"
4614
4615 CacheView
4616 *image_view,
4617 *statistic_view;
4618
4619 Image
4620 *statistic_image;
4621
4622 MagickBooleanType
4623 status;
4624
4625 MagickOffsetType
4626 progress;
4627
4628 PixelList
4629 **restrict pixel_list;
4630
cristy0834d642011-03-18 18:26:08 +00004631 ssize_t
4632 y;
4633
4634 /*
4635 Initialize statistics image attributes.
4636 */
4637 assert(image != (Image *) NULL);
4638 assert(image->signature == MagickSignature);
4639 if (image->debug != MagickFalse)
4640 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4641 assert(exception != (ExceptionInfo *) NULL);
4642 assert(exception->signature == MagickSignature);
cristy0834d642011-03-18 18:26:08 +00004643 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4644 exception);
4645 if (statistic_image == (Image *) NULL)
4646 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004647 if (SetImageStorageClass(statistic_image,DirectClass,exception) == MagickFalse)
cristy0834d642011-03-18 18:26:08 +00004648 {
cristy0834d642011-03-18 18:26:08 +00004649 statistic_image=DestroyImage(statistic_image);
4650 return((Image *) NULL);
4651 }
cristy6fc86bb2011-03-18 23:45:16 +00004652 pixel_list=AcquirePixelListThreadSet(StatisticWidth,StatisticHeight);
cristy0834d642011-03-18 18:26:08 +00004653 if (pixel_list == (PixelList **) NULL)
4654 {
4655 statistic_image=DestroyImage(statistic_image);
4656 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4657 }
4658 /*
cristy8d752042011-03-19 01:00:36 +00004659 Make each pixel the min / max / median / mode / etc. of the neighborhood.
cristy0834d642011-03-18 18:26:08 +00004660 */
4661 status=MagickTrue;
4662 progress=0;
4663 image_view=AcquireCacheView(image);
4664 statistic_view=AcquireCacheView(statistic_image);
4665#if defined(MAGICKCORE_OPENMP_SUPPORT)
4666 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4667#endif
4668 for (y=0; y < (ssize_t) statistic_image->rows; y++)
4669 {
4670 const int
4671 id = GetOpenMPThreadId();
4672
cristy4c08aed2011-07-01 19:47:50 +00004673 register const Quantum
cristy0834d642011-03-18 18:26:08 +00004674 *restrict p;
4675
cristy4c08aed2011-07-01 19:47:50 +00004676 register Quantum
cristy0834d642011-03-18 18:26:08 +00004677 *restrict q;
4678
4679 register ssize_t
4680 x;
4681
4682 if (status == MagickFalse)
4683 continue;
cristy6fc86bb2011-03-18 23:45:16 +00004684 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) StatisticWidth/2L),y-
4685 (ssize_t) (StatisticHeight/2L),image->columns+StatisticWidth,
4686 StatisticHeight,exception);
cristy3cba8ca2011-03-19 01:29:12 +00004687 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004688 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy0834d642011-03-18 18:26:08 +00004689 {
4690 status=MagickFalse;
4691 continue;
4692 }
cristy0834d642011-03-18 18:26:08 +00004693 for (x=0; x < (ssize_t) statistic_image->columns; x++)
4694 {
cristy4c08aed2011-07-01 19:47:50 +00004695 PixelInfo
cristy0834d642011-03-18 18:26:08 +00004696 pixel;
4697
cristy4c08aed2011-07-01 19:47:50 +00004698 register const Quantum
cristy6e3026a2011-03-19 00:54:38 +00004699 *restrict r;
4700
cristy0834d642011-03-18 18:26:08 +00004701 register ssize_t
4702 u,
4703 v;
4704
4705 r=p;
cristy0834d642011-03-18 18:26:08 +00004706 ResetPixelList(pixel_list[id]);
cristy6e4c3292011-03-19 00:53:55 +00004707 for (v=0; v < (ssize_t) StatisticHeight; v++)
cristy0834d642011-03-18 18:26:08 +00004708 {
cristy6e4c3292011-03-19 00:53:55 +00004709 for (u=0; u < (ssize_t) StatisticWidth; u++)
cristyed231572011-07-14 02:18:59 +00004710 InsertPixelList(image,r+u*GetPixelChannels(image),pixel_list[id]);
4711 r+=(image->columns+StatisticWidth)*GetPixelChannels(image);
cristy0834d642011-03-18 18:26:08 +00004712 }
cristy4c08aed2011-07-01 19:47:50 +00004713 GetPixelInfo(image,&pixel);
cristy490408a2011-07-07 14:42:05 +00004714 SetPixelInfo(image,p+(StatisticWidth*StatisticHeight/2)*
cristyed231572011-07-14 02:18:59 +00004715 GetPixelChannels(image),&pixel);
cristy0834d642011-03-18 18:26:08 +00004716 switch (type)
4717 {
cristy80c99742011-04-04 14:46:39 +00004718 case GradientStatistic:
4719 {
cristy4c08aed2011-07-01 19:47:50 +00004720 PixelInfo
cristy80c99742011-04-04 14:46:39 +00004721 maximum,
4722 minimum;
4723
4724 minimum=GetMinimumPixelList(pixel_list[id]);
4725 maximum=GetMaximumPixelList(pixel_list[id]);
4726 pixel.red=MagickAbsoluteValue(maximum.red-minimum.red);
4727 pixel.green=MagickAbsoluteValue(maximum.green-minimum.green);
4728 pixel.blue=MagickAbsoluteValue(maximum.blue-minimum.blue);
cristy4c08aed2011-07-01 19:47:50 +00004729 pixel.alpha=MagickAbsoluteValue(maximum.alpha-minimum.alpha);
cristy80c99742011-04-04 14:46:39 +00004730 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00004731 pixel.black=MagickAbsoluteValue(maximum.black-minimum.black);
cristy80c99742011-04-04 14:46:39 +00004732 break;
4733 }
cristy6fc86bb2011-03-18 23:45:16 +00004734 case MaximumStatistic:
4735 {
4736 pixel=GetMaximumPixelList(pixel_list[id]);
4737 break;
4738 }
cristy49f37242011-03-22 18:18:23 +00004739 case MeanStatistic:
4740 {
4741 pixel=GetMeanPixelList(pixel_list[id]);
4742 break;
4743 }
cristyf2ad14a2011-03-18 18:57:25 +00004744 case MedianStatistic:
cristy6fc86bb2011-03-18 23:45:16 +00004745 default:
cristyf2ad14a2011-03-18 18:57:25 +00004746 {
4747 pixel=GetMedianPixelList(pixel_list[id]);
4748 break;
4749 }
cristy6fc86bb2011-03-18 23:45:16 +00004750 case MinimumStatistic:
4751 {
4752 pixel=GetMinimumPixelList(pixel_list[id]);
4753 break;
4754 }
cristyf2ad14a2011-03-18 18:57:25 +00004755 case ModeStatistic:
4756 {
4757 pixel=GetModePixelList(pixel_list[id]);
4758 break;
4759 }
4760 case NonpeakStatistic:
4761 {
4762 pixel=GetNonpeakPixelList(pixel_list[id]);
4763 break;
4764 }
cristy9a68cbb2011-03-29 00:51:23 +00004765 case StandardDeviationStatistic:
4766 {
4767 pixel=GetStandardDeviationPixelList(pixel_list[id]);
4768 break;
4769 }
cristy0834d642011-03-18 18:26:08 +00004770 }
cristyed231572011-07-14 02:18:59 +00004771 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004772 SetPixelRed(statistic_image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00004773 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004774 SetPixelGreen(statistic_image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00004775 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy490408a2011-07-07 14:42:05 +00004776 SetPixelBlue(statistic_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00004777 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy0834d642011-03-18 18:26:08 +00004778 (image->colorspace == CMYKColorspace))
cristy490408a2011-07-07 14:42:05 +00004779 SetPixelBlack(statistic_image,ClampToQuantum(pixel.black),q);
cristyed231572011-07-14 02:18:59 +00004780 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00004781 (image->matte != MagickFalse))
cristy490408a2011-07-07 14:42:05 +00004782 SetPixelAlpha(statistic_image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00004783 p+=GetPixelChannels(image);
4784 q+=GetPixelChannels(statistic_image);
cristy0834d642011-03-18 18:26:08 +00004785 }
4786 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
4787 status=MagickFalse;
4788 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4789 {
4790 MagickBooleanType
4791 proceed;
4792
4793#if defined(MAGICKCORE_OPENMP_SUPPORT)
4794 #pragma omp critical (MagickCore_StatisticImage)
4795#endif
4796 proceed=SetImageProgress(image,StatisticImageTag,progress++,
4797 image->rows);
4798 if (proceed == MagickFalse)
4799 status=MagickFalse;
4800 }
4801 }
4802 statistic_view=DestroyCacheView(statistic_view);
4803 image_view=DestroyCacheView(image_view);
4804 pixel_list=DestroyPixelListThreadSet(pixel_list);
4805 return(statistic_image);
4806}
4807
4808/*
4809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4810% %
4811% %
4812% %
cristy3ed852e2009-09-05 21:47:34 +00004813% U n s h a r p M a s k I m a g e %
4814% %
4815% %
4816% %
4817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4818%
4819% UnsharpMaskImage() sharpens one or more image channels. We convolve the
4820% image with a Gaussian operator of the given radius and standard deviation
4821% (sigma). For reasonable results, radius should be larger than sigma. Use a
4822% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
4823%
4824% The format of the UnsharpMaskImage method is:
4825%
4826% Image *UnsharpMaskImage(const Image *image,const double radius,
4827% const double sigma,const double amount,const double threshold,
4828% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004829%
4830% A description of each parameter follows:
4831%
4832% o image: the image.
4833%
cristy3ed852e2009-09-05 21:47:34 +00004834% o radius: the radius of the Gaussian, in pixels, not counting the center
4835% pixel.
4836%
4837% o sigma: the standard deviation of the Gaussian, in pixels.
4838%
4839% o amount: the percentage of the difference between the original and the
4840% blur image that is added back into the original.
4841%
4842% o threshold: the threshold in pixels needed to apply the diffence amount.
4843%
4844% o exception: return any errors or warnings in this structure.
4845%
4846*/
cristyf4ad9df2011-07-08 16:49:03 +00004847MagickExport Image *UnsharpMaskImage(const Image *image,
4848 const double radius,const double sigma,const double amount,
4849 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004850{
4851#define SharpenImageTag "Sharpen/Image"
4852
cristyc4c8d132010-01-07 01:58:38 +00004853 CacheView
4854 *image_view,
4855 *unsharp_view;
4856
cristy3ed852e2009-09-05 21:47:34 +00004857 Image
4858 *unsharp_image;
4859
cristy3ed852e2009-09-05 21:47:34 +00004860 MagickBooleanType
4861 status;
4862
cristybb503372010-05-27 20:51:26 +00004863 MagickOffsetType
4864 progress;
4865
cristy3ed852e2009-09-05 21:47:34 +00004866 MagickRealType
4867 quantum_threshold;
4868
cristybb503372010-05-27 20:51:26 +00004869 ssize_t
4870 y;
4871
cristy3ed852e2009-09-05 21:47:34 +00004872 assert(image != (const Image *) NULL);
4873 assert(image->signature == MagickSignature);
4874 if (image->debug != MagickFalse)
4875 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4876 assert(exception != (ExceptionInfo *) NULL);
cristy05c0c9a2011-09-05 23:16:13 +00004877 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004878 if (unsharp_image == (Image *) NULL)
4879 return((Image *) NULL);
4880 quantum_threshold=(MagickRealType) QuantumRange*threshold;
4881 /*
4882 Unsharp-mask image.
4883 */
4884 status=MagickTrue;
4885 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004886 image_view=AcquireCacheView(image);
4887 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00004888#if defined(MAGICKCORE_OPENMP_SUPPORT)
4889 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004890#endif
cristybb503372010-05-27 20:51:26 +00004891 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004892 {
cristy4c08aed2011-07-01 19:47:50 +00004893 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004894 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004895
cristy4c08aed2011-07-01 19:47:50 +00004896 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004897 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004898
cristy117ff172010-08-15 21:35:32 +00004899 register ssize_t
4900 x;
4901
cristy3ed852e2009-09-05 21:47:34 +00004902 if (status == MagickFalse)
4903 continue;
4904 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4905 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
4906 exception);
cristy4c08aed2011-07-01 19:47:50 +00004907 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004908 {
4909 status=MagickFalse;
4910 continue;
4911 }
cristybb503372010-05-27 20:51:26 +00004912 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004913 {
cristy7f3a0d12011-09-05 23:27:59 +00004914 register ssize_t
4915 i;
4916
4917 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4918 {
4919 MagickRealType
4920 pixel;
4921
4922 PixelChannel
4923 channel;
4924
4925 PixelTrait
4926 traits,
4927 unsharp_traits;
4928
4929 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
4930 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
4931 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
4932 if ((traits == UndefinedPixelTrait) ||
4933 (unsharp_traits == UndefinedPixelTrait))
4934 continue;
4935 if ((unsharp_traits & CopyPixelTrait) != 0)
4936 {
4937 q[channel]=p[i];
4938 continue;
4939 }
4940 pixel=p[i]-(MagickRealType) q[channel];
4941 if (fabs(2.0*pixel) < quantum_threshold)
4942 pixel=(MagickRealType) p[i];
4943 else
4944 pixel=(MagickRealType) p[i]+amount*pixel;
4945 q[channel]=ClampToQuantum(pixel);
4946 }
cristyed231572011-07-14 02:18:59 +00004947 p+=GetPixelChannels(image);
4948 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00004949 }
4950 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
4951 status=MagickFalse;
4952 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4953 {
4954 MagickBooleanType
4955 proceed;
4956
cristyb5d5f722009-11-04 03:03:49 +00004957#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00004958 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00004959#endif
4960 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4961 if (proceed == MagickFalse)
4962 status=MagickFalse;
4963 }
4964 }
4965 unsharp_image->type=image->type;
4966 unsharp_view=DestroyCacheView(unsharp_view);
4967 image_view=DestroyCacheView(image_view);
4968 if (status == MagickFalse)
4969 unsharp_image=DestroyImage(unsharp_image);
4970 return(unsharp_image);
4971}