blob: b8840e779f7a1eab005f66042fa53cdb222ec1c9 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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"
cristyc53413d2011-11-17 13:04:26 +000052#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/draw.h"
54#include "MagickCore/enhance.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/exception-private.h"
57#include "MagickCore/effect.h"
58#include "MagickCore/fx.h"
59#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000060#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000061#include "MagickCore/geometry.h"
62#include "MagickCore/image-private.h"
63#include "MagickCore/list.h"
64#include "MagickCore/log.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/monitor.h"
67#include "MagickCore/monitor-private.h"
68#include "MagickCore/montage.h"
69#include "MagickCore/morphology.h"
70#include "MagickCore/paint.h"
71#include "MagickCore/pixel-accessor.h"
72#include "MagickCore/property.h"
73#include "MagickCore/quantize.h"
74#include "MagickCore/quantum.h"
75#include "MagickCore/quantum-private.h"
76#include "MagickCore/random_.h"
77#include "MagickCore/random-private.h"
78#include "MagickCore/resample.h"
79#include "MagickCore/resample-private.h"
80#include "MagickCore/resize.h"
81#include "MagickCore/resource_.h"
82#include "MagickCore/segment.h"
cristy31bbf2f2011-11-17 13:19:37 +000083#include "MagickCore/shear.h"
cristy4c08aed2011-07-01 19:47:50 +000084#include "MagickCore/signature-private.h"
cristy99bd5232011-12-07 14:38:20 +000085#include "MagickCore/statistic.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/string_.h"
87#include "MagickCore/thread-private.h"
88#include "MagickCore/transform.h"
89#include "MagickCore/threshold.h"
cristy3ed852e2009-09-05 21:47:34 +000090
91/*
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93% %
94% %
95% %
96% A d a p t i v e B l u r I m a g e %
97% %
98% %
99% %
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101%
102% AdaptiveBlurImage() adaptively blurs the image by blurring less
103% intensely near image edges and more intensely far from edges. We blur the
104% image with a Gaussian operator of the given radius and standard deviation
105% (sigma). For reasonable results, radius should be larger than sigma. Use a
106% radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
107%
108% The format of the AdaptiveBlurImage method is:
109%
110% Image *AdaptiveBlurImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000111% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000112%
113% A description of each parameter follows:
114%
115% o image: the image.
116%
cristy3ed852e2009-09-05 21:47:34 +0000117% o radius: the radius of the Gaussian, in pixels, not counting the center
118% pixel.
119%
120% o sigma: the standard deviation of the Laplacian, in pixels.
121%
cristy4c11c2b2011-09-05 20:17:07 +0000122% o bias: the bias.
123%
cristy3ed852e2009-09-05 21:47:34 +0000124% o exception: return any errors or warnings in this structure.
125%
126*/
127
cristyf89cb1d2011-07-07 01:24:37 +0000128MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
cristy051718b2011-08-28 22:49:25 +0000129 const char *levels,ExceptionInfo *exception)
cristyf89cb1d2011-07-07 01:24:37 +0000130{
131 double
132 black_point,
133 gamma,
134 white_point;
135
136 GeometryInfo
137 geometry_info;
138
139 MagickBooleanType
140 status;
141
142 MagickStatusType
143 flags;
144
145 /*
146 Parse levels.
147 */
148 if (levels == (char *) NULL)
149 return(MagickFalse);
150 flags=ParseGeometry(levels,&geometry_info);
151 black_point=geometry_info.rho;
152 white_point=(double) QuantumRange;
153 if ((flags & SigmaValue) != 0)
154 white_point=geometry_info.sigma;
155 gamma=1.0;
156 if ((flags & XiValue) != 0)
157 gamma=geometry_info.xi;
158 if ((flags & PercentValue) != 0)
159 {
160 black_point*=(double) image->columns*image->rows/100.0;
161 white_point*=(double) image->columns*image->rows/100.0;
162 }
163 if ((flags & SigmaValue) == 0)
164 white_point=(double) QuantumRange-black_point;
165 if ((flags & AspectValue ) == 0)
cristy7c0a0a42011-08-23 17:57:25 +0000166 status=LevelImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000167 else
cristy7c0a0a42011-08-23 17:57:25 +0000168 status=LevelizeImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000169 return(status);
170}
171
cristy4282c702011-11-21 00:01:06 +0000172MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
173 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000174{
175#define AdaptiveBlurImageTag "Convolve/Image"
176#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
177
cristyc4c8d132010-01-07 01:58:38 +0000178 CacheView
179 *blur_view,
180 *edge_view,
181 *image_view;
182
cristy3ed852e2009-09-05 21:47:34 +0000183 double
cristy47e00502009-12-17 19:19:57 +0000184 **kernel,
185 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000186
187 Image
188 *blur_image,
189 *edge_image,
190 *gaussian_image;
191
cristy3ed852e2009-09-05 21:47:34 +0000192 MagickBooleanType
193 status;
194
cristybb503372010-05-27 20:51:26 +0000195 MagickOffsetType
196 progress;
197
cristybb503372010-05-27 20:51:26 +0000198 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000199 i;
cristy3ed852e2009-09-05 21:47:34 +0000200
cristybb503372010-05-27 20:51:26 +0000201 size_t
cristy3ed852e2009-09-05 21:47:34 +0000202 width;
203
cristybb503372010-05-27 20:51:26 +0000204 ssize_t
205 j,
206 k,
207 u,
208 v,
209 y;
210
cristy3ed852e2009-09-05 21:47:34 +0000211 assert(image != (const Image *) NULL);
212 assert(image->signature == MagickSignature);
213 if (image->debug != MagickFalse)
214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
215 assert(exception != (ExceptionInfo *) NULL);
216 assert(exception->signature == MagickSignature);
217 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
218 if (blur_image == (Image *) NULL)
219 return((Image *) NULL);
220 if (fabs(sigma) <= MagickEpsilon)
221 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000222 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000223 {
cristy3ed852e2009-09-05 21:47:34 +0000224 blur_image=DestroyImage(blur_image);
225 return((Image *) NULL);
226 }
227 /*
228 Edge detect the image brighness channel, level, blur, and level again.
229 */
cristy8ae632d2011-09-05 17:29:53 +0000230 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000231 if (edge_image == (Image *) NULL)
232 {
233 blur_image=DestroyImage(blur_image);
234 return((Image *) NULL);
235 }
cristy051718b2011-08-28 22:49:25 +0000236 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristyd89705a2012-01-20 02:52:24 +0000237 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000238 if (gaussian_image != (Image *) NULL)
239 {
240 edge_image=DestroyImage(edge_image);
241 edge_image=gaussian_image;
242 }
cristy051718b2011-08-28 22:49:25 +0000243 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000244 /*
245 Create a set of kernels from maximum (radius,sigma) to minimum.
246 */
247 width=GetOptimalKernelWidth2D(radius,sigma);
cristy219a63a2012-01-17 12:30:33 +0000248 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000249 if (kernel == (double **) NULL)
250 {
251 edge_image=DestroyImage(edge_image);
252 blur_image=DestroyImage(blur_image);
253 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
254 }
255 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000256 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000257 {
cristyf5d0f3e2012-01-17 03:20:33 +0000258 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
cristy3ed852e2009-09-05 21:47:34 +0000259 sizeof(**kernel));
260 if (kernel[i] == (double *) NULL)
261 break;
cristy47e00502009-12-17 19:19:57 +0000262 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000263 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000264 k=0;
265 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000266 {
cristy47e00502009-12-17 19:19:57 +0000267 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000268 {
cristy4205a3c2010-09-12 20:19:59 +0000269 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
270 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000271 normalize+=kernel[i][k];
272 k++;
cristy3ed852e2009-09-05 21:47:34 +0000273 }
274 }
cristy3ed852e2009-09-05 21:47:34 +0000275 if (fabs(normalize) <= MagickEpsilon)
276 normalize=1.0;
277 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000278 for (k=0; k < (j*j); k++)
279 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000280 }
cristybb503372010-05-27 20:51:26 +0000281 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000282 {
283 for (i-=2; i >= 0; i-=2)
cristyf5d0f3e2012-01-17 03:20:33 +0000284 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000285 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000286 edge_image=DestroyImage(edge_image);
287 blur_image=DestroyImage(blur_image);
288 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
289 }
290 /*
291 Adaptively blur image.
292 */
293 status=MagickTrue;
294 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000295 image_view=AcquireCacheView(image);
296 edge_view=AcquireCacheView(edge_image);
297 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000298#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000299 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000300#endif
cristybb503372010-05-27 20:51:26 +0000301 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
cristy4c08aed2011-07-01 19:47:50 +0000303 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000304 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000305
cristy4c08aed2011-07-01 19:47:50 +0000306 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000307 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000308
cristy117ff172010-08-15 21:35:32 +0000309 register ssize_t
310 x;
311
cristy3ed852e2009-09-05 21:47:34 +0000312 if (status == MagickFalse)
313 continue;
314 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
315 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
316 exception);
cristyacd2ed22011-08-30 01:44:23 +0000317 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000318 {
319 status=MagickFalse;
320 continue;
321 }
cristybb503372010-05-27 20:51:26 +0000322 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000323 {
cristy4c11c2b2011-09-05 20:17:07 +0000324 register const Quantum
325 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000326
cristybb503372010-05-27 20:51:26 +0000327 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000328 i;
cristy3ed852e2009-09-05 21:47:34 +0000329
cristy4c11c2b2011-09-05 20:17:07 +0000330 ssize_t
331 center,
332 j;
333
334 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000335 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000336 if (j < 0)
337 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000338 else
cristy4c11c2b2011-09-05 20:17:07 +0000339 if (j > (ssize_t) width)
340 j=(ssize_t) width;
341 if ((j & 0x01) != 0)
342 j--;
343 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
344 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000345 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000346 break;
cristy4c11c2b2011-09-05 20:17:07 +0000347 center=(ssize_t) GetPixelChannels(image)*(width-j)*
cristy075ff302011-09-07 01:51:24 +0000348 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2L);
cristy4c11c2b2011-09-05 20:17:07 +0000349 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristy4c11c2b2011-09-05 20:17:07 +0000351 MagickRealType
352 alpha,
353 gamma,
354 pixel;
355
356 PixelChannel
357 channel;
358
359 PixelTrait
360 blur_traits,
361 traits;
362
363 register const double
364 *restrict k;
365
366 register const Quantum
367 *restrict pixels;
368
369 register ssize_t
370 u;
371
372 ssize_t
373 v;
374
cristye2a912b2011-12-05 20:02:07 +0000375 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000376 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000377 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
378 if ((traits == UndefinedPixelTrait) ||
379 (blur_traits == UndefinedPixelTrait))
380 continue;
381 if ((blur_traits & CopyPixelTrait) != 0)
382 {
cristy0beccfa2011-09-25 20:47:53 +0000383 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000384 continue;
385 }
386 k=kernel[j];
387 pixels=p;
388 pixel=bias;
389 gamma=0.0;
390 if ((blur_traits & BlendPixelTrait) == 0)
391 {
392 /*
393 No alpha blending.
394 */
395 for (v=0; v < (ssize_t) (width-j); v++)
396 {
397 for (u=0; u < (ssize_t) (width-j); u++)
398 {
399 pixel+=(*k)*pixels[i];
400 gamma+=(*k);
401 k++;
402 pixels+=GetPixelChannels(image);
403 }
404 }
405 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000406 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000407 continue;
408 }
409 /*
410 Alpha blending.
411 */
412 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000413 {
cristy4c11c2b2011-09-05 20:17:07 +0000414 for (u=0; u < (ssize_t) (width-j); u++)
415 {
416 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
417 pixel+=(*k)*alpha*pixels[i];
418 gamma+=(*k)*alpha;
419 k++;
420 pixels+=GetPixelChannels(image);
421 }
cristy3ed852e2009-09-05 21:47:34 +0000422 }
cristy4c11c2b2011-09-05 20:17:07 +0000423 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000424 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000425 }
cristyed231572011-07-14 02:18:59 +0000426 q+=GetPixelChannels(blur_image);
427 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000428 }
429 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
430 status=MagickFalse;
431 if (image->progress_monitor != (MagickProgressMonitor) NULL)
432 {
433 MagickBooleanType
434 proceed;
435
cristyb5d5f722009-11-04 03:03:49 +0000436#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8ae632d2011-09-05 17:29:53 +0000437 #pragma omp critical (MagickCore_AdaptiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +0000438#endif
439 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
440 image->rows);
441 if (proceed == MagickFalse)
442 status=MagickFalse;
443 }
444 }
445 blur_image->type=image->type;
446 blur_view=DestroyCacheView(blur_view);
447 edge_view=DestroyCacheView(edge_view);
448 image_view=DestroyCacheView(image_view);
449 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000450 for (i=0; i < (ssize_t) width; i+=2)
cristyf5d0f3e2012-01-17 03:20:33 +0000451 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000452 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000453 if (status == MagickFalse)
454 blur_image=DestroyImage(blur_image);
455 return(blur_image);
456}
457
458/*
459%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460% %
461% %
462% %
463% A d a p t i v e S h a r p e n I m a g e %
464% %
465% %
466% %
467%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468%
469% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
470% intensely near image edges and less intensely far from edges. We sharpen the
471% image with a Gaussian operator of the given radius and standard deviation
472% (sigma). For reasonable results, radius should be larger than sigma. Use a
473% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
474%
475% The format of the AdaptiveSharpenImage method is:
476%
477% Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000478% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000479%
480% A description of each parameter follows:
481%
482% o image: the image.
483%
cristy3ed852e2009-09-05 21:47:34 +0000484% o radius: the radius of the Gaussian, in pixels, not counting the center
485% pixel.
486%
487% o sigma: the standard deviation of the Laplacian, in pixels.
488%
cristy4c11c2b2011-09-05 20:17:07 +0000489% o bias: the bias.
490%
cristy3ed852e2009-09-05 21:47:34 +0000491% o exception: return any errors or warnings in this structure.
492%
493*/
cristy3ed852e2009-09-05 21:47:34 +0000494MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000495 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000496{
cristy3ed852e2009-09-05 21:47:34 +0000497#define AdaptiveSharpenImageTag "Convolve/Image"
498#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
499
cristyc4c8d132010-01-07 01:58:38 +0000500 CacheView
501 *sharp_view,
502 *edge_view,
503 *image_view;
504
cristy3ed852e2009-09-05 21:47:34 +0000505 double
cristy47e00502009-12-17 19:19:57 +0000506 **kernel,
507 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000508
509 Image
510 *sharp_image,
511 *edge_image,
512 *gaussian_image;
513
cristy3ed852e2009-09-05 21:47:34 +0000514 MagickBooleanType
515 status;
516
cristybb503372010-05-27 20:51:26 +0000517 MagickOffsetType
518 progress;
519
cristybb503372010-05-27 20:51:26 +0000520 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000521 i;
cristy3ed852e2009-09-05 21:47:34 +0000522
cristybb503372010-05-27 20:51:26 +0000523 size_t
cristy3ed852e2009-09-05 21:47:34 +0000524 width;
525
cristybb503372010-05-27 20:51:26 +0000526 ssize_t
527 j,
528 k,
529 u,
530 v,
531 y;
532
cristy3ed852e2009-09-05 21:47:34 +0000533 assert(image != (const Image *) NULL);
534 assert(image->signature == MagickSignature);
535 if (image->debug != MagickFalse)
536 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
537 assert(exception != (ExceptionInfo *) NULL);
538 assert(exception->signature == MagickSignature);
539 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
540 if (sharp_image == (Image *) NULL)
541 return((Image *) NULL);
542 if (fabs(sigma) <= MagickEpsilon)
543 return(sharp_image);
cristy574cc262011-08-05 01:23:58 +0000544 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
cristy3ed852e2009-09-05 21:47:34 +0000546 sharp_image=DestroyImage(sharp_image);
547 return((Image *) NULL);
548 }
549 /*
550 Edge detect the image brighness channel, level, sharp, and level again.
551 */
cristy8ae632d2011-09-05 17:29:53 +0000552 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000553 if (edge_image == (Image *) NULL)
554 {
555 sharp_image=DestroyImage(sharp_image);
556 return((Image *) NULL);
557 }
cristy051718b2011-08-28 22:49:25 +0000558 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristyd89705a2012-01-20 02:52:24 +0000559 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000560 if (gaussian_image != (Image *) NULL)
561 {
562 edge_image=DestroyImage(edge_image);
563 edge_image=gaussian_image;
564 }
cristy051718b2011-08-28 22:49:25 +0000565 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000566 /*
567 Create a set of kernels from maximum (radius,sigma) to minimum.
568 */
569 width=GetOptimalKernelWidth2D(radius,sigma);
cristy219a63a2012-01-17 12:30:33 +0000570 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000571 if (kernel == (double **) NULL)
572 {
573 edge_image=DestroyImage(edge_image);
574 sharp_image=DestroyImage(sharp_image);
575 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
576 }
577 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000578 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000579 {
cristy59d1f6d2012-01-17 03:17:40 +0000580 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
cristy3ed852e2009-09-05 21:47:34 +0000581 sizeof(**kernel));
582 if (kernel[i] == (double *) NULL)
583 break;
cristy47e00502009-12-17 19:19:57 +0000584 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000585 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000586 k=0;
587 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000588 {
cristy47e00502009-12-17 19:19:57 +0000589 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000590 {
cristy4205a3c2010-09-12 20:19:59 +0000591 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
592 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000593 normalize+=kernel[i][k];
594 k++;
cristy3ed852e2009-09-05 21:47:34 +0000595 }
596 }
cristy3ed852e2009-09-05 21:47:34 +0000597 if (fabs(normalize) <= MagickEpsilon)
598 normalize=1.0;
599 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000600 for (k=0; k < (j*j); k++)
601 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000602 }
cristybb503372010-05-27 20:51:26 +0000603 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000604 {
605 for (i-=2; i >= 0; i-=2)
cristy59d1f6d2012-01-17 03:17:40 +0000606 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000607 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000608 edge_image=DestroyImage(edge_image);
609 sharp_image=DestroyImage(sharp_image);
610 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
611 }
612 /*
613 Adaptively sharpen image.
614 */
615 status=MagickTrue;
616 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000617 image_view=AcquireCacheView(image);
618 edge_view=AcquireCacheView(edge_image);
619 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000620#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000621 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000622#endif
cristybb503372010-05-27 20:51:26 +0000623 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000624 {
cristy4c08aed2011-07-01 19:47:50 +0000625 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000626 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000627
cristy4c08aed2011-07-01 19:47:50 +0000628 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000629 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000630
cristy117ff172010-08-15 21:35:32 +0000631 register ssize_t
632 x;
633
cristy3ed852e2009-09-05 21:47:34 +0000634 if (status == MagickFalse)
635 continue;
636 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
637 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
638 exception);
cristy4c08aed2011-07-01 19:47:50 +0000639 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000640 {
641 status=MagickFalse;
642 continue;
643 }
cristybb503372010-05-27 20:51:26 +0000644 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000645 {
cristy4c11c2b2011-09-05 20:17:07 +0000646 register const Quantum
647 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000648
cristybb503372010-05-27 20:51:26 +0000649 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000650 i;
cristy3ed852e2009-09-05 21:47:34 +0000651
cristy4c11c2b2011-09-05 20:17:07 +0000652 ssize_t
653 center,
654 j;
655
656 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000657 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000658 if (j < 0)
659 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000660 else
cristy4c11c2b2011-09-05 20:17:07 +0000661 if (j > (ssize_t) width)
662 j=(ssize_t) width;
663 if ((j & 0x01) != 0)
664 j--;
665 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
666 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000667 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000668 break;
cristy4c11c2b2011-09-05 20:17:07 +0000669 center=(ssize_t) GetPixelChannels(image)*(width-j)*
670 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2);
671 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000672 {
cristy4c11c2b2011-09-05 20:17:07 +0000673 MagickRealType
674 alpha,
675 gamma,
676 pixel;
677
678 PixelChannel
679 channel;
680
681 PixelTrait
682 sharp_traits,
683 traits;
684
685 register const double
686 *restrict k;
687
688 register const Quantum
689 *restrict pixels;
690
691 register ssize_t
692 u;
693
694 ssize_t
695 v;
696
cristye2a912b2011-12-05 20:02:07 +0000697 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000698 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000699 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
700 if ((traits == UndefinedPixelTrait) ||
701 (sharp_traits == UndefinedPixelTrait))
702 continue;
703 if ((sharp_traits & CopyPixelTrait) != 0)
704 {
cristy0beccfa2011-09-25 20:47:53 +0000705 SetPixelChannel(sharp_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000706 continue;
707 }
708 k=kernel[j];
709 pixels=p;
710 pixel=bias;
711 gamma=0.0;
712 if ((sharp_traits & BlendPixelTrait) == 0)
713 {
714 /*
715 No alpha blending.
716 */
717 for (v=0; v < (ssize_t) (width-j); v++)
718 {
719 for (u=0; u < (ssize_t) (width-j); u++)
720 {
721 pixel+=(*k)*pixels[i];
722 gamma+=(*k);
723 k++;
724 pixels+=GetPixelChannels(image);
725 }
726 }
727 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000728 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000729 continue;
730 }
731 /*
732 Alpha blending.
733 */
734 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000735 {
cristy4c11c2b2011-09-05 20:17:07 +0000736 for (u=0; u < (ssize_t) (width-j); u++)
737 {
738 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
739 pixel+=(*k)*alpha*pixels[i];
740 gamma+=(*k)*alpha;
741 k++;
742 pixels+=GetPixelChannels(image);
743 }
cristy3ed852e2009-09-05 21:47:34 +0000744 }
cristy4c11c2b2011-09-05 20:17:07 +0000745 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000746 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000747 }
cristyed231572011-07-14 02:18:59 +0000748 q+=GetPixelChannels(sharp_image);
749 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000750 }
751 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
752 status=MagickFalse;
753 if (image->progress_monitor != (MagickProgressMonitor) NULL)
754 {
755 MagickBooleanType
756 proceed;
757
cristyb5d5f722009-11-04 03:03:49 +0000758#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +0000759 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000760#endif
761 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
762 image->rows);
763 if (proceed == MagickFalse)
764 status=MagickFalse;
765 }
766 }
767 sharp_image->type=image->type;
768 sharp_view=DestroyCacheView(sharp_view);
769 edge_view=DestroyCacheView(edge_view);
770 image_view=DestroyCacheView(image_view);
771 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000772 for (i=0; i < (ssize_t) width; i+=2)
cristy59d1f6d2012-01-17 03:17:40 +0000773 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000774 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000775 if (status == MagickFalse)
776 sharp_image=DestroyImage(sharp_image);
777 return(sharp_image);
778}
779
780/*
781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782% %
783% %
784% %
785% B l u r I m a g e %
786% %
787% %
788% %
789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790%
791% BlurImage() blurs an image. We convolve the image with a Gaussian operator
792% of the given radius and standard deviation (sigma). For reasonable results,
793% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
794% selects a suitable radius for you.
795%
796% BlurImage() differs from GaussianBlurImage() in that it uses a separable
797% kernel which is faster but mathematically equivalent to the non-separable
798% kernel.
799%
800% The format of the BlurImage method is:
801%
802% Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000803% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000804%
805% A description of each parameter follows:
806%
807% o image: the image.
808%
cristy3ed852e2009-09-05 21:47:34 +0000809% o radius: the radius of the Gaussian, in pixels, not counting the center
810% pixel.
811%
812% o sigma: the standard deviation of the Gaussian, in pixels.
813%
cristy05c0c9a2011-09-05 23:16:13 +0000814% o bias: the bias.
815%
cristy3ed852e2009-09-05 21:47:34 +0000816% o exception: return any errors or warnings in this structure.
817%
818*/
819
cristybb503372010-05-27 20:51:26 +0000820static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000821{
cristy3ed852e2009-09-05 21:47:34 +0000822 double
cristy47e00502009-12-17 19:19:57 +0000823 *kernel,
824 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000825
cristy117ff172010-08-15 21:35:32 +0000826 register ssize_t
827 i;
828
cristybb503372010-05-27 20:51:26 +0000829 ssize_t
cristy47e00502009-12-17 19:19:57 +0000830 j,
831 k;
cristy3ed852e2009-09-05 21:47:34 +0000832
cristy3ed852e2009-09-05 21:47:34 +0000833 /*
834 Generate a 1-D convolution kernel.
835 */
836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +0000837 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000838 if (kernel == (double *) NULL)
839 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000840 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000841 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000842 i=0;
843 for (k=(-j); k <= j; k++)
844 {
cristy4205a3c2010-09-12 20:19:59 +0000845 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
846 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000847 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000848 i++;
849 }
cristybb503372010-05-27 20:51:26 +0000850 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000851 kernel[i]/=normalize;
852 return(kernel);
853}
854
cristyf4ad9df2011-07-08 16:49:03 +0000855MagickExport Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000856 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000857{
858#define BlurImageTag "Blur/Image"
859
cristyc4c8d132010-01-07 01:58:38 +0000860 CacheView
861 *blur_view,
862 *image_view;
863
cristy3ed852e2009-09-05 21:47:34 +0000864 double
865 *kernel;
866
867 Image
868 *blur_image;
869
cristy3ed852e2009-09-05 21:47:34 +0000870 MagickBooleanType
871 status;
872
cristybb503372010-05-27 20:51:26 +0000873 MagickOffsetType
874 progress;
875
cristybb503372010-05-27 20:51:26 +0000876 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000877 i;
878
cristybb503372010-05-27 20:51:26 +0000879 size_t
cristy3ed852e2009-09-05 21:47:34 +0000880 width;
881
cristybb503372010-05-27 20:51:26 +0000882 ssize_t
cristyb41a1172011-09-06 00:55:14 +0000883 center,
cristybb503372010-05-27 20:51:26 +0000884 x,
885 y;
886
cristy3ed852e2009-09-05 21:47:34 +0000887 /*
888 Initialize blur image attributes.
889 */
890 assert(image != (Image *) NULL);
891 assert(image->signature == MagickSignature);
892 if (image->debug != MagickFalse)
893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
894 assert(exception != (ExceptionInfo *) NULL);
895 assert(exception->signature == MagickSignature);
cristyd25c77e2011-09-06 00:10:24 +0000896 blur_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000897 if (blur_image == (Image *) NULL)
898 return((Image *) NULL);
899 if (fabs(sigma) <= MagickEpsilon)
900 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000901 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000902 {
cristy3ed852e2009-09-05 21:47:34 +0000903 blur_image=DestroyImage(blur_image);
904 return((Image *) NULL);
905 }
906 width=GetOptimalKernelWidth1D(radius,sigma);
907 kernel=GetBlurKernel(width,sigma);
908 if (kernel == (double *) NULL)
909 {
910 blur_image=DestroyImage(blur_image);
911 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
912 }
913 if (image->debug != MagickFalse)
914 {
915 char
916 format[MaxTextExtent],
917 *message;
918
919 register const double
920 *k;
921
922 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristyef1f9072012-01-20 14:43:06 +0000923 " blur image with kernel width %.20g:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000924 message=AcquireString("");
925 k=kernel;
cristybb503372010-05-27 20:51:26 +0000926 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000927 {
928 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000929 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000930 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000931 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000932 (void) ConcatenateString(&message,format);
933 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
934 }
935 message=DestroyString(message);
936 }
937 /*
938 Blur rows.
939 */
940 status=MagickTrue;
941 progress=0;
cristyb41a1172011-09-06 00:55:14 +0000942 center=(ssize_t) GetPixelChannels(image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +0000943 image_view=AcquireCacheView(image);
944 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000945#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000946 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000947#endif
cristyb41a1172011-09-06 00:55:14 +0000948 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000949 {
cristy4c08aed2011-07-01 19:47:50 +0000950 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000951 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000952
cristy4c08aed2011-07-01 19:47:50 +0000953 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000954 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000955
cristy117ff172010-08-15 21:35:32 +0000956 register ssize_t
957 x;
958
cristy3ed852e2009-09-05 21:47:34 +0000959 if (status == MagickFalse)
960 continue;
cristy117ff172010-08-15 21:35:32 +0000961 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
962 image->columns+width,1,exception);
cristy8b49f382012-01-17 03:11:03 +0000963 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000964 exception);
cristy4c08aed2011-07-01 19:47:50 +0000965 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000966 {
967 status=MagickFalse;
968 continue;
969 }
cristyb41a1172011-09-06 00:55:14 +0000970 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000971 {
cristybb503372010-05-27 20:51:26 +0000972 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000973 i;
974
cristyb41a1172011-09-06 00:55:14 +0000975 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
976 {
977 MagickRealType
978 alpha,
979 gamma,
980 pixel;
cristyd25c77e2011-09-06 00:10:24 +0000981
cristyb41a1172011-09-06 00:55:14 +0000982 PixelChannel
983 channel;
984
985 PixelTrait
986 blur_traits,
987 traits;
988
989 register const double
990 *restrict k;
991
992 register const Quantum
993 *restrict pixels;
994
995 register ssize_t
996 u;
997
cristye2a912b2011-12-05 20:02:07 +0000998 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000999 traits=GetPixelChannelMapTraits(image,channel);
cristyb41a1172011-09-06 00:55:14 +00001000 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1001 if ((traits == UndefinedPixelTrait) ||
1002 (blur_traits == UndefinedPixelTrait))
1003 continue;
1004 if ((blur_traits & CopyPixelTrait) != 0)
cristyd25c77e2011-09-06 00:10:24 +00001005 {
cristy0beccfa2011-09-25 20:47:53 +00001006 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001007 continue;
cristyd25c77e2011-09-06 00:10:24 +00001008 }
cristyb41a1172011-09-06 00:55:14 +00001009 k=kernel;
1010 pixels=p;
1011 pixel=0.0;
1012 if ((blur_traits & BlendPixelTrait) == 0)
1013 {
1014 /*
1015 No alpha blending.
1016 */
1017 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001018 {
cristyb41a1172011-09-06 00:55:14 +00001019 pixel+=(*k)*pixels[i];
1020 k++;
1021 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001022 }
cristy0beccfa2011-09-25 20:47:53 +00001023 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001024 continue;
1025 }
1026 /*
1027 Alpha blending.
1028 */
1029 gamma=0.0;
1030 for (u=0; u < (ssize_t) width; u++)
1031 {
1032 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1033 pixel+=(*k)*alpha*pixels[i];
1034 gamma+=(*k)*alpha;
1035 k++;
1036 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001037 }
cristyb41a1172011-09-06 00:55:14 +00001038 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001039 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001040 }
cristyed231572011-07-14 02:18:59 +00001041 p+=GetPixelChannels(image);
1042 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001043 }
1044 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1045 status=MagickFalse;
1046 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1047 {
1048 MagickBooleanType
1049 proceed;
1050
cristyb5d5f722009-11-04 03:03:49 +00001051#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyef1f9072012-01-20 14:43:06 +00001052 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001053#endif
1054 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1055 blur_image->columns);
1056 if (proceed == MagickFalse)
1057 status=MagickFalse;
1058 }
1059 }
1060 blur_view=DestroyCacheView(blur_view);
1061 image_view=DestroyCacheView(image_view);
1062 /*
1063 Blur columns.
1064 */
cristyef1f9072012-01-20 14:43:06 +00001065 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +00001066 image_view=AcquireCacheView(blur_image);
1067 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001068#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001069 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001070#endif
cristyb6dc4b72011-12-10 23:29:53 +00001071 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001072 {
cristy4c08aed2011-07-01 19:47:50 +00001073 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001074 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001075
cristy4c08aed2011-07-01 19:47:50 +00001076 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001077 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001078
cristy117ff172010-08-15 21:35:32 +00001079 register ssize_t
1080 y;
1081
cristy3ed852e2009-09-05 21:47:34 +00001082 if (status == MagickFalse)
1083 continue;
cristy117ff172010-08-15 21:35:32 +00001084 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
cristyb6dc4b72011-12-10 23:29:53 +00001085 blur_image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001086 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001087 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001088 {
1089 status=MagickFalse;
1090 continue;
1091 }
cristyb6dc4b72011-12-10 23:29:53 +00001092 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001093 {
cristybb503372010-05-27 20:51:26 +00001094 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001095 i;
1096
cristyb6dc4b72011-12-10 23:29:53 +00001097 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
cristyb41a1172011-09-06 00:55:14 +00001098 {
1099 MagickRealType
1100 alpha,
1101 gamma,
1102 pixel;
cristyd25c77e2011-09-06 00:10:24 +00001103
cristyb41a1172011-09-06 00:55:14 +00001104 PixelChannel
1105 channel;
1106
1107 PixelTrait
1108 blur_traits,
1109 traits;
1110
1111 register const double
1112 *restrict k;
1113
1114 register const Quantum
1115 *restrict pixels;
1116
1117 register ssize_t
1118 u;
1119
cristye2a912b2011-12-05 20:02:07 +00001120 channel=GetPixelChannelMapChannel(blur_image,i);
cristyabace412011-12-11 15:56:53 +00001121 traits=GetPixelChannelMapTraits(blur_image,channel);
cristyb41a1172011-09-06 00:55:14 +00001122 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1123 if ((traits == UndefinedPixelTrait) ||
1124 (blur_traits == UndefinedPixelTrait))
1125 continue;
1126 if ((blur_traits & CopyPixelTrait) != 0)
cristyd25c77e2011-09-06 00:10:24 +00001127 {
cristy0beccfa2011-09-25 20:47:53 +00001128 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001129 continue;
cristyd25c77e2011-09-06 00:10:24 +00001130 }
cristyb41a1172011-09-06 00:55:14 +00001131 k=kernel;
1132 pixels=p;
1133 pixel=0.0;
1134 if ((blur_traits & BlendPixelTrait) == 0)
1135 {
1136 /*
1137 No alpha blending.
1138 */
1139 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001140 {
cristyb41a1172011-09-06 00:55:14 +00001141 pixel+=(*k)*pixels[i];
1142 k++;
1143 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001144 }
cristy0beccfa2011-09-25 20:47:53 +00001145 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001146 continue;
1147 }
1148 /*
1149 Alpha blending.
1150 */
1151 gamma=0.0;
1152 for (u=0; u < (ssize_t) width; u++)
1153 {
cristyb6dc4b72011-12-10 23:29:53 +00001154 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1155 pixels));
cristyb41a1172011-09-06 00:55:14 +00001156 pixel+=(*k)*alpha*pixels[i];
1157 gamma+=(*k)*alpha;
1158 k++;
1159 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001160 }
cristyb41a1172011-09-06 00:55:14 +00001161 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001162 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001163 }
cristyd25c77e2011-09-06 00:10:24 +00001164 p+=GetPixelChannels(blur_image);
cristyed231572011-07-14 02:18:59 +00001165 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001166 }
1167 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1168 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001169 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001170 {
1171 MagickBooleanType
1172 proceed;
1173
cristyb5d5f722009-11-04 03:03:49 +00001174#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyef1f9072012-01-20 14:43:06 +00001175 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001176#endif
cristy4c08aed2011-07-01 19:47:50 +00001177 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1178 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001179 if (proceed == MagickFalse)
1180 status=MagickFalse;
1181 }
1182 }
1183 blur_view=DestroyCacheView(blur_view);
1184 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00001185 kernel=(double *) RelinquishAlignedMemory(kernel);
1186 blur_image->type=image->type;
cristy3ed852e2009-09-05 21:47:34 +00001187 if (status == MagickFalse)
1188 blur_image=DestroyImage(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001189 return(blur_image);
1190}
1191
1192/*
1193%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1194% %
1195% %
1196% %
cristyfccdab92009-11-30 16:43:57 +00001197% C o n v o l v e I m a g e %
1198% %
1199% %
1200% %
1201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1202%
1203% ConvolveImage() applies a custom convolution kernel to the image.
1204%
1205% The format of the ConvolveImage method is:
1206%
cristy5e6be1e2011-07-16 01:23:39 +00001207% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1208% ExceptionInfo *exception)
1209%
cristyfccdab92009-11-30 16:43:57 +00001210% A description of each parameter follows:
1211%
1212% o image: the image.
1213%
cristy5e6be1e2011-07-16 01:23:39 +00001214% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001215%
1216% o exception: return any errors or warnings in this structure.
1217%
1218*/
cristy5e6be1e2011-07-16 01:23:39 +00001219MagickExport Image *ConvolveImage(const Image *image,
1220 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001221{
cristyfccdab92009-11-30 16:43:57 +00001222#define ConvolveImageTag "Convolve/Image"
1223
cristyc4c8d132010-01-07 01:58:38 +00001224 CacheView
1225 *convolve_view,
cristy105ba3c2011-07-18 02:28:38 +00001226 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00001227
cristyfccdab92009-11-30 16:43:57 +00001228 Image
1229 *convolve_image;
1230
cristyfccdab92009-11-30 16:43:57 +00001231 MagickBooleanType
1232 status;
1233
cristybb503372010-05-27 20:51:26 +00001234 MagickOffsetType
1235 progress;
1236
cristybb503372010-05-27 20:51:26 +00001237 ssize_t
cristy574cc262011-08-05 01:23:58 +00001238 center,
cristybb503372010-05-27 20:51:26 +00001239 y;
1240
cristyfccdab92009-11-30 16:43:57 +00001241 /*
1242 Initialize convolve image attributes.
1243 */
1244 assert(image != (Image *) NULL);
1245 assert(image->signature == MagickSignature);
1246 if (image->debug != MagickFalse)
1247 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1248 assert(exception != (ExceptionInfo *) NULL);
1249 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001250 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001251 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001252 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1253 exception);
cristyfccdab92009-11-30 16:43:57 +00001254 if (convolve_image == (Image *) NULL)
1255 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001256 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001257 {
cristyfccdab92009-11-30 16:43:57 +00001258 convolve_image=DestroyImage(convolve_image);
1259 return((Image *) NULL);
1260 }
1261 if (image->debug != MagickFalse)
1262 {
1263 char
1264 format[MaxTextExtent],
1265 *message;
1266
cristya96f2492011-12-14 18:25:41 +00001267 register const MagickRealType
cristy117ff172010-08-15 21:35:32 +00001268 *k;
1269
cristy4e154852011-07-14 13:28:53 +00001270 register ssize_t
1271 u;
1272
cristybb503372010-05-27 20:51:26 +00001273 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001274 v;
1275
cristyfccdab92009-11-30 16:43:57 +00001276 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001277 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1278 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001279 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001280 k=kernel_info->values;
1281 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001282 {
1283 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001284 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001285 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001286 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001287 {
cristyb51dff52011-05-19 16:55:47 +00001288 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001289 (void) ConcatenateString(&message,format);
1290 }
1291 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1292 }
1293 message=DestroyString(message);
1294 }
cristy7ea27962011-09-12 18:12:49 +00001295 status=AccelerateConvolveImage(image,kernel_info,convolve_image,exception);
1296 if (status == MagickTrue)
1297 return(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001298 /*
cristyfccdab92009-11-30 16:43:57 +00001299 Convolve image.
anthonyfd706f92012-01-19 04:22:02 +00001300 FUTURE: Use Morphology Convolve instead.
cristyfccdab92009-11-30 16:43:57 +00001301 */
cristy574cc262011-08-05 01:23:58 +00001302 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
cristy075ff302011-09-07 01:51:24 +00001303 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2L);
cristyfccdab92009-11-30 16:43:57 +00001304 status=MagickTrue;
1305 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001306 image_view=AcquireCacheView(image);
1307 convolve_view=AcquireCacheView(convolve_image);
1308#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd36a25e2012-01-18 14:30:53 +00001309 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001310#endif
cristybb503372010-05-27 20:51:26 +00001311 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001312 {
cristy4c08aed2011-07-01 19:47:50 +00001313 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001314 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001315
cristy4c08aed2011-07-01 19:47:50 +00001316 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001317 *restrict q;
1318
cristy117ff172010-08-15 21:35:32 +00001319 register ssize_t
1320 x;
1321
cristyfccdab92009-11-30 16:43:57 +00001322 if (status == MagickFalse)
1323 continue;
cristy105ba3c2011-07-18 02:28:38 +00001324 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1325 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1326 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001327 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001328 exception);
cristy105ba3c2011-07-18 02:28:38 +00001329 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001330 {
1331 status=MagickFalse;
1332 continue;
1333 }
cristybb503372010-05-27 20:51:26 +00001334 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001335 {
cristybb503372010-05-27 20:51:26 +00001336 register ssize_t
cristyed231572011-07-14 02:18:59 +00001337 i;
cristyfccdab92009-11-30 16:43:57 +00001338
cristya30d9ba2011-07-23 21:00:48 +00001339 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001340 {
cristyed231572011-07-14 02:18:59 +00001341 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001342 alpha,
1343 gamma,
cristyed231572011-07-14 02:18:59 +00001344 pixel;
1345
1346 PixelChannel
1347 channel;
1348
1349 PixelTrait
1350 convolve_traits,
1351 traits;
1352
cristya96f2492011-12-14 18:25:41 +00001353 register const MagickRealType
cristyed231572011-07-14 02:18:59 +00001354 *restrict k;
1355
1356 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001357 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001358
1359 register ssize_t
1360 u;
1361
1362 ssize_t
1363 v;
1364
cristye2a912b2011-12-05 20:02:07 +00001365 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001366 traits=GetPixelChannelMapTraits(image,channel);
cristy4e154852011-07-14 13:28:53 +00001367 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001368 if ((traits == UndefinedPixelTrait) ||
1369 (convolve_traits == UndefinedPixelTrait))
cristy4e154852011-07-14 13:28:53 +00001370 continue;
1371 if ((convolve_traits & CopyPixelTrait) != 0)
1372 {
cristy0beccfa2011-09-25 20:47:53 +00001373 SetPixelChannel(convolve_image,channel,p[center+i],q);
cristy4e154852011-07-14 13:28:53 +00001374 continue;
1375 }
cristy5e6be1e2011-07-16 01:23:39 +00001376 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001377 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001378 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001379 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001380 {
cristyed231572011-07-14 02:18:59 +00001381 /*
cristy4e154852011-07-14 13:28:53 +00001382 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001383 */
cristyeb52cde2011-07-17 01:52:52 +00001384 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001385 {
cristyeb52cde2011-07-17 01:52:52 +00001386 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001387 {
cristyeb52cde2011-07-17 01:52:52 +00001388 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001389 k++;
cristya30d9ba2011-07-23 21:00:48 +00001390 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001391 }
cristya30d9ba2011-07-23 21:00:48 +00001392 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001393 }
cristy0beccfa2011-09-25 20:47:53 +00001394 SetPixelChannel(convolve_image,channel,ClampToQuantum(pixel),q);
cristy4e154852011-07-14 13:28:53 +00001395 continue;
cristyed231572011-07-14 02:18:59 +00001396 }
cristy4e154852011-07-14 13:28:53 +00001397 /*
1398 Alpha blending.
1399 */
1400 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001401 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001402 {
cristyeb52cde2011-07-17 01:52:52 +00001403 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001404 {
cristyeb52cde2011-07-17 01:52:52 +00001405 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1406 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001407 gamma+=(*k)*alpha;
1408 k++;
cristya30d9ba2011-07-23 21:00:48 +00001409 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001410 }
cristya30d9ba2011-07-23 21:00:48 +00001411 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001412 }
cristy1ce96d02011-07-14 17:57:24 +00001413 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001414 SetPixelChannel(convolve_image,channel,ClampToQuantum(gamma*pixel),q);
cristyed231572011-07-14 02:18:59 +00001415 }
cristya30d9ba2011-07-23 21:00:48 +00001416 p+=GetPixelChannels(image);
1417 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001418 }
cristyed231572011-07-14 02:18:59 +00001419 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001420 status=MagickFalse;
1421 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1422 {
1423 MagickBooleanType
1424 proceed;
1425
1426#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyef1f9072012-01-20 14:43:06 +00001427 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001428#endif
1429 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1430 if (proceed == MagickFalse)
1431 status=MagickFalse;
1432 }
1433 }
1434 convolve_image->type=image->type;
1435 convolve_view=DestroyCacheView(convolve_view);
1436 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001437 if (status == MagickFalse)
1438 convolve_image=DestroyImage(convolve_image);
1439 return(convolve_image);
1440}
1441
1442/*
1443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1444% %
1445% %
1446% %
cristy3ed852e2009-09-05 21:47:34 +00001447% D e s p e c k l e I m a g e %
1448% %
1449% %
1450% %
1451%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1452%
1453% DespeckleImage() reduces the speckle noise in an image while perserving the
cristyf0ae7762012-01-15 22:04:03 +00001454% edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1455% neighbors, then complementarily lowering pixels that are brighter than their
1456% surrounding neighbors) to reduce the speckle index of that image (reference
1457% Crimmins speckle removal).
cristy3ed852e2009-09-05 21:47:34 +00001458%
1459% The format of the DespeckleImage method is:
1460%
1461% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1462%
1463% A description of each parameter follows:
1464%
1465% o image: the image.
1466%
1467% o exception: return any errors or warnings in this structure.
1468%
1469*/
1470
cristy5473bf02012-01-16 19:15:08 +00001471static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1472 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1473 Quantum *restrict g)
cristy3ed852e2009-09-05 21:47:34 +00001474{
cristy7b4b8862012-01-16 19:52:19 +00001475 register Quantum
cristy5473bf02012-01-16 19:15:08 +00001476 *p,
1477 *q,
1478 *r,
1479 *s;
cristy78f3de22012-01-16 00:43:18 +00001480
cristy5473bf02012-01-16 19:15:08 +00001481 ssize_t
1482 y;
1483
1484 assert(f != (Quantum *) NULL);
1485 assert(g != (Quantum *) NULL);
1486 p=f+(columns+2);
1487 q=g+(columns+2);
1488 r=p+(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001489#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8de86be2012-01-20 02:05:06 +00001490 #pragma omp parallel for schedule(static)
cristy5473bf02012-01-16 19:15:08 +00001491#endif
1492 for (y=0; y < (ssize_t) rows; y++)
1493 {
cristy7b4b8862012-01-16 19:52:19 +00001494 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001495 i,
1496 x;
1497
cristy7b4b8862012-01-16 19:52:19 +00001498 SignedQuantum
1499 v;
1500
cristy5473bf02012-01-16 19:15:08 +00001501 i=(2*y+1)+y*columns;
1502 if (polarity > 0)
1503 for (x=0; x < (ssize_t) columns; x++)
1504 {
1505 v=(SignedQuantum) p[i];
1506 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1507 v+=ScaleCharToQuantum(1);
1508 q[i]=(Quantum) v;
1509 i++;
1510 }
1511 else
1512 for (x=0; x < (ssize_t) columns; x++)
1513 {
1514 v=(SignedQuantum) p[i];
1515 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1516 v-=ScaleCharToQuantum(1);
1517 q[i]=(Quantum) v;
1518 i++;
1519 }
1520 }
1521 p=f+(columns+2);
1522 q=g+(columns+2);
1523 r=q+(y_offset*(columns+2)+x_offset);
1524 s=q-(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001525#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8de86be2012-01-20 02:05:06 +00001526 #pragma omp parallel for schedule(static)
cristy5473bf02012-01-16 19:15:08 +00001527#endif
1528 for (y=0; y < (ssize_t) rows; y++)
1529 {
cristy7b4b8862012-01-16 19:52:19 +00001530 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001531 i,
1532 x;
1533
cristy7b4b8862012-01-16 19:52:19 +00001534 SignedQuantum
1535 v;
1536
cristy5473bf02012-01-16 19:15:08 +00001537 i=(2*y+1)+y*columns;
1538 if (polarity > 0)
1539 for (x=0; x < (ssize_t) columns; x++)
1540 {
1541 v=(SignedQuantum) q[i];
1542 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1543 ((SignedQuantum) r[i] > v))
1544 v+=ScaleCharToQuantum(1);
1545 p[i]=(Quantum) v;
1546 i++;
1547 }
1548 else
1549 for (x=0; x < (ssize_t) columns; x++)
1550 {
1551 v=(SignedQuantum) q[i];
1552 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1553 ((SignedQuantum) r[i] < v))
1554 v-=ScaleCharToQuantum(1);
1555 p[i]=(Quantum) v;
1556 i++;
1557 }
1558 }
cristy3ed852e2009-09-05 21:47:34 +00001559}
1560
1561MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1562{
1563#define DespeckleImageTag "Despeckle/Image"
1564
cristy2407fc22009-09-11 00:55:25 +00001565 CacheView
1566 *despeckle_view,
1567 *image_view;
1568
cristy3ed852e2009-09-05 21:47:34 +00001569 Image
1570 *despeckle_image;
1571
cristy3ed852e2009-09-05 21:47:34 +00001572 MagickBooleanType
1573 status;
1574
cristy5473bf02012-01-16 19:15:08 +00001575 Quantum
1576 *restrict buffer,
1577 *restrict pixels;
cristya63e4a92011-09-09 00:47:59 +00001578
cristy5473bf02012-01-16 19:15:08 +00001579 register ssize_t
1580 i;
1581
1582 size_t
1583 length;
cristy117ff172010-08-15 21:35:32 +00001584
cristybb503372010-05-27 20:51:26 +00001585 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001586 X[4] = {0, 1, 1,-1},
1587 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001588
cristy3ed852e2009-09-05 21:47:34 +00001589 /*
1590 Allocate despeckled image.
1591 */
1592 assert(image != (const Image *) NULL);
1593 assert(image->signature == MagickSignature);
1594 if (image->debug != MagickFalse)
1595 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1596 assert(exception != (ExceptionInfo *) NULL);
1597 assert(exception->signature == MagickSignature);
cristy5473bf02012-01-16 19:15:08 +00001598 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001599 if (despeckle_image == (Image *) NULL)
1600 return((Image *) NULL);
cristya63e4a92011-09-09 00:47:59 +00001601 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1602 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001603 {
cristy3ed852e2009-09-05 21:47:34 +00001604 despeckle_image=DestroyImage(despeckle_image);
1605 return((Image *) NULL);
1606 }
1607 /*
cristy5473bf02012-01-16 19:15:08 +00001608 Allocate image buffer.
1609 */
1610 length=(size_t) ((image->columns+2)*(image->rows+2));
1611 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1612 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1613 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1614 {
1615 if (buffer != (Quantum *) NULL)
1616 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1617 if (pixels != (Quantum *) NULL)
1618 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1619 despeckle_image=DestroyImage(despeckle_image);
1620 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1621 }
1622 /*
1623 Reduce speckle in the image.
cristy3ed852e2009-09-05 21:47:34 +00001624 */
1625 status=MagickTrue;
cristyb06da092012-01-16 17:11:38 +00001626 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00001627 despeckle_view=AcquireCacheView(despeckle_image);
cristy5473bf02012-01-16 19:15:08 +00001628 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +00001629 {
cristy5473bf02012-01-16 19:15:08 +00001630 PixelChannel
1631 channel;
1632
1633 PixelTrait
1634 despeckle_traits,
1635 traits;
cristy3ed852e2009-09-05 21:47:34 +00001636
cristyc1488b52011-02-19 18:54:15 +00001637 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001638 k,
cristyf0ae7762012-01-15 22:04:03 +00001639 x;
cristyc1488b52011-02-19 18:54:15 +00001640
cristy5473bf02012-01-16 19:15:08 +00001641 ssize_t
1642 j,
1643 y;
1644
1645 if (status == MagickFalse)
1646 continue;
1647 channel=GetPixelChannelMapChannel(image,i);
1648 traits=GetPixelChannelMapTraits(image,channel);
1649 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1650 if ((traits == UndefinedPixelTrait) ||
1651 (despeckle_traits == UndefinedPixelTrait))
1652 continue;
1653 if ((despeckle_traits & CopyPixelTrait) != 0)
1654 continue;
1655 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1656 j=(ssize_t) image->columns+2;
1657 for (y=0; y < (ssize_t) image->rows; y++)
cristyfc830f42012-01-15 02:45:06 +00001658 {
cristy5473bf02012-01-16 19:15:08 +00001659 register const Quantum
1660 *restrict p;
cristyfc830f42012-01-15 02:45:06 +00001661
cristy5473bf02012-01-16 19:15:08 +00001662 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1663 if (p == (const Quantum *) NULL)
cristyf0ae7762012-01-15 22:04:03 +00001664 {
cristy5473bf02012-01-16 19:15:08 +00001665 status=MagickFalse;
1666 continue;
cristyf0ae7762012-01-15 22:04:03 +00001667 }
cristy5473bf02012-01-16 19:15:08 +00001668 j++;
1669 for (x=0; x < (ssize_t) image->columns; x++)
1670 {
1671 pixels[j++]=p[i];
1672 p+=GetPixelChannels(image);
cristyfc830f42012-01-15 02:45:06 +00001673 }
cristy5473bf02012-01-16 19:15:08 +00001674 j++;
cristy3ed852e2009-09-05 21:47:34 +00001675 }
cristy5473bf02012-01-16 19:15:08 +00001676 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1677 for (k=0; k < 4; k++)
1678 {
1679 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1680 Hull(-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1681 Hull(-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1682 Hull(X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1683 }
1684 j=(ssize_t) image->columns+2;
1685 for (y=0; y < (ssize_t) image->rows; y++)
1686 {
1687 MagickBooleanType
1688 sync;
1689
1690 register Quantum
1691 *restrict q;
1692
cristyd13e8eb2012-01-17 02:36:42 +00001693 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1694 despeckle_image->columns,1,exception);
cristy5473bf02012-01-16 19:15:08 +00001695 if (q == (Quantum *) NULL)
1696 {
1697 status=MagickFalse;
1698 continue;
1699 }
1700 j++;
1701 for (x=0; x < (ssize_t) image->columns; x++)
1702 {
1703 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1704 q+=GetPixelChannels(despeckle_image);
1705 }
1706 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1707 if (sync == MagickFalse)
1708 status=MagickFalse;
1709 j++;
1710 }
cristy3ed852e2009-09-05 21:47:34 +00001711 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1712 {
1713 MagickBooleanType
1714 proceed;
1715
cristy5473bf02012-01-16 19:15:08 +00001716 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1717 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00001718 if (proceed == MagickFalse)
1719 status=MagickFalse;
1720 }
1721 }
1722 despeckle_view=DestroyCacheView(despeckle_view);
1723 image_view=DestroyCacheView(image_view);
cristy5473bf02012-01-16 19:15:08 +00001724 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1725 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001726 despeckle_image->type=image->type;
1727 if (status == MagickFalse)
1728 despeckle_image=DestroyImage(despeckle_image);
1729 return(despeckle_image);
1730}
1731
1732/*
1733%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1734% %
1735% %
1736% %
1737% E d g e I m a g e %
1738% %
1739% %
1740% %
1741%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1742%
1743% EdgeImage() finds edges in an image. Radius defines the radius of the
1744% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1745% radius for you.
1746%
1747% The format of the EdgeImage method is:
1748%
1749% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001750% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001751%
1752% A description of each parameter follows:
1753%
1754% o image: the image.
1755%
1756% o radius: the radius of the pixel neighborhood.
1757%
cristy8ae632d2011-09-05 17:29:53 +00001758% o sigma: the standard deviation of the Gaussian, in pixels.
1759%
cristy3ed852e2009-09-05 21:47:34 +00001760% o exception: return any errors or warnings in this structure.
1761%
1762*/
1763MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001764 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001765{
1766 Image
1767 *edge_image;
1768
cristy41cbe682011-07-15 19:12:37 +00001769 KernelInfo
1770 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001771
cristybb503372010-05-27 20:51:26 +00001772 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001773 i;
1774
cristybb503372010-05-27 20:51:26 +00001775 size_t
cristy3ed852e2009-09-05 21:47:34 +00001776 width;
1777
cristy41cbe682011-07-15 19:12:37 +00001778 ssize_t
1779 j,
1780 u,
1781 v;
1782
cristy3ed852e2009-09-05 21:47:34 +00001783 assert(image != (const Image *) NULL);
1784 assert(image->signature == MagickSignature);
1785 if (image->debug != MagickFalse)
1786 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1787 assert(exception != (ExceptionInfo *) NULL);
1788 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001789 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001790 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001791 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001792 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001793 kernel_info->width=width;
1794 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001795 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1796 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1797 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001798 {
1799 kernel_info=DestroyKernelInfo(kernel_info);
1800 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1801 }
1802 j=(ssize_t) kernel_info->width/2;
1803 i=0;
1804 for (v=(-j); v <= j; v++)
1805 {
1806 for (u=(-j); u <= j; u++)
1807 {
1808 kernel_info->values[i]=(-1.0);
1809 i++;
1810 }
1811 }
1812 kernel_info->values[i/2]=(double) (width*width-1.0);
anthony736a1602011-10-06 12:38:17 +00001813 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
cristy5e6be1e2011-07-16 01:23:39 +00001814 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001815 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001816 return(edge_image);
1817}
1818
1819/*
1820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1821% %
1822% %
1823% %
1824% E m b o s s I m a g e %
1825% %
1826% %
1827% %
1828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1829%
1830% EmbossImage() returns a grayscale image with a three-dimensional effect.
1831% We convolve the image with a Gaussian operator of the given radius and
1832% standard deviation (sigma). For reasonable results, radius should be
1833% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1834% radius for you.
1835%
1836% The format of the EmbossImage method is:
1837%
1838% Image *EmbossImage(const Image *image,const double radius,
1839% const double sigma,ExceptionInfo *exception)
1840%
1841% A description of each parameter follows:
1842%
1843% o image: the image.
1844%
1845% o radius: the radius of the pixel neighborhood.
1846%
1847% o sigma: the standard deviation of the Gaussian, in pixels.
1848%
1849% o exception: return any errors or warnings in this structure.
1850%
1851*/
1852MagickExport Image *EmbossImage(const Image *image,const double radius,
1853 const double sigma,ExceptionInfo *exception)
1854{
cristy3ed852e2009-09-05 21:47:34 +00001855 Image
1856 *emboss_image;
1857
cristy41cbe682011-07-15 19:12:37 +00001858 KernelInfo
1859 *kernel_info;
1860
cristybb503372010-05-27 20:51:26 +00001861 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001862 i;
1863
cristybb503372010-05-27 20:51:26 +00001864 size_t
cristy3ed852e2009-09-05 21:47:34 +00001865 width;
1866
cristy117ff172010-08-15 21:35:32 +00001867 ssize_t
1868 j,
1869 k,
1870 u,
1871 v;
1872
cristy41cbe682011-07-15 19:12:37 +00001873 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001874 assert(image->signature == MagickSignature);
1875 if (image->debug != MagickFalse)
1876 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1877 assert(exception != (ExceptionInfo *) NULL);
1878 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001879 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001880 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001881 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001882 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001883 kernel_info->width=width;
1884 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001885 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1886 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1887 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001888 {
1889 kernel_info=DestroyKernelInfo(kernel_info);
1890 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1891 }
1892 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001893 k=j;
1894 i=0;
1895 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001896 {
cristy47e00502009-12-17 19:19:57 +00001897 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001898 {
cristy41cbe682011-07-15 19:12:37 +00001899 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001900 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001901 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001902 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001903 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001904 i++;
1905 }
cristy47e00502009-12-17 19:19:57 +00001906 k--;
cristy3ed852e2009-09-05 21:47:34 +00001907 }
anthony736a1602011-10-06 12:38:17 +00001908 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
cristy5e6be1e2011-07-16 01:23:39 +00001909 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001910 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001911 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001912 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001913 return(emboss_image);
1914}
1915
1916/*
1917%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1918% %
1919% %
1920% %
1921% G a u s s i a n B l u r I m a g e %
1922% %
1923% %
1924% %
1925%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1926%
1927% GaussianBlurImage() blurs an image. We convolve the image with a
1928% Gaussian operator of the given radius and standard deviation (sigma).
1929% For reasonable results, the radius should be larger than sigma. Use a
1930% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1931%
1932% The format of the GaussianBlurImage method is:
1933%
1934% Image *GaussianBlurImage(const Image *image,onst double radius,
cristyd89705a2012-01-20 02:52:24 +00001935% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001936%
1937% A description of each parameter follows:
1938%
1939% o image: the image.
1940%
cristy3ed852e2009-09-05 21:47:34 +00001941% o radius: the radius of the Gaussian, in pixels, not counting the center
1942% pixel.
1943%
1944% o sigma: the standard deviation of the Gaussian, in pixels.
1945%
1946% o exception: return any errors or warnings in this structure.
1947%
1948*/
cristy41cbe682011-07-15 19:12:37 +00001949MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristyd89705a2012-01-20 02:52:24 +00001950 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001951{
cristy3ed852e2009-09-05 21:47:34 +00001952 Image
1953 *blur_image;
1954
cristy41cbe682011-07-15 19:12:37 +00001955 KernelInfo
1956 *kernel_info;
1957
cristybb503372010-05-27 20:51:26 +00001958 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001959 i;
1960
cristybb503372010-05-27 20:51:26 +00001961 size_t
cristy3ed852e2009-09-05 21:47:34 +00001962 width;
1963
cristy117ff172010-08-15 21:35:32 +00001964 ssize_t
1965 j,
1966 u,
1967 v;
1968
cristy3ed852e2009-09-05 21:47:34 +00001969 assert(image != (const Image *) NULL);
1970 assert(image->signature == MagickSignature);
1971 if (image->debug != MagickFalse)
1972 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1973 assert(exception != (ExceptionInfo *) NULL);
1974 assert(exception->signature == MagickSignature);
1975 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001976 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001977 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001978 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001979 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1980 kernel_info->width=width;
1981 kernel_info->height=width;
1982 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00001983 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1984 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1985 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001986 {
1987 kernel_info=DestroyKernelInfo(kernel_info);
1988 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1989 }
1990 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001991 i=0;
cristy47e00502009-12-17 19:19:57 +00001992 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001993 {
cristy47e00502009-12-17 19:19:57 +00001994 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001995 {
1996 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1997 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1998 i++;
1999 }
cristy3ed852e2009-09-05 21:47:34 +00002000 }
cristy5e6be1e2011-07-16 01:23:39 +00002001 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00002002 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00002003 return(blur_image);
2004}
2005
2006/*
2007%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2008% %
2009% %
2010% %
cristy3ed852e2009-09-05 21:47:34 +00002011% M o t i o n B l u r I m a g e %
2012% %
2013% %
2014% %
2015%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2016%
2017% MotionBlurImage() simulates motion blur. We convolve the image with a
2018% Gaussian operator of the given radius and standard deviation (sigma).
2019% For reasonable results, radius should be larger than sigma. Use a
2020% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2021% Angle gives the angle of the blurring motion.
2022%
2023% Andrew Protano contributed this effect.
2024%
2025% The format of the MotionBlurImage method is:
2026%
2027% Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002028% const double sigma,const double angle,const double bias,
2029% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002030%
2031% A description of each parameter follows:
2032%
2033% o image: the image.
2034%
cristy3ed852e2009-09-05 21:47:34 +00002035% o radius: the radius of the Gaussian, in pixels, not counting
2036% the center pixel.
2037%
2038% o sigma: the standard deviation of the Gaussian, in pixels.
2039%
cristycee97112010-05-28 00:44:52 +00002040% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002041%
cristyf7ef0252011-09-09 14:50:06 +00002042% o bias: the bias.
2043%
cristy3ed852e2009-09-05 21:47:34 +00002044% o exception: return any errors or warnings in this structure.
2045%
2046*/
2047
cristybb503372010-05-27 20:51:26 +00002048static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002049{
cristy3ed852e2009-09-05 21:47:34 +00002050 double
cristy47e00502009-12-17 19:19:57 +00002051 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002052 normalize;
2053
cristybb503372010-05-27 20:51:26 +00002054 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002055 i;
2056
2057 /*
cristy47e00502009-12-17 19:19:57 +00002058 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002059 */
2060 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +00002061 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00002062 if (kernel == (double *) NULL)
2063 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002064 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002065 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002066 {
cristy4205a3c2010-09-12 20:19:59 +00002067 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2068 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002069 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002070 }
cristybb503372010-05-27 20:51:26 +00002071 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002072 kernel[i]/=normalize;
2073 return(kernel);
2074}
2075
cristya63e4a92011-09-09 00:47:59 +00002076MagickExport Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002077 const double sigma,const double angle,const double bias,
2078 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002079{
cristyc4c8d132010-01-07 01:58:38 +00002080 CacheView
2081 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002082 *image_view,
2083 *motion_view;
cristyc4c8d132010-01-07 01:58:38 +00002084
cristy3ed852e2009-09-05 21:47:34 +00002085 double
2086 *kernel;
2087
2088 Image
2089 *blur_image;
2090
cristy3ed852e2009-09-05 21:47:34 +00002091 MagickBooleanType
2092 status;
2093
cristybb503372010-05-27 20:51:26 +00002094 MagickOffsetType
2095 progress;
2096
cristy3ed852e2009-09-05 21:47:34 +00002097 OffsetInfo
2098 *offset;
2099
2100 PointInfo
2101 point;
2102
cristybb503372010-05-27 20:51:26 +00002103 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002104 i;
2105
cristybb503372010-05-27 20:51:26 +00002106 size_t
cristy3ed852e2009-09-05 21:47:34 +00002107 width;
2108
cristybb503372010-05-27 20:51:26 +00002109 ssize_t
2110 y;
2111
cristy3ed852e2009-09-05 21:47:34 +00002112 assert(image != (Image *) NULL);
2113 assert(image->signature == MagickSignature);
2114 if (image->debug != MagickFalse)
2115 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2116 assert(exception != (ExceptionInfo *) NULL);
2117 width=GetOptimalKernelWidth1D(radius,sigma);
2118 kernel=GetMotionBlurKernel(width,sigma);
2119 if (kernel == (double *) NULL)
2120 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2121 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2122 if (offset == (OffsetInfo *) NULL)
2123 {
cristy8b49f382012-01-17 03:11:03 +00002124 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002125 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2126 }
cristy1e7aa312011-09-10 20:01:36 +00002127 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002128 if (blur_image == (Image *) NULL)
2129 {
cristy8b49f382012-01-17 03:11:03 +00002130 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002131 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2132 return((Image *) NULL);
2133 }
cristy574cc262011-08-05 01:23:58 +00002134 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002135 {
cristy8b49f382012-01-17 03:11:03 +00002136 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002137 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002138 blur_image=DestroyImage(blur_image);
2139 return((Image *) NULL);
2140 }
2141 point.x=(double) width*sin(DegreesToRadians(angle));
2142 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002143 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002144 {
cristybb503372010-05-27 20:51:26 +00002145 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2146 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002147 }
2148 /*
2149 Motion blur image.
2150 */
2151 status=MagickTrue;
2152 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002153 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002154 motion_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002155 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002156#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00002157 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002158#endif
cristybb503372010-05-27 20:51:26 +00002159 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002160 {
cristyf7ef0252011-09-09 14:50:06 +00002161 register const Quantum
2162 *restrict p;
2163
cristy4c08aed2011-07-01 19:47:50 +00002164 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002165 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002166
cristy117ff172010-08-15 21:35:32 +00002167 register ssize_t
2168 x;
2169
cristy3ed852e2009-09-05 21:47:34 +00002170 if (status == MagickFalse)
2171 continue;
cristy8b49f382012-01-17 03:11:03 +00002172 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2173 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002174 exception);
cristyf7ef0252011-09-09 14:50:06 +00002175 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002176 {
2177 status=MagickFalse;
2178 continue;
2179 }
cristybb503372010-05-27 20:51:26 +00002180 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002181 {
cristybb503372010-05-27 20:51:26 +00002182 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002183 i;
2184
cristyf7ef0252011-09-09 14:50:06 +00002185 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2186 {
2187 MagickRealType
2188 alpha,
2189 gamma,
2190 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002191
cristyf7ef0252011-09-09 14:50:06 +00002192 PixelChannel
2193 channel;
2194
2195 PixelTrait
2196 blur_traits,
2197 traits;
2198
2199 register const Quantum
2200 *restrict r;
2201
2202 register double
2203 *restrict k;
2204
2205 register ssize_t
2206 j;
2207
cristye2a912b2011-12-05 20:02:07 +00002208 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002209 traits=GetPixelChannelMapTraits(image,channel);
cristyf7ef0252011-09-09 14:50:06 +00002210 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2211 if ((traits == UndefinedPixelTrait) ||
2212 (blur_traits == UndefinedPixelTrait))
2213 continue;
2214 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002215 {
cristy0beccfa2011-09-25 20:47:53 +00002216 SetPixelChannel(blur_image,channel,p[i],q);
cristyf7ef0252011-09-09 14:50:06 +00002217 continue;
cristy3ed852e2009-09-05 21:47:34 +00002218 }
cristyf7ef0252011-09-09 14:50:06 +00002219 k=kernel;
2220 pixel=bias;
2221 if ((blur_traits & BlendPixelTrait) == 0)
2222 {
2223 for (j=0; j < (ssize_t) width; j++)
2224 {
cristy8b49f382012-01-17 03:11:03 +00002225 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
cristyf7ef0252011-09-09 14:50:06 +00002226 offset[j].y,1,1,exception);
2227 if (r == (const Quantum *) NULL)
2228 {
2229 status=MagickFalse;
2230 continue;
2231 }
2232 pixel+=(*k)*r[i];
2233 k++;
2234 }
cristy0beccfa2011-09-25 20:47:53 +00002235 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002236 continue;
2237 }
2238 alpha=0.0;
2239 gamma=0.0;
2240 for (j=0; j < (ssize_t) width; j++)
2241 {
cristy8b49f382012-01-17 03:11:03 +00002242 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
cristyf7ef0252011-09-09 14:50:06 +00002243 1,exception);
2244 if (r == (const Quantum *) NULL)
2245 {
2246 status=MagickFalse;
2247 continue;
2248 }
2249 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2250 pixel+=(*k)*alpha*r[i];
2251 gamma+=(*k)*alpha;
2252 k++;
cristy3ed852e2009-09-05 21:47:34 +00002253 }
cristyf7ef0252011-09-09 14:50:06 +00002254 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002255 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002256 }
2257 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002258 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002259 }
2260 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2261 status=MagickFalse;
2262 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2263 {
2264 MagickBooleanType
2265 proceed;
2266
cristyb557a152011-02-22 12:14:30 +00002267#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002268 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002269#endif
2270 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2271 if (proceed == MagickFalse)
2272 status=MagickFalse;
2273 }
2274 }
2275 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00002276 motion_view=DestroyCacheView(motion_view);
cristy3ed852e2009-09-05 21:47:34 +00002277 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00002278 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002279 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2280 if (status == MagickFalse)
2281 blur_image=DestroyImage(blur_image);
2282 return(blur_image);
2283}
2284
2285/*
2286%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2287% %
2288% %
2289% %
2290% P r e v i e w I m a g e %
2291% %
2292% %
2293% %
2294%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2295%
2296% PreviewImage() tiles 9 thumbnails of the specified image with an image
2297% processing operation applied with varying parameters. This may be helpful
2298% pin-pointing an appropriate parameter for a particular image processing
2299% operation.
2300%
2301% The format of the PreviewImages method is:
2302%
2303% Image *PreviewImages(const Image *image,const PreviewType preview,
2304% ExceptionInfo *exception)
2305%
2306% A description of each parameter follows:
2307%
2308% o image: the image.
2309%
2310% o preview: the image processing operation.
2311%
2312% o exception: return any errors or warnings in this structure.
2313%
2314*/
2315MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2316 ExceptionInfo *exception)
2317{
2318#define NumberTiles 9
2319#define PreviewImageTag "Preview/Image"
2320#define DefaultPreviewGeometry "204x204+10+10"
2321
2322 char
2323 factor[MaxTextExtent],
2324 label[MaxTextExtent];
2325
2326 double
2327 degrees,
2328 gamma,
2329 percentage,
2330 radius,
2331 sigma,
2332 threshold;
2333
2334 Image
2335 *images,
2336 *montage_image,
2337 *preview_image,
2338 *thumbnail;
2339
2340 ImageInfo
2341 *preview_info;
2342
cristy3ed852e2009-09-05 21:47:34 +00002343 MagickBooleanType
2344 proceed;
2345
2346 MontageInfo
2347 *montage_info;
2348
2349 QuantizeInfo
2350 quantize_info;
2351
2352 RectangleInfo
2353 geometry;
2354
cristybb503372010-05-27 20:51:26 +00002355 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002356 i,
2357 x;
2358
cristybb503372010-05-27 20:51:26 +00002359 size_t
cristy3ed852e2009-09-05 21:47:34 +00002360 colors;
2361
cristy117ff172010-08-15 21:35:32 +00002362 ssize_t
2363 y;
2364
cristy3ed852e2009-09-05 21:47:34 +00002365 /*
2366 Open output image file.
2367 */
2368 assert(image != (Image *) NULL);
2369 assert(image->signature == MagickSignature);
2370 if (image->debug != MagickFalse)
2371 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2372 colors=2;
2373 degrees=0.0;
2374 gamma=(-0.2f);
2375 preview_info=AcquireImageInfo();
2376 SetGeometry(image,&geometry);
2377 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2378 &geometry.width,&geometry.height);
2379 images=NewImageList();
2380 percentage=12.5;
2381 GetQuantizeInfo(&quantize_info);
2382 radius=0.0;
2383 sigma=1.0;
2384 threshold=0.0;
2385 x=0;
2386 y=0;
2387 for (i=0; i < NumberTiles; i++)
2388 {
2389 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2390 if (thumbnail == (Image *) NULL)
2391 break;
2392 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2393 (void *) NULL);
cristyd15e6592011-10-15 00:13:06 +00002394 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002395 if (i == (NumberTiles/2))
2396 {
cristy9950d572011-10-01 18:22:35 +00002397 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2398 &thumbnail->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002399 AppendImageToList(&images,thumbnail);
2400 continue;
2401 }
2402 switch (preview)
2403 {
2404 case RotatePreview:
2405 {
2406 degrees+=45.0;
2407 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002408 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002409 break;
2410 }
2411 case ShearPreview:
2412 {
2413 degrees+=5.0;
2414 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002415 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002416 degrees,2.0*degrees);
2417 break;
2418 }
2419 case RollPreview:
2420 {
cristybb503372010-05-27 20:51:26 +00002421 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2422 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002423 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002424 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002425 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002426 break;
2427 }
2428 case HuePreview:
2429 {
2430 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2431 if (preview_image == (Image *) NULL)
2432 break;
cristyb51dff52011-05-19 16:55:47 +00002433 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002434 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002435 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002436 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002437 break;
2438 }
2439 case SaturationPreview:
2440 {
2441 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2442 if (preview_image == (Image *) NULL)
2443 break;
cristyb51dff52011-05-19 16:55:47 +00002444 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002445 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002446 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002447 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002448 break;
2449 }
2450 case BrightnessPreview:
2451 {
2452 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2453 if (preview_image == (Image *) NULL)
2454 break;
cristyb51dff52011-05-19 16:55:47 +00002455 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002456 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002457 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002458 break;
2459 }
2460 case GammaPreview:
2461 default:
2462 {
2463 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2464 if (preview_image == (Image *) NULL)
2465 break;
2466 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002467 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002468 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002469 break;
2470 }
2471 case SpiffPreview:
2472 {
2473 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2474 if (preview_image != (Image *) NULL)
2475 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002476 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002477 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002478 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002479 break;
2480 }
2481 case DullPreview:
2482 {
2483 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2484 if (preview_image == (Image *) NULL)
2485 break;
2486 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002487 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002488 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002489 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002490 break;
2491 }
2492 case GrayscalePreview:
2493 {
2494 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2495 if (preview_image == (Image *) NULL)
2496 break;
2497 colors<<=1;
2498 quantize_info.number_colors=colors;
2499 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002500 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002501 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002502 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002503 break;
2504 }
2505 case QuantizePreview:
2506 {
2507 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2508 if (preview_image == (Image *) NULL)
2509 break;
2510 colors<<=1;
2511 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002512 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002513 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002514 colors);
cristy3ed852e2009-09-05 21:47:34 +00002515 break;
2516 }
2517 case DespecklePreview:
2518 {
2519 for (x=0; x < (i-1); x++)
2520 {
2521 preview_image=DespeckleImage(thumbnail,exception);
2522 if (preview_image == (Image *) NULL)
2523 break;
2524 thumbnail=DestroyImage(thumbnail);
2525 thumbnail=preview_image;
2526 }
2527 preview_image=DespeckleImage(thumbnail,exception);
2528 if (preview_image == (Image *) NULL)
2529 break;
cristyb51dff52011-05-19 16:55:47 +00002530 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002531 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002532 break;
2533 }
2534 case ReduceNoisePreview:
2535 {
cristy95c38342011-03-18 22:39:51 +00002536 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2537 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002538 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002539 break;
2540 }
2541 case AddNoisePreview:
2542 {
2543 switch ((int) i)
2544 {
2545 case 0:
2546 {
2547 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2548 break;
2549 }
2550 case 1:
2551 {
2552 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2553 break;
2554 }
2555 case 2:
2556 {
2557 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2558 break;
2559 }
2560 case 3:
2561 {
2562 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2563 break;
2564 }
2565 case 4:
2566 {
2567 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2568 break;
2569 }
2570 case 5:
2571 {
2572 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2573 break;
2574 }
2575 default:
2576 {
2577 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2578 break;
2579 }
2580 }
cristyd76c51e2011-03-26 00:21:26 +00002581 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2582 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002583 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002584 break;
2585 }
2586 case SharpenPreview:
2587 {
anthony736a1602011-10-06 12:38:17 +00002588 /* FUTURE: user bias on sharpen! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002589 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2590 exception);
cristyb51dff52011-05-19 16:55:47 +00002591 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002592 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002593 break;
2594 }
2595 case BlurPreview:
2596 {
anthony736a1602011-10-06 12:38:17 +00002597 /* FUTURE: user bias on blur! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002598 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002599 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002600 sigma);
2601 break;
2602 }
2603 case ThresholdPreview:
2604 {
2605 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2606 if (preview_image == (Image *) NULL)
2607 break;
cristye941a752011-10-15 01:52:48 +00002608 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2609 QuantumRange+1.0))/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002610 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002611 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2612 break;
2613 }
2614 case EdgeDetectPreview:
2615 {
cristy8ae632d2011-09-05 17:29:53 +00002616 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002617 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002618 break;
2619 }
2620 case SpreadPreview:
2621 {
cristy5c4e2582011-09-11 19:21:03 +00002622 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2623 exception);
cristyb51dff52011-05-19 16:55:47 +00002624 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002625 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002626 break;
2627 }
2628 case SolarizePreview:
2629 {
2630 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2631 if (preview_image == (Image *) NULL)
2632 break;
2633 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002634 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002635 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002636 (QuantumRange*percentage)/100.0);
2637 break;
2638 }
2639 case ShadePreview:
2640 {
2641 degrees+=10.0;
2642 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2643 exception);
cristyb51dff52011-05-19 16:55:47 +00002644 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002645 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002646 break;
2647 }
2648 case RaisePreview:
2649 {
2650 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2651 if (preview_image == (Image *) NULL)
2652 break;
cristybb503372010-05-27 20:51:26 +00002653 geometry.width=(size_t) (2*i+2);
2654 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002655 geometry.x=i/2;
2656 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002657 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002658 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002659 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002660 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002661 break;
2662 }
2663 case SegmentPreview:
2664 {
2665 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2666 if (preview_image == (Image *) NULL)
2667 break;
2668 threshold+=0.4f;
2669 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002670 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002671 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002672 threshold,threshold);
2673 break;
2674 }
2675 case SwirlPreview:
2676 {
cristy76f512e2011-09-12 01:26:56 +00002677 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2678 exception);
cristyb51dff52011-05-19 16:55:47 +00002679 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002680 degrees+=45.0;
2681 break;
2682 }
2683 case ImplodePreview:
2684 {
2685 degrees+=0.1f;
cristy76f512e2011-09-12 01:26:56 +00002686 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2687 exception);
cristyb51dff52011-05-19 16:55:47 +00002688 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002689 break;
2690 }
2691 case WavePreview:
2692 {
2693 degrees+=5.0f;
cristy5c4e2582011-09-11 19:21:03 +00002694 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2695 image->interpolate,exception);
cristyb51dff52011-05-19 16:55:47 +00002696 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002697 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002698 break;
2699 }
2700 case OilPaintPreview:
2701 {
cristy14973ba2011-08-27 23:48:07 +00002702 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2703 exception);
2704 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2705 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002706 break;
2707 }
2708 case CharcoalDrawingPreview:
2709 {
anthony736a1602011-10-06 12:38:17 +00002710 /* FUTURE: user bias on charcoal! This is non-sensical! */
cristy3ed852e2009-09-05 21:47:34 +00002711 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristy05c0c9a2011-09-05 23:16:13 +00002712 image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002713 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002714 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002715 break;
2716 }
2717 case JPEGPreview:
2718 {
2719 char
2720 filename[MaxTextExtent];
2721
2722 int
2723 file;
2724
2725 MagickBooleanType
2726 status;
2727
2728 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2729 if (preview_image == (Image *) NULL)
2730 break;
cristybb503372010-05-27 20:51:26 +00002731 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002732 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002733 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002734 file=AcquireUniqueFileResource(filename);
2735 if (file != -1)
2736 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002737 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002738 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002739 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002740 if (status != MagickFalse)
2741 {
2742 Image
2743 *quality_image;
2744
2745 (void) CopyMagickString(preview_info->filename,
2746 preview_image->filename,MaxTextExtent);
2747 quality_image=ReadImage(preview_info,exception);
2748 if (quality_image != (Image *) NULL)
2749 {
2750 preview_image=DestroyImage(preview_image);
2751 preview_image=quality_image;
2752 }
2753 }
2754 (void) RelinquishUniqueFileResource(preview_image->filename);
2755 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002756 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002757 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2758 1024.0/1024.0);
2759 else
2760 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002761 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002762 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002763 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002764 else
cristyb51dff52011-05-19 16:55:47 +00002765 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002766 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002767 break;
2768 }
2769 }
2770 thumbnail=DestroyImage(thumbnail);
2771 percentage+=12.5;
2772 radius+=0.5;
2773 sigma+=0.25;
2774 if (preview_image == (Image *) NULL)
2775 break;
2776 (void) DeleteImageProperty(preview_image,"label");
cristyd15e6592011-10-15 00:13:06 +00002777 (void) SetImageProperty(preview_image,"label",label,exception);
cristy3ed852e2009-09-05 21:47:34 +00002778 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002779 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2780 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002781 if (proceed == MagickFalse)
2782 break;
2783 }
2784 if (images == (Image *) NULL)
2785 {
2786 preview_info=DestroyImageInfo(preview_info);
2787 return((Image *) NULL);
2788 }
2789 /*
2790 Create the montage.
2791 */
2792 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2793 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2794 montage_info->shadow=MagickTrue;
2795 (void) CloneString(&montage_info->tile,"3x3");
2796 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2797 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2798 montage_image=MontageImages(images,montage_info,exception);
2799 montage_info=DestroyMontageInfo(montage_info);
2800 images=DestroyImageList(images);
2801 if (montage_image == (Image *) NULL)
2802 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2803 if (montage_image->montage != (char *) NULL)
2804 {
2805 /*
2806 Free image directory.
2807 */
2808 montage_image->montage=(char *) RelinquishMagickMemory(
2809 montage_image->montage);
2810 if (image->directory != (char *) NULL)
2811 montage_image->directory=(char *) RelinquishMagickMemory(
2812 montage_image->directory);
2813 }
2814 preview_info=DestroyImageInfo(preview_info);
2815 return(montage_image);
2816}
2817
2818/*
2819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2820% %
2821% %
2822% %
2823% R a d i a l B l u r I m a g e %
2824% %
2825% %
2826% %
2827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2828%
2829% RadialBlurImage() applies a radial blur to the image.
2830%
2831% Andrew Protano contributed this effect.
2832%
2833% The format of the RadialBlurImage method is:
2834%
2835% Image *RadialBlurImage(const Image *image,const double angle,
cristy6435bd92011-09-10 02:10:07 +00002836% const double blur,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002837%
2838% A description of each parameter follows:
2839%
2840% o image: the image.
2841%
cristy3ed852e2009-09-05 21:47:34 +00002842% o angle: the angle of the radial blur.
2843%
cristy6435bd92011-09-10 02:10:07 +00002844% o blur: the blur.
2845%
cristy3ed852e2009-09-05 21:47:34 +00002846% o exception: return any errors or warnings in this structure.
2847%
2848*/
cristy4282c702011-11-21 00:01:06 +00002849MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2850 const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002851{
cristyc4c8d132010-01-07 01:58:38 +00002852 CacheView
2853 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002854 *image_view,
2855 *radial_view;
cristyc4c8d132010-01-07 01:58:38 +00002856
cristy3ed852e2009-09-05 21:47:34 +00002857 Image
2858 *blur_image;
2859
cristy3ed852e2009-09-05 21:47:34 +00002860 MagickBooleanType
2861 status;
2862
cristybb503372010-05-27 20:51:26 +00002863 MagickOffsetType
2864 progress;
2865
cristy3ed852e2009-09-05 21:47:34 +00002866 MagickRealType
2867 blur_radius,
2868 *cos_theta,
2869 offset,
2870 *sin_theta,
2871 theta;
2872
2873 PointInfo
2874 blur_center;
2875
cristybb503372010-05-27 20:51:26 +00002876 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002877 i;
2878
cristybb503372010-05-27 20:51:26 +00002879 size_t
cristy3ed852e2009-09-05 21:47:34 +00002880 n;
2881
cristybb503372010-05-27 20:51:26 +00002882 ssize_t
2883 y;
2884
cristy3ed852e2009-09-05 21:47:34 +00002885 /*
2886 Allocate blur image.
2887 */
2888 assert(image != (Image *) NULL);
2889 assert(image->signature == MagickSignature);
2890 if (image->debug != MagickFalse)
2891 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2892 assert(exception != (ExceptionInfo *) NULL);
2893 assert(exception->signature == MagickSignature);
cristy1e7aa312011-09-10 20:01:36 +00002894 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002895 if (blur_image == (Image *) NULL)
2896 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002897 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002898 {
cristy3ed852e2009-09-05 21:47:34 +00002899 blur_image=DestroyImage(blur_image);
2900 return((Image *) NULL);
2901 }
2902 blur_center.x=(double) image->columns/2.0;
2903 blur_center.y=(double) image->rows/2.0;
2904 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002905 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002906 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2907 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2908 sizeof(*cos_theta));
2909 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2910 sizeof(*sin_theta));
2911 if ((cos_theta == (MagickRealType *) NULL) ||
2912 (sin_theta == (MagickRealType *) NULL))
2913 {
2914 blur_image=DestroyImage(blur_image);
2915 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2916 }
2917 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002918 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002919 {
2920 cos_theta[i]=cos((double) (theta*i-offset));
2921 sin_theta[i]=sin((double) (theta*i-offset));
2922 }
2923 /*
2924 Radial blur image.
2925 */
2926 status=MagickTrue;
2927 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002928 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002929 radial_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002930 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002931#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002932 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002933#endif
cristy1e7aa312011-09-10 20:01:36 +00002934 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002935 {
cristy1e7aa312011-09-10 20:01:36 +00002936 register const Quantum
2937 *restrict p;
2938
cristy4c08aed2011-07-01 19:47:50 +00002939 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002940 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002941
cristy117ff172010-08-15 21:35:32 +00002942 register ssize_t
2943 x;
2944
cristy3ed852e2009-09-05 21:47:34 +00002945 if (status == MagickFalse)
2946 continue;
cristy8b49f382012-01-17 03:11:03 +00002947 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2948 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002949 exception);
cristy1e7aa312011-09-10 20:01:36 +00002950 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002951 {
2952 status=MagickFalse;
2953 continue;
2954 }
cristy1e7aa312011-09-10 20:01:36 +00002955 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002956 {
cristy3ed852e2009-09-05 21:47:34 +00002957 MagickRealType
cristy3ed852e2009-09-05 21:47:34 +00002958 radius;
2959
cristy3ed852e2009-09-05 21:47:34 +00002960 PointInfo
2961 center;
2962
cristybb503372010-05-27 20:51:26 +00002963 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002964 i;
2965
cristybb503372010-05-27 20:51:26 +00002966 size_t
cristy3ed852e2009-09-05 21:47:34 +00002967 step;
2968
2969 center.x=(double) x-blur_center.x;
2970 center.y=(double) y-blur_center.y;
2971 radius=hypot((double) center.x,center.y);
2972 if (radius == 0)
2973 step=1;
2974 else
2975 {
cristybb503372010-05-27 20:51:26 +00002976 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002977 if (step == 0)
2978 step=1;
2979 else
2980 if (step >= n)
2981 step=n-1;
2982 }
cristy1e7aa312011-09-10 20:01:36 +00002983 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2984 {
2985 MagickRealType
2986 gamma,
2987 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002988
cristy1e7aa312011-09-10 20:01:36 +00002989 PixelChannel
2990 channel;
2991
2992 PixelTrait
2993 blur_traits,
2994 traits;
2995
2996 register const Quantum
2997 *restrict r;
2998
2999 register ssize_t
3000 j;
3001
cristye2a912b2011-12-05 20:02:07 +00003002 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003003 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003004 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3005 if ((traits == UndefinedPixelTrait) ||
3006 (blur_traits == UndefinedPixelTrait))
3007 continue;
3008 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003009 {
cristy0beccfa2011-09-25 20:47:53 +00003010 SetPixelChannel(blur_image,channel,p[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003011 continue;
cristy3ed852e2009-09-05 21:47:34 +00003012 }
cristy1e7aa312011-09-10 20:01:36 +00003013 gamma=0.0;
3014 pixel=bias;
3015 if ((blur_traits & BlendPixelTrait) == 0)
3016 {
3017 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3018 {
cristy8b49f382012-01-17 03:11:03 +00003019 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003020 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3021 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3022 1,1,exception);
3023 if (r == (const Quantum *) NULL)
3024 {
3025 status=MagickFalse;
3026 continue;
3027 }
3028 pixel+=r[i];
3029 gamma++;
3030 }
3031 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003032 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003033 continue;
3034 }
3035 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3036 {
cristy8b49f382012-01-17 03:11:03 +00003037 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003038 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3039 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3040 1,1,exception);
3041 if (r == (const Quantum *) NULL)
3042 {
3043 status=MagickFalse;
3044 continue;
3045 }
3046 pixel+=GetPixelAlpha(image,r)*r[i];
3047 gamma+=GetPixelAlpha(image,r);
cristy3ed852e2009-09-05 21:47:34 +00003048 }
cristy1e7aa312011-09-10 20:01:36 +00003049 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003050 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003051 }
3052 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003053 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003054 }
3055 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3056 status=MagickFalse;
3057 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3058 {
3059 MagickBooleanType
3060 proceed;
3061
cristyb5d5f722009-11-04 03:03:49 +00003062#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003063 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003064#endif
3065 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3066 if (proceed == MagickFalse)
3067 status=MagickFalse;
3068 }
3069 }
3070 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00003071 radial_view=DestroyCacheView(radial_view);
cristy3ed852e2009-09-05 21:47:34 +00003072 image_view=DestroyCacheView(image_view);
3073 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3074 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3075 if (status == MagickFalse)
3076 blur_image=DestroyImage(blur_image);
3077 return(blur_image);
3078}
3079
3080/*
3081%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3082% %
3083% %
3084% %
cristy3ed852e2009-09-05 21:47:34 +00003085% S e l e c t i v e B l u r I m a g e %
3086% %
3087% %
3088% %
3089%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3090%
3091% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3092% It is similar to the unsharpen mask that sharpens everything with contrast
3093% above a certain threshold.
3094%
3095% The format of the SelectiveBlurImage method is:
3096%
3097% Image *SelectiveBlurImage(const Image *image,const double radius,
cristy1e7aa312011-09-10 20:01:36 +00003098% const double sigma,const double threshold,const double bias,
3099% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003100%
3101% A description of each parameter follows:
3102%
3103% o image: the image.
3104%
cristy3ed852e2009-09-05 21:47:34 +00003105% o radius: the radius of the Gaussian, in pixels, not counting the center
3106% pixel.
3107%
3108% o sigma: the standard deviation of the Gaussian, in pixels.
3109%
3110% o threshold: only pixels within this contrast threshold are included
3111% in the blur operation.
3112%
cristy1e7aa312011-09-10 20:01:36 +00003113% o bias: the bias.
3114%
cristy3ed852e2009-09-05 21:47:34 +00003115% o exception: return any errors or warnings in this structure.
3116%
3117*/
cristy4282c702011-11-21 00:01:06 +00003118MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3119 const double sigma,const double threshold,const double bias,
3120 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003121{
3122#define SelectiveBlurImageTag "SelectiveBlur/Image"
3123
cristy47e00502009-12-17 19:19:57 +00003124 CacheView
3125 *blur_view,
3126 *image_view;
3127
cristy3ed852e2009-09-05 21:47:34 +00003128 double
cristy3ed852e2009-09-05 21:47:34 +00003129 *kernel;
3130
3131 Image
3132 *blur_image;
3133
cristy3ed852e2009-09-05 21:47:34 +00003134 MagickBooleanType
3135 status;
3136
cristybb503372010-05-27 20:51:26 +00003137 MagickOffsetType
3138 progress;
3139
cristybb503372010-05-27 20:51:26 +00003140 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003141 i;
cristy3ed852e2009-09-05 21:47:34 +00003142
cristybb503372010-05-27 20:51:26 +00003143 size_t
cristy3ed852e2009-09-05 21:47:34 +00003144 width;
3145
cristybb503372010-05-27 20:51:26 +00003146 ssize_t
cristyc8523c12011-09-13 00:02:53 +00003147 center,
cristybb503372010-05-27 20:51:26 +00003148 j,
3149 u,
3150 v,
3151 y;
3152
cristy3ed852e2009-09-05 21:47:34 +00003153 /*
3154 Initialize blur image attributes.
3155 */
3156 assert(image != (Image *) NULL);
3157 assert(image->signature == MagickSignature);
3158 if (image->debug != MagickFalse)
3159 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3160 assert(exception != (ExceptionInfo *) NULL);
3161 assert(exception->signature == MagickSignature);
3162 width=GetOptimalKernelWidth1D(radius,sigma);
cristy8b49f382012-01-17 03:11:03 +00003163 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00003164 if (kernel == (double *) NULL)
3165 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003166 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003167 i=0;
cristy47e00502009-12-17 19:19:57 +00003168 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003169 {
cristy47e00502009-12-17 19:19:57 +00003170 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003171 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3172 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003173 }
3174 if (image->debug != MagickFalse)
3175 {
3176 char
3177 format[MaxTextExtent],
3178 *message;
3179
cristy117ff172010-08-15 21:35:32 +00003180 register const double
3181 *k;
3182
cristybb503372010-05-27 20:51:26 +00003183 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003184 u,
3185 v;
3186
cristy3ed852e2009-09-05 21:47:34 +00003187 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003188 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3189 width);
cristy3ed852e2009-09-05 21:47:34 +00003190 message=AcquireString("");
3191 k=kernel;
cristybb503372010-05-27 20:51:26 +00003192 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003193 {
3194 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003195 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003196 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003197 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003198 {
cristyb51dff52011-05-19 16:55:47 +00003199 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003200 (void) ConcatenateString(&message,format);
3201 }
3202 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3203 }
3204 message=DestroyString(message);
3205 }
cristy1e7aa312011-09-10 20:01:36 +00003206 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003207 if (blur_image == (Image *) NULL)
3208 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003209 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003210 {
cristy3ed852e2009-09-05 21:47:34 +00003211 blur_image=DestroyImage(blur_image);
3212 return((Image *) NULL);
3213 }
3214 /*
3215 Threshold blur image.
3216 */
3217 status=MagickTrue;
3218 progress=0;
cristyc8523c12011-09-13 00:02:53 +00003219 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3220 GetPixelChannels(image)*(width/2L));
cristy3ed852e2009-09-05 21:47:34 +00003221 image_view=AcquireCacheView(image);
3222 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003223#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003224 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003225#endif
cristybb503372010-05-27 20:51:26 +00003226 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003227 {
cristy4c08aed2011-07-01 19:47:50 +00003228 double
3229 contrast;
3230
cristy3ed852e2009-09-05 21:47:34 +00003231 MagickBooleanType
3232 sync;
3233
cristy4c08aed2011-07-01 19:47:50 +00003234 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003235 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003236
cristy4c08aed2011-07-01 19:47:50 +00003237 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003238 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003239
cristy117ff172010-08-15 21:35:32 +00003240 register ssize_t
3241 x;
3242
cristy3ed852e2009-09-05 21:47:34 +00003243 if (status == MagickFalse)
3244 continue;
cristy117ff172010-08-15 21:35:32 +00003245 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3246 (width/2L),image->columns+width,width,exception);
cristy8b49f382012-01-17 03:11:03 +00003247 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003248 exception);
cristy4c08aed2011-07-01 19:47:50 +00003249 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003250 {
3251 status=MagickFalse;
3252 continue;
3253 }
cristybb503372010-05-27 20:51:26 +00003254 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003255 {
cristybb503372010-05-27 20:51:26 +00003256 register ssize_t
cristy1e7aa312011-09-10 20:01:36 +00003257 i;
cristy3ed852e2009-09-05 21:47:34 +00003258
cristy1e7aa312011-09-10 20:01:36 +00003259 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3260 {
3261 MagickRealType
3262 alpha,
3263 gamma,
3264 intensity,
3265 pixel;
cristy117ff172010-08-15 21:35:32 +00003266
cristy1e7aa312011-09-10 20:01:36 +00003267 PixelChannel
3268 channel;
3269
3270 PixelTrait
3271 blur_traits,
3272 traits;
3273
3274 register const double
3275 *restrict k;
3276
3277 register const Quantum
3278 *restrict pixels;
3279
3280 register ssize_t
3281 u;
3282
3283 ssize_t
3284 v;
3285
cristye2a912b2011-12-05 20:02:07 +00003286 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003287 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003288 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3289 if ((traits == UndefinedPixelTrait) ||
3290 (blur_traits == UndefinedPixelTrait))
3291 continue;
3292 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003293 {
cristy0beccfa2011-09-25 20:47:53 +00003294 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003295 continue;
cristy3ed852e2009-09-05 21:47:34 +00003296 }
cristy1e7aa312011-09-10 20:01:36 +00003297 k=kernel;
3298 pixel=bias;
3299 pixels=p;
cristyc8523c12011-09-13 00:02:53 +00003300 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
cristy1e7aa312011-09-10 20:01:36 +00003301 gamma=0.0;
3302 if ((blur_traits & BlendPixelTrait) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003303 {
cristy1e7aa312011-09-10 20:01:36 +00003304 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003305 {
cristy1e7aa312011-09-10 20:01:36 +00003306 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003307 {
cristy1e7aa312011-09-10 20:01:36 +00003308 contrast=GetPixelIntensity(image,pixels)-intensity;
3309 if (fabs(contrast) < threshold)
3310 {
3311 pixel+=(*k)*pixels[i];
3312 gamma+=(*k);
3313 }
3314 k++;
3315 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003316 }
cristy1e7aa312011-09-10 20:01:36 +00003317 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003318 }
cristy1e7aa312011-09-10 20:01:36 +00003319 if (fabs((double) gamma) < MagickEpsilon)
3320 {
cristy0beccfa2011-09-25 20:47:53 +00003321 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003322 continue;
3323 }
3324 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003325 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003326 continue;
3327 }
3328 for (v=0; v < (ssize_t) width; v++)
3329 {
3330 for (u=0; u < (ssize_t) width; u++)
3331 {
3332 contrast=GetPixelIntensity(image,pixels)-intensity;
3333 if (fabs(contrast) < threshold)
3334 {
3335 alpha=(MagickRealType) (QuantumScale*
3336 GetPixelAlpha(image,pixels));
3337 pixel+=(*k)*alpha*pixels[i];
3338 gamma+=(*k)*alpha;
3339 }
3340 k++;
3341 pixels+=GetPixelChannels(image);
3342 }
3343 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003344 }
cristy1e7aa312011-09-10 20:01:36 +00003345 if (fabs((double) gamma) < MagickEpsilon)
3346 {
cristy0beccfa2011-09-25 20:47:53 +00003347 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003348 continue;
3349 }
3350 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003351 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003352 }
cristyed231572011-07-14 02:18:59 +00003353 p+=GetPixelChannels(image);
3354 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003355 }
3356 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3357 if (sync == MagickFalse)
3358 status=MagickFalse;
3359 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3360 {
3361 MagickBooleanType
3362 proceed;
3363
cristyb5d5f722009-11-04 03:03:49 +00003364#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003365 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003366#endif
3367 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3368 image->rows);
3369 if (proceed == MagickFalse)
3370 status=MagickFalse;
3371 }
3372 }
3373 blur_image->type=image->type;
3374 blur_view=DestroyCacheView(blur_view);
3375 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00003376 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00003377 if (status == MagickFalse)
3378 blur_image=DestroyImage(blur_image);
3379 return(blur_image);
3380}
3381
3382/*
3383%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3384% %
3385% %
3386% %
3387% S h a d e I m a g e %
3388% %
3389% %
3390% %
3391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3392%
3393% ShadeImage() shines a distant light on an image to create a
3394% three-dimensional effect. You control the positioning of the light with
3395% azimuth and elevation; azimuth is measured in degrees off the x axis
3396% and elevation is measured in pixels above the Z axis.
3397%
3398% The format of the ShadeImage method is:
3399%
3400% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3401% const double azimuth,const double elevation,ExceptionInfo *exception)
3402%
3403% A description of each parameter follows:
3404%
3405% o image: the image.
3406%
3407% o gray: A value other than zero shades the intensity of each pixel.
3408%
3409% o azimuth, elevation: Define the light source direction.
3410%
3411% o exception: return any errors or warnings in this structure.
3412%
3413*/
3414MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3415 const double azimuth,const double elevation,ExceptionInfo *exception)
3416{
3417#define ShadeImageTag "Shade/Image"
3418
cristyc4c8d132010-01-07 01:58:38 +00003419 CacheView
3420 *image_view,
3421 *shade_view;
3422
cristy3ed852e2009-09-05 21:47:34 +00003423 Image
3424 *shade_image;
3425
cristy3ed852e2009-09-05 21:47:34 +00003426 MagickBooleanType
3427 status;
3428
cristybb503372010-05-27 20:51:26 +00003429 MagickOffsetType
3430 progress;
3431
cristy3ed852e2009-09-05 21:47:34 +00003432 PrimaryInfo
3433 light;
3434
cristybb503372010-05-27 20:51:26 +00003435 ssize_t
3436 y;
3437
cristy3ed852e2009-09-05 21:47:34 +00003438 /*
3439 Initialize shaded image attributes.
3440 */
3441 assert(image != (const Image *) NULL);
3442 assert(image->signature == MagickSignature);
3443 if (image->debug != MagickFalse)
3444 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3445 assert(exception != (ExceptionInfo *) NULL);
3446 assert(exception->signature == MagickSignature);
3447 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3448 if (shade_image == (Image *) NULL)
3449 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003450 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003451 {
cristy3ed852e2009-09-05 21:47:34 +00003452 shade_image=DestroyImage(shade_image);
3453 return((Image *) NULL);
3454 }
3455 /*
3456 Compute the light vector.
3457 */
3458 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3459 cos(DegreesToRadians(elevation));
3460 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3461 cos(DegreesToRadians(elevation));
3462 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3463 /*
3464 Shade image.
3465 */
3466 status=MagickTrue;
3467 progress=0;
3468 image_view=AcquireCacheView(image);
3469 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003470#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003471 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003472#endif
cristybb503372010-05-27 20:51:26 +00003473 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003474 {
3475 MagickRealType
3476 distance,
3477 normal_distance,
3478 shade;
3479
3480 PrimaryInfo
3481 normal;
3482
cristy4c08aed2011-07-01 19:47:50 +00003483 register const Quantum
cristy1e7aa312011-09-10 20:01:36 +00003484 *restrict center,
cristyc47d1f82009-11-26 01:44:43 +00003485 *restrict p,
cristy1e7aa312011-09-10 20:01:36 +00003486 *restrict post,
3487 *restrict pre;
cristy3ed852e2009-09-05 21:47:34 +00003488
cristy4c08aed2011-07-01 19:47:50 +00003489 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003490 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003491
cristy117ff172010-08-15 21:35:32 +00003492 register ssize_t
3493 x;
3494
cristy3ed852e2009-09-05 21:47:34 +00003495 if (status == MagickFalse)
3496 continue;
3497 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3498 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3499 exception);
cristy4c08aed2011-07-01 19:47:50 +00003500 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003501 {
3502 status=MagickFalse;
3503 continue;
3504 }
3505 /*
3506 Shade this row of pixels.
3507 */
3508 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristy1e7aa312011-09-10 20:01:36 +00003509 pre=p+GetPixelChannels(image);
3510 center=pre+(image->columns+2)*GetPixelChannels(image);
3511 post=center+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003512 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003513 {
cristy1e7aa312011-09-10 20:01:36 +00003514 register ssize_t
3515 i;
3516
cristy3ed852e2009-09-05 21:47:34 +00003517 /*
3518 Determine the surface normal and compute shading.
3519 */
cristy1e7aa312011-09-10 20:01:36 +00003520 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3521 GetPixelIntensity(image,center-GetPixelChannels(image))+
3522 GetPixelIntensity(image,post-GetPixelChannels(image))-
3523 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3524 GetPixelIntensity(image,center+GetPixelChannels(image))-
3525 GetPixelIntensity(image,post+GetPixelChannels(image)));
3526 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3527 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3528 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3529 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3530 GetPixelIntensity(image,pre+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003531 if ((normal.x == 0.0) && (normal.y == 0.0))
3532 shade=light.z;
3533 else
3534 {
3535 shade=0.0;
3536 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3537 if (distance > MagickEpsilon)
3538 {
3539 normal_distance=
3540 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3541 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3542 shade=distance/sqrt((double) normal_distance);
3543 }
3544 }
cristy1e7aa312011-09-10 20:01:36 +00003545 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3546 {
3547 PixelChannel
3548 channel;
3549
3550 PixelTrait
3551 shade_traits,
3552 traits;
3553
cristye2a912b2011-12-05 20:02:07 +00003554 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003555 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003556 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3557 if ((traits == UndefinedPixelTrait) ||
3558 (shade_traits == UndefinedPixelTrait))
3559 continue;
3560 if ((shade_traits & CopyPixelTrait) != 0)
3561 {
cristy0beccfa2011-09-25 20:47:53 +00003562 SetPixelChannel(shade_image,channel,center[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003563 continue;
3564 }
3565 if (gray != MagickFalse)
3566 {
cristy0beccfa2011-09-25 20:47:53 +00003567 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
cristy1e7aa312011-09-10 20:01:36 +00003568 continue;
3569 }
cristy0beccfa2011-09-25 20:47:53 +00003570 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3571 center[i]),q);
cristy1e7aa312011-09-10 20:01:36 +00003572 }
3573 pre+=GetPixelChannels(image);
3574 center+=GetPixelChannels(image);
3575 post+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003576 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003577 }
3578 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3579 status=MagickFalse;
3580 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3581 {
3582 MagickBooleanType
3583 proceed;
3584
cristyb5d5f722009-11-04 03:03:49 +00003585#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003586 #pragma omp critical (MagickCore_ShadeImage)
3587#endif
3588 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3589 if (proceed == MagickFalse)
3590 status=MagickFalse;
3591 }
3592 }
3593 shade_view=DestroyCacheView(shade_view);
3594 image_view=DestroyCacheView(image_view);
3595 if (status == MagickFalse)
3596 shade_image=DestroyImage(shade_image);
3597 return(shade_image);
3598}
3599
3600/*
3601%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3602% %
3603% %
3604% %
3605% S h a r p e n I m a g e %
3606% %
3607% %
3608% %
3609%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3610%
3611% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3612% operator of the given radius and standard deviation (sigma). For
3613% reasonable results, radius should be larger than sigma. Use a radius of 0
3614% and SharpenImage() selects a suitable radius for you.
3615%
3616% Using a separable kernel would be faster, but the negative weights cancel
3617% out on the corners of the kernel producing often undesirable ringing in the
3618% filtered result; this can be avoided by using a 2D gaussian shaped image
3619% sharpening kernel instead.
3620%
3621% The format of the SharpenImage method is:
3622%
3623% Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003624% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003625%
3626% A description of each parameter follows:
3627%
3628% o image: the image.
3629%
cristy3ed852e2009-09-05 21:47:34 +00003630% o radius: the radius of the Gaussian, in pixels, not counting the center
3631% pixel.
3632%
3633% o sigma: the standard deviation of the Laplacian, in pixels.
3634%
cristy05c0c9a2011-09-05 23:16:13 +00003635% o bias: bias.
3636%
cristy3ed852e2009-09-05 21:47:34 +00003637% o exception: return any errors or warnings in this structure.
3638%
3639*/
cristy3ed852e2009-09-05 21:47:34 +00003640MagickExport Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003641 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003642{
cristy3ed852e2009-09-05 21:47:34 +00003643 double
cristy47e00502009-12-17 19:19:57 +00003644 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003645
3646 Image
3647 *sharp_image;
3648
cristy41cbe682011-07-15 19:12:37 +00003649 KernelInfo
3650 *kernel_info;
3651
cristybb503372010-05-27 20:51:26 +00003652 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003653 i;
3654
cristybb503372010-05-27 20:51:26 +00003655 size_t
cristy3ed852e2009-09-05 21:47:34 +00003656 width;
3657
cristy117ff172010-08-15 21:35:32 +00003658 ssize_t
3659 j,
3660 u,
3661 v;
3662
cristy3ed852e2009-09-05 21:47:34 +00003663 assert(image != (const Image *) NULL);
3664 assert(image->signature == MagickSignature);
3665 if (image->debug != MagickFalse)
3666 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3667 assert(exception != (ExceptionInfo *) NULL);
3668 assert(exception->signature == MagickSignature);
3669 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003670 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003671 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003672 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003673 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3674 kernel_info->width=width;
3675 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00003676 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
cristy41cbe682011-07-15 19:12:37 +00003677 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00003678 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3679 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3680 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00003681 {
3682 kernel_info=DestroyKernelInfo(kernel_info);
3683 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3684 }
cristy3ed852e2009-09-05 21:47:34 +00003685 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003686 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003687 i=0;
3688 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003689 {
cristy47e00502009-12-17 19:19:57 +00003690 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003691 {
cristy41cbe682011-07-15 19:12:37 +00003692 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3693 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3694 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003695 i++;
3696 }
3697 }
cristy41cbe682011-07-15 19:12:37 +00003698 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003699 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003700 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003701 return(sharp_image);
3702}
3703
3704/*
3705%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3706% %
3707% %
3708% %
3709% S p r e a d I m a g e %
3710% %
3711% %
3712% %
3713%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3714%
3715% SpreadImage() is a special effects method that randomly displaces each
3716% pixel in a block defined by the radius parameter.
3717%
3718% The format of the SpreadImage method is:
3719%
3720% Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003721% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003722%
3723% A description of each parameter follows:
3724%
3725% o image: the image.
3726%
cristy5c4e2582011-09-11 19:21:03 +00003727% o radius: choose a random pixel in a neighborhood of this extent.
3728%
3729% o method: the pixel interpolation method.
cristy3ed852e2009-09-05 21:47:34 +00003730%
3731% o exception: return any errors or warnings in this structure.
3732%
3733*/
3734MagickExport Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003735 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003736{
3737#define SpreadImageTag "Spread/Image"
3738
cristyfa112112010-01-04 17:48:07 +00003739 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003740 *image_view,
3741 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003742
cristy3ed852e2009-09-05 21:47:34 +00003743 Image
3744 *spread_image;
3745
cristy3ed852e2009-09-05 21:47:34 +00003746 MagickBooleanType
3747 status;
3748
cristybb503372010-05-27 20:51:26 +00003749 MagickOffsetType
3750 progress;
3751
cristy3ed852e2009-09-05 21:47:34 +00003752 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003753 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003754
cristybb503372010-05-27 20:51:26 +00003755 size_t
cristy3ed852e2009-09-05 21:47:34 +00003756 width;
3757
cristybb503372010-05-27 20:51:26 +00003758 ssize_t
3759 y;
3760
cristy3ed852e2009-09-05 21:47:34 +00003761 /*
3762 Initialize spread image attributes.
3763 */
3764 assert(image != (Image *) NULL);
3765 assert(image->signature == MagickSignature);
3766 if (image->debug != MagickFalse)
3767 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3768 assert(exception != (ExceptionInfo *) NULL);
3769 assert(exception->signature == MagickSignature);
3770 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3771 exception);
3772 if (spread_image == (Image *) NULL)
3773 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003774 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003775 {
cristy3ed852e2009-09-05 21:47:34 +00003776 spread_image=DestroyImage(spread_image);
3777 return((Image *) NULL);
3778 }
3779 /*
3780 Spread image.
3781 */
3782 status=MagickTrue;
3783 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003784 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003785 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003786 image_view=AcquireCacheView(image);
3787 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003788#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy78778cb2012-01-17 14:48:47 +00003789 #pragma omp parallel for schedule(static,8) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003790#endif
cristybe82ad52011-09-06 01:17:20 +00003791 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003792 {
cristy5c9e6f22010-09-17 17:31:01 +00003793 const int
3794 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003795
cristybe82ad52011-09-06 01:17:20 +00003796 register const Quantum
3797 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003798
cristy4c08aed2011-07-01 19:47:50 +00003799 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003800 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003801
cristy117ff172010-08-15 21:35:32 +00003802 register ssize_t
3803 x;
3804
cristy3ed852e2009-09-05 21:47:34 +00003805 if (status == MagickFalse)
3806 continue;
cristybe82ad52011-09-06 01:17:20 +00003807 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy9f7e7cb2011-03-26 00:49:57 +00003808 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003809 exception);
cristybe82ad52011-09-06 01:17:20 +00003810 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003811 {
3812 status=MagickFalse;
3813 continue;
3814 }
cristybe82ad52011-09-06 01:17:20 +00003815 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003816 {
cristybe82ad52011-09-06 01:17:20 +00003817 PointInfo
3818 point;
3819
cristybe82ad52011-09-06 01:17:20 +00003820 point.x=GetPseudoRandomValue(random_info[id]);
3821 point.y=GetPseudoRandomValue(random_info[id]);
cristy5c4e2582011-09-11 19:21:03 +00003822 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3823 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
cristyed231572011-07-14 02:18:59 +00003824 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003825 }
cristy9f7e7cb2011-03-26 00:49:57 +00003826 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003827 status=MagickFalse;
3828 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3829 {
3830 MagickBooleanType
3831 proceed;
3832
cristyb557a152011-02-22 12:14:30 +00003833#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003834 #pragma omp critical (MagickCore_SpreadImage)
3835#endif
3836 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3837 if (proceed == MagickFalse)
3838 status=MagickFalse;
3839 }
3840 }
cristy9f7e7cb2011-03-26 00:49:57 +00003841 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003842 image_view=DestroyCacheView(image_view);
3843 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003844 return(spread_image);
3845}
3846
3847/*
3848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3849% %
3850% %
3851% %
3852% U n s h a r p M a s k I m a g e %
3853% %
3854% %
3855% %
3856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3857%
3858% UnsharpMaskImage() sharpens one or more image channels. We convolve the
3859% image with a Gaussian operator of the given radius and standard deviation
3860% (sigma). For reasonable results, radius should be larger than sigma. Use a
3861% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3862%
3863% The format of the UnsharpMaskImage method is:
3864%
3865% Image *UnsharpMaskImage(const Image *image,const double radius,
3866% const double sigma,const double amount,const double threshold,
3867% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003868%
3869% A description of each parameter follows:
3870%
3871% o image: the image.
3872%
cristy3ed852e2009-09-05 21:47:34 +00003873% o radius: the radius of the Gaussian, in pixels, not counting the center
3874% pixel.
3875%
3876% o sigma: the standard deviation of the Gaussian, in pixels.
3877%
3878% o amount: the percentage of the difference between the original and the
3879% blur image that is added back into the original.
3880%
3881% o threshold: the threshold in pixels needed to apply the diffence amount.
3882%
3883% o exception: return any errors or warnings in this structure.
3884%
3885*/
cristy31bb6272011-11-20 23:57:25 +00003886MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3887 const double sigma,const double amount,const double threshold,
3888 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003889{
3890#define SharpenImageTag "Sharpen/Image"
3891
cristyc4c8d132010-01-07 01:58:38 +00003892 CacheView
3893 *image_view,
3894 *unsharp_view;
3895
cristy3ed852e2009-09-05 21:47:34 +00003896 Image
3897 *unsharp_image;
3898
cristy3ed852e2009-09-05 21:47:34 +00003899 MagickBooleanType
3900 status;
3901
cristybb503372010-05-27 20:51:26 +00003902 MagickOffsetType
3903 progress;
3904
cristy3ed852e2009-09-05 21:47:34 +00003905 MagickRealType
3906 quantum_threshold;
3907
cristybb503372010-05-27 20:51:26 +00003908 ssize_t
3909 y;
3910
cristy3ed852e2009-09-05 21:47:34 +00003911 assert(image != (const Image *) NULL);
3912 assert(image->signature == MagickSignature);
3913 if (image->debug != MagickFalse)
3914 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3915 assert(exception != (ExceptionInfo *) NULL);
cristy05c0c9a2011-09-05 23:16:13 +00003916 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00003917 if (unsharp_image == (Image *) NULL)
3918 return((Image *) NULL);
3919 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3920 /*
3921 Unsharp-mask image.
3922 */
3923 status=MagickTrue;
3924 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003925 image_view=AcquireCacheView(image);
3926 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00003927#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003928 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003929#endif
cristybb503372010-05-27 20:51:26 +00003930 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003931 {
cristy4c08aed2011-07-01 19:47:50 +00003932 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003933 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003934
cristy4c08aed2011-07-01 19:47:50 +00003935 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003936 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003937
cristy117ff172010-08-15 21:35:32 +00003938 register ssize_t
3939 x;
3940
cristy3ed852e2009-09-05 21:47:34 +00003941 if (status == MagickFalse)
3942 continue;
3943 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy8b49f382012-01-17 03:11:03 +00003944 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003945 exception);
cristy4c08aed2011-07-01 19:47:50 +00003946 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003947 {
3948 status=MagickFalse;
3949 continue;
3950 }
cristybb503372010-05-27 20:51:26 +00003951 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003952 {
cristy7f3a0d12011-09-05 23:27:59 +00003953 register ssize_t
3954 i;
3955
3956 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3957 {
3958 MagickRealType
3959 pixel;
3960
3961 PixelChannel
3962 channel;
3963
3964 PixelTrait
3965 traits,
3966 unsharp_traits;
3967
cristye2a912b2011-12-05 20:02:07 +00003968 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003969 traits=GetPixelChannelMapTraits(image,channel);
cristy7f3a0d12011-09-05 23:27:59 +00003970 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3971 if ((traits == UndefinedPixelTrait) ||
3972 (unsharp_traits == UndefinedPixelTrait))
3973 continue;
3974 if ((unsharp_traits & CopyPixelTrait) != 0)
3975 {
cristy0beccfa2011-09-25 20:47:53 +00003976 SetPixelChannel(unsharp_image,channel,p[i],q);
cristy7f3a0d12011-09-05 23:27:59 +00003977 continue;
3978 }
cristy0beccfa2011-09-25 20:47:53 +00003979 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
cristy7f3a0d12011-09-05 23:27:59 +00003980 if (fabs(2.0*pixel) < quantum_threshold)
3981 pixel=(MagickRealType) p[i];
3982 else
3983 pixel=(MagickRealType) p[i]+amount*pixel;
cristy0beccfa2011-09-25 20:47:53 +00003984 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
cristy7f3a0d12011-09-05 23:27:59 +00003985 }
cristyed231572011-07-14 02:18:59 +00003986 p+=GetPixelChannels(image);
3987 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00003988 }
3989 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3990 status=MagickFalse;
3991 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3992 {
3993 MagickBooleanType
3994 proceed;
3995
cristyb5d5f722009-11-04 03:03:49 +00003996#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003997 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00003998#endif
3999 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4000 if (proceed == MagickFalse)
4001 status=MagickFalse;
4002 }
4003 }
4004 unsharp_image->type=image->type;
4005 unsharp_view=DestroyCacheView(unsharp_view);
4006 image_view=DestroyCacheView(image_view);
4007 if (status == MagickFalse)
4008 unsharp_image=DestroyImage(unsharp_image);
4009 return(unsharp_image);
4010}