blob: 8148dd60751b447f988cbe7de2b45891cd3db98b [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.
1299 */
cristy574cc262011-08-05 01:23:58 +00001300 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
cristy075ff302011-09-07 01:51:24 +00001301 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2L);
cristyfccdab92009-11-30 16:43:57 +00001302 status=MagickTrue;
1303 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001304 image_view=AcquireCacheView(image);
1305 convolve_view=AcquireCacheView(convolve_image);
1306#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy78778cb2012-01-17 14:48:47 +00001307 #pragma omp parallel for schedule(guided) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001308#endif
cristybb503372010-05-27 20:51:26 +00001309 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001310 {
cristy4c08aed2011-07-01 19:47:50 +00001311 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001312 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001313
cristy4c08aed2011-07-01 19:47:50 +00001314 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001315 *restrict q;
1316
cristy117ff172010-08-15 21:35:32 +00001317 register ssize_t
1318 x;
1319
cristyfccdab92009-11-30 16:43:57 +00001320 if (status == MagickFalse)
1321 continue;
cristy105ba3c2011-07-18 02:28:38 +00001322 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1323 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1324 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001325 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001326 exception);
cristy105ba3c2011-07-18 02:28:38 +00001327 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001328 {
1329 status=MagickFalse;
1330 continue;
1331 }
cristybb503372010-05-27 20:51:26 +00001332 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001333 {
cristybb503372010-05-27 20:51:26 +00001334 register ssize_t
cristyed231572011-07-14 02:18:59 +00001335 i;
cristyfccdab92009-11-30 16:43:57 +00001336
cristya30d9ba2011-07-23 21:00:48 +00001337 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001338 {
cristyed231572011-07-14 02:18:59 +00001339 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001340 alpha,
1341 gamma,
cristyed231572011-07-14 02:18:59 +00001342 pixel;
1343
1344 PixelChannel
1345 channel;
1346
1347 PixelTrait
1348 convolve_traits,
1349 traits;
1350
cristya96f2492011-12-14 18:25:41 +00001351 register const MagickRealType
cristyed231572011-07-14 02:18:59 +00001352 *restrict k;
1353
1354 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001355 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001356
1357 register ssize_t
1358 u;
1359
1360 ssize_t
1361 v;
1362
cristye2a912b2011-12-05 20:02:07 +00001363 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001364 traits=GetPixelChannelMapTraits(image,channel);
cristy4e154852011-07-14 13:28:53 +00001365 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001366 if ((traits == UndefinedPixelTrait) ||
1367 (convolve_traits == UndefinedPixelTrait))
cristy4e154852011-07-14 13:28:53 +00001368 continue;
1369 if ((convolve_traits & CopyPixelTrait) != 0)
1370 {
cristy0beccfa2011-09-25 20:47:53 +00001371 SetPixelChannel(convolve_image,channel,p[center+i],q);
cristy4e154852011-07-14 13:28:53 +00001372 continue;
1373 }
cristy5e6be1e2011-07-16 01:23:39 +00001374 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001375 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001376 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001377 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001378 {
cristyed231572011-07-14 02:18:59 +00001379 /*
cristy4e154852011-07-14 13:28:53 +00001380 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001381 */
cristyeb52cde2011-07-17 01:52:52 +00001382 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001383 {
cristyeb52cde2011-07-17 01:52:52 +00001384 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001385 {
cristyeb52cde2011-07-17 01:52:52 +00001386 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001387 k++;
cristya30d9ba2011-07-23 21:00:48 +00001388 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001389 }
cristya30d9ba2011-07-23 21:00:48 +00001390 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001391 }
cristy0beccfa2011-09-25 20:47:53 +00001392 SetPixelChannel(convolve_image,channel,ClampToQuantum(pixel),q);
cristy4e154852011-07-14 13:28:53 +00001393 continue;
cristyed231572011-07-14 02:18:59 +00001394 }
cristy4e154852011-07-14 13:28:53 +00001395 /*
1396 Alpha blending.
1397 */
1398 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001399 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001400 {
cristyeb52cde2011-07-17 01:52:52 +00001401 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001402 {
cristyeb52cde2011-07-17 01:52:52 +00001403 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1404 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001405 gamma+=(*k)*alpha;
1406 k++;
cristya30d9ba2011-07-23 21:00:48 +00001407 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001408 }
cristya30d9ba2011-07-23 21:00:48 +00001409 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001410 }
cristy1ce96d02011-07-14 17:57:24 +00001411 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001412 SetPixelChannel(convolve_image,channel,ClampToQuantum(gamma*pixel),q);
cristyed231572011-07-14 02:18:59 +00001413 }
cristya30d9ba2011-07-23 21:00:48 +00001414 p+=GetPixelChannels(image);
1415 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001416 }
cristyed231572011-07-14 02:18:59 +00001417 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001418 status=MagickFalse;
1419 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1420 {
1421 MagickBooleanType
1422 proceed;
1423
1424#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001425 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001426#endif
1427 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1428 if (proceed == MagickFalse)
1429 status=MagickFalse;
1430 }
1431 }
1432 convolve_image->type=image->type;
1433 convolve_view=DestroyCacheView(convolve_view);
1434 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001435 if (status == MagickFalse)
1436 convolve_image=DestroyImage(convolve_image);
1437 return(convolve_image);
1438}
1439
1440/*
1441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442% %
1443% %
1444% %
cristy3ed852e2009-09-05 21:47:34 +00001445% D e s p e c k l e I m a g e %
1446% %
1447% %
1448% %
1449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450%
1451% DespeckleImage() reduces the speckle noise in an image while perserving the
cristyf0ae7762012-01-15 22:04:03 +00001452% edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1453% neighbors, then complementarily lowering pixels that are brighter than their
1454% surrounding neighbors) to reduce the speckle index of that image (reference
1455% Crimmins speckle removal).
cristy3ed852e2009-09-05 21:47:34 +00001456%
1457% The format of the DespeckleImage method is:
1458%
1459% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1460%
1461% A description of each parameter follows:
1462%
1463% o image: the image.
1464%
1465% o exception: return any errors or warnings in this structure.
1466%
1467*/
1468
cristy5473bf02012-01-16 19:15:08 +00001469static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1470 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1471 Quantum *restrict g)
cristy3ed852e2009-09-05 21:47:34 +00001472{
cristy7b4b8862012-01-16 19:52:19 +00001473 register Quantum
cristy5473bf02012-01-16 19:15:08 +00001474 *p,
1475 *q,
1476 *r,
1477 *s;
cristy78f3de22012-01-16 00:43:18 +00001478
cristy5473bf02012-01-16 19:15:08 +00001479 ssize_t
1480 y;
1481
1482 assert(f != (Quantum *) NULL);
1483 assert(g != (Quantum *) NULL);
1484 p=f+(columns+2);
1485 q=g+(columns+2);
1486 r=p+(y_offset*(columns+2)+x_offset);
1487#if defined(HAVE_OPENMP)
cristy78778cb2012-01-17 14:48:47 +00001488 #pragma omp parallel for schedule(guided)
cristy5473bf02012-01-16 19:15:08 +00001489#endif
1490 for (y=0; y < (ssize_t) rows; y++)
1491 {
cristy7b4b8862012-01-16 19:52:19 +00001492 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001493 i,
1494 x;
1495
cristy7b4b8862012-01-16 19:52:19 +00001496 SignedQuantum
1497 v;
1498
cristy5473bf02012-01-16 19:15:08 +00001499 i=(2*y+1)+y*columns;
1500 if (polarity > 0)
1501 for (x=0; x < (ssize_t) columns; x++)
1502 {
1503 v=(SignedQuantum) p[i];
1504 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1505 v+=ScaleCharToQuantum(1);
1506 q[i]=(Quantum) v;
1507 i++;
1508 }
1509 else
1510 for (x=0; x < (ssize_t) columns; x++)
1511 {
1512 v=(SignedQuantum) p[i];
1513 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1514 v-=ScaleCharToQuantum(1);
1515 q[i]=(Quantum) v;
1516 i++;
1517 }
1518 }
1519 p=f+(columns+2);
1520 q=g+(columns+2);
1521 r=q+(y_offset*(columns+2)+x_offset);
1522 s=q-(y_offset*(columns+2)+x_offset);
1523#if defined(HAVE_OPENMP)
1524 #pragma omp parallel for schedule(guided)
1525#endif
1526 for (y=0; y < (ssize_t) rows; y++)
1527 {
cristy7b4b8862012-01-16 19:52:19 +00001528 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001529 i,
1530 x;
1531
cristy7b4b8862012-01-16 19:52:19 +00001532 SignedQuantum
1533 v;
1534
cristy5473bf02012-01-16 19:15:08 +00001535 i=(2*y+1)+y*columns;
1536 if (polarity > 0)
1537 for (x=0; x < (ssize_t) columns; x++)
1538 {
1539 v=(SignedQuantum) q[i];
1540 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1541 ((SignedQuantum) r[i] > v))
1542 v+=ScaleCharToQuantum(1);
1543 p[i]=(Quantum) v;
1544 i++;
1545 }
1546 else
1547 for (x=0; x < (ssize_t) columns; x++)
1548 {
1549 v=(SignedQuantum) q[i];
1550 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1551 ((SignedQuantum) r[i] < v))
1552 v-=ScaleCharToQuantum(1);
1553 p[i]=(Quantum) v;
1554 i++;
1555 }
1556 }
cristy3ed852e2009-09-05 21:47:34 +00001557}
1558
1559MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1560{
1561#define DespeckleImageTag "Despeckle/Image"
1562
cristy2407fc22009-09-11 00:55:25 +00001563 CacheView
1564 *despeckle_view,
1565 *image_view;
1566
cristy3ed852e2009-09-05 21:47:34 +00001567 Image
1568 *despeckle_image;
1569
cristy3ed852e2009-09-05 21:47:34 +00001570 MagickBooleanType
1571 status;
1572
cristy5473bf02012-01-16 19:15:08 +00001573 Quantum
1574 *restrict buffer,
1575 *restrict pixels;
cristya63e4a92011-09-09 00:47:59 +00001576
cristy5473bf02012-01-16 19:15:08 +00001577 register ssize_t
1578 i;
1579
1580 size_t
1581 length;
cristy117ff172010-08-15 21:35:32 +00001582
cristybb503372010-05-27 20:51:26 +00001583 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001584 X[4] = {0, 1, 1,-1},
1585 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001586
cristy3ed852e2009-09-05 21:47:34 +00001587 /*
1588 Allocate despeckled image.
1589 */
1590 assert(image != (const Image *) NULL);
1591 assert(image->signature == MagickSignature);
1592 if (image->debug != MagickFalse)
1593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1594 assert(exception != (ExceptionInfo *) NULL);
1595 assert(exception->signature == MagickSignature);
cristy5473bf02012-01-16 19:15:08 +00001596 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001597 if (despeckle_image == (Image *) NULL)
1598 return((Image *) NULL);
cristya63e4a92011-09-09 00:47:59 +00001599 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1600 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001601 {
cristy3ed852e2009-09-05 21:47:34 +00001602 despeckle_image=DestroyImage(despeckle_image);
1603 return((Image *) NULL);
1604 }
1605 /*
cristy5473bf02012-01-16 19:15:08 +00001606 Allocate image buffer.
1607 */
1608 length=(size_t) ((image->columns+2)*(image->rows+2));
1609 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1610 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1611 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1612 {
1613 if (buffer != (Quantum *) NULL)
1614 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1615 if (pixels != (Quantum *) NULL)
1616 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1617 despeckle_image=DestroyImage(despeckle_image);
1618 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1619 }
1620 /*
1621 Reduce speckle in the image.
cristy3ed852e2009-09-05 21:47:34 +00001622 */
1623 status=MagickTrue;
cristyb06da092012-01-16 17:11:38 +00001624 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00001625 despeckle_view=AcquireCacheView(despeckle_image);
cristy5473bf02012-01-16 19:15:08 +00001626 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +00001627 {
cristy5473bf02012-01-16 19:15:08 +00001628 PixelChannel
1629 channel;
1630
1631 PixelTrait
1632 despeckle_traits,
1633 traits;
cristy3ed852e2009-09-05 21:47:34 +00001634
cristyc1488b52011-02-19 18:54:15 +00001635 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001636 k,
cristyf0ae7762012-01-15 22:04:03 +00001637 x;
cristyc1488b52011-02-19 18:54:15 +00001638
cristy5473bf02012-01-16 19:15:08 +00001639 ssize_t
1640 j,
1641 y;
1642
1643 if (status == MagickFalse)
1644 continue;
1645 channel=GetPixelChannelMapChannel(image,i);
1646 traits=GetPixelChannelMapTraits(image,channel);
1647 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1648 if ((traits == UndefinedPixelTrait) ||
1649 (despeckle_traits == UndefinedPixelTrait))
1650 continue;
1651 if ((despeckle_traits & CopyPixelTrait) != 0)
1652 continue;
1653 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1654 j=(ssize_t) image->columns+2;
1655 for (y=0; y < (ssize_t) image->rows; y++)
cristyfc830f42012-01-15 02:45:06 +00001656 {
cristy5473bf02012-01-16 19:15:08 +00001657 register const Quantum
1658 *restrict p;
cristyfc830f42012-01-15 02:45:06 +00001659
cristy5473bf02012-01-16 19:15:08 +00001660 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1661 if (p == (const Quantum *) NULL)
cristyf0ae7762012-01-15 22:04:03 +00001662 {
cristy5473bf02012-01-16 19:15:08 +00001663 status=MagickFalse;
1664 continue;
cristyf0ae7762012-01-15 22:04:03 +00001665 }
cristy5473bf02012-01-16 19:15:08 +00001666 j++;
1667 for (x=0; x < (ssize_t) image->columns; x++)
1668 {
1669 pixels[j++]=p[i];
1670 p+=GetPixelChannels(image);
cristyfc830f42012-01-15 02:45:06 +00001671 }
cristy5473bf02012-01-16 19:15:08 +00001672 j++;
cristy3ed852e2009-09-05 21:47:34 +00001673 }
cristy5473bf02012-01-16 19:15:08 +00001674 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1675 for (k=0; k < 4; k++)
1676 {
1677 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
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 }
1682 j=(ssize_t) image->columns+2;
1683 for (y=0; y < (ssize_t) image->rows; y++)
1684 {
1685 MagickBooleanType
1686 sync;
1687
1688 register Quantum
1689 *restrict q;
1690
cristyd13e8eb2012-01-17 02:36:42 +00001691 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1692 despeckle_image->columns,1,exception);
cristy5473bf02012-01-16 19:15:08 +00001693 if (q == (Quantum *) NULL)
1694 {
1695 status=MagickFalse;
1696 continue;
1697 }
1698 j++;
1699 for (x=0; x < (ssize_t) image->columns; x++)
1700 {
1701 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1702 q+=GetPixelChannels(despeckle_image);
1703 }
1704 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1705 if (sync == MagickFalse)
1706 status=MagickFalse;
1707 j++;
1708 }
cristy3ed852e2009-09-05 21:47:34 +00001709 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1710 {
1711 MagickBooleanType
1712 proceed;
1713
cristy5473bf02012-01-16 19:15:08 +00001714 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1715 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00001716 if (proceed == MagickFalse)
1717 status=MagickFalse;
1718 }
1719 }
1720 despeckle_view=DestroyCacheView(despeckle_view);
1721 image_view=DestroyCacheView(image_view);
cristy5473bf02012-01-16 19:15:08 +00001722 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1723 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001724 despeckle_image->type=image->type;
1725 if (status == MagickFalse)
1726 despeckle_image=DestroyImage(despeckle_image);
1727 return(despeckle_image);
1728}
1729
1730/*
1731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1732% %
1733% %
1734% %
1735% E d g e I m a g e %
1736% %
1737% %
1738% %
1739%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1740%
1741% EdgeImage() finds edges in an image. Radius defines the radius of the
1742% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1743% radius for you.
1744%
1745% The format of the EdgeImage method is:
1746%
1747% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001748% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001749%
1750% A description of each parameter follows:
1751%
1752% o image: the image.
1753%
1754% o radius: the radius of the pixel neighborhood.
1755%
cristy8ae632d2011-09-05 17:29:53 +00001756% o sigma: the standard deviation of the Gaussian, in pixels.
1757%
cristy3ed852e2009-09-05 21:47:34 +00001758% o exception: return any errors or warnings in this structure.
1759%
1760*/
1761MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001762 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001763{
1764 Image
1765 *edge_image;
1766
cristy41cbe682011-07-15 19:12:37 +00001767 KernelInfo
1768 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001769
cristybb503372010-05-27 20:51:26 +00001770 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001771 i;
1772
cristybb503372010-05-27 20:51:26 +00001773 size_t
cristy3ed852e2009-09-05 21:47:34 +00001774 width;
1775
cristy41cbe682011-07-15 19:12:37 +00001776 ssize_t
1777 j,
1778 u,
1779 v;
1780
cristy3ed852e2009-09-05 21:47:34 +00001781 assert(image != (const Image *) NULL);
1782 assert(image->signature == MagickSignature);
1783 if (image->debug != MagickFalse)
1784 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1785 assert(exception != (ExceptionInfo *) NULL);
1786 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001787 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001788 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001789 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001790 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001791 kernel_info->width=width;
1792 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001793 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1794 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1795 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001796 {
1797 kernel_info=DestroyKernelInfo(kernel_info);
1798 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1799 }
1800 j=(ssize_t) kernel_info->width/2;
1801 i=0;
1802 for (v=(-j); v <= j; v++)
1803 {
1804 for (u=(-j); u <= j; u++)
1805 {
1806 kernel_info->values[i]=(-1.0);
1807 i++;
1808 }
1809 }
1810 kernel_info->values[i/2]=(double) (width*width-1.0);
anthony736a1602011-10-06 12:38:17 +00001811 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
cristy5e6be1e2011-07-16 01:23:39 +00001812 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001813 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001814 return(edge_image);
1815}
1816
1817/*
1818%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1819% %
1820% %
1821% %
1822% E m b o s s I m a g e %
1823% %
1824% %
1825% %
1826%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1827%
1828% EmbossImage() returns a grayscale image with a three-dimensional effect.
1829% We convolve the image with a Gaussian operator of the given radius and
1830% standard deviation (sigma). For reasonable results, radius should be
1831% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1832% radius for you.
1833%
1834% The format of the EmbossImage method is:
1835%
1836% Image *EmbossImage(const Image *image,const double radius,
1837% const double sigma,ExceptionInfo *exception)
1838%
1839% A description of each parameter follows:
1840%
1841% o image: the image.
1842%
1843% o radius: the radius of the pixel neighborhood.
1844%
1845% o sigma: the standard deviation of the Gaussian, in pixels.
1846%
1847% o exception: return any errors or warnings in this structure.
1848%
1849*/
1850MagickExport Image *EmbossImage(const Image *image,const double radius,
1851 const double sigma,ExceptionInfo *exception)
1852{
cristy3ed852e2009-09-05 21:47:34 +00001853 Image
1854 *emboss_image;
1855
cristy41cbe682011-07-15 19:12:37 +00001856 KernelInfo
1857 *kernel_info;
1858
cristybb503372010-05-27 20:51:26 +00001859 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001860 i;
1861
cristybb503372010-05-27 20:51:26 +00001862 size_t
cristy3ed852e2009-09-05 21:47:34 +00001863 width;
1864
cristy117ff172010-08-15 21:35:32 +00001865 ssize_t
1866 j,
1867 k,
1868 u,
1869 v;
1870
cristy41cbe682011-07-15 19:12:37 +00001871 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001872 assert(image->signature == MagickSignature);
1873 if (image->debug != MagickFalse)
1874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1875 assert(exception != (ExceptionInfo *) NULL);
1876 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001877 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001878 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001879 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001880 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001881 kernel_info->width=width;
1882 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001883 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1884 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1885 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001886 {
1887 kernel_info=DestroyKernelInfo(kernel_info);
1888 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1889 }
1890 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001891 k=j;
1892 i=0;
1893 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001894 {
cristy47e00502009-12-17 19:19:57 +00001895 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001896 {
cristy41cbe682011-07-15 19:12:37 +00001897 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001898 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001899 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001900 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001901 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001902 i++;
1903 }
cristy47e00502009-12-17 19:19:57 +00001904 k--;
cristy3ed852e2009-09-05 21:47:34 +00001905 }
anthony736a1602011-10-06 12:38:17 +00001906 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
cristy5e6be1e2011-07-16 01:23:39 +00001907 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001908 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001909 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001910 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001911 return(emboss_image);
1912}
1913
1914/*
1915%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1916% %
1917% %
1918% %
1919% G a u s s i a n B l u r I m a g e %
1920% %
1921% %
1922% %
1923%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1924%
1925% GaussianBlurImage() blurs an image. We convolve the image with a
1926% Gaussian operator of the given radius and standard deviation (sigma).
1927% For reasonable results, the radius should be larger than sigma. Use a
1928% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1929%
1930% The format of the GaussianBlurImage method is:
1931%
1932% Image *GaussianBlurImage(const Image *image,onst double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001933% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001934%
1935% A description of each parameter follows:
1936%
1937% o image: the image.
1938%
cristy3ed852e2009-09-05 21:47:34 +00001939% o radius: the radius of the Gaussian, in pixels, not counting the center
1940% pixel.
1941%
1942% o sigma: the standard deviation of the Gaussian, in pixels.
1943%
cristy05c0c9a2011-09-05 23:16:13 +00001944% o bias: the bias.
1945%
cristy3ed852e2009-09-05 21:47:34 +00001946% o exception: return any errors or warnings in this structure.
1947%
1948*/
cristy41cbe682011-07-15 19:12:37 +00001949MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001950 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001951{
cristy3ed852e2009-09-05 21:47:34 +00001952 Image
1953 *blur_image;
1954
cristy41cbe682011-07-15 19:12:37 +00001955 KernelInfo
1956 *kernel_info;
1957
cristybb503372010-05-27 20:51:26 +00001958 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001959 i;
1960
cristybb503372010-05-27 20:51:26 +00001961 size_t
cristy3ed852e2009-09-05 21:47:34 +00001962 width;
1963
cristy117ff172010-08-15 21:35:32 +00001964 ssize_t
1965 j,
1966 u,
1967 v;
1968
cristy3ed852e2009-09-05 21:47:34 +00001969 assert(image != (const Image *) NULL);
1970 assert(image->signature == MagickSignature);
1971 if (image->debug != MagickFalse)
1972 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1973 assert(exception != (ExceptionInfo *) NULL);
1974 assert(exception->signature == MagickSignature);
1975 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001976 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001977 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001978 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001979 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1980 kernel_info->width=width;
1981 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00001982 kernel_info->bias=bias; /* FUTURE: user bias on Gaussian Blur! non-sense */
cristy41cbe682011-07-15 19:12:37 +00001983 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00001984 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1985 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1986 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001987 {
1988 kernel_info=DestroyKernelInfo(kernel_info);
1989 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1990 }
1991 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001992 i=0;
cristy47e00502009-12-17 19:19:57 +00001993 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001994 {
cristy47e00502009-12-17 19:19:57 +00001995 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001996 {
1997 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1998 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1999 i++;
2000 }
cristy3ed852e2009-09-05 21:47:34 +00002001 }
cristy5e6be1e2011-07-16 01:23:39 +00002002 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00002003 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00002004 return(blur_image);
2005}
2006
2007/*
2008%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2009% %
2010% %
2011% %
cristy3ed852e2009-09-05 21:47:34 +00002012% M o t i o n B l u r I m a g e %
2013% %
2014% %
2015% %
2016%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017%
2018% MotionBlurImage() simulates motion blur. We convolve the image with a
2019% Gaussian operator of the given radius and standard deviation (sigma).
2020% For reasonable results, radius should be larger than sigma. Use a
2021% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2022% Angle gives the angle of the blurring motion.
2023%
2024% Andrew Protano contributed this effect.
2025%
2026% The format of the MotionBlurImage method is:
2027%
2028% Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002029% const double sigma,const double angle,const double bias,
2030% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002031%
2032% A description of each parameter follows:
2033%
2034% o image: the image.
2035%
cristy3ed852e2009-09-05 21:47:34 +00002036% o radius: the radius of the Gaussian, in pixels, not counting
2037% the center pixel.
2038%
2039% o sigma: the standard deviation of the Gaussian, in pixels.
2040%
cristycee97112010-05-28 00:44:52 +00002041% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002042%
cristyf7ef0252011-09-09 14:50:06 +00002043% o bias: the bias.
2044%
cristy3ed852e2009-09-05 21:47:34 +00002045% o exception: return any errors or warnings in this structure.
2046%
2047*/
2048
cristybb503372010-05-27 20:51:26 +00002049static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002050{
cristy3ed852e2009-09-05 21:47:34 +00002051 double
cristy47e00502009-12-17 19:19:57 +00002052 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002053 normalize;
2054
cristybb503372010-05-27 20:51:26 +00002055 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002056 i;
2057
2058 /*
cristy47e00502009-12-17 19:19:57 +00002059 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002060 */
2061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +00002062 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00002063 if (kernel == (double *) NULL)
2064 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002065 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002066 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002067 {
cristy4205a3c2010-09-12 20:19:59 +00002068 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2069 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002070 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002071 }
cristybb503372010-05-27 20:51:26 +00002072 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002073 kernel[i]/=normalize;
2074 return(kernel);
2075}
2076
cristya63e4a92011-09-09 00:47:59 +00002077MagickExport Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002078 const double sigma,const double angle,const double bias,
2079 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002080{
cristyc4c8d132010-01-07 01:58:38 +00002081 CacheView
2082 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002083 *image_view,
2084 *motion_view;
cristyc4c8d132010-01-07 01:58:38 +00002085
cristy3ed852e2009-09-05 21:47:34 +00002086 double
2087 *kernel;
2088
2089 Image
2090 *blur_image;
2091
cristy3ed852e2009-09-05 21:47:34 +00002092 MagickBooleanType
2093 status;
2094
cristybb503372010-05-27 20:51:26 +00002095 MagickOffsetType
2096 progress;
2097
cristy3ed852e2009-09-05 21:47:34 +00002098 OffsetInfo
2099 *offset;
2100
2101 PointInfo
2102 point;
2103
cristybb503372010-05-27 20:51:26 +00002104 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002105 i;
2106
cristybb503372010-05-27 20:51:26 +00002107 size_t
cristy3ed852e2009-09-05 21:47:34 +00002108 width;
2109
cristybb503372010-05-27 20:51:26 +00002110 ssize_t
2111 y;
2112
cristy3ed852e2009-09-05 21:47:34 +00002113 assert(image != (Image *) NULL);
2114 assert(image->signature == MagickSignature);
2115 if (image->debug != MagickFalse)
2116 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2117 assert(exception != (ExceptionInfo *) NULL);
2118 width=GetOptimalKernelWidth1D(radius,sigma);
2119 kernel=GetMotionBlurKernel(width,sigma);
2120 if (kernel == (double *) NULL)
2121 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2122 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2123 if (offset == (OffsetInfo *) NULL)
2124 {
cristy8b49f382012-01-17 03:11:03 +00002125 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002126 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2127 }
cristy1e7aa312011-09-10 20:01:36 +00002128 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002129 if (blur_image == (Image *) NULL)
2130 {
cristy8b49f382012-01-17 03:11:03 +00002131 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002132 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2133 return((Image *) NULL);
2134 }
cristy574cc262011-08-05 01:23:58 +00002135 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002136 {
cristy8b49f382012-01-17 03:11:03 +00002137 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002138 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002139 blur_image=DestroyImage(blur_image);
2140 return((Image *) NULL);
2141 }
2142 point.x=(double) width*sin(DegreesToRadians(angle));
2143 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002144 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002145 {
cristybb503372010-05-27 20:51:26 +00002146 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2147 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002148 }
2149 /*
2150 Motion blur image.
2151 */
2152 status=MagickTrue;
2153 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002154 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002155 motion_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002156 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002157#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00002158 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002159#endif
cristybb503372010-05-27 20:51:26 +00002160 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002161 {
cristyf7ef0252011-09-09 14:50:06 +00002162 register const Quantum
2163 *restrict p;
2164
cristy4c08aed2011-07-01 19:47:50 +00002165 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002166 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002167
cristy117ff172010-08-15 21:35:32 +00002168 register ssize_t
2169 x;
2170
cristy3ed852e2009-09-05 21:47:34 +00002171 if (status == MagickFalse)
2172 continue;
cristy8b49f382012-01-17 03:11:03 +00002173 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2174 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002175 exception);
cristyf7ef0252011-09-09 14:50:06 +00002176 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002177 {
2178 status=MagickFalse;
2179 continue;
2180 }
cristybb503372010-05-27 20:51:26 +00002181 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002182 {
cristybb503372010-05-27 20:51:26 +00002183 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002184 i;
2185
cristyf7ef0252011-09-09 14:50:06 +00002186 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2187 {
2188 MagickRealType
2189 alpha,
2190 gamma,
2191 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002192
cristyf7ef0252011-09-09 14:50:06 +00002193 PixelChannel
2194 channel;
2195
2196 PixelTrait
2197 blur_traits,
2198 traits;
2199
2200 register const Quantum
2201 *restrict r;
2202
2203 register double
2204 *restrict k;
2205
2206 register ssize_t
2207 j;
2208
cristye2a912b2011-12-05 20:02:07 +00002209 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002210 traits=GetPixelChannelMapTraits(image,channel);
cristyf7ef0252011-09-09 14:50:06 +00002211 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2212 if ((traits == UndefinedPixelTrait) ||
2213 (blur_traits == UndefinedPixelTrait))
2214 continue;
2215 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002216 {
cristy0beccfa2011-09-25 20:47:53 +00002217 SetPixelChannel(blur_image,channel,p[i],q);
cristyf7ef0252011-09-09 14:50:06 +00002218 continue;
cristy3ed852e2009-09-05 21:47:34 +00002219 }
cristyf7ef0252011-09-09 14:50:06 +00002220 k=kernel;
2221 pixel=bias;
2222 if ((blur_traits & BlendPixelTrait) == 0)
2223 {
2224 for (j=0; j < (ssize_t) width; j++)
2225 {
cristy8b49f382012-01-17 03:11:03 +00002226 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
cristyf7ef0252011-09-09 14:50:06 +00002227 offset[j].y,1,1,exception);
2228 if (r == (const Quantum *) NULL)
2229 {
2230 status=MagickFalse;
2231 continue;
2232 }
2233 pixel+=(*k)*r[i];
2234 k++;
2235 }
cristy0beccfa2011-09-25 20:47:53 +00002236 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002237 continue;
2238 }
2239 alpha=0.0;
2240 gamma=0.0;
2241 for (j=0; j < (ssize_t) width; j++)
2242 {
cristy8b49f382012-01-17 03:11:03 +00002243 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
cristyf7ef0252011-09-09 14:50:06 +00002244 1,exception);
2245 if (r == (const Quantum *) NULL)
2246 {
2247 status=MagickFalse;
2248 continue;
2249 }
2250 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2251 pixel+=(*k)*alpha*r[i];
2252 gamma+=(*k)*alpha;
2253 k++;
cristy3ed852e2009-09-05 21:47:34 +00002254 }
cristyf7ef0252011-09-09 14:50:06 +00002255 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002256 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002257 }
2258 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002259 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002260 }
2261 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2262 status=MagickFalse;
2263 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2264 {
2265 MagickBooleanType
2266 proceed;
2267
cristyb557a152011-02-22 12:14:30 +00002268#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002269 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002270#endif
2271 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2272 if (proceed == MagickFalse)
2273 status=MagickFalse;
2274 }
2275 }
2276 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00002277 motion_view=DestroyCacheView(motion_view);
cristy3ed852e2009-09-05 21:47:34 +00002278 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00002279 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002280 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2281 if (status == MagickFalse)
2282 blur_image=DestroyImage(blur_image);
2283 return(blur_image);
2284}
2285
2286/*
2287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2288% %
2289% %
2290% %
2291% P r e v i e w I m a g e %
2292% %
2293% %
2294% %
2295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2296%
2297% PreviewImage() tiles 9 thumbnails of the specified image with an image
2298% processing operation applied with varying parameters. This may be helpful
2299% pin-pointing an appropriate parameter for a particular image processing
2300% operation.
2301%
2302% The format of the PreviewImages method is:
2303%
2304% Image *PreviewImages(const Image *image,const PreviewType preview,
2305% ExceptionInfo *exception)
2306%
2307% A description of each parameter follows:
2308%
2309% o image: the image.
2310%
2311% o preview: the image processing operation.
2312%
2313% o exception: return any errors or warnings in this structure.
2314%
2315*/
2316MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2317 ExceptionInfo *exception)
2318{
2319#define NumberTiles 9
2320#define PreviewImageTag "Preview/Image"
2321#define DefaultPreviewGeometry "204x204+10+10"
2322
2323 char
2324 factor[MaxTextExtent],
2325 label[MaxTextExtent];
2326
2327 double
2328 degrees,
2329 gamma,
2330 percentage,
2331 radius,
2332 sigma,
2333 threshold;
2334
2335 Image
2336 *images,
2337 *montage_image,
2338 *preview_image,
2339 *thumbnail;
2340
2341 ImageInfo
2342 *preview_info;
2343
cristy3ed852e2009-09-05 21:47:34 +00002344 MagickBooleanType
2345 proceed;
2346
2347 MontageInfo
2348 *montage_info;
2349
2350 QuantizeInfo
2351 quantize_info;
2352
2353 RectangleInfo
2354 geometry;
2355
cristybb503372010-05-27 20:51:26 +00002356 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002357 i,
2358 x;
2359
cristybb503372010-05-27 20:51:26 +00002360 size_t
cristy3ed852e2009-09-05 21:47:34 +00002361 colors;
2362
cristy117ff172010-08-15 21:35:32 +00002363 ssize_t
2364 y;
2365
cristy3ed852e2009-09-05 21:47:34 +00002366 /*
2367 Open output image file.
2368 */
2369 assert(image != (Image *) NULL);
2370 assert(image->signature == MagickSignature);
2371 if (image->debug != MagickFalse)
2372 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2373 colors=2;
2374 degrees=0.0;
2375 gamma=(-0.2f);
2376 preview_info=AcquireImageInfo();
2377 SetGeometry(image,&geometry);
2378 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2379 &geometry.width,&geometry.height);
2380 images=NewImageList();
2381 percentage=12.5;
2382 GetQuantizeInfo(&quantize_info);
2383 radius=0.0;
2384 sigma=1.0;
2385 threshold=0.0;
2386 x=0;
2387 y=0;
2388 for (i=0; i < NumberTiles; i++)
2389 {
2390 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2391 if (thumbnail == (Image *) NULL)
2392 break;
2393 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2394 (void *) NULL);
cristyd15e6592011-10-15 00:13:06 +00002395 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002396 if (i == (NumberTiles/2))
2397 {
cristy9950d572011-10-01 18:22:35 +00002398 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2399 &thumbnail->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002400 AppendImageToList(&images,thumbnail);
2401 continue;
2402 }
2403 switch (preview)
2404 {
2405 case RotatePreview:
2406 {
2407 degrees+=45.0;
2408 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002409 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002410 break;
2411 }
2412 case ShearPreview:
2413 {
2414 degrees+=5.0;
2415 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002416 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002417 degrees,2.0*degrees);
2418 break;
2419 }
2420 case RollPreview:
2421 {
cristybb503372010-05-27 20:51:26 +00002422 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2423 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002424 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002425 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002426 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002427 break;
2428 }
2429 case HuePreview:
2430 {
2431 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2432 if (preview_image == (Image *) NULL)
2433 break;
cristyb51dff52011-05-19 16:55:47 +00002434 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002435 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002436 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002437 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002438 break;
2439 }
2440 case SaturationPreview:
2441 {
2442 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2443 if (preview_image == (Image *) NULL)
2444 break;
cristyb51dff52011-05-19 16:55:47 +00002445 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002446 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002447 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002448 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002449 break;
2450 }
2451 case BrightnessPreview:
2452 {
2453 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2454 if (preview_image == (Image *) NULL)
2455 break;
cristyb51dff52011-05-19 16:55:47 +00002456 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002457 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002458 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002459 break;
2460 }
2461 case GammaPreview:
2462 default:
2463 {
2464 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2465 if (preview_image == (Image *) NULL)
2466 break;
2467 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002468 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002469 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002470 break;
2471 }
2472 case SpiffPreview:
2473 {
2474 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2475 if (preview_image != (Image *) NULL)
2476 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002477 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002478 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002479 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002480 break;
2481 }
2482 case DullPreview:
2483 {
2484 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2485 if (preview_image == (Image *) NULL)
2486 break;
2487 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002488 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002489 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002490 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002491 break;
2492 }
2493 case GrayscalePreview:
2494 {
2495 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2496 if (preview_image == (Image *) NULL)
2497 break;
2498 colors<<=1;
2499 quantize_info.number_colors=colors;
2500 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002501 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002502 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002503 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002504 break;
2505 }
2506 case QuantizePreview:
2507 {
2508 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2509 if (preview_image == (Image *) NULL)
2510 break;
2511 colors<<=1;
2512 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002513 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002514 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002515 colors);
cristy3ed852e2009-09-05 21:47:34 +00002516 break;
2517 }
2518 case DespecklePreview:
2519 {
2520 for (x=0; x < (i-1); x++)
2521 {
2522 preview_image=DespeckleImage(thumbnail,exception);
2523 if (preview_image == (Image *) NULL)
2524 break;
2525 thumbnail=DestroyImage(thumbnail);
2526 thumbnail=preview_image;
2527 }
2528 preview_image=DespeckleImage(thumbnail,exception);
2529 if (preview_image == (Image *) NULL)
2530 break;
cristyb51dff52011-05-19 16:55:47 +00002531 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002532 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002533 break;
2534 }
2535 case ReduceNoisePreview:
2536 {
cristy95c38342011-03-18 22:39:51 +00002537 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2538 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002539 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002540 break;
2541 }
2542 case AddNoisePreview:
2543 {
2544 switch ((int) i)
2545 {
2546 case 0:
2547 {
2548 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2549 break;
2550 }
2551 case 1:
2552 {
2553 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2554 break;
2555 }
2556 case 2:
2557 {
2558 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2559 break;
2560 }
2561 case 3:
2562 {
2563 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2564 break;
2565 }
2566 case 4:
2567 {
2568 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2569 break;
2570 }
2571 case 5:
2572 {
2573 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2574 break;
2575 }
2576 default:
2577 {
2578 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2579 break;
2580 }
2581 }
cristyd76c51e2011-03-26 00:21:26 +00002582 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2583 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002584 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002585 break;
2586 }
2587 case SharpenPreview:
2588 {
anthony736a1602011-10-06 12:38:17 +00002589 /* FUTURE: user bias on sharpen! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002590 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2591 exception);
cristyb51dff52011-05-19 16:55:47 +00002592 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002593 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002594 break;
2595 }
2596 case BlurPreview:
2597 {
anthony736a1602011-10-06 12:38:17 +00002598 /* FUTURE: user bias on blur! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002599 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002600 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002601 sigma);
2602 break;
2603 }
2604 case ThresholdPreview:
2605 {
2606 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2607 if (preview_image == (Image *) NULL)
2608 break;
cristye941a752011-10-15 01:52:48 +00002609 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2610 QuantumRange+1.0))/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002611 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002612 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2613 break;
2614 }
2615 case EdgeDetectPreview:
2616 {
cristy8ae632d2011-09-05 17:29:53 +00002617 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002618 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002619 break;
2620 }
2621 case SpreadPreview:
2622 {
cristy5c4e2582011-09-11 19:21:03 +00002623 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2624 exception);
cristyb51dff52011-05-19 16:55:47 +00002625 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002626 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002627 break;
2628 }
2629 case SolarizePreview:
2630 {
2631 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2632 if (preview_image == (Image *) NULL)
2633 break;
2634 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002635 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002636 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002637 (QuantumRange*percentage)/100.0);
2638 break;
2639 }
2640 case ShadePreview:
2641 {
2642 degrees+=10.0;
2643 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2644 exception);
cristyb51dff52011-05-19 16:55:47 +00002645 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002646 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002647 break;
2648 }
2649 case RaisePreview:
2650 {
2651 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2652 if (preview_image == (Image *) NULL)
2653 break;
cristybb503372010-05-27 20:51:26 +00002654 geometry.width=(size_t) (2*i+2);
2655 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002656 geometry.x=i/2;
2657 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002658 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002659 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002660 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002661 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002662 break;
2663 }
2664 case SegmentPreview:
2665 {
2666 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2667 if (preview_image == (Image *) NULL)
2668 break;
2669 threshold+=0.4f;
2670 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002671 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002672 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002673 threshold,threshold);
2674 break;
2675 }
2676 case SwirlPreview:
2677 {
cristy76f512e2011-09-12 01:26:56 +00002678 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2679 exception);
cristyb51dff52011-05-19 16:55:47 +00002680 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002681 degrees+=45.0;
2682 break;
2683 }
2684 case ImplodePreview:
2685 {
2686 degrees+=0.1f;
cristy76f512e2011-09-12 01:26:56 +00002687 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2688 exception);
cristyb51dff52011-05-19 16:55:47 +00002689 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002690 break;
2691 }
2692 case WavePreview:
2693 {
2694 degrees+=5.0f;
cristy5c4e2582011-09-11 19:21:03 +00002695 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2696 image->interpolate,exception);
cristyb51dff52011-05-19 16:55:47 +00002697 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002698 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002699 break;
2700 }
2701 case OilPaintPreview:
2702 {
cristy14973ba2011-08-27 23:48:07 +00002703 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2704 exception);
2705 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2706 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002707 break;
2708 }
2709 case CharcoalDrawingPreview:
2710 {
anthony736a1602011-10-06 12:38:17 +00002711 /* FUTURE: user bias on charcoal! This is non-sensical! */
cristy3ed852e2009-09-05 21:47:34 +00002712 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristy05c0c9a2011-09-05 23:16:13 +00002713 image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002714 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002715 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002716 break;
2717 }
2718 case JPEGPreview:
2719 {
2720 char
2721 filename[MaxTextExtent];
2722
2723 int
2724 file;
2725
2726 MagickBooleanType
2727 status;
2728
2729 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2730 if (preview_image == (Image *) NULL)
2731 break;
cristybb503372010-05-27 20:51:26 +00002732 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002733 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002734 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002735 file=AcquireUniqueFileResource(filename);
2736 if (file != -1)
2737 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002738 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002739 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002740 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002741 if (status != MagickFalse)
2742 {
2743 Image
2744 *quality_image;
2745
2746 (void) CopyMagickString(preview_info->filename,
2747 preview_image->filename,MaxTextExtent);
2748 quality_image=ReadImage(preview_info,exception);
2749 if (quality_image != (Image *) NULL)
2750 {
2751 preview_image=DestroyImage(preview_image);
2752 preview_image=quality_image;
2753 }
2754 }
2755 (void) RelinquishUniqueFileResource(preview_image->filename);
2756 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002757 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002758 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2759 1024.0/1024.0);
2760 else
2761 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002762 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002763 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002764 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002765 else
cristyb51dff52011-05-19 16:55:47 +00002766 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002767 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002768 break;
2769 }
2770 }
2771 thumbnail=DestroyImage(thumbnail);
2772 percentage+=12.5;
2773 radius+=0.5;
2774 sigma+=0.25;
2775 if (preview_image == (Image *) NULL)
2776 break;
2777 (void) DeleteImageProperty(preview_image,"label");
cristyd15e6592011-10-15 00:13:06 +00002778 (void) SetImageProperty(preview_image,"label",label,exception);
cristy3ed852e2009-09-05 21:47:34 +00002779 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002780 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2781 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002782 if (proceed == MagickFalse)
2783 break;
2784 }
2785 if (images == (Image *) NULL)
2786 {
2787 preview_info=DestroyImageInfo(preview_info);
2788 return((Image *) NULL);
2789 }
2790 /*
2791 Create the montage.
2792 */
2793 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2794 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2795 montage_info->shadow=MagickTrue;
2796 (void) CloneString(&montage_info->tile,"3x3");
2797 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2798 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2799 montage_image=MontageImages(images,montage_info,exception);
2800 montage_info=DestroyMontageInfo(montage_info);
2801 images=DestroyImageList(images);
2802 if (montage_image == (Image *) NULL)
2803 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2804 if (montage_image->montage != (char *) NULL)
2805 {
2806 /*
2807 Free image directory.
2808 */
2809 montage_image->montage=(char *) RelinquishMagickMemory(
2810 montage_image->montage);
2811 if (image->directory != (char *) NULL)
2812 montage_image->directory=(char *) RelinquishMagickMemory(
2813 montage_image->directory);
2814 }
2815 preview_info=DestroyImageInfo(preview_info);
2816 return(montage_image);
2817}
2818
2819/*
2820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2821% %
2822% %
2823% %
2824% R a d i a l B l u r I m a g e %
2825% %
2826% %
2827% %
2828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2829%
2830% RadialBlurImage() applies a radial blur to the image.
2831%
2832% Andrew Protano contributed this effect.
2833%
2834% The format of the RadialBlurImage method is:
2835%
2836% Image *RadialBlurImage(const Image *image,const double angle,
cristy6435bd92011-09-10 02:10:07 +00002837% const double blur,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002838%
2839% A description of each parameter follows:
2840%
2841% o image: the image.
2842%
cristy3ed852e2009-09-05 21:47:34 +00002843% o angle: the angle of the radial blur.
2844%
cristy6435bd92011-09-10 02:10:07 +00002845% o blur: the blur.
2846%
cristy3ed852e2009-09-05 21:47:34 +00002847% o exception: return any errors or warnings in this structure.
2848%
2849*/
cristy4282c702011-11-21 00:01:06 +00002850MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2851 const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002852{
cristyc4c8d132010-01-07 01:58:38 +00002853 CacheView
2854 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002855 *image_view,
2856 *radial_view;
cristyc4c8d132010-01-07 01:58:38 +00002857
cristy3ed852e2009-09-05 21:47:34 +00002858 Image
2859 *blur_image;
2860
cristy3ed852e2009-09-05 21:47:34 +00002861 MagickBooleanType
2862 status;
2863
cristybb503372010-05-27 20:51:26 +00002864 MagickOffsetType
2865 progress;
2866
cristy3ed852e2009-09-05 21:47:34 +00002867 MagickRealType
2868 blur_radius,
2869 *cos_theta,
2870 offset,
2871 *sin_theta,
2872 theta;
2873
2874 PointInfo
2875 blur_center;
2876
cristybb503372010-05-27 20:51:26 +00002877 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002878 i;
2879
cristybb503372010-05-27 20:51:26 +00002880 size_t
cristy3ed852e2009-09-05 21:47:34 +00002881 n;
2882
cristybb503372010-05-27 20:51:26 +00002883 ssize_t
2884 y;
2885
cristy3ed852e2009-09-05 21:47:34 +00002886 /*
2887 Allocate blur image.
2888 */
2889 assert(image != (Image *) NULL);
2890 assert(image->signature == MagickSignature);
2891 if (image->debug != MagickFalse)
2892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2893 assert(exception != (ExceptionInfo *) NULL);
2894 assert(exception->signature == MagickSignature);
cristy1e7aa312011-09-10 20:01:36 +00002895 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002896 if (blur_image == (Image *) NULL)
2897 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002898 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002899 {
cristy3ed852e2009-09-05 21:47:34 +00002900 blur_image=DestroyImage(blur_image);
2901 return((Image *) NULL);
2902 }
2903 blur_center.x=(double) image->columns/2.0;
2904 blur_center.y=(double) image->rows/2.0;
2905 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002906 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002907 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2908 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2909 sizeof(*cos_theta));
2910 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2911 sizeof(*sin_theta));
2912 if ((cos_theta == (MagickRealType *) NULL) ||
2913 (sin_theta == (MagickRealType *) NULL))
2914 {
2915 blur_image=DestroyImage(blur_image);
2916 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2917 }
2918 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002919 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002920 {
2921 cos_theta[i]=cos((double) (theta*i-offset));
2922 sin_theta[i]=sin((double) (theta*i-offset));
2923 }
2924 /*
2925 Radial blur image.
2926 */
2927 status=MagickTrue;
2928 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002929 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002930 radial_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002931 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002932#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002933 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002934#endif
cristy1e7aa312011-09-10 20:01:36 +00002935 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002936 {
cristy1e7aa312011-09-10 20:01:36 +00002937 register const Quantum
2938 *restrict p;
2939
cristy4c08aed2011-07-01 19:47:50 +00002940 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002941 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002942
cristy117ff172010-08-15 21:35:32 +00002943 register ssize_t
2944 x;
2945
cristy3ed852e2009-09-05 21:47:34 +00002946 if (status == MagickFalse)
2947 continue;
cristy8b49f382012-01-17 03:11:03 +00002948 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2949 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002950 exception);
cristy1e7aa312011-09-10 20:01:36 +00002951 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002952 {
2953 status=MagickFalse;
2954 continue;
2955 }
cristy1e7aa312011-09-10 20:01:36 +00002956 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002957 {
cristy3ed852e2009-09-05 21:47:34 +00002958 MagickRealType
cristy3ed852e2009-09-05 21:47:34 +00002959 radius;
2960
cristy3ed852e2009-09-05 21:47:34 +00002961 PointInfo
2962 center;
2963
cristybb503372010-05-27 20:51:26 +00002964 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002965 i;
2966
cristybb503372010-05-27 20:51:26 +00002967 size_t
cristy3ed852e2009-09-05 21:47:34 +00002968 step;
2969
2970 center.x=(double) x-blur_center.x;
2971 center.y=(double) y-blur_center.y;
2972 radius=hypot((double) center.x,center.y);
2973 if (radius == 0)
2974 step=1;
2975 else
2976 {
cristybb503372010-05-27 20:51:26 +00002977 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002978 if (step == 0)
2979 step=1;
2980 else
2981 if (step >= n)
2982 step=n-1;
2983 }
cristy1e7aa312011-09-10 20:01:36 +00002984 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2985 {
2986 MagickRealType
2987 gamma,
2988 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002989
cristy1e7aa312011-09-10 20:01:36 +00002990 PixelChannel
2991 channel;
2992
2993 PixelTrait
2994 blur_traits,
2995 traits;
2996
2997 register const Quantum
2998 *restrict r;
2999
3000 register ssize_t
3001 j;
3002
cristye2a912b2011-12-05 20:02:07 +00003003 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003004 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003005 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3006 if ((traits == UndefinedPixelTrait) ||
3007 (blur_traits == UndefinedPixelTrait))
3008 continue;
3009 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003010 {
cristy0beccfa2011-09-25 20:47:53 +00003011 SetPixelChannel(blur_image,channel,p[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003012 continue;
cristy3ed852e2009-09-05 21:47:34 +00003013 }
cristy1e7aa312011-09-10 20:01:36 +00003014 gamma=0.0;
3015 pixel=bias;
3016 if ((blur_traits & BlendPixelTrait) == 0)
3017 {
3018 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3019 {
cristy8b49f382012-01-17 03:11:03 +00003020 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003021 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3022 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3023 1,1,exception);
3024 if (r == (const Quantum *) NULL)
3025 {
3026 status=MagickFalse;
3027 continue;
3028 }
3029 pixel+=r[i];
3030 gamma++;
3031 }
3032 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003033 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003034 continue;
3035 }
3036 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3037 {
cristy8b49f382012-01-17 03:11:03 +00003038 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003039 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3040 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3041 1,1,exception);
3042 if (r == (const Quantum *) NULL)
3043 {
3044 status=MagickFalse;
3045 continue;
3046 }
3047 pixel+=GetPixelAlpha(image,r)*r[i];
3048 gamma+=GetPixelAlpha(image,r);
cristy3ed852e2009-09-05 21:47:34 +00003049 }
cristy1e7aa312011-09-10 20:01:36 +00003050 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003051 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003052 }
3053 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003054 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003055 }
3056 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3057 status=MagickFalse;
3058 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3059 {
3060 MagickBooleanType
3061 proceed;
3062
cristyb5d5f722009-11-04 03:03:49 +00003063#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003064 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003065#endif
3066 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3067 if (proceed == MagickFalse)
3068 status=MagickFalse;
3069 }
3070 }
3071 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00003072 radial_view=DestroyCacheView(radial_view);
cristy3ed852e2009-09-05 21:47:34 +00003073 image_view=DestroyCacheView(image_view);
3074 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3075 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3076 if (status == MagickFalse)
3077 blur_image=DestroyImage(blur_image);
3078 return(blur_image);
3079}
3080
3081/*
3082%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3083% %
3084% %
3085% %
cristy3ed852e2009-09-05 21:47:34 +00003086% S e l e c t i v e B l u r I m a g e %
3087% %
3088% %
3089% %
3090%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3091%
3092% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3093% It is similar to the unsharpen mask that sharpens everything with contrast
3094% above a certain threshold.
3095%
3096% The format of the SelectiveBlurImage method is:
3097%
3098% Image *SelectiveBlurImage(const Image *image,const double radius,
cristy1e7aa312011-09-10 20:01:36 +00003099% const double sigma,const double threshold,const double bias,
3100% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003101%
3102% A description of each parameter follows:
3103%
3104% o image: the image.
3105%
cristy3ed852e2009-09-05 21:47:34 +00003106% o radius: the radius of the Gaussian, in pixels, not counting the center
3107% pixel.
3108%
3109% o sigma: the standard deviation of the Gaussian, in pixels.
3110%
3111% o threshold: only pixels within this contrast threshold are included
3112% in the blur operation.
3113%
cristy1e7aa312011-09-10 20:01:36 +00003114% o bias: the bias.
3115%
cristy3ed852e2009-09-05 21:47:34 +00003116% o exception: return any errors or warnings in this structure.
3117%
3118*/
cristy4282c702011-11-21 00:01:06 +00003119MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3120 const double sigma,const double threshold,const double bias,
3121 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003122{
3123#define SelectiveBlurImageTag "SelectiveBlur/Image"
3124
cristy47e00502009-12-17 19:19:57 +00003125 CacheView
3126 *blur_view,
3127 *image_view;
3128
cristy3ed852e2009-09-05 21:47:34 +00003129 double
cristy3ed852e2009-09-05 21:47:34 +00003130 *kernel;
3131
3132 Image
3133 *blur_image;
3134
cristy3ed852e2009-09-05 21:47:34 +00003135 MagickBooleanType
3136 status;
3137
cristybb503372010-05-27 20:51:26 +00003138 MagickOffsetType
3139 progress;
3140
cristybb503372010-05-27 20:51:26 +00003141 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003142 i;
cristy3ed852e2009-09-05 21:47:34 +00003143
cristybb503372010-05-27 20:51:26 +00003144 size_t
cristy3ed852e2009-09-05 21:47:34 +00003145 width;
3146
cristybb503372010-05-27 20:51:26 +00003147 ssize_t
cristyc8523c12011-09-13 00:02:53 +00003148 center,
cristybb503372010-05-27 20:51:26 +00003149 j,
3150 u,
3151 v,
3152 y;
3153
cristy3ed852e2009-09-05 21:47:34 +00003154 /*
3155 Initialize blur image attributes.
3156 */
3157 assert(image != (Image *) NULL);
3158 assert(image->signature == MagickSignature);
3159 if (image->debug != MagickFalse)
3160 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3161 assert(exception != (ExceptionInfo *) NULL);
3162 assert(exception->signature == MagickSignature);
3163 width=GetOptimalKernelWidth1D(radius,sigma);
cristy8b49f382012-01-17 03:11:03 +00003164 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00003165 if (kernel == (double *) NULL)
3166 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003167 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003168 i=0;
cristy47e00502009-12-17 19:19:57 +00003169 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003170 {
cristy47e00502009-12-17 19:19:57 +00003171 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003172 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3173 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003174 }
3175 if (image->debug != MagickFalse)
3176 {
3177 char
3178 format[MaxTextExtent],
3179 *message;
3180
cristy117ff172010-08-15 21:35:32 +00003181 register const double
3182 *k;
3183
cristybb503372010-05-27 20:51:26 +00003184 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003185 u,
3186 v;
3187
cristy3ed852e2009-09-05 21:47:34 +00003188 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003189 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3190 width);
cristy3ed852e2009-09-05 21:47:34 +00003191 message=AcquireString("");
3192 k=kernel;
cristybb503372010-05-27 20:51:26 +00003193 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003194 {
3195 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003196 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003197 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003198 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003199 {
cristyb51dff52011-05-19 16:55:47 +00003200 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003201 (void) ConcatenateString(&message,format);
3202 }
3203 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3204 }
3205 message=DestroyString(message);
3206 }
cristy1e7aa312011-09-10 20:01:36 +00003207 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003208 if (blur_image == (Image *) NULL)
3209 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003210 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003211 {
cristy3ed852e2009-09-05 21:47:34 +00003212 blur_image=DestroyImage(blur_image);
3213 return((Image *) NULL);
3214 }
3215 /*
3216 Threshold blur image.
3217 */
3218 status=MagickTrue;
3219 progress=0;
cristyc8523c12011-09-13 00:02:53 +00003220 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3221 GetPixelChannels(image)*(width/2L));
cristy3ed852e2009-09-05 21:47:34 +00003222 image_view=AcquireCacheView(image);
3223 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003224#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003225 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003226#endif
cristybb503372010-05-27 20:51:26 +00003227 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003228 {
cristy4c08aed2011-07-01 19:47:50 +00003229 double
3230 contrast;
3231
cristy3ed852e2009-09-05 21:47:34 +00003232 MagickBooleanType
3233 sync;
3234
cristy4c08aed2011-07-01 19:47:50 +00003235 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003236 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003237
cristy4c08aed2011-07-01 19:47:50 +00003238 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003239 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003240
cristy117ff172010-08-15 21:35:32 +00003241 register ssize_t
3242 x;
3243
cristy3ed852e2009-09-05 21:47:34 +00003244 if (status == MagickFalse)
3245 continue;
cristy117ff172010-08-15 21:35:32 +00003246 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3247 (width/2L),image->columns+width,width,exception);
cristy8b49f382012-01-17 03:11:03 +00003248 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003249 exception);
cristy4c08aed2011-07-01 19:47:50 +00003250 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003251 {
3252 status=MagickFalse;
3253 continue;
3254 }
cristybb503372010-05-27 20:51:26 +00003255 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003256 {
cristybb503372010-05-27 20:51:26 +00003257 register ssize_t
cristy1e7aa312011-09-10 20:01:36 +00003258 i;
cristy3ed852e2009-09-05 21:47:34 +00003259
cristy1e7aa312011-09-10 20:01:36 +00003260 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3261 {
3262 MagickRealType
3263 alpha,
3264 gamma,
3265 intensity,
3266 pixel;
cristy117ff172010-08-15 21:35:32 +00003267
cristy1e7aa312011-09-10 20:01:36 +00003268 PixelChannel
3269 channel;
3270
3271 PixelTrait
3272 blur_traits,
3273 traits;
3274
3275 register const double
3276 *restrict k;
3277
3278 register const Quantum
3279 *restrict pixels;
3280
3281 register ssize_t
3282 u;
3283
3284 ssize_t
3285 v;
3286
cristye2a912b2011-12-05 20:02:07 +00003287 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003288 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003289 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3290 if ((traits == UndefinedPixelTrait) ||
3291 (blur_traits == UndefinedPixelTrait))
3292 continue;
3293 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003294 {
cristy0beccfa2011-09-25 20:47:53 +00003295 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003296 continue;
cristy3ed852e2009-09-05 21:47:34 +00003297 }
cristy1e7aa312011-09-10 20:01:36 +00003298 k=kernel;
3299 pixel=bias;
3300 pixels=p;
cristyc8523c12011-09-13 00:02:53 +00003301 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
cristy1e7aa312011-09-10 20:01:36 +00003302 gamma=0.0;
3303 if ((blur_traits & BlendPixelTrait) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003304 {
cristy1e7aa312011-09-10 20:01:36 +00003305 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003306 {
cristy1e7aa312011-09-10 20:01:36 +00003307 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003308 {
cristy1e7aa312011-09-10 20:01:36 +00003309 contrast=GetPixelIntensity(image,pixels)-intensity;
3310 if (fabs(contrast) < threshold)
3311 {
3312 pixel+=(*k)*pixels[i];
3313 gamma+=(*k);
3314 }
3315 k++;
3316 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003317 }
cristy1e7aa312011-09-10 20:01:36 +00003318 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003319 }
cristy1e7aa312011-09-10 20:01:36 +00003320 if (fabs((double) gamma) < MagickEpsilon)
3321 {
cristy0beccfa2011-09-25 20:47:53 +00003322 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003323 continue;
3324 }
3325 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003326 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003327 continue;
3328 }
3329 for (v=0; v < (ssize_t) width; v++)
3330 {
3331 for (u=0; u < (ssize_t) width; u++)
3332 {
3333 contrast=GetPixelIntensity(image,pixels)-intensity;
3334 if (fabs(contrast) < threshold)
3335 {
3336 alpha=(MagickRealType) (QuantumScale*
3337 GetPixelAlpha(image,pixels));
3338 pixel+=(*k)*alpha*pixels[i];
3339 gamma+=(*k)*alpha;
3340 }
3341 k++;
3342 pixels+=GetPixelChannels(image);
3343 }
3344 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003345 }
cristy1e7aa312011-09-10 20:01:36 +00003346 if (fabs((double) gamma) < MagickEpsilon)
3347 {
cristy0beccfa2011-09-25 20:47:53 +00003348 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003349 continue;
3350 }
3351 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003352 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003353 }
cristyed231572011-07-14 02:18:59 +00003354 p+=GetPixelChannels(image);
3355 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003356 }
3357 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3358 if (sync == MagickFalse)
3359 status=MagickFalse;
3360 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3361 {
3362 MagickBooleanType
3363 proceed;
3364
cristyb5d5f722009-11-04 03:03:49 +00003365#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003366 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003367#endif
3368 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3369 image->rows);
3370 if (proceed == MagickFalse)
3371 status=MagickFalse;
3372 }
3373 }
3374 blur_image->type=image->type;
3375 blur_view=DestroyCacheView(blur_view);
3376 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00003377 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00003378 if (status == MagickFalse)
3379 blur_image=DestroyImage(blur_image);
3380 return(blur_image);
3381}
3382
3383/*
3384%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3385% %
3386% %
3387% %
3388% S h a d e I m a g e %
3389% %
3390% %
3391% %
3392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3393%
3394% ShadeImage() shines a distant light on an image to create a
3395% three-dimensional effect. You control the positioning of the light with
3396% azimuth and elevation; azimuth is measured in degrees off the x axis
3397% and elevation is measured in pixels above the Z axis.
3398%
3399% The format of the ShadeImage method is:
3400%
3401% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3402% const double azimuth,const double elevation,ExceptionInfo *exception)
3403%
3404% A description of each parameter follows:
3405%
3406% o image: the image.
3407%
3408% o gray: A value other than zero shades the intensity of each pixel.
3409%
3410% o azimuth, elevation: Define the light source direction.
3411%
3412% o exception: return any errors or warnings in this structure.
3413%
3414*/
3415MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3416 const double azimuth,const double elevation,ExceptionInfo *exception)
3417{
3418#define ShadeImageTag "Shade/Image"
3419
cristyc4c8d132010-01-07 01:58:38 +00003420 CacheView
3421 *image_view,
3422 *shade_view;
3423
cristy3ed852e2009-09-05 21:47:34 +00003424 Image
3425 *shade_image;
3426
cristy3ed852e2009-09-05 21:47:34 +00003427 MagickBooleanType
3428 status;
3429
cristybb503372010-05-27 20:51:26 +00003430 MagickOffsetType
3431 progress;
3432
cristy3ed852e2009-09-05 21:47:34 +00003433 PrimaryInfo
3434 light;
3435
cristybb503372010-05-27 20:51:26 +00003436 ssize_t
3437 y;
3438
cristy3ed852e2009-09-05 21:47:34 +00003439 /*
3440 Initialize shaded image attributes.
3441 */
3442 assert(image != (const Image *) NULL);
3443 assert(image->signature == MagickSignature);
3444 if (image->debug != MagickFalse)
3445 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3446 assert(exception != (ExceptionInfo *) NULL);
3447 assert(exception->signature == MagickSignature);
3448 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3449 if (shade_image == (Image *) NULL)
3450 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003451 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003452 {
cristy3ed852e2009-09-05 21:47:34 +00003453 shade_image=DestroyImage(shade_image);
3454 return((Image *) NULL);
3455 }
3456 /*
3457 Compute the light vector.
3458 */
3459 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3460 cos(DegreesToRadians(elevation));
3461 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3462 cos(DegreesToRadians(elevation));
3463 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3464 /*
3465 Shade image.
3466 */
3467 status=MagickTrue;
3468 progress=0;
3469 image_view=AcquireCacheView(image);
3470 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003471#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003472 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003473#endif
cristybb503372010-05-27 20:51:26 +00003474 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003475 {
3476 MagickRealType
3477 distance,
3478 normal_distance,
3479 shade;
3480
3481 PrimaryInfo
3482 normal;
3483
cristy4c08aed2011-07-01 19:47:50 +00003484 register const Quantum
cristy1e7aa312011-09-10 20:01:36 +00003485 *restrict center,
cristyc47d1f82009-11-26 01:44:43 +00003486 *restrict p,
cristy1e7aa312011-09-10 20:01:36 +00003487 *restrict post,
3488 *restrict pre;
cristy3ed852e2009-09-05 21:47:34 +00003489
cristy4c08aed2011-07-01 19:47:50 +00003490 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003491 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003492
cristy117ff172010-08-15 21:35:32 +00003493 register ssize_t
3494 x;
3495
cristy3ed852e2009-09-05 21:47:34 +00003496 if (status == MagickFalse)
3497 continue;
3498 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3499 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3500 exception);
cristy4c08aed2011-07-01 19:47:50 +00003501 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003502 {
3503 status=MagickFalse;
3504 continue;
3505 }
3506 /*
3507 Shade this row of pixels.
3508 */
3509 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristy1e7aa312011-09-10 20:01:36 +00003510 pre=p+GetPixelChannels(image);
3511 center=pre+(image->columns+2)*GetPixelChannels(image);
3512 post=center+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003513 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003514 {
cristy1e7aa312011-09-10 20:01:36 +00003515 register ssize_t
3516 i;
3517
cristy3ed852e2009-09-05 21:47:34 +00003518 /*
3519 Determine the surface normal and compute shading.
3520 */
cristy1e7aa312011-09-10 20:01:36 +00003521 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3522 GetPixelIntensity(image,center-GetPixelChannels(image))+
3523 GetPixelIntensity(image,post-GetPixelChannels(image))-
3524 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3525 GetPixelIntensity(image,center+GetPixelChannels(image))-
3526 GetPixelIntensity(image,post+GetPixelChannels(image)));
3527 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3528 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3529 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3530 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3531 GetPixelIntensity(image,pre+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003532 if ((normal.x == 0.0) && (normal.y == 0.0))
3533 shade=light.z;
3534 else
3535 {
3536 shade=0.0;
3537 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3538 if (distance > MagickEpsilon)
3539 {
3540 normal_distance=
3541 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3542 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3543 shade=distance/sqrt((double) normal_distance);
3544 }
3545 }
cristy1e7aa312011-09-10 20:01:36 +00003546 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3547 {
3548 PixelChannel
3549 channel;
3550
3551 PixelTrait
3552 shade_traits,
3553 traits;
3554
cristye2a912b2011-12-05 20:02:07 +00003555 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003556 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003557 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3558 if ((traits == UndefinedPixelTrait) ||
3559 (shade_traits == UndefinedPixelTrait))
3560 continue;
3561 if ((shade_traits & CopyPixelTrait) != 0)
3562 {
cristy0beccfa2011-09-25 20:47:53 +00003563 SetPixelChannel(shade_image,channel,center[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003564 continue;
3565 }
3566 if (gray != MagickFalse)
3567 {
cristy0beccfa2011-09-25 20:47:53 +00003568 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
cristy1e7aa312011-09-10 20:01:36 +00003569 continue;
3570 }
cristy0beccfa2011-09-25 20:47:53 +00003571 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3572 center[i]),q);
cristy1e7aa312011-09-10 20:01:36 +00003573 }
3574 pre+=GetPixelChannels(image);
3575 center+=GetPixelChannels(image);
3576 post+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003577 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003578 }
3579 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3580 status=MagickFalse;
3581 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3582 {
3583 MagickBooleanType
3584 proceed;
3585
cristyb5d5f722009-11-04 03:03:49 +00003586#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003587 #pragma omp critical (MagickCore_ShadeImage)
3588#endif
3589 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3590 if (proceed == MagickFalse)
3591 status=MagickFalse;
3592 }
3593 }
3594 shade_view=DestroyCacheView(shade_view);
3595 image_view=DestroyCacheView(image_view);
3596 if (status == MagickFalse)
3597 shade_image=DestroyImage(shade_image);
3598 return(shade_image);
3599}
3600
3601/*
3602%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3603% %
3604% %
3605% %
3606% S h a r p e n I m a g e %
3607% %
3608% %
3609% %
3610%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3611%
3612% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3613% operator of the given radius and standard deviation (sigma). For
3614% reasonable results, radius should be larger than sigma. Use a radius of 0
3615% and SharpenImage() selects a suitable radius for you.
3616%
3617% Using a separable kernel would be faster, but the negative weights cancel
3618% out on the corners of the kernel producing often undesirable ringing in the
3619% filtered result; this can be avoided by using a 2D gaussian shaped image
3620% sharpening kernel instead.
3621%
3622% The format of the SharpenImage method is:
3623%
3624% Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003625% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003626%
3627% A description of each parameter follows:
3628%
3629% o image: the image.
3630%
cristy3ed852e2009-09-05 21:47:34 +00003631% o radius: the radius of the Gaussian, in pixels, not counting the center
3632% pixel.
3633%
3634% o sigma: the standard deviation of the Laplacian, in pixels.
3635%
cristy05c0c9a2011-09-05 23:16:13 +00003636% o bias: bias.
3637%
cristy3ed852e2009-09-05 21:47:34 +00003638% o exception: return any errors or warnings in this structure.
3639%
3640*/
cristy3ed852e2009-09-05 21:47:34 +00003641MagickExport Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003642 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003643{
cristy3ed852e2009-09-05 21:47:34 +00003644 double
cristy47e00502009-12-17 19:19:57 +00003645 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003646
3647 Image
3648 *sharp_image;
3649
cristy41cbe682011-07-15 19:12:37 +00003650 KernelInfo
3651 *kernel_info;
3652
cristybb503372010-05-27 20:51:26 +00003653 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003654 i;
3655
cristybb503372010-05-27 20:51:26 +00003656 size_t
cristy3ed852e2009-09-05 21:47:34 +00003657 width;
3658
cristy117ff172010-08-15 21:35:32 +00003659 ssize_t
3660 j,
3661 u,
3662 v;
3663
cristy3ed852e2009-09-05 21:47:34 +00003664 assert(image != (const Image *) NULL);
3665 assert(image->signature == MagickSignature);
3666 if (image->debug != MagickFalse)
3667 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3668 assert(exception != (ExceptionInfo *) NULL);
3669 assert(exception->signature == MagickSignature);
3670 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003671 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003672 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003673 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003674 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3675 kernel_info->width=width;
3676 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00003677 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
cristy41cbe682011-07-15 19:12:37 +00003678 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00003679 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3680 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3681 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00003682 {
3683 kernel_info=DestroyKernelInfo(kernel_info);
3684 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3685 }
cristy3ed852e2009-09-05 21:47:34 +00003686 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003687 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003688 i=0;
3689 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003690 {
cristy47e00502009-12-17 19:19:57 +00003691 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003692 {
cristy41cbe682011-07-15 19:12:37 +00003693 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3694 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3695 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003696 i++;
3697 }
3698 }
cristy41cbe682011-07-15 19:12:37 +00003699 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003700 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003701 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003702 return(sharp_image);
3703}
3704
3705/*
3706%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3707% %
3708% %
3709% %
3710% S p r e a d I m a g e %
3711% %
3712% %
3713% %
3714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3715%
3716% SpreadImage() is a special effects method that randomly displaces each
3717% pixel in a block defined by the radius parameter.
3718%
3719% The format of the SpreadImage method is:
3720%
3721% Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003722% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003723%
3724% A description of each parameter follows:
3725%
3726% o image: the image.
3727%
cristy5c4e2582011-09-11 19:21:03 +00003728% o radius: choose a random pixel in a neighborhood of this extent.
3729%
3730% o method: the pixel interpolation method.
cristy3ed852e2009-09-05 21:47:34 +00003731%
3732% o exception: return any errors or warnings in this structure.
3733%
3734*/
3735MagickExport Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003736 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003737{
3738#define SpreadImageTag "Spread/Image"
3739
cristyfa112112010-01-04 17:48:07 +00003740 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003741 *image_view,
3742 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003743
cristy3ed852e2009-09-05 21:47:34 +00003744 Image
3745 *spread_image;
3746
cristy3ed852e2009-09-05 21:47:34 +00003747 MagickBooleanType
3748 status;
3749
cristybb503372010-05-27 20:51:26 +00003750 MagickOffsetType
3751 progress;
3752
cristy3ed852e2009-09-05 21:47:34 +00003753 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003754 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003755
cristybb503372010-05-27 20:51:26 +00003756 size_t
cristy3ed852e2009-09-05 21:47:34 +00003757 width;
3758
cristybb503372010-05-27 20:51:26 +00003759 ssize_t
3760 y;
3761
cristy3ed852e2009-09-05 21:47:34 +00003762 /*
3763 Initialize spread image attributes.
3764 */
3765 assert(image != (Image *) NULL);
3766 assert(image->signature == MagickSignature);
3767 if (image->debug != MagickFalse)
3768 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3769 assert(exception != (ExceptionInfo *) NULL);
3770 assert(exception->signature == MagickSignature);
3771 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3772 exception);
3773 if (spread_image == (Image *) NULL)
3774 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003775 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003776 {
cristy3ed852e2009-09-05 21:47:34 +00003777 spread_image=DestroyImage(spread_image);
3778 return((Image *) NULL);
3779 }
3780 /*
3781 Spread image.
3782 */
3783 status=MagickTrue;
3784 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003785 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003786 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003787 image_view=AcquireCacheView(image);
3788 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003789#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy78778cb2012-01-17 14:48:47 +00003790 #pragma omp parallel for schedule(static,8) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003791#endif
cristybe82ad52011-09-06 01:17:20 +00003792 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003793 {
cristy5c9e6f22010-09-17 17:31:01 +00003794 const int
3795 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003796
cristybe82ad52011-09-06 01:17:20 +00003797 register const Quantum
3798 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003799
cristy4c08aed2011-07-01 19:47:50 +00003800 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003801 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003802
cristy117ff172010-08-15 21:35:32 +00003803 register ssize_t
3804 x;
3805
cristy3ed852e2009-09-05 21:47:34 +00003806 if (status == MagickFalse)
3807 continue;
cristybe82ad52011-09-06 01:17:20 +00003808 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy9f7e7cb2011-03-26 00:49:57 +00003809 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003810 exception);
cristybe82ad52011-09-06 01:17:20 +00003811 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003812 {
3813 status=MagickFalse;
3814 continue;
3815 }
cristybe82ad52011-09-06 01:17:20 +00003816 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003817 {
cristybe82ad52011-09-06 01:17:20 +00003818 PointInfo
3819 point;
3820
cristybe82ad52011-09-06 01:17:20 +00003821 point.x=GetPseudoRandomValue(random_info[id]);
3822 point.y=GetPseudoRandomValue(random_info[id]);
cristy5c4e2582011-09-11 19:21:03 +00003823 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3824 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
cristyed231572011-07-14 02:18:59 +00003825 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003826 }
cristy9f7e7cb2011-03-26 00:49:57 +00003827 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003828 status=MagickFalse;
3829 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3830 {
3831 MagickBooleanType
3832 proceed;
3833
cristyb557a152011-02-22 12:14:30 +00003834#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003835 #pragma omp critical (MagickCore_SpreadImage)
3836#endif
3837 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3838 if (proceed == MagickFalse)
3839 status=MagickFalse;
3840 }
3841 }
cristy9f7e7cb2011-03-26 00:49:57 +00003842 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003843 image_view=DestroyCacheView(image_view);
3844 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003845 return(spread_image);
3846}
3847
3848/*
3849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3850% %
3851% %
3852% %
3853% U n s h a r p M a s k I m a g e %
3854% %
3855% %
3856% %
3857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3858%
3859% UnsharpMaskImage() sharpens one or more image channels. We convolve the
3860% image with a Gaussian operator of the given radius and standard deviation
3861% (sigma). For reasonable results, radius should be larger than sigma. Use a
3862% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3863%
3864% The format of the UnsharpMaskImage method is:
3865%
3866% Image *UnsharpMaskImage(const Image *image,const double radius,
3867% const double sigma,const double amount,const double threshold,
3868% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003869%
3870% A description of each parameter follows:
3871%
3872% o image: the image.
3873%
cristy3ed852e2009-09-05 21:47:34 +00003874% o radius: the radius of the Gaussian, in pixels, not counting the center
3875% pixel.
3876%
3877% o sigma: the standard deviation of the Gaussian, in pixels.
3878%
3879% o amount: the percentage of the difference between the original and the
3880% blur image that is added back into the original.
3881%
3882% o threshold: the threshold in pixels needed to apply the diffence amount.
3883%
3884% o exception: return any errors or warnings in this structure.
3885%
3886*/
cristy31bb6272011-11-20 23:57:25 +00003887MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3888 const double sigma,const double amount,const double threshold,
3889 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003890{
3891#define SharpenImageTag "Sharpen/Image"
3892
cristyc4c8d132010-01-07 01:58:38 +00003893 CacheView
3894 *image_view,
3895 *unsharp_view;
3896
cristy3ed852e2009-09-05 21:47:34 +00003897 Image
3898 *unsharp_image;
3899
cristy3ed852e2009-09-05 21:47:34 +00003900 MagickBooleanType
3901 status;
3902
cristybb503372010-05-27 20:51:26 +00003903 MagickOffsetType
3904 progress;
3905
cristy3ed852e2009-09-05 21:47:34 +00003906 MagickRealType
3907 quantum_threshold;
3908
cristybb503372010-05-27 20:51:26 +00003909 ssize_t
3910 y;
3911
cristy3ed852e2009-09-05 21:47:34 +00003912 assert(image != (const Image *) NULL);
3913 assert(image->signature == MagickSignature);
3914 if (image->debug != MagickFalse)
3915 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3916 assert(exception != (ExceptionInfo *) NULL);
cristy05c0c9a2011-09-05 23:16:13 +00003917 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00003918 if (unsharp_image == (Image *) NULL)
3919 return((Image *) NULL);
3920 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3921 /*
3922 Unsharp-mask image.
3923 */
3924 status=MagickTrue;
3925 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003926 image_view=AcquireCacheView(image);
3927 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00003928#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003929 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003930#endif
cristybb503372010-05-27 20:51:26 +00003931 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003932 {
cristy4c08aed2011-07-01 19:47:50 +00003933 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003934 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003935
cristy4c08aed2011-07-01 19:47:50 +00003936 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003937 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003938
cristy117ff172010-08-15 21:35:32 +00003939 register ssize_t
3940 x;
3941
cristy3ed852e2009-09-05 21:47:34 +00003942 if (status == MagickFalse)
3943 continue;
3944 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy8b49f382012-01-17 03:11:03 +00003945 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003946 exception);
cristy4c08aed2011-07-01 19:47:50 +00003947 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003948 {
3949 status=MagickFalse;
3950 continue;
3951 }
cristybb503372010-05-27 20:51:26 +00003952 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003953 {
cristy7f3a0d12011-09-05 23:27:59 +00003954 register ssize_t
3955 i;
3956
3957 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3958 {
3959 MagickRealType
3960 pixel;
3961
3962 PixelChannel
3963 channel;
3964
3965 PixelTrait
3966 traits,
3967 unsharp_traits;
3968
cristye2a912b2011-12-05 20:02:07 +00003969 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003970 traits=GetPixelChannelMapTraits(image,channel);
cristy7f3a0d12011-09-05 23:27:59 +00003971 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3972 if ((traits == UndefinedPixelTrait) ||
3973 (unsharp_traits == UndefinedPixelTrait))
3974 continue;
3975 if ((unsharp_traits & CopyPixelTrait) != 0)
3976 {
cristy0beccfa2011-09-25 20:47:53 +00003977 SetPixelChannel(unsharp_image,channel,p[i],q);
cristy7f3a0d12011-09-05 23:27:59 +00003978 continue;
3979 }
cristy0beccfa2011-09-25 20:47:53 +00003980 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
cristy7f3a0d12011-09-05 23:27:59 +00003981 if (fabs(2.0*pixel) < quantum_threshold)
3982 pixel=(MagickRealType) p[i];
3983 else
3984 pixel=(MagickRealType) p[i]+amount*pixel;
cristy0beccfa2011-09-25 20:47:53 +00003985 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
cristy7f3a0d12011-09-05 23:27:59 +00003986 }
cristyed231572011-07-14 02:18:59 +00003987 p+=GetPixelChannels(image);
3988 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00003989 }
3990 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3991 status=MagickFalse;
3992 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3993 {
3994 MagickBooleanType
3995 proceed;
3996
cristyb5d5f722009-11-04 03:03:49 +00003997#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003998 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00003999#endif
4000 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4001 if (proceed == MagickFalse)
4002 status=MagickFalse;
4003 }
4004 }
4005 unsharp_image->type=image->type;
4006 unsharp_view=DestroyCacheView(unsharp_view);
4007 image_view=DestroyCacheView(image_view);
4008 if (status == MagickFalse)
4009 unsharp_image=DestroyImage(unsharp_image);
4010 return(unsharp_image);
4011}