blob: c7781c033424acb1a66bf0631f818f6657f1ef35 [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);
cristy05c0c9a2011-09-05 23:16:13 +0000237 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,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);
cristy05c0c9a2011-09-05 23:16:13 +0000559 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,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(),
cristye8c25f92010-06-03 00:53:06 +0000923 " BlurImage with %.20g kernel:",(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)
cristyf4ad9df2011-07-08 16:49:03 +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 */
1065 image_view=AcquireCacheView(blur_image);
1066 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001067#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001068 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001069#endif
cristyb6dc4b72011-12-10 23:29:53 +00001070 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001071 {
cristy4c08aed2011-07-01 19:47:50 +00001072 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001073 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001074
cristy4c08aed2011-07-01 19:47:50 +00001075 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001076 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001077
cristy117ff172010-08-15 21:35:32 +00001078 register ssize_t
1079 y;
1080
cristy3ed852e2009-09-05 21:47:34 +00001081 if (status == MagickFalse)
1082 continue;
cristy117ff172010-08-15 21:35:32 +00001083 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
cristyb6dc4b72011-12-10 23:29:53 +00001084 blur_image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001085 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001086 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001087 {
1088 status=MagickFalse;
1089 continue;
1090 }
cristyb6dc4b72011-12-10 23:29:53 +00001091 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001092 {
cristybb503372010-05-27 20:51:26 +00001093 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001094 i;
1095
cristyb6dc4b72011-12-10 23:29:53 +00001096 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
cristyb41a1172011-09-06 00:55:14 +00001097 {
1098 MagickRealType
1099 alpha,
1100 gamma,
1101 pixel;
cristyd25c77e2011-09-06 00:10:24 +00001102
cristyb41a1172011-09-06 00:55:14 +00001103 PixelChannel
1104 channel;
1105
1106 PixelTrait
1107 blur_traits,
1108 traits;
1109
1110 register const double
1111 *restrict k;
1112
1113 register const Quantum
1114 *restrict pixels;
1115
1116 register ssize_t
1117 u;
1118
cristye2a912b2011-12-05 20:02:07 +00001119 channel=GetPixelChannelMapChannel(blur_image,i);
cristyabace412011-12-11 15:56:53 +00001120 traits=GetPixelChannelMapTraits(blur_image,channel);
cristyb41a1172011-09-06 00:55:14 +00001121 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1122 if ((traits == UndefinedPixelTrait) ||
1123 (blur_traits == UndefinedPixelTrait))
1124 continue;
1125 if ((blur_traits & CopyPixelTrait) != 0)
cristyd25c77e2011-09-06 00:10:24 +00001126 {
cristy0beccfa2011-09-25 20:47:53 +00001127 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001128 continue;
cristyd25c77e2011-09-06 00:10:24 +00001129 }
cristyb41a1172011-09-06 00:55:14 +00001130 k=kernel;
1131 pixels=p;
1132 pixel=0.0;
1133 if ((blur_traits & BlendPixelTrait) == 0)
1134 {
1135 /*
1136 No alpha blending.
1137 */
1138 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001139 {
cristyb41a1172011-09-06 00:55:14 +00001140 pixel+=(*k)*pixels[i];
1141 k++;
1142 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001143 }
cristy0beccfa2011-09-25 20:47:53 +00001144 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001145 continue;
1146 }
1147 /*
1148 Alpha blending.
1149 */
1150 gamma=0.0;
1151 for (u=0; u < (ssize_t) width; u++)
1152 {
cristyb6dc4b72011-12-10 23:29:53 +00001153 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1154 pixels));
cristyb41a1172011-09-06 00:55:14 +00001155 pixel+=(*k)*alpha*pixels[i];
1156 gamma+=(*k)*alpha;
1157 k++;
1158 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001159 }
cristyb41a1172011-09-06 00:55:14 +00001160 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001161 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001162 }
cristyd25c77e2011-09-06 00:10:24 +00001163 p+=GetPixelChannels(blur_image);
cristyed231572011-07-14 02:18:59 +00001164 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001165 }
1166 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1167 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001168 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001169 {
1170 MagickBooleanType
1171 proceed;
1172
cristyb5d5f722009-11-04 03:03:49 +00001173#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001174 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001175#endif
cristy4c08aed2011-07-01 19:47:50 +00001176 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1177 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001178 if (proceed == MagickFalse)
1179 status=MagickFalse;
1180 }
1181 }
1182 blur_view=DestroyCacheView(blur_view);
1183 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00001184 kernel=(double *) RelinquishAlignedMemory(kernel);
1185 blur_image->type=image->type;
cristy3ed852e2009-09-05 21:47:34 +00001186 if (status == MagickFalse)
1187 blur_image=DestroyImage(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001188 return(blur_image);
1189}
1190
1191/*
1192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193% %
1194% %
1195% %
cristyfccdab92009-11-30 16:43:57 +00001196% C o n v o l v e I m a g e %
1197% %
1198% %
1199% %
1200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201%
1202% ConvolveImage() applies a custom convolution kernel to the image.
1203%
1204% The format of the ConvolveImage method is:
1205%
cristy5e6be1e2011-07-16 01:23:39 +00001206% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1207% ExceptionInfo *exception)
1208%
cristyfccdab92009-11-30 16:43:57 +00001209% A description of each parameter follows:
1210%
1211% o image: the image.
1212%
cristy5e6be1e2011-07-16 01:23:39 +00001213% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001214%
1215% o exception: return any errors or warnings in this structure.
1216%
1217*/
cristy5e6be1e2011-07-16 01:23:39 +00001218MagickExport Image *ConvolveImage(const Image *image,
1219 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001220{
cristyfccdab92009-11-30 16:43:57 +00001221#define ConvolveImageTag "Convolve/Image"
1222
cristyc4c8d132010-01-07 01:58:38 +00001223 CacheView
1224 *convolve_view,
cristy105ba3c2011-07-18 02:28:38 +00001225 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00001226
cristyfccdab92009-11-30 16:43:57 +00001227 Image
1228 *convolve_image;
1229
cristyfccdab92009-11-30 16:43:57 +00001230 MagickBooleanType
1231 status;
1232
cristybb503372010-05-27 20:51:26 +00001233 MagickOffsetType
1234 progress;
1235
cristybb503372010-05-27 20:51:26 +00001236 ssize_t
cristy574cc262011-08-05 01:23:58 +00001237 center,
cristybb503372010-05-27 20:51:26 +00001238 y;
1239
cristyfccdab92009-11-30 16:43:57 +00001240 /*
1241 Initialize convolve image attributes.
1242 */
1243 assert(image != (Image *) NULL);
1244 assert(image->signature == MagickSignature);
1245 if (image->debug != MagickFalse)
1246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1247 assert(exception != (ExceptionInfo *) NULL);
1248 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001249 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001250 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001251 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1252 exception);
cristyfccdab92009-11-30 16:43:57 +00001253 if (convolve_image == (Image *) NULL)
1254 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001255 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001256 {
cristyfccdab92009-11-30 16:43:57 +00001257 convolve_image=DestroyImage(convolve_image);
1258 return((Image *) NULL);
1259 }
1260 if (image->debug != MagickFalse)
1261 {
1262 char
1263 format[MaxTextExtent],
1264 *message;
1265
cristya96f2492011-12-14 18:25:41 +00001266 register const MagickRealType
cristy117ff172010-08-15 21:35:32 +00001267 *k;
1268
cristy4e154852011-07-14 13:28:53 +00001269 register ssize_t
1270 u;
1271
cristybb503372010-05-27 20:51:26 +00001272 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001273 v;
1274
cristyfccdab92009-11-30 16:43:57 +00001275 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001276 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1277 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001278 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001279 k=kernel_info->values;
1280 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001281 {
1282 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001283 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001284 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001285 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001286 {
cristyb51dff52011-05-19 16:55:47 +00001287 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001288 (void) ConcatenateString(&message,format);
1289 }
1290 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1291 }
1292 message=DestroyString(message);
1293 }
cristy7ea27962011-09-12 18:12:49 +00001294 status=AccelerateConvolveImage(image,kernel_info,convolve_image,exception);
1295 if (status == MagickTrue)
1296 return(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001297 /*
cristyfccdab92009-11-30 16:43:57 +00001298 Convolve image.
anthonyfd706f92012-01-19 04:22:02 +00001299 FUTURE: Use Morphology Convolve instead.
cristyfccdab92009-11-30 16:43:57 +00001300 */
cristy574cc262011-08-05 01:23:58 +00001301 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
cristy075ff302011-09-07 01:51:24 +00001302 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2L);
cristyfccdab92009-11-30 16:43:57 +00001303 status=MagickTrue;
1304 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001305 image_view=AcquireCacheView(image);
1306 convolve_view=AcquireCacheView(convolve_image);
1307#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd36a25e2012-01-18 14:30:53 +00001308 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001309#endif
cristybb503372010-05-27 20:51:26 +00001310 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001311 {
cristy4c08aed2011-07-01 19:47:50 +00001312 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001313 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001314
cristy4c08aed2011-07-01 19:47:50 +00001315 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001316 *restrict q;
1317
cristy117ff172010-08-15 21:35:32 +00001318 register ssize_t
1319 x;
1320
cristyfccdab92009-11-30 16:43:57 +00001321 if (status == MagickFalse)
1322 continue;
cristy105ba3c2011-07-18 02:28:38 +00001323 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1324 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1325 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001326 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001327 exception);
cristy105ba3c2011-07-18 02:28:38 +00001328 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001329 {
1330 status=MagickFalse;
1331 continue;
1332 }
cristybb503372010-05-27 20:51:26 +00001333 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001334 {
cristybb503372010-05-27 20:51:26 +00001335 register ssize_t
cristyed231572011-07-14 02:18:59 +00001336 i;
cristyfccdab92009-11-30 16:43:57 +00001337
cristya30d9ba2011-07-23 21:00:48 +00001338 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001339 {
cristyed231572011-07-14 02:18:59 +00001340 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001341 alpha,
1342 gamma,
cristyed231572011-07-14 02:18:59 +00001343 pixel;
1344
1345 PixelChannel
1346 channel;
1347
1348 PixelTrait
1349 convolve_traits,
1350 traits;
1351
cristya96f2492011-12-14 18:25:41 +00001352 register const MagickRealType
cristyed231572011-07-14 02:18:59 +00001353 *restrict k;
1354
1355 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001356 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001357
1358 register ssize_t
1359 u;
1360
1361 ssize_t
1362 v;
1363
cristye2a912b2011-12-05 20:02:07 +00001364 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001365 traits=GetPixelChannelMapTraits(image,channel);
cristy4e154852011-07-14 13:28:53 +00001366 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001367 if ((traits == UndefinedPixelTrait) ||
1368 (convolve_traits == UndefinedPixelTrait))
cristy4e154852011-07-14 13:28:53 +00001369 continue;
1370 if ((convolve_traits & CopyPixelTrait) != 0)
1371 {
cristy0beccfa2011-09-25 20:47:53 +00001372 SetPixelChannel(convolve_image,channel,p[center+i],q);
cristy4e154852011-07-14 13:28:53 +00001373 continue;
1374 }
cristy5e6be1e2011-07-16 01:23:39 +00001375 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001376 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001377 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001378 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001379 {
cristyed231572011-07-14 02:18:59 +00001380 /*
cristy4e154852011-07-14 13:28:53 +00001381 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001382 */
cristyeb52cde2011-07-17 01:52:52 +00001383 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001384 {
cristyeb52cde2011-07-17 01:52:52 +00001385 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001386 {
cristyeb52cde2011-07-17 01:52:52 +00001387 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001388 k++;
cristya30d9ba2011-07-23 21:00:48 +00001389 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001390 }
cristya30d9ba2011-07-23 21:00:48 +00001391 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001392 }
cristy0beccfa2011-09-25 20:47:53 +00001393 SetPixelChannel(convolve_image,channel,ClampToQuantum(pixel),q);
cristy4e154852011-07-14 13:28:53 +00001394 continue;
cristyed231572011-07-14 02:18:59 +00001395 }
cristy4e154852011-07-14 13:28:53 +00001396 /*
1397 Alpha blending.
1398 */
1399 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001400 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001401 {
cristyeb52cde2011-07-17 01:52:52 +00001402 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001403 {
cristyeb52cde2011-07-17 01:52:52 +00001404 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1405 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001406 gamma+=(*k)*alpha;
1407 k++;
cristya30d9ba2011-07-23 21:00:48 +00001408 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001409 }
cristya30d9ba2011-07-23 21:00:48 +00001410 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001411 }
cristy1ce96d02011-07-14 17:57:24 +00001412 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001413 SetPixelChannel(convolve_image,channel,ClampToQuantum(gamma*pixel),q);
cristyed231572011-07-14 02:18:59 +00001414 }
cristya30d9ba2011-07-23 21:00:48 +00001415 p+=GetPixelChannels(image);
1416 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001417 }
cristyed231572011-07-14 02:18:59 +00001418 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001419 status=MagickFalse;
1420 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1421 {
1422 MagickBooleanType
1423 proceed;
1424
1425#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001426 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001427#endif
1428 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1429 if (proceed == MagickFalse)
1430 status=MagickFalse;
1431 }
1432 }
1433 convolve_image->type=image->type;
1434 convolve_view=DestroyCacheView(convolve_view);
1435 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001436 if (status == MagickFalse)
1437 convolve_image=DestroyImage(convolve_image);
1438 return(convolve_image);
1439}
1440
1441/*
1442%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1443% %
1444% %
1445% %
cristy3ed852e2009-09-05 21:47:34 +00001446% D e s p e c k l e I m a g e %
1447% %
1448% %
1449% %
1450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451%
1452% DespeckleImage() reduces the speckle noise in an image while perserving the
cristyf0ae7762012-01-15 22:04:03 +00001453% edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1454% neighbors, then complementarily lowering pixels that are brighter than their
1455% surrounding neighbors) to reduce the speckle index of that image (reference
1456% Crimmins speckle removal).
cristy3ed852e2009-09-05 21:47:34 +00001457%
1458% The format of the DespeckleImage method is:
1459%
1460% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1461%
1462% A description of each parameter follows:
1463%
1464% o image: the image.
1465%
1466% o exception: return any errors or warnings in this structure.
1467%
1468*/
1469
cristy5473bf02012-01-16 19:15:08 +00001470static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1471 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1472 Quantum *restrict g)
cristy3ed852e2009-09-05 21:47:34 +00001473{
cristy7b4b8862012-01-16 19:52:19 +00001474 register Quantum
cristy5473bf02012-01-16 19:15:08 +00001475 *p,
1476 *q,
1477 *r,
1478 *s;
cristy78f3de22012-01-16 00:43:18 +00001479
cristy5473bf02012-01-16 19:15:08 +00001480 ssize_t
1481 y;
1482
1483 assert(f != (Quantum *) NULL);
1484 assert(g != (Quantum *) NULL);
1485 p=f+(columns+2);
1486 q=g+(columns+2);
1487 r=p+(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001488#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd36a25e2012-01-18 14:30:53 +00001489 #pragma omp parallel for schedule(static,4)
cristy5473bf02012-01-16 19:15:08 +00001490#endif
1491 for (y=0; y < (ssize_t) rows; y++)
1492 {
cristy7b4b8862012-01-16 19:52:19 +00001493 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001494 i,
1495 x;
1496
cristy7b4b8862012-01-16 19:52:19 +00001497 SignedQuantum
1498 v;
1499
cristy5473bf02012-01-16 19:15:08 +00001500 i=(2*y+1)+y*columns;
1501 if (polarity > 0)
1502 for (x=0; x < (ssize_t) columns; x++)
1503 {
1504 v=(SignedQuantum) p[i];
1505 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1506 v+=ScaleCharToQuantum(1);
1507 q[i]=(Quantum) v;
1508 i++;
1509 }
1510 else
1511 for (x=0; x < (ssize_t) columns; x++)
1512 {
1513 v=(SignedQuantum) p[i];
1514 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1515 v-=ScaleCharToQuantum(1);
1516 q[i]=(Quantum) v;
1517 i++;
1518 }
1519 }
1520 p=f+(columns+2);
1521 q=g+(columns+2);
1522 r=q+(y_offset*(columns+2)+x_offset);
1523 s=q-(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001524#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd36a25e2012-01-18 14:30:53 +00001525 #pragma omp parallel for schedule(static,4)
cristy5473bf02012-01-16 19:15:08 +00001526#endif
1527 for (y=0; y < (ssize_t) rows; y++)
1528 {
cristy7b4b8862012-01-16 19:52:19 +00001529 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001530 i,
1531 x;
1532
cristy7b4b8862012-01-16 19:52:19 +00001533 SignedQuantum
1534 v;
1535
cristy5473bf02012-01-16 19:15:08 +00001536 i=(2*y+1)+y*columns;
1537 if (polarity > 0)
1538 for (x=0; x < (ssize_t) columns; x++)
1539 {
1540 v=(SignedQuantum) q[i];
1541 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1542 ((SignedQuantum) r[i] > v))
1543 v+=ScaleCharToQuantum(1);
1544 p[i]=(Quantum) v;
1545 i++;
1546 }
1547 else
1548 for (x=0; x < (ssize_t) columns; x++)
1549 {
1550 v=(SignedQuantum) q[i];
1551 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1552 ((SignedQuantum) r[i] < v))
1553 v-=ScaleCharToQuantum(1);
1554 p[i]=(Quantum) v;
1555 i++;
1556 }
1557 }
cristy3ed852e2009-09-05 21:47:34 +00001558}
1559
1560MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1561{
1562#define DespeckleImageTag "Despeckle/Image"
1563
cristy2407fc22009-09-11 00:55:25 +00001564 CacheView
1565 *despeckle_view,
1566 *image_view;
1567
cristy3ed852e2009-09-05 21:47:34 +00001568 Image
1569 *despeckle_image;
1570
cristy3ed852e2009-09-05 21:47:34 +00001571 MagickBooleanType
1572 status;
1573
cristy5473bf02012-01-16 19:15:08 +00001574 Quantum
1575 *restrict buffer,
1576 *restrict pixels;
cristya63e4a92011-09-09 00:47:59 +00001577
cristy5473bf02012-01-16 19:15:08 +00001578 register ssize_t
1579 i;
1580
1581 size_t
1582 length;
cristy117ff172010-08-15 21:35:32 +00001583
cristybb503372010-05-27 20:51:26 +00001584 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001585 X[4] = {0, 1, 1,-1},
1586 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001587
cristy3ed852e2009-09-05 21:47:34 +00001588 /*
1589 Allocate despeckled image.
1590 */
1591 assert(image != (const Image *) NULL);
1592 assert(image->signature == MagickSignature);
1593 if (image->debug != MagickFalse)
1594 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1595 assert(exception != (ExceptionInfo *) NULL);
1596 assert(exception->signature == MagickSignature);
cristy5473bf02012-01-16 19:15:08 +00001597 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001598 if (despeckle_image == (Image *) NULL)
1599 return((Image *) NULL);
cristya63e4a92011-09-09 00:47:59 +00001600 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1601 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001602 {
cristy3ed852e2009-09-05 21:47:34 +00001603 despeckle_image=DestroyImage(despeckle_image);
1604 return((Image *) NULL);
1605 }
1606 /*
cristy5473bf02012-01-16 19:15:08 +00001607 Allocate image buffer.
1608 */
1609 length=(size_t) ((image->columns+2)*(image->rows+2));
1610 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1611 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1612 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1613 {
1614 if (buffer != (Quantum *) NULL)
1615 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1616 if (pixels != (Quantum *) NULL)
1617 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1618 despeckle_image=DestroyImage(despeckle_image);
1619 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1620 }
1621 /*
1622 Reduce speckle in the image.
cristy3ed852e2009-09-05 21:47:34 +00001623 */
1624 status=MagickTrue;
cristyb06da092012-01-16 17:11:38 +00001625 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00001626 despeckle_view=AcquireCacheView(despeckle_image);
cristy5473bf02012-01-16 19:15:08 +00001627 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +00001628 {
cristy5473bf02012-01-16 19:15:08 +00001629 PixelChannel
1630 channel;
1631
1632 PixelTrait
1633 despeckle_traits,
1634 traits;
cristy3ed852e2009-09-05 21:47:34 +00001635
cristyc1488b52011-02-19 18:54:15 +00001636 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001637 k,
cristyf0ae7762012-01-15 22:04:03 +00001638 x;
cristyc1488b52011-02-19 18:54:15 +00001639
cristy5473bf02012-01-16 19:15:08 +00001640 ssize_t
1641 j,
1642 y;
1643
1644 if (status == MagickFalse)
1645 continue;
1646 channel=GetPixelChannelMapChannel(image,i);
1647 traits=GetPixelChannelMapTraits(image,channel);
1648 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1649 if ((traits == UndefinedPixelTrait) ||
1650 (despeckle_traits == UndefinedPixelTrait))
1651 continue;
1652 if ((despeckle_traits & CopyPixelTrait) != 0)
1653 continue;
1654 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1655 j=(ssize_t) image->columns+2;
1656 for (y=0; y < (ssize_t) image->rows; y++)
cristyfc830f42012-01-15 02:45:06 +00001657 {
cristy5473bf02012-01-16 19:15:08 +00001658 register const Quantum
1659 *restrict p;
cristyfc830f42012-01-15 02:45:06 +00001660
cristy5473bf02012-01-16 19:15:08 +00001661 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1662 if (p == (const Quantum *) NULL)
cristyf0ae7762012-01-15 22:04:03 +00001663 {
cristy5473bf02012-01-16 19:15:08 +00001664 status=MagickFalse;
1665 continue;
cristyf0ae7762012-01-15 22:04:03 +00001666 }
cristy5473bf02012-01-16 19:15:08 +00001667 j++;
1668 for (x=0; x < (ssize_t) image->columns; x++)
1669 {
1670 pixels[j++]=p[i];
1671 p+=GetPixelChannels(image);
cristyfc830f42012-01-15 02:45:06 +00001672 }
cristy5473bf02012-01-16 19:15:08 +00001673 j++;
cristy3ed852e2009-09-05 21:47:34 +00001674 }
cristy5473bf02012-01-16 19:15:08 +00001675 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1676 for (k=0; k < 4; k++)
1677 {
1678 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
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 }
1683 j=(ssize_t) image->columns+2;
1684 for (y=0; y < (ssize_t) image->rows; y++)
1685 {
1686 MagickBooleanType
1687 sync;
1688
1689 register Quantum
1690 *restrict q;
1691
cristyd13e8eb2012-01-17 02:36:42 +00001692 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1693 despeckle_image->columns,1,exception);
cristy5473bf02012-01-16 19:15:08 +00001694 if (q == (Quantum *) NULL)
1695 {
1696 status=MagickFalse;
1697 continue;
1698 }
1699 j++;
1700 for (x=0; x < (ssize_t) image->columns; x++)
1701 {
1702 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1703 q+=GetPixelChannels(despeckle_image);
1704 }
1705 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1706 if (sync == MagickFalse)
1707 status=MagickFalse;
1708 j++;
1709 }
cristy3ed852e2009-09-05 21:47:34 +00001710 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1711 {
1712 MagickBooleanType
1713 proceed;
1714
cristy5473bf02012-01-16 19:15:08 +00001715 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1716 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00001717 if (proceed == MagickFalse)
1718 status=MagickFalse;
1719 }
1720 }
1721 despeckle_view=DestroyCacheView(despeckle_view);
1722 image_view=DestroyCacheView(image_view);
cristy5473bf02012-01-16 19:15:08 +00001723 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1724 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001725 despeckle_image->type=image->type;
1726 if (status == MagickFalse)
1727 despeckle_image=DestroyImage(despeckle_image);
1728 return(despeckle_image);
1729}
1730
1731/*
1732%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733% %
1734% %
1735% %
1736% E d g e I m a g e %
1737% %
1738% %
1739% %
1740%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1741%
1742% EdgeImage() finds edges in an image. Radius defines the radius of the
1743% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1744% radius for you.
1745%
1746% The format of the EdgeImage method is:
1747%
1748% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001749% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001750%
1751% A description of each parameter follows:
1752%
1753% o image: the image.
1754%
1755% o radius: the radius of the pixel neighborhood.
1756%
cristy8ae632d2011-09-05 17:29:53 +00001757% o sigma: the standard deviation of the Gaussian, in pixels.
1758%
cristy3ed852e2009-09-05 21:47:34 +00001759% o exception: return any errors or warnings in this structure.
1760%
1761*/
1762MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001763 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001764{
1765 Image
1766 *edge_image;
1767
cristy41cbe682011-07-15 19:12:37 +00001768 KernelInfo
1769 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001770
cristybb503372010-05-27 20:51:26 +00001771 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001772 i;
1773
cristybb503372010-05-27 20:51:26 +00001774 size_t
cristy3ed852e2009-09-05 21:47:34 +00001775 width;
1776
cristy41cbe682011-07-15 19:12:37 +00001777 ssize_t
1778 j,
1779 u,
1780 v;
1781
cristy3ed852e2009-09-05 21:47:34 +00001782 assert(image != (const Image *) NULL);
1783 assert(image->signature == MagickSignature);
1784 if (image->debug != MagickFalse)
1785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1786 assert(exception != (ExceptionInfo *) NULL);
1787 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001788 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001789 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001790 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001791 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001792 kernel_info->width=width;
1793 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001794 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1795 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1796 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001797 {
1798 kernel_info=DestroyKernelInfo(kernel_info);
1799 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1800 }
1801 j=(ssize_t) kernel_info->width/2;
1802 i=0;
1803 for (v=(-j); v <= j; v++)
1804 {
1805 for (u=(-j); u <= j; u++)
1806 {
1807 kernel_info->values[i]=(-1.0);
1808 i++;
1809 }
1810 }
1811 kernel_info->values[i/2]=(double) (width*width-1.0);
anthony736a1602011-10-06 12:38:17 +00001812 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
cristy5e6be1e2011-07-16 01:23:39 +00001813 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001814 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001815 return(edge_image);
1816}
1817
1818/*
1819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1820% %
1821% %
1822% %
1823% E m b o s s I m a g e %
1824% %
1825% %
1826% %
1827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1828%
1829% EmbossImage() returns a grayscale image with a three-dimensional effect.
1830% We convolve the image with a Gaussian operator of the given radius and
1831% standard deviation (sigma). For reasonable results, radius should be
1832% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1833% radius for you.
1834%
1835% The format of the EmbossImage method is:
1836%
1837% Image *EmbossImage(const Image *image,const double radius,
1838% const double sigma,ExceptionInfo *exception)
1839%
1840% A description of each parameter follows:
1841%
1842% o image: the image.
1843%
1844% o radius: the radius of the pixel neighborhood.
1845%
1846% o sigma: the standard deviation of the Gaussian, in pixels.
1847%
1848% o exception: return any errors or warnings in this structure.
1849%
1850*/
1851MagickExport Image *EmbossImage(const Image *image,const double radius,
1852 const double sigma,ExceptionInfo *exception)
1853{
cristy3ed852e2009-09-05 21:47:34 +00001854 Image
1855 *emboss_image;
1856
cristy41cbe682011-07-15 19:12:37 +00001857 KernelInfo
1858 *kernel_info;
1859
cristybb503372010-05-27 20:51:26 +00001860 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001861 i;
1862
cristybb503372010-05-27 20:51:26 +00001863 size_t
cristy3ed852e2009-09-05 21:47:34 +00001864 width;
1865
cristy117ff172010-08-15 21:35:32 +00001866 ssize_t
1867 j,
1868 k,
1869 u,
1870 v;
1871
cristy41cbe682011-07-15 19:12:37 +00001872 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001873 assert(image->signature == MagickSignature);
1874 if (image->debug != MagickFalse)
1875 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1876 assert(exception != (ExceptionInfo *) NULL);
1877 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001878 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001879 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001880 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001881 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001882 kernel_info->width=width;
1883 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001884 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1885 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1886 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001887 {
1888 kernel_info=DestroyKernelInfo(kernel_info);
1889 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1890 }
1891 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001892 k=j;
1893 i=0;
1894 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001895 {
cristy47e00502009-12-17 19:19:57 +00001896 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001897 {
cristy41cbe682011-07-15 19:12:37 +00001898 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001899 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001900 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001901 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001902 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001903 i++;
1904 }
cristy47e00502009-12-17 19:19:57 +00001905 k--;
cristy3ed852e2009-09-05 21:47:34 +00001906 }
anthony736a1602011-10-06 12:38:17 +00001907 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
cristy5e6be1e2011-07-16 01:23:39 +00001908 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001909 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001910 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001911 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001912 return(emboss_image);
1913}
1914
1915/*
1916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1917% %
1918% %
1919% %
1920% G a u s s i a n B l u r I m a g e %
1921% %
1922% %
1923% %
1924%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1925%
1926% GaussianBlurImage() blurs an image. We convolve the image with a
1927% Gaussian operator of the given radius and standard deviation (sigma).
1928% For reasonable results, the radius should be larger than sigma. Use a
1929% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1930%
1931% The format of the GaussianBlurImage method is:
1932%
1933% Image *GaussianBlurImage(const Image *image,onst double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001934% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001935%
1936% A description of each parameter follows:
1937%
1938% o image: the image.
1939%
cristy3ed852e2009-09-05 21:47:34 +00001940% o radius: the radius of the Gaussian, in pixels, not counting the center
1941% pixel.
1942%
1943% o sigma: the standard deviation of the Gaussian, in pixels.
1944%
cristy05c0c9a2011-09-05 23:16:13 +00001945% o bias: the bias.
1946%
cristy3ed852e2009-09-05 21:47:34 +00001947% o exception: return any errors or warnings in this structure.
1948%
1949*/
cristy41cbe682011-07-15 19:12:37 +00001950MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001951 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001952{
cristy3ed852e2009-09-05 21:47:34 +00001953 Image
1954 *blur_image;
1955
cristy41cbe682011-07-15 19:12:37 +00001956 KernelInfo
1957 *kernel_info;
1958
cristybb503372010-05-27 20:51:26 +00001959 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001960 i;
1961
cristybb503372010-05-27 20:51:26 +00001962 size_t
cristy3ed852e2009-09-05 21:47:34 +00001963 width;
1964
cristy117ff172010-08-15 21:35:32 +00001965 ssize_t
1966 j,
1967 u,
1968 v;
1969
cristy3ed852e2009-09-05 21:47:34 +00001970 assert(image != (const Image *) NULL);
1971 assert(image->signature == MagickSignature);
1972 if (image->debug != MagickFalse)
1973 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1974 assert(exception != (ExceptionInfo *) NULL);
1975 assert(exception->signature == MagickSignature);
1976 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001977 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001978 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001979 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001980 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1981 kernel_info->width=width;
1982 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00001983 kernel_info->bias=bias; /* FUTURE: user bias on Gaussian Blur! non-sense */
cristy41cbe682011-07-15 19:12:37 +00001984 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00001985 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1986 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1987 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001988 {
1989 kernel_info=DestroyKernelInfo(kernel_info);
1990 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1991 }
1992 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001993 i=0;
cristy47e00502009-12-17 19:19:57 +00001994 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001995 {
cristy47e00502009-12-17 19:19:57 +00001996 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001997 {
1998 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1999 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2000 i++;
2001 }
cristy3ed852e2009-09-05 21:47:34 +00002002 }
cristy5e6be1e2011-07-16 01:23:39 +00002003 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00002004 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00002005 return(blur_image);
2006}
2007
2008/*
2009%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2010% %
2011% %
2012% %
cristy3ed852e2009-09-05 21:47:34 +00002013% M o t i o n B l u r I m a g e %
2014% %
2015% %
2016% %
2017%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2018%
2019% MotionBlurImage() simulates motion blur. We convolve the image with a
2020% Gaussian operator of the given radius and standard deviation (sigma).
2021% For reasonable results, radius should be larger than sigma. Use a
2022% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2023% Angle gives the angle of the blurring motion.
2024%
2025% Andrew Protano contributed this effect.
2026%
2027% The format of the MotionBlurImage method is:
2028%
2029% Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002030% const double sigma,const double angle,const double bias,
2031% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002032%
2033% A description of each parameter follows:
2034%
2035% o image: the image.
2036%
cristy3ed852e2009-09-05 21:47:34 +00002037% o radius: the radius of the Gaussian, in pixels, not counting
2038% the center pixel.
2039%
2040% o sigma: the standard deviation of the Gaussian, in pixels.
2041%
cristycee97112010-05-28 00:44:52 +00002042% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002043%
cristyf7ef0252011-09-09 14:50:06 +00002044% o bias: the bias.
2045%
cristy3ed852e2009-09-05 21:47:34 +00002046% o exception: return any errors or warnings in this structure.
2047%
2048*/
2049
cristybb503372010-05-27 20:51:26 +00002050static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002051{
cristy3ed852e2009-09-05 21:47:34 +00002052 double
cristy47e00502009-12-17 19:19:57 +00002053 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002054 normalize;
2055
cristybb503372010-05-27 20:51:26 +00002056 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002057 i;
2058
2059 /*
cristy47e00502009-12-17 19:19:57 +00002060 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002061 */
2062 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +00002063 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00002064 if (kernel == (double *) NULL)
2065 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002066 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002067 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002068 {
cristy4205a3c2010-09-12 20:19:59 +00002069 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2070 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002071 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002072 }
cristybb503372010-05-27 20:51:26 +00002073 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002074 kernel[i]/=normalize;
2075 return(kernel);
2076}
2077
cristya63e4a92011-09-09 00:47:59 +00002078MagickExport Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002079 const double sigma,const double angle,const double bias,
2080 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002081{
cristyc4c8d132010-01-07 01:58:38 +00002082 CacheView
2083 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002084 *image_view,
2085 *motion_view;
cristyc4c8d132010-01-07 01:58:38 +00002086
cristy3ed852e2009-09-05 21:47:34 +00002087 double
2088 *kernel;
2089
2090 Image
2091 *blur_image;
2092
cristy3ed852e2009-09-05 21:47:34 +00002093 MagickBooleanType
2094 status;
2095
cristybb503372010-05-27 20:51:26 +00002096 MagickOffsetType
2097 progress;
2098
cristy3ed852e2009-09-05 21:47:34 +00002099 OffsetInfo
2100 *offset;
2101
2102 PointInfo
2103 point;
2104
cristybb503372010-05-27 20:51:26 +00002105 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002106 i;
2107
cristybb503372010-05-27 20:51:26 +00002108 size_t
cristy3ed852e2009-09-05 21:47:34 +00002109 width;
2110
cristybb503372010-05-27 20:51:26 +00002111 ssize_t
2112 y;
2113
cristy3ed852e2009-09-05 21:47:34 +00002114 assert(image != (Image *) NULL);
2115 assert(image->signature == MagickSignature);
2116 if (image->debug != MagickFalse)
2117 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2118 assert(exception != (ExceptionInfo *) NULL);
2119 width=GetOptimalKernelWidth1D(radius,sigma);
2120 kernel=GetMotionBlurKernel(width,sigma);
2121 if (kernel == (double *) NULL)
2122 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2123 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2124 if (offset == (OffsetInfo *) NULL)
2125 {
cristy8b49f382012-01-17 03:11:03 +00002126 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002127 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2128 }
cristy1e7aa312011-09-10 20:01:36 +00002129 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002130 if (blur_image == (Image *) NULL)
2131 {
cristy8b49f382012-01-17 03:11:03 +00002132 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002133 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2134 return((Image *) NULL);
2135 }
cristy574cc262011-08-05 01:23:58 +00002136 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002137 {
cristy8b49f382012-01-17 03:11:03 +00002138 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002139 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002140 blur_image=DestroyImage(blur_image);
2141 return((Image *) NULL);
2142 }
2143 point.x=(double) width*sin(DegreesToRadians(angle));
2144 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002145 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002146 {
cristybb503372010-05-27 20:51:26 +00002147 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2148 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002149 }
2150 /*
2151 Motion blur image.
2152 */
2153 status=MagickTrue;
2154 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002155 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002156 motion_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002157 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002158#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00002159 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002160#endif
cristybb503372010-05-27 20:51:26 +00002161 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002162 {
cristyf7ef0252011-09-09 14:50:06 +00002163 register const Quantum
2164 *restrict p;
2165
cristy4c08aed2011-07-01 19:47:50 +00002166 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002167 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002168
cristy117ff172010-08-15 21:35:32 +00002169 register ssize_t
2170 x;
2171
cristy3ed852e2009-09-05 21:47:34 +00002172 if (status == MagickFalse)
2173 continue;
cristy8b49f382012-01-17 03:11:03 +00002174 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2175 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002176 exception);
cristyf7ef0252011-09-09 14:50:06 +00002177 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002178 {
2179 status=MagickFalse;
2180 continue;
2181 }
cristybb503372010-05-27 20:51:26 +00002182 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002183 {
cristybb503372010-05-27 20:51:26 +00002184 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002185 i;
2186
cristyf7ef0252011-09-09 14:50:06 +00002187 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2188 {
2189 MagickRealType
2190 alpha,
2191 gamma,
2192 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002193
cristyf7ef0252011-09-09 14:50:06 +00002194 PixelChannel
2195 channel;
2196
2197 PixelTrait
2198 blur_traits,
2199 traits;
2200
2201 register const Quantum
2202 *restrict r;
2203
2204 register double
2205 *restrict k;
2206
2207 register ssize_t
2208 j;
2209
cristye2a912b2011-12-05 20:02:07 +00002210 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002211 traits=GetPixelChannelMapTraits(image,channel);
cristyf7ef0252011-09-09 14:50:06 +00002212 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2213 if ((traits == UndefinedPixelTrait) ||
2214 (blur_traits == UndefinedPixelTrait))
2215 continue;
2216 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002217 {
cristy0beccfa2011-09-25 20:47:53 +00002218 SetPixelChannel(blur_image,channel,p[i],q);
cristyf7ef0252011-09-09 14:50:06 +00002219 continue;
cristy3ed852e2009-09-05 21:47:34 +00002220 }
cristyf7ef0252011-09-09 14:50:06 +00002221 k=kernel;
2222 pixel=bias;
2223 if ((blur_traits & BlendPixelTrait) == 0)
2224 {
2225 for (j=0; j < (ssize_t) width; j++)
2226 {
cristy8b49f382012-01-17 03:11:03 +00002227 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
cristyf7ef0252011-09-09 14:50:06 +00002228 offset[j].y,1,1,exception);
2229 if (r == (const Quantum *) NULL)
2230 {
2231 status=MagickFalse;
2232 continue;
2233 }
2234 pixel+=(*k)*r[i];
2235 k++;
2236 }
cristy0beccfa2011-09-25 20:47:53 +00002237 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002238 continue;
2239 }
2240 alpha=0.0;
2241 gamma=0.0;
2242 for (j=0; j < (ssize_t) width; j++)
2243 {
cristy8b49f382012-01-17 03:11:03 +00002244 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
cristyf7ef0252011-09-09 14:50:06 +00002245 1,exception);
2246 if (r == (const Quantum *) NULL)
2247 {
2248 status=MagickFalse;
2249 continue;
2250 }
2251 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2252 pixel+=(*k)*alpha*r[i];
2253 gamma+=(*k)*alpha;
2254 k++;
cristy3ed852e2009-09-05 21:47:34 +00002255 }
cristyf7ef0252011-09-09 14:50:06 +00002256 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002257 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002258 }
2259 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002260 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002261 }
2262 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2263 status=MagickFalse;
2264 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2265 {
2266 MagickBooleanType
2267 proceed;
2268
cristyb557a152011-02-22 12:14:30 +00002269#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002270 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002271#endif
2272 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2273 if (proceed == MagickFalse)
2274 status=MagickFalse;
2275 }
2276 }
2277 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00002278 motion_view=DestroyCacheView(motion_view);
cristy3ed852e2009-09-05 21:47:34 +00002279 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00002280 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002281 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2282 if (status == MagickFalse)
2283 blur_image=DestroyImage(blur_image);
2284 return(blur_image);
2285}
2286
2287/*
2288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2289% %
2290% %
2291% %
2292% P r e v i e w I m a g e %
2293% %
2294% %
2295% %
2296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2297%
2298% PreviewImage() tiles 9 thumbnails of the specified image with an image
2299% processing operation applied with varying parameters. This may be helpful
2300% pin-pointing an appropriate parameter for a particular image processing
2301% operation.
2302%
2303% The format of the PreviewImages method is:
2304%
2305% Image *PreviewImages(const Image *image,const PreviewType preview,
2306% ExceptionInfo *exception)
2307%
2308% A description of each parameter follows:
2309%
2310% o image: the image.
2311%
2312% o preview: the image processing operation.
2313%
2314% o exception: return any errors or warnings in this structure.
2315%
2316*/
2317MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2318 ExceptionInfo *exception)
2319{
2320#define NumberTiles 9
2321#define PreviewImageTag "Preview/Image"
2322#define DefaultPreviewGeometry "204x204+10+10"
2323
2324 char
2325 factor[MaxTextExtent],
2326 label[MaxTextExtent];
2327
2328 double
2329 degrees,
2330 gamma,
2331 percentage,
2332 radius,
2333 sigma,
2334 threshold;
2335
2336 Image
2337 *images,
2338 *montage_image,
2339 *preview_image,
2340 *thumbnail;
2341
2342 ImageInfo
2343 *preview_info;
2344
cristy3ed852e2009-09-05 21:47:34 +00002345 MagickBooleanType
2346 proceed;
2347
2348 MontageInfo
2349 *montage_info;
2350
2351 QuantizeInfo
2352 quantize_info;
2353
2354 RectangleInfo
2355 geometry;
2356
cristybb503372010-05-27 20:51:26 +00002357 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002358 i,
2359 x;
2360
cristybb503372010-05-27 20:51:26 +00002361 size_t
cristy3ed852e2009-09-05 21:47:34 +00002362 colors;
2363
cristy117ff172010-08-15 21:35:32 +00002364 ssize_t
2365 y;
2366
cristy3ed852e2009-09-05 21:47:34 +00002367 /*
2368 Open output image file.
2369 */
2370 assert(image != (Image *) NULL);
2371 assert(image->signature == MagickSignature);
2372 if (image->debug != MagickFalse)
2373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2374 colors=2;
2375 degrees=0.0;
2376 gamma=(-0.2f);
2377 preview_info=AcquireImageInfo();
2378 SetGeometry(image,&geometry);
2379 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2380 &geometry.width,&geometry.height);
2381 images=NewImageList();
2382 percentage=12.5;
2383 GetQuantizeInfo(&quantize_info);
2384 radius=0.0;
2385 sigma=1.0;
2386 threshold=0.0;
2387 x=0;
2388 y=0;
2389 for (i=0; i < NumberTiles; i++)
2390 {
2391 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2392 if (thumbnail == (Image *) NULL)
2393 break;
2394 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2395 (void *) NULL);
cristyd15e6592011-10-15 00:13:06 +00002396 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002397 if (i == (NumberTiles/2))
2398 {
cristy9950d572011-10-01 18:22:35 +00002399 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2400 &thumbnail->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002401 AppendImageToList(&images,thumbnail);
2402 continue;
2403 }
2404 switch (preview)
2405 {
2406 case RotatePreview:
2407 {
2408 degrees+=45.0;
2409 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002410 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002411 break;
2412 }
2413 case ShearPreview:
2414 {
2415 degrees+=5.0;
2416 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002417 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002418 degrees,2.0*degrees);
2419 break;
2420 }
2421 case RollPreview:
2422 {
cristybb503372010-05-27 20:51:26 +00002423 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2424 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002425 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002426 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002427 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002428 break;
2429 }
2430 case HuePreview:
2431 {
2432 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2433 if (preview_image == (Image *) NULL)
2434 break;
cristyb51dff52011-05-19 16:55:47 +00002435 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002436 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002437 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002438 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002439 break;
2440 }
2441 case SaturationPreview:
2442 {
2443 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2444 if (preview_image == (Image *) NULL)
2445 break;
cristyb51dff52011-05-19 16:55:47 +00002446 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002447 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002448 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002449 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002450 break;
2451 }
2452 case BrightnessPreview:
2453 {
2454 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2455 if (preview_image == (Image *) NULL)
2456 break;
cristyb51dff52011-05-19 16:55:47 +00002457 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002458 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002459 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002460 break;
2461 }
2462 case GammaPreview:
2463 default:
2464 {
2465 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2466 if (preview_image == (Image *) NULL)
2467 break;
2468 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002469 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002470 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002471 break;
2472 }
2473 case SpiffPreview:
2474 {
2475 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2476 if (preview_image != (Image *) NULL)
2477 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002478 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002479 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002480 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002481 break;
2482 }
2483 case DullPreview:
2484 {
2485 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2486 if (preview_image == (Image *) NULL)
2487 break;
2488 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002489 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002490 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002491 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002492 break;
2493 }
2494 case GrayscalePreview:
2495 {
2496 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2497 if (preview_image == (Image *) NULL)
2498 break;
2499 colors<<=1;
2500 quantize_info.number_colors=colors;
2501 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002502 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002503 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002504 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002505 break;
2506 }
2507 case QuantizePreview:
2508 {
2509 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2510 if (preview_image == (Image *) NULL)
2511 break;
2512 colors<<=1;
2513 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002514 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002515 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002516 colors);
cristy3ed852e2009-09-05 21:47:34 +00002517 break;
2518 }
2519 case DespecklePreview:
2520 {
2521 for (x=0; x < (i-1); x++)
2522 {
2523 preview_image=DespeckleImage(thumbnail,exception);
2524 if (preview_image == (Image *) NULL)
2525 break;
2526 thumbnail=DestroyImage(thumbnail);
2527 thumbnail=preview_image;
2528 }
2529 preview_image=DespeckleImage(thumbnail,exception);
2530 if (preview_image == (Image *) NULL)
2531 break;
cristyb51dff52011-05-19 16:55:47 +00002532 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002533 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002534 break;
2535 }
2536 case ReduceNoisePreview:
2537 {
cristy95c38342011-03-18 22:39:51 +00002538 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2539 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002540 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002541 break;
2542 }
2543 case AddNoisePreview:
2544 {
2545 switch ((int) i)
2546 {
2547 case 0:
2548 {
2549 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2550 break;
2551 }
2552 case 1:
2553 {
2554 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2555 break;
2556 }
2557 case 2:
2558 {
2559 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2560 break;
2561 }
2562 case 3:
2563 {
2564 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2565 break;
2566 }
2567 case 4:
2568 {
2569 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2570 break;
2571 }
2572 case 5:
2573 {
2574 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2575 break;
2576 }
2577 default:
2578 {
2579 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2580 break;
2581 }
2582 }
cristyd76c51e2011-03-26 00:21:26 +00002583 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2584 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002585 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002586 break;
2587 }
2588 case SharpenPreview:
2589 {
anthony736a1602011-10-06 12:38:17 +00002590 /* FUTURE: user bias on sharpen! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002591 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2592 exception);
cristyb51dff52011-05-19 16:55:47 +00002593 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002594 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002595 break;
2596 }
2597 case BlurPreview:
2598 {
anthony736a1602011-10-06 12:38:17 +00002599 /* FUTURE: user bias on blur! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002600 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002601 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002602 sigma);
2603 break;
2604 }
2605 case ThresholdPreview:
2606 {
2607 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2608 if (preview_image == (Image *) NULL)
2609 break;
cristye941a752011-10-15 01:52:48 +00002610 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2611 QuantumRange+1.0))/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002612 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002613 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2614 break;
2615 }
2616 case EdgeDetectPreview:
2617 {
cristy8ae632d2011-09-05 17:29:53 +00002618 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002619 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002620 break;
2621 }
2622 case SpreadPreview:
2623 {
cristy5c4e2582011-09-11 19:21:03 +00002624 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2625 exception);
cristyb51dff52011-05-19 16:55:47 +00002626 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002627 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002628 break;
2629 }
2630 case SolarizePreview:
2631 {
2632 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2633 if (preview_image == (Image *) NULL)
2634 break;
2635 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002636 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002637 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002638 (QuantumRange*percentage)/100.0);
2639 break;
2640 }
2641 case ShadePreview:
2642 {
2643 degrees+=10.0;
2644 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2645 exception);
cristyb51dff52011-05-19 16:55:47 +00002646 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002647 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002648 break;
2649 }
2650 case RaisePreview:
2651 {
2652 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2653 if (preview_image == (Image *) NULL)
2654 break;
cristybb503372010-05-27 20:51:26 +00002655 geometry.width=(size_t) (2*i+2);
2656 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002657 geometry.x=i/2;
2658 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002659 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002660 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002661 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002662 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002663 break;
2664 }
2665 case SegmentPreview:
2666 {
2667 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2668 if (preview_image == (Image *) NULL)
2669 break;
2670 threshold+=0.4f;
2671 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002672 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002673 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002674 threshold,threshold);
2675 break;
2676 }
2677 case SwirlPreview:
2678 {
cristy76f512e2011-09-12 01:26:56 +00002679 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2680 exception);
cristyb51dff52011-05-19 16:55:47 +00002681 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002682 degrees+=45.0;
2683 break;
2684 }
2685 case ImplodePreview:
2686 {
2687 degrees+=0.1f;
cristy76f512e2011-09-12 01:26:56 +00002688 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2689 exception);
cristyb51dff52011-05-19 16:55:47 +00002690 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002691 break;
2692 }
2693 case WavePreview:
2694 {
2695 degrees+=5.0f;
cristy5c4e2582011-09-11 19:21:03 +00002696 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2697 image->interpolate,exception);
cristyb51dff52011-05-19 16:55:47 +00002698 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002699 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002700 break;
2701 }
2702 case OilPaintPreview:
2703 {
cristy14973ba2011-08-27 23:48:07 +00002704 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2705 exception);
2706 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2707 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002708 break;
2709 }
2710 case CharcoalDrawingPreview:
2711 {
anthony736a1602011-10-06 12:38:17 +00002712 /* FUTURE: user bias on charcoal! This is non-sensical! */
cristy3ed852e2009-09-05 21:47:34 +00002713 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristy05c0c9a2011-09-05 23:16:13 +00002714 image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002715 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002716 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002717 break;
2718 }
2719 case JPEGPreview:
2720 {
2721 char
2722 filename[MaxTextExtent];
2723
2724 int
2725 file;
2726
2727 MagickBooleanType
2728 status;
2729
2730 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2731 if (preview_image == (Image *) NULL)
2732 break;
cristybb503372010-05-27 20:51:26 +00002733 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002734 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002735 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002736 file=AcquireUniqueFileResource(filename);
2737 if (file != -1)
2738 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002739 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002740 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002741 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002742 if (status != MagickFalse)
2743 {
2744 Image
2745 *quality_image;
2746
2747 (void) CopyMagickString(preview_info->filename,
2748 preview_image->filename,MaxTextExtent);
2749 quality_image=ReadImage(preview_info,exception);
2750 if (quality_image != (Image *) NULL)
2751 {
2752 preview_image=DestroyImage(preview_image);
2753 preview_image=quality_image;
2754 }
2755 }
2756 (void) RelinquishUniqueFileResource(preview_image->filename);
2757 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002758 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002759 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2760 1024.0/1024.0);
2761 else
2762 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002763 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002764 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002765 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002766 else
cristyb51dff52011-05-19 16:55:47 +00002767 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002768 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002769 break;
2770 }
2771 }
2772 thumbnail=DestroyImage(thumbnail);
2773 percentage+=12.5;
2774 radius+=0.5;
2775 sigma+=0.25;
2776 if (preview_image == (Image *) NULL)
2777 break;
2778 (void) DeleteImageProperty(preview_image,"label");
cristyd15e6592011-10-15 00:13:06 +00002779 (void) SetImageProperty(preview_image,"label",label,exception);
cristy3ed852e2009-09-05 21:47:34 +00002780 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002781 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2782 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002783 if (proceed == MagickFalse)
2784 break;
2785 }
2786 if (images == (Image *) NULL)
2787 {
2788 preview_info=DestroyImageInfo(preview_info);
2789 return((Image *) NULL);
2790 }
2791 /*
2792 Create the montage.
2793 */
2794 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2795 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2796 montage_info->shadow=MagickTrue;
2797 (void) CloneString(&montage_info->tile,"3x3");
2798 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2799 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2800 montage_image=MontageImages(images,montage_info,exception);
2801 montage_info=DestroyMontageInfo(montage_info);
2802 images=DestroyImageList(images);
2803 if (montage_image == (Image *) NULL)
2804 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2805 if (montage_image->montage != (char *) NULL)
2806 {
2807 /*
2808 Free image directory.
2809 */
2810 montage_image->montage=(char *) RelinquishMagickMemory(
2811 montage_image->montage);
2812 if (image->directory != (char *) NULL)
2813 montage_image->directory=(char *) RelinquishMagickMemory(
2814 montage_image->directory);
2815 }
2816 preview_info=DestroyImageInfo(preview_info);
2817 return(montage_image);
2818}
2819
2820/*
2821%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2822% %
2823% %
2824% %
2825% R a d i a l B l u r I m a g e %
2826% %
2827% %
2828% %
2829%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2830%
2831% RadialBlurImage() applies a radial blur to the image.
2832%
2833% Andrew Protano contributed this effect.
2834%
2835% The format of the RadialBlurImage method is:
2836%
2837% Image *RadialBlurImage(const Image *image,const double angle,
cristy6435bd92011-09-10 02:10:07 +00002838% const double blur,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002839%
2840% A description of each parameter follows:
2841%
2842% o image: the image.
2843%
cristy3ed852e2009-09-05 21:47:34 +00002844% o angle: the angle of the radial blur.
2845%
cristy6435bd92011-09-10 02:10:07 +00002846% o blur: the blur.
2847%
cristy3ed852e2009-09-05 21:47:34 +00002848% o exception: return any errors or warnings in this structure.
2849%
2850*/
cristy4282c702011-11-21 00:01:06 +00002851MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2852 const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002853{
cristyc4c8d132010-01-07 01:58:38 +00002854 CacheView
2855 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002856 *image_view,
2857 *radial_view;
cristyc4c8d132010-01-07 01:58:38 +00002858
cristy3ed852e2009-09-05 21:47:34 +00002859 Image
2860 *blur_image;
2861
cristy3ed852e2009-09-05 21:47:34 +00002862 MagickBooleanType
2863 status;
2864
cristybb503372010-05-27 20:51:26 +00002865 MagickOffsetType
2866 progress;
2867
cristy3ed852e2009-09-05 21:47:34 +00002868 MagickRealType
2869 blur_radius,
2870 *cos_theta,
2871 offset,
2872 *sin_theta,
2873 theta;
2874
2875 PointInfo
2876 blur_center;
2877
cristybb503372010-05-27 20:51:26 +00002878 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002879 i;
2880
cristybb503372010-05-27 20:51:26 +00002881 size_t
cristy3ed852e2009-09-05 21:47:34 +00002882 n;
2883
cristybb503372010-05-27 20:51:26 +00002884 ssize_t
2885 y;
2886
cristy3ed852e2009-09-05 21:47:34 +00002887 /*
2888 Allocate blur image.
2889 */
2890 assert(image != (Image *) NULL);
2891 assert(image->signature == MagickSignature);
2892 if (image->debug != MagickFalse)
2893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2894 assert(exception != (ExceptionInfo *) NULL);
2895 assert(exception->signature == MagickSignature);
cristy1e7aa312011-09-10 20:01:36 +00002896 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002897 if (blur_image == (Image *) NULL)
2898 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002899 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002900 {
cristy3ed852e2009-09-05 21:47:34 +00002901 blur_image=DestroyImage(blur_image);
2902 return((Image *) NULL);
2903 }
2904 blur_center.x=(double) image->columns/2.0;
2905 blur_center.y=(double) image->rows/2.0;
2906 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002907 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002908 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2909 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2910 sizeof(*cos_theta));
2911 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2912 sizeof(*sin_theta));
2913 if ((cos_theta == (MagickRealType *) NULL) ||
2914 (sin_theta == (MagickRealType *) NULL))
2915 {
2916 blur_image=DestroyImage(blur_image);
2917 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2918 }
2919 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002920 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002921 {
2922 cos_theta[i]=cos((double) (theta*i-offset));
2923 sin_theta[i]=sin((double) (theta*i-offset));
2924 }
2925 /*
2926 Radial blur image.
2927 */
2928 status=MagickTrue;
2929 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002930 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002931 radial_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002932 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002933#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002934 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002935#endif
cristy1e7aa312011-09-10 20:01:36 +00002936 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002937 {
cristy1e7aa312011-09-10 20:01:36 +00002938 register const Quantum
2939 *restrict p;
2940
cristy4c08aed2011-07-01 19:47:50 +00002941 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002942 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002943
cristy117ff172010-08-15 21:35:32 +00002944 register ssize_t
2945 x;
2946
cristy3ed852e2009-09-05 21:47:34 +00002947 if (status == MagickFalse)
2948 continue;
cristy8b49f382012-01-17 03:11:03 +00002949 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2950 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002951 exception);
cristy1e7aa312011-09-10 20:01:36 +00002952 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002953 {
2954 status=MagickFalse;
2955 continue;
2956 }
cristy1e7aa312011-09-10 20:01:36 +00002957 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002958 {
cristy3ed852e2009-09-05 21:47:34 +00002959 MagickRealType
cristy3ed852e2009-09-05 21:47:34 +00002960 radius;
2961
cristy3ed852e2009-09-05 21:47:34 +00002962 PointInfo
2963 center;
2964
cristybb503372010-05-27 20:51:26 +00002965 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002966 i;
2967
cristybb503372010-05-27 20:51:26 +00002968 size_t
cristy3ed852e2009-09-05 21:47:34 +00002969 step;
2970
2971 center.x=(double) x-blur_center.x;
2972 center.y=(double) y-blur_center.y;
2973 radius=hypot((double) center.x,center.y);
2974 if (radius == 0)
2975 step=1;
2976 else
2977 {
cristybb503372010-05-27 20:51:26 +00002978 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002979 if (step == 0)
2980 step=1;
2981 else
2982 if (step >= n)
2983 step=n-1;
2984 }
cristy1e7aa312011-09-10 20:01:36 +00002985 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2986 {
2987 MagickRealType
2988 gamma,
2989 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002990
cristy1e7aa312011-09-10 20:01:36 +00002991 PixelChannel
2992 channel;
2993
2994 PixelTrait
2995 blur_traits,
2996 traits;
2997
2998 register const Quantum
2999 *restrict r;
3000
3001 register ssize_t
3002 j;
3003
cristye2a912b2011-12-05 20:02:07 +00003004 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003005 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003006 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3007 if ((traits == UndefinedPixelTrait) ||
3008 (blur_traits == UndefinedPixelTrait))
3009 continue;
3010 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003011 {
cristy0beccfa2011-09-25 20:47:53 +00003012 SetPixelChannel(blur_image,channel,p[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003013 continue;
cristy3ed852e2009-09-05 21:47:34 +00003014 }
cristy1e7aa312011-09-10 20:01:36 +00003015 gamma=0.0;
3016 pixel=bias;
3017 if ((blur_traits & BlendPixelTrait) == 0)
3018 {
3019 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3020 {
cristy8b49f382012-01-17 03:11:03 +00003021 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003022 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3023 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3024 1,1,exception);
3025 if (r == (const Quantum *) NULL)
3026 {
3027 status=MagickFalse;
3028 continue;
3029 }
3030 pixel+=r[i];
3031 gamma++;
3032 }
3033 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003034 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003035 continue;
3036 }
3037 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3038 {
cristy8b49f382012-01-17 03:11:03 +00003039 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003040 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3041 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3042 1,1,exception);
3043 if (r == (const Quantum *) NULL)
3044 {
3045 status=MagickFalse;
3046 continue;
3047 }
3048 pixel+=GetPixelAlpha(image,r)*r[i];
3049 gamma+=GetPixelAlpha(image,r);
cristy3ed852e2009-09-05 21:47:34 +00003050 }
cristy1e7aa312011-09-10 20:01:36 +00003051 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003052 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003053 }
3054 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003055 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003056 }
3057 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3058 status=MagickFalse;
3059 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3060 {
3061 MagickBooleanType
3062 proceed;
3063
cristyb5d5f722009-11-04 03:03:49 +00003064#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003065 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003066#endif
3067 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3068 if (proceed == MagickFalse)
3069 status=MagickFalse;
3070 }
3071 }
3072 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00003073 radial_view=DestroyCacheView(radial_view);
cristy3ed852e2009-09-05 21:47:34 +00003074 image_view=DestroyCacheView(image_view);
3075 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3076 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3077 if (status == MagickFalse)
3078 blur_image=DestroyImage(blur_image);
3079 return(blur_image);
3080}
3081
3082/*
3083%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3084% %
3085% %
3086% %
cristy3ed852e2009-09-05 21:47:34 +00003087% S e l e c t i v e B l u r I m a g e %
3088% %
3089% %
3090% %
3091%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3092%
3093% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3094% It is similar to the unsharpen mask that sharpens everything with contrast
3095% above a certain threshold.
3096%
3097% The format of the SelectiveBlurImage method is:
3098%
3099% Image *SelectiveBlurImage(const Image *image,const double radius,
cristy1e7aa312011-09-10 20:01:36 +00003100% const double sigma,const double threshold,const double bias,
3101% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003102%
3103% A description of each parameter follows:
3104%
3105% o image: the image.
3106%
cristy3ed852e2009-09-05 21:47:34 +00003107% o radius: the radius of the Gaussian, in pixels, not counting the center
3108% pixel.
3109%
3110% o sigma: the standard deviation of the Gaussian, in pixels.
3111%
3112% o threshold: only pixels within this contrast threshold are included
3113% in the blur operation.
3114%
cristy1e7aa312011-09-10 20:01:36 +00003115% o bias: the bias.
3116%
cristy3ed852e2009-09-05 21:47:34 +00003117% o exception: return any errors or warnings in this structure.
3118%
3119*/
cristy4282c702011-11-21 00:01:06 +00003120MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3121 const double sigma,const double threshold,const double bias,
3122 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003123{
3124#define SelectiveBlurImageTag "SelectiveBlur/Image"
3125
cristy47e00502009-12-17 19:19:57 +00003126 CacheView
3127 *blur_view,
3128 *image_view;
3129
cristy3ed852e2009-09-05 21:47:34 +00003130 double
cristy3ed852e2009-09-05 21:47:34 +00003131 *kernel;
3132
3133 Image
3134 *blur_image;
3135
cristy3ed852e2009-09-05 21:47:34 +00003136 MagickBooleanType
3137 status;
3138
cristybb503372010-05-27 20:51:26 +00003139 MagickOffsetType
3140 progress;
3141
cristybb503372010-05-27 20:51:26 +00003142 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003143 i;
cristy3ed852e2009-09-05 21:47:34 +00003144
cristybb503372010-05-27 20:51:26 +00003145 size_t
cristy3ed852e2009-09-05 21:47:34 +00003146 width;
3147
cristybb503372010-05-27 20:51:26 +00003148 ssize_t
cristyc8523c12011-09-13 00:02:53 +00003149 center,
cristybb503372010-05-27 20:51:26 +00003150 j,
3151 u,
3152 v,
3153 y;
3154
cristy3ed852e2009-09-05 21:47:34 +00003155 /*
3156 Initialize blur image attributes.
3157 */
3158 assert(image != (Image *) NULL);
3159 assert(image->signature == MagickSignature);
3160 if (image->debug != MagickFalse)
3161 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3162 assert(exception != (ExceptionInfo *) NULL);
3163 assert(exception->signature == MagickSignature);
3164 width=GetOptimalKernelWidth1D(radius,sigma);
cristy8b49f382012-01-17 03:11:03 +00003165 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00003166 if (kernel == (double *) NULL)
3167 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003168 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003169 i=0;
cristy47e00502009-12-17 19:19:57 +00003170 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003171 {
cristy47e00502009-12-17 19:19:57 +00003172 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003173 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3174 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003175 }
3176 if (image->debug != MagickFalse)
3177 {
3178 char
3179 format[MaxTextExtent],
3180 *message;
3181
cristy117ff172010-08-15 21:35:32 +00003182 register const double
3183 *k;
3184
cristybb503372010-05-27 20:51:26 +00003185 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003186 u,
3187 v;
3188
cristy3ed852e2009-09-05 21:47:34 +00003189 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003190 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3191 width);
cristy3ed852e2009-09-05 21:47:34 +00003192 message=AcquireString("");
3193 k=kernel;
cristybb503372010-05-27 20:51:26 +00003194 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003195 {
3196 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003197 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003198 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003199 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003200 {
cristyb51dff52011-05-19 16:55:47 +00003201 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003202 (void) ConcatenateString(&message,format);
3203 }
3204 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3205 }
3206 message=DestroyString(message);
3207 }
cristy1e7aa312011-09-10 20:01:36 +00003208 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003209 if (blur_image == (Image *) NULL)
3210 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003211 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003212 {
cristy3ed852e2009-09-05 21:47:34 +00003213 blur_image=DestroyImage(blur_image);
3214 return((Image *) NULL);
3215 }
3216 /*
3217 Threshold blur image.
3218 */
3219 status=MagickTrue;
3220 progress=0;
cristyc8523c12011-09-13 00:02:53 +00003221 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3222 GetPixelChannels(image)*(width/2L));
cristy3ed852e2009-09-05 21:47:34 +00003223 image_view=AcquireCacheView(image);
3224 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003225#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003226 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003227#endif
cristybb503372010-05-27 20:51:26 +00003228 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003229 {
cristy4c08aed2011-07-01 19:47:50 +00003230 double
3231 contrast;
3232
cristy3ed852e2009-09-05 21:47:34 +00003233 MagickBooleanType
3234 sync;
3235
cristy4c08aed2011-07-01 19:47:50 +00003236 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003237 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003238
cristy4c08aed2011-07-01 19:47:50 +00003239 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003240 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003241
cristy117ff172010-08-15 21:35:32 +00003242 register ssize_t
3243 x;
3244
cristy3ed852e2009-09-05 21:47:34 +00003245 if (status == MagickFalse)
3246 continue;
cristy117ff172010-08-15 21:35:32 +00003247 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3248 (width/2L),image->columns+width,width,exception);
cristy8b49f382012-01-17 03:11:03 +00003249 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003250 exception);
cristy4c08aed2011-07-01 19:47:50 +00003251 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003252 {
3253 status=MagickFalse;
3254 continue;
3255 }
cristybb503372010-05-27 20:51:26 +00003256 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003257 {
cristybb503372010-05-27 20:51:26 +00003258 register ssize_t
cristy1e7aa312011-09-10 20:01:36 +00003259 i;
cristy3ed852e2009-09-05 21:47:34 +00003260
cristy1e7aa312011-09-10 20:01:36 +00003261 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3262 {
3263 MagickRealType
3264 alpha,
3265 gamma,
3266 intensity,
3267 pixel;
cristy117ff172010-08-15 21:35:32 +00003268
cristy1e7aa312011-09-10 20:01:36 +00003269 PixelChannel
3270 channel;
3271
3272 PixelTrait
3273 blur_traits,
3274 traits;
3275
3276 register const double
3277 *restrict k;
3278
3279 register const Quantum
3280 *restrict pixels;
3281
3282 register ssize_t
3283 u;
3284
3285 ssize_t
3286 v;
3287
cristye2a912b2011-12-05 20:02:07 +00003288 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003289 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003290 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3291 if ((traits == UndefinedPixelTrait) ||
3292 (blur_traits == UndefinedPixelTrait))
3293 continue;
3294 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003295 {
cristy0beccfa2011-09-25 20:47:53 +00003296 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003297 continue;
cristy3ed852e2009-09-05 21:47:34 +00003298 }
cristy1e7aa312011-09-10 20:01:36 +00003299 k=kernel;
3300 pixel=bias;
3301 pixels=p;
cristyc8523c12011-09-13 00:02:53 +00003302 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
cristy1e7aa312011-09-10 20:01:36 +00003303 gamma=0.0;
3304 if ((blur_traits & BlendPixelTrait) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003305 {
cristy1e7aa312011-09-10 20:01:36 +00003306 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003307 {
cristy1e7aa312011-09-10 20:01:36 +00003308 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003309 {
cristy1e7aa312011-09-10 20:01:36 +00003310 contrast=GetPixelIntensity(image,pixels)-intensity;
3311 if (fabs(contrast) < threshold)
3312 {
3313 pixel+=(*k)*pixels[i];
3314 gamma+=(*k);
3315 }
3316 k++;
3317 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003318 }
cristy1e7aa312011-09-10 20:01:36 +00003319 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003320 }
cristy1e7aa312011-09-10 20:01:36 +00003321 if (fabs((double) gamma) < MagickEpsilon)
3322 {
cristy0beccfa2011-09-25 20:47:53 +00003323 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003324 continue;
3325 }
3326 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003327 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003328 continue;
3329 }
3330 for (v=0; v < (ssize_t) width; v++)
3331 {
3332 for (u=0; u < (ssize_t) width; u++)
3333 {
3334 contrast=GetPixelIntensity(image,pixels)-intensity;
3335 if (fabs(contrast) < threshold)
3336 {
3337 alpha=(MagickRealType) (QuantumScale*
3338 GetPixelAlpha(image,pixels));
3339 pixel+=(*k)*alpha*pixels[i];
3340 gamma+=(*k)*alpha;
3341 }
3342 k++;
3343 pixels+=GetPixelChannels(image);
3344 }
3345 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003346 }
cristy1e7aa312011-09-10 20:01:36 +00003347 if (fabs((double) gamma) < MagickEpsilon)
3348 {
cristy0beccfa2011-09-25 20:47:53 +00003349 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003350 continue;
3351 }
3352 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003353 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003354 }
cristyed231572011-07-14 02:18:59 +00003355 p+=GetPixelChannels(image);
3356 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003357 }
3358 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3359 if (sync == MagickFalse)
3360 status=MagickFalse;
3361 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3362 {
3363 MagickBooleanType
3364 proceed;
3365
cristyb5d5f722009-11-04 03:03:49 +00003366#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003367 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003368#endif
3369 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3370 image->rows);
3371 if (proceed == MagickFalse)
3372 status=MagickFalse;
3373 }
3374 }
3375 blur_image->type=image->type;
3376 blur_view=DestroyCacheView(blur_view);
3377 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00003378 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00003379 if (status == MagickFalse)
3380 blur_image=DestroyImage(blur_image);
3381 return(blur_image);
3382}
3383
3384/*
3385%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3386% %
3387% %
3388% %
3389% S h a d e I m a g e %
3390% %
3391% %
3392% %
3393%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3394%
3395% ShadeImage() shines a distant light on an image to create a
3396% three-dimensional effect. You control the positioning of the light with
3397% azimuth and elevation; azimuth is measured in degrees off the x axis
3398% and elevation is measured in pixels above the Z axis.
3399%
3400% The format of the ShadeImage method is:
3401%
3402% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3403% const double azimuth,const double elevation,ExceptionInfo *exception)
3404%
3405% A description of each parameter follows:
3406%
3407% o image: the image.
3408%
3409% o gray: A value other than zero shades the intensity of each pixel.
3410%
3411% o azimuth, elevation: Define the light source direction.
3412%
3413% o exception: return any errors or warnings in this structure.
3414%
3415*/
3416MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3417 const double azimuth,const double elevation,ExceptionInfo *exception)
3418{
3419#define ShadeImageTag "Shade/Image"
3420
cristyc4c8d132010-01-07 01:58:38 +00003421 CacheView
3422 *image_view,
3423 *shade_view;
3424
cristy3ed852e2009-09-05 21:47:34 +00003425 Image
3426 *shade_image;
3427
cristy3ed852e2009-09-05 21:47:34 +00003428 MagickBooleanType
3429 status;
3430
cristybb503372010-05-27 20:51:26 +00003431 MagickOffsetType
3432 progress;
3433
cristy3ed852e2009-09-05 21:47:34 +00003434 PrimaryInfo
3435 light;
3436
cristybb503372010-05-27 20:51:26 +00003437 ssize_t
3438 y;
3439
cristy3ed852e2009-09-05 21:47:34 +00003440 /*
3441 Initialize shaded image attributes.
3442 */
3443 assert(image != (const Image *) NULL);
3444 assert(image->signature == MagickSignature);
3445 if (image->debug != MagickFalse)
3446 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3447 assert(exception != (ExceptionInfo *) NULL);
3448 assert(exception->signature == MagickSignature);
3449 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3450 if (shade_image == (Image *) NULL)
3451 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003452 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003453 {
cristy3ed852e2009-09-05 21:47:34 +00003454 shade_image=DestroyImage(shade_image);
3455 return((Image *) NULL);
3456 }
3457 /*
3458 Compute the light vector.
3459 */
3460 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3461 cos(DegreesToRadians(elevation));
3462 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3463 cos(DegreesToRadians(elevation));
3464 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3465 /*
3466 Shade image.
3467 */
3468 status=MagickTrue;
3469 progress=0;
3470 image_view=AcquireCacheView(image);
3471 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003472#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003473 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003474#endif
cristybb503372010-05-27 20:51:26 +00003475 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003476 {
3477 MagickRealType
3478 distance,
3479 normal_distance,
3480 shade;
3481
3482 PrimaryInfo
3483 normal;
3484
cristy4c08aed2011-07-01 19:47:50 +00003485 register const Quantum
cristy1e7aa312011-09-10 20:01:36 +00003486 *restrict center,
cristyc47d1f82009-11-26 01:44:43 +00003487 *restrict p,
cristy1e7aa312011-09-10 20:01:36 +00003488 *restrict post,
3489 *restrict pre;
cristy3ed852e2009-09-05 21:47:34 +00003490
cristy4c08aed2011-07-01 19:47:50 +00003491 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003492 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003493
cristy117ff172010-08-15 21:35:32 +00003494 register ssize_t
3495 x;
3496
cristy3ed852e2009-09-05 21:47:34 +00003497 if (status == MagickFalse)
3498 continue;
3499 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3500 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3501 exception);
cristy4c08aed2011-07-01 19:47:50 +00003502 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003503 {
3504 status=MagickFalse;
3505 continue;
3506 }
3507 /*
3508 Shade this row of pixels.
3509 */
3510 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristy1e7aa312011-09-10 20:01:36 +00003511 pre=p+GetPixelChannels(image);
3512 center=pre+(image->columns+2)*GetPixelChannels(image);
3513 post=center+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003514 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003515 {
cristy1e7aa312011-09-10 20:01:36 +00003516 register ssize_t
3517 i;
3518
cristy3ed852e2009-09-05 21:47:34 +00003519 /*
3520 Determine the surface normal and compute shading.
3521 */
cristy1e7aa312011-09-10 20:01:36 +00003522 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3523 GetPixelIntensity(image,center-GetPixelChannels(image))+
3524 GetPixelIntensity(image,post-GetPixelChannels(image))-
3525 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3526 GetPixelIntensity(image,center+GetPixelChannels(image))-
3527 GetPixelIntensity(image,post+GetPixelChannels(image)));
3528 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3529 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3530 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3531 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3532 GetPixelIntensity(image,pre+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003533 if ((normal.x == 0.0) && (normal.y == 0.0))
3534 shade=light.z;
3535 else
3536 {
3537 shade=0.0;
3538 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3539 if (distance > MagickEpsilon)
3540 {
3541 normal_distance=
3542 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3543 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3544 shade=distance/sqrt((double) normal_distance);
3545 }
3546 }
cristy1e7aa312011-09-10 20:01:36 +00003547 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3548 {
3549 PixelChannel
3550 channel;
3551
3552 PixelTrait
3553 shade_traits,
3554 traits;
3555
cristye2a912b2011-12-05 20:02:07 +00003556 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003557 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003558 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3559 if ((traits == UndefinedPixelTrait) ||
3560 (shade_traits == UndefinedPixelTrait))
3561 continue;
3562 if ((shade_traits & CopyPixelTrait) != 0)
3563 {
cristy0beccfa2011-09-25 20:47:53 +00003564 SetPixelChannel(shade_image,channel,center[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003565 continue;
3566 }
3567 if (gray != MagickFalse)
3568 {
cristy0beccfa2011-09-25 20:47:53 +00003569 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
cristy1e7aa312011-09-10 20:01:36 +00003570 continue;
3571 }
cristy0beccfa2011-09-25 20:47:53 +00003572 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3573 center[i]),q);
cristy1e7aa312011-09-10 20:01:36 +00003574 }
3575 pre+=GetPixelChannels(image);
3576 center+=GetPixelChannels(image);
3577 post+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003578 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003579 }
3580 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3581 status=MagickFalse;
3582 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3583 {
3584 MagickBooleanType
3585 proceed;
3586
cristyb5d5f722009-11-04 03:03:49 +00003587#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003588 #pragma omp critical (MagickCore_ShadeImage)
3589#endif
3590 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3591 if (proceed == MagickFalse)
3592 status=MagickFalse;
3593 }
3594 }
3595 shade_view=DestroyCacheView(shade_view);
3596 image_view=DestroyCacheView(image_view);
3597 if (status == MagickFalse)
3598 shade_image=DestroyImage(shade_image);
3599 return(shade_image);
3600}
3601
3602/*
3603%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3604% %
3605% %
3606% %
3607% S h a r p e n I m a g e %
3608% %
3609% %
3610% %
3611%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3612%
3613% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3614% operator of the given radius and standard deviation (sigma). For
3615% reasonable results, radius should be larger than sigma. Use a radius of 0
3616% and SharpenImage() selects a suitable radius for you.
3617%
3618% Using a separable kernel would be faster, but the negative weights cancel
3619% out on the corners of the kernel producing often undesirable ringing in the
3620% filtered result; this can be avoided by using a 2D gaussian shaped image
3621% sharpening kernel instead.
3622%
3623% The format of the SharpenImage method is:
3624%
3625% Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003626% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003627%
3628% A description of each parameter follows:
3629%
3630% o image: the image.
3631%
cristy3ed852e2009-09-05 21:47:34 +00003632% o radius: the radius of the Gaussian, in pixels, not counting the center
3633% pixel.
3634%
3635% o sigma: the standard deviation of the Laplacian, in pixels.
3636%
cristy05c0c9a2011-09-05 23:16:13 +00003637% o bias: bias.
3638%
cristy3ed852e2009-09-05 21:47:34 +00003639% o exception: return any errors or warnings in this structure.
3640%
3641*/
cristy3ed852e2009-09-05 21:47:34 +00003642MagickExport Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003643 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003644{
cristy3ed852e2009-09-05 21:47:34 +00003645 double
cristy47e00502009-12-17 19:19:57 +00003646 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003647
3648 Image
3649 *sharp_image;
3650
cristy41cbe682011-07-15 19:12:37 +00003651 KernelInfo
3652 *kernel_info;
3653
cristybb503372010-05-27 20:51:26 +00003654 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003655 i;
3656
cristybb503372010-05-27 20:51:26 +00003657 size_t
cristy3ed852e2009-09-05 21:47:34 +00003658 width;
3659
cristy117ff172010-08-15 21:35:32 +00003660 ssize_t
3661 j,
3662 u,
3663 v;
3664
cristy3ed852e2009-09-05 21:47:34 +00003665 assert(image != (const Image *) NULL);
3666 assert(image->signature == MagickSignature);
3667 if (image->debug != MagickFalse)
3668 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3669 assert(exception != (ExceptionInfo *) NULL);
3670 assert(exception->signature == MagickSignature);
3671 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003672 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003673 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003674 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003675 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3676 kernel_info->width=width;
3677 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00003678 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
cristy41cbe682011-07-15 19:12:37 +00003679 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00003680 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3681 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3682 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00003683 {
3684 kernel_info=DestroyKernelInfo(kernel_info);
3685 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3686 }
cristy3ed852e2009-09-05 21:47:34 +00003687 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003688 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003689 i=0;
3690 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003691 {
cristy47e00502009-12-17 19:19:57 +00003692 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003693 {
cristy41cbe682011-07-15 19:12:37 +00003694 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3695 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3696 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003697 i++;
3698 }
3699 }
cristy41cbe682011-07-15 19:12:37 +00003700 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003701 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003702 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003703 return(sharp_image);
3704}
3705
3706/*
3707%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3708% %
3709% %
3710% %
3711% S p r e a d I m a g e %
3712% %
3713% %
3714% %
3715%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3716%
3717% SpreadImage() is a special effects method that randomly displaces each
3718% pixel in a block defined by the radius parameter.
3719%
3720% The format of the SpreadImage method is:
3721%
3722% Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003723% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003724%
3725% A description of each parameter follows:
3726%
3727% o image: the image.
3728%
cristy5c4e2582011-09-11 19:21:03 +00003729% o radius: choose a random pixel in a neighborhood of this extent.
3730%
3731% o method: the pixel interpolation method.
cristy3ed852e2009-09-05 21:47:34 +00003732%
3733% o exception: return any errors or warnings in this structure.
3734%
3735*/
3736MagickExport Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003737 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003738{
3739#define SpreadImageTag "Spread/Image"
3740
cristyfa112112010-01-04 17:48:07 +00003741 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003742 *image_view,
3743 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003744
cristy3ed852e2009-09-05 21:47:34 +00003745 Image
3746 *spread_image;
3747
cristy3ed852e2009-09-05 21:47:34 +00003748 MagickBooleanType
3749 status;
3750
cristybb503372010-05-27 20:51:26 +00003751 MagickOffsetType
3752 progress;
3753
cristy3ed852e2009-09-05 21:47:34 +00003754 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003755 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003756
cristybb503372010-05-27 20:51:26 +00003757 size_t
cristy3ed852e2009-09-05 21:47:34 +00003758 width;
3759
cristybb503372010-05-27 20:51:26 +00003760 ssize_t
3761 y;
3762
cristy3ed852e2009-09-05 21:47:34 +00003763 /*
3764 Initialize spread image attributes.
3765 */
3766 assert(image != (Image *) NULL);
3767 assert(image->signature == MagickSignature);
3768 if (image->debug != MagickFalse)
3769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3770 assert(exception != (ExceptionInfo *) NULL);
3771 assert(exception->signature == MagickSignature);
3772 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3773 exception);
3774 if (spread_image == (Image *) NULL)
3775 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003776 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003777 {
cristy3ed852e2009-09-05 21:47:34 +00003778 spread_image=DestroyImage(spread_image);
3779 return((Image *) NULL);
3780 }
3781 /*
3782 Spread image.
3783 */
3784 status=MagickTrue;
3785 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003786 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003787 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003788 image_view=AcquireCacheView(image);
3789 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003790#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy78778cb2012-01-17 14:48:47 +00003791 #pragma omp parallel for schedule(static,8) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003792#endif
cristybe82ad52011-09-06 01:17:20 +00003793 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003794 {
cristy5c9e6f22010-09-17 17:31:01 +00003795 const int
3796 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003797
cristybe82ad52011-09-06 01:17:20 +00003798 register const Quantum
3799 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003800
cristy4c08aed2011-07-01 19:47:50 +00003801 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003802 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003803
cristy117ff172010-08-15 21:35:32 +00003804 register ssize_t
3805 x;
3806
cristy3ed852e2009-09-05 21:47:34 +00003807 if (status == MagickFalse)
3808 continue;
cristybe82ad52011-09-06 01:17:20 +00003809 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy9f7e7cb2011-03-26 00:49:57 +00003810 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003811 exception);
cristybe82ad52011-09-06 01:17:20 +00003812 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003813 {
3814 status=MagickFalse;
3815 continue;
3816 }
cristybe82ad52011-09-06 01:17:20 +00003817 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003818 {
cristybe82ad52011-09-06 01:17:20 +00003819 PointInfo
3820 point;
3821
cristybe82ad52011-09-06 01:17:20 +00003822 point.x=GetPseudoRandomValue(random_info[id]);
3823 point.y=GetPseudoRandomValue(random_info[id]);
cristy5c4e2582011-09-11 19:21:03 +00003824 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3825 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
cristyed231572011-07-14 02:18:59 +00003826 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003827 }
cristy9f7e7cb2011-03-26 00:49:57 +00003828 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003829 status=MagickFalse;
3830 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3831 {
3832 MagickBooleanType
3833 proceed;
3834
cristyb557a152011-02-22 12:14:30 +00003835#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003836 #pragma omp critical (MagickCore_SpreadImage)
3837#endif
3838 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3839 if (proceed == MagickFalse)
3840 status=MagickFalse;
3841 }
3842 }
cristy9f7e7cb2011-03-26 00:49:57 +00003843 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003844 image_view=DestroyCacheView(image_view);
3845 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003846 return(spread_image);
3847}
3848
3849/*
3850%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3851% %
3852% %
3853% %
3854% U n s h a r p M a s k I m a g e %
3855% %
3856% %
3857% %
3858%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3859%
3860% UnsharpMaskImage() sharpens one or more image channels. We convolve the
3861% image with a Gaussian operator of the given radius and standard deviation
3862% (sigma). For reasonable results, radius should be larger than sigma. Use a
3863% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3864%
3865% The format of the UnsharpMaskImage method is:
3866%
3867% Image *UnsharpMaskImage(const Image *image,const double radius,
3868% const double sigma,const double amount,const double threshold,
3869% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003870%
3871% A description of each parameter follows:
3872%
3873% o image: the image.
3874%
cristy3ed852e2009-09-05 21:47:34 +00003875% o radius: the radius of the Gaussian, in pixels, not counting the center
3876% pixel.
3877%
3878% o sigma: the standard deviation of the Gaussian, in pixels.
3879%
3880% o amount: the percentage of the difference between the original and the
3881% blur image that is added back into the original.
3882%
3883% o threshold: the threshold in pixels needed to apply the diffence amount.
3884%
3885% o exception: return any errors or warnings in this structure.
3886%
3887*/
cristy31bb6272011-11-20 23:57:25 +00003888MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3889 const double sigma,const double amount,const double threshold,
3890 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003891{
3892#define SharpenImageTag "Sharpen/Image"
3893
cristyc4c8d132010-01-07 01:58:38 +00003894 CacheView
3895 *image_view,
3896 *unsharp_view;
3897
cristy3ed852e2009-09-05 21:47:34 +00003898 Image
3899 *unsharp_image;
3900
cristy3ed852e2009-09-05 21:47:34 +00003901 MagickBooleanType
3902 status;
3903
cristybb503372010-05-27 20:51:26 +00003904 MagickOffsetType
3905 progress;
3906
cristy3ed852e2009-09-05 21:47:34 +00003907 MagickRealType
3908 quantum_threshold;
3909
cristybb503372010-05-27 20:51:26 +00003910 ssize_t
3911 y;
3912
cristy3ed852e2009-09-05 21:47:34 +00003913 assert(image != (const Image *) NULL);
3914 assert(image->signature == MagickSignature);
3915 if (image->debug != MagickFalse)
3916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3917 assert(exception != (ExceptionInfo *) NULL);
cristy05c0c9a2011-09-05 23:16:13 +00003918 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00003919 if (unsharp_image == (Image *) NULL)
3920 return((Image *) NULL);
3921 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3922 /*
3923 Unsharp-mask image.
3924 */
3925 status=MagickTrue;
3926 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003927 image_view=AcquireCacheView(image);
3928 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00003929#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003930 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003931#endif
cristybb503372010-05-27 20:51:26 +00003932 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003933 {
cristy4c08aed2011-07-01 19:47:50 +00003934 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003935 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003936
cristy4c08aed2011-07-01 19:47:50 +00003937 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003938 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003939
cristy117ff172010-08-15 21:35:32 +00003940 register ssize_t
3941 x;
3942
cristy3ed852e2009-09-05 21:47:34 +00003943 if (status == MagickFalse)
3944 continue;
3945 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy8b49f382012-01-17 03:11:03 +00003946 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003947 exception);
cristy4c08aed2011-07-01 19:47:50 +00003948 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003949 {
3950 status=MagickFalse;
3951 continue;
3952 }
cristybb503372010-05-27 20:51:26 +00003953 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003954 {
cristy7f3a0d12011-09-05 23:27:59 +00003955 register ssize_t
3956 i;
3957
3958 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3959 {
3960 MagickRealType
3961 pixel;
3962
3963 PixelChannel
3964 channel;
3965
3966 PixelTrait
3967 traits,
3968 unsharp_traits;
3969
cristye2a912b2011-12-05 20:02:07 +00003970 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003971 traits=GetPixelChannelMapTraits(image,channel);
cristy7f3a0d12011-09-05 23:27:59 +00003972 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3973 if ((traits == UndefinedPixelTrait) ||
3974 (unsharp_traits == UndefinedPixelTrait))
3975 continue;
3976 if ((unsharp_traits & CopyPixelTrait) != 0)
3977 {
cristy0beccfa2011-09-25 20:47:53 +00003978 SetPixelChannel(unsharp_image,channel,p[i],q);
cristy7f3a0d12011-09-05 23:27:59 +00003979 continue;
3980 }
cristy0beccfa2011-09-25 20:47:53 +00003981 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
cristy7f3a0d12011-09-05 23:27:59 +00003982 if (fabs(2.0*pixel) < quantum_threshold)
3983 pixel=(MagickRealType) p[i];
3984 else
3985 pixel=(MagickRealType) p[i]+amount*pixel;
cristy0beccfa2011-09-25 20:47:53 +00003986 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
cristy7f3a0d12011-09-05 23:27:59 +00003987 }
cristyed231572011-07-14 02:18:59 +00003988 p+=GetPixelChannels(image);
3989 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00003990 }
3991 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3992 status=MagickFalse;
3993 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3994 {
3995 MagickBooleanType
3996 proceed;
3997
cristyb5d5f722009-11-04 03:03:49 +00003998#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003999 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00004000#endif
4001 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4002 if (proceed == MagickFalse)
4003 status=MagickFalse;
4004 }
4005 }
4006 unsharp_image->type=image->type;
4007 unsharp_view=DestroyCacheView(unsharp_view);
4008 image_view=DestroyCacheView(image_view);
4009 if (status == MagickFalse)
4010 unsharp_image=DestroyImage(unsharp_image);
4011 return(unsharp_image);
4012}