blob: 865105e2451c4dbdb7c831a8c2a54f915481b6c7 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% EEEEE FFFFF FFFFF EEEEE CCCC TTTTT %
7% E F F E C T %
8% EEE FFF FFF EEE C T %
9% E F F E C T %
10% EEEEE F F EEEEE CCCC T %
11% %
12% %
13% MagickCore Image Effects Methods %
14% %
15% Software Design %
16% John Cristy %
17% October 1996 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/accelerate.h"
45#include "MagickCore/blob.h"
46#include "MagickCore/cache-view.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colorspace.h"
50#include "MagickCore/constitute.h"
51#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000052#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/draw.h"
54#include "MagickCore/enhance.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/exception-private.h"
57#include "MagickCore/effect.h"
58#include "MagickCore/fx.h"
59#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000060#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000061#include "MagickCore/geometry.h"
62#include "MagickCore/image-private.h"
63#include "MagickCore/list.h"
64#include "MagickCore/log.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/monitor.h"
67#include "MagickCore/monitor-private.h"
68#include "MagickCore/montage.h"
69#include "MagickCore/morphology.h"
70#include "MagickCore/paint.h"
71#include "MagickCore/pixel-accessor.h"
72#include "MagickCore/property.h"
73#include "MagickCore/quantize.h"
74#include "MagickCore/quantum.h"
75#include "MagickCore/quantum-private.h"
76#include "MagickCore/random_.h"
77#include "MagickCore/random-private.h"
78#include "MagickCore/resample.h"
79#include "MagickCore/resample-private.h"
80#include "MagickCore/resize.h"
81#include "MagickCore/resource_.h"
82#include "MagickCore/segment.h"
cristy31bbf2f2011-11-17 13:19:37 +000083#include "MagickCore/shear.h"
cristy4c08aed2011-07-01 19:47:50 +000084#include "MagickCore/signature-private.h"
cristy99bd5232011-12-07 14:38:20 +000085#include "MagickCore/statistic.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/string_.h"
87#include "MagickCore/thread-private.h"
88#include "MagickCore/transform.h"
89#include "MagickCore/threshold.h"
cristy3ed852e2009-09-05 21:47:34 +000090
91/*
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93% %
94% %
95% %
96% A d a p t i v e B l u r I m a g e %
97% %
98% %
99% %
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101%
102% AdaptiveBlurImage() adaptively blurs the image by blurring less
103% intensely near image edges and more intensely far from edges. We blur the
104% image with a Gaussian operator of the given radius and standard deviation
105% (sigma). For reasonable results, radius should be larger than sigma. Use a
106% radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
107%
108% The format of the AdaptiveBlurImage method is:
109%
110% Image *AdaptiveBlurImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000111% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000112%
113% A description of each parameter follows:
114%
115% o image: the image.
116%
cristy3ed852e2009-09-05 21:47:34 +0000117% o radius: the radius of the Gaussian, in pixels, not counting the center
118% pixel.
119%
120% o sigma: the standard deviation of the Laplacian, in pixels.
121%
cristy4c11c2b2011-09-05 20:17:07 +0000122% o bias: the bias.
123%
cristy3ed852e2009-09-05 21:47:34 +0000124% o exception: return any errors or warnings in this structure.
125%
126*/
127
cristyf89cb1d2011-07-07 01:24:37 +0000128MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
cristy051718b2011-08-28 22:49:25 +0000129 const char *levels,ExceptionInfo *exception)
cristyf89cb1d2011-07-07 01:24:37 +0000130{
131 double
132 black_point,
133 gamma,
134 white_point;
135
136 GeometryInfo
137 geometry_info;
138
139 MagickBooleanType
140 status;
141
142 MagickStatusType
143 flags;
144
145 /*
146 Parse levels.
147 */
148 if (levels == (char *) NULL)
149 return(MagickFalse);
150 flags=ParseGeometry(levels,&geometry_info);
151 black_point=geometry_info.rho;
152 white_point=(double) QuantumRange;
153 if ((flags & SigmaValue) != 0)
154 white_point=geometry_info.sigma;
155 gamma=1.0;
156 if ((flags & XiValue) != 0)
157 gamma=geometry_info.xi;
158 if ((flags & PercentValue) != 0)
159 {
160 black_point*=(double) image->columns*image->rows/100.0;
161 white_point*=(double) image->columns*image->rows/100.0;
162 }
163 if ((flags & SigmaValue) == 0)
164 white_point=(double) QuantumRange-black_point;
165 if ((flags & AspectValue ) == 0)
cristy7c0a0a42011-08-23 17:57:25 +0000166 status=LevelImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000167 else
cristy7c0a0a42011-08-23 17:57:25 +0000168 status=LevelizeImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000169 return(status);
170}
171
cristy4282c702011-11-21 00:01:06 +0000172MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
173 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000174{
175#define AdaptiveBlurImageTag "Convolve/Image"
176#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
177
cristyc4c8d132010-01-07 01:58:38 +0000178 CacheView
179 *blur_view,
180 *edge_view,
181 *image_view;
182
cristy3ed852e2009-09-05 21:47:34 +0000183 double
cristy47e00502009-12-17 19:19:57 +0000184 **kernel,
185 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000186
187 Image
188 *blur_image,
189 *edge_image,
190 *gaussian_image;
191
cristy3ed852e2009-09-05 21:47:34 +0000192 MagickBooleanType
193 status;
194
cristybb503372010-05-27 20:51:26 +0000195 MagickOffsetType
196 progress;
197
cristybb503372010-05-27 20:51:26 +0000198 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000199 i;
cristy3ed852e2009-09-05 21:47:34 +0000200
cristybb503372010-05-27 20:51:26 +0000201 size_t
cristy3ed852e2009-09-05 21:47:34 +0000202 width;
203
cristybb503372010-05-27 20:51:26 +0000204 ssize_t
205 j,
206 k,
207 u,
208 v,
209 y;
210
cristy3ed852e2009-09-05 21:47:34 +0000211 assert(image != (const Image *) NULL);
212 assert(image->signature == MagickSignature);
213 if (image->debug != MagickFalse)
214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
215 assert(exception != (ExceptionInfo *) NULL);
216 assert(exception->signature == MagickSignature);
217 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
218 if (blur_image == (Image *) NULL)
219 return((Image *) NULL);
220 if (fabs(sigma) <= MagickEpsilon)
221 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000222 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000223 {
cristy3ed852e2009-09-05 21:47:34 +0000224 blur_image=DestroyImage(blur_image);
225 return((Image *) NULL);
226 }
227 /*
228 Edge detect the image brighness channel, level, blur, and level again.
229 */
cristy8ae632d2011-09-05 17:29:53 +0000230 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000231 if (edge_image == (Image *) NULL)
232 {
233 blur_image=DestroyImage(blur_image);
234 return((Image *) NULL);
235 }
cristy051718b2011-08-28 22:49:25 +0000236 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristyd89705a2012-01-20 02:52:24 +0000237 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000238 if (gaussian_image != (Image *) NULL)
239 {
240 edge_image=DestroyImage(edge_image);
241 edge_image=gaussian_image;
242 }
cristy051718b2011-08-28 22:49:25 +0000243 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000244 /*
245 Create a set of kernels from maximum (radius,sigma) to minimum.
246 */
247 width=GetOptimalKernelWidth2D(radius,sigma);
cristy219a63a2012-01-17 12:30:33 +0000248 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000249 if (kernel == (double **) NULL)
250 {
251 edge_image=DestroyImage(edge_image);
252 blur_image=DestroyImage(blur_image);
253 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
254 }
255 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000256 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000257 {
cristyf5d0f3e2012-01-17 03:20:33 +0000258 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
cristy3ed852e2009-09-05 21:47:34 +0000259 sizeof(**kernel));
260 if (kernel[i] == (double *) NULL)
261 break;
cristy47e00502009-12-17 19:19:57 +0000262 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000263 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000264 k=0;
265 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000266 {
cristy47e00502009-12-17 19:19:57 +0000267 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000268 {
cristy4205a3c2010-09-12 20:19:59 +0000269 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
270 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000271 normalize+=kernel[i][k];
272 k++;
cristy3ed852e2009-09-05 21:47:34 +0000273 }
274 }
cristy3ed852e2009-09-05 21:47:34 +0000275 if (fabs(normalize) <= MagickEpsilon)
276 normalize=1.0;
277 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000278 for (k=0; k < (j*j); k++)
279 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000280 }
cristybb503372010-05-27 20:51:26 +0000281 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000282 {
283 for (i-=2; i >= 0; i-=2)
cristyf5d0f3e2012-01-17 03:20:33 +0000284 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000285 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000286 edge_image=DestroyImage(edge_image);
287 blur_image=DestroyImage(blur_image);
288 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
289 }
290 /*
291 Adaptively blur image.
292 */
293 status=MagickTrue;
294 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000295 image_view=AcquireCacheView(image);
296 edge_view=AcquireCacheView(edge_image);
297 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000298#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000299 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000300#endif
cristybb503372010-05-27 20:51:26 +0000301 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
cristy4c08aed2011-07-01 19:47:50 +0000303 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000304 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000305
cristy4c08aed2011-07-01 19:47:50 +0000306 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000307 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000308
cristy117ff172010-08-15 21:35:32 +0000309 register ssize_t
310 x;
311
cristy3ed852e2009-09-05 21:47:34 +0000312 if (status == MagickFalse)
313 continue;
314 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
315 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
316 exception);
cristyacd2ed22011-08-30 01:44:23 +0000317 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000318 {
319 status=MagickFalse;
320 continue;
321 }
cristybb503372010-05-27 20:51:26 +0000322 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000323 {
cristy4c11c2b2011-09-05 20:17:07 +0000324 register const Quantum
325 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000326
cristybb503372010-05-27 20:51:26 +0000327 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000328 i;
cristy3ed852e2009-09-05 21:47:34 +0000329
cristy4c11c2b2011-09-05 20:17:07 +0000330 ssize_t
331 center,
332 j;
333
334 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000335 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000336 if (j < 0)
337 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000338 else
cristy4c11c2b2011-09-05 20:17:07 +0000339 if (j > (ssize_t) width)
340 j=(ssize_t) width;
341 if ((j & 0x01) != 0)
342 j--;
343 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
344 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000345 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000346 break;
cristy4c11c2b2011-09-05 20:17:07 +0000347 center=(ssize_t) GetPixelChannels(image)*(width-j)*
cristy075ff302011-09-07 01:51:24 +0000348 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2L);
cristy4c11c2b2011-09-05 20:17:07 +0000349 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristy4c11c2b2011-09-05 20:17:07 +0000351 MagickRealType
352 alpha,
353 gamma,
354 pixel;
355
356 PixelChannel
357 channel;
358
359 PixelTrait
360 blur_traits,
361 traits;
362
363 register const double
364 *restrict k;
365
366 register const Quantum
367 *restrict pixels;
368
369 register ssize_t
370 u;
371
372 ssize_t
373 v;
374
cristye2a912b2011-12-05 20:02:07 +0000375 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000376 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000377 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
378 if ((traits == UndefinedPixelTrait) ||
379 (blur_traits == UndefinedPixelTrait))
380 continue;
381 if ((blur_traits & CopyPixelTrait) != 0)
382 {
cristy0beccfa2011-09-25 20:47:53 +0000383 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000384 continue;
385 }
386 k=kernel[j];
387 pixels=p;
388 pixel=bias;
389 gamma=0.0;
390 if ((blur_traits & BlendPixelTrait) == 0)
391 {
392 /*
393 No alpha blending.
394 */
395 for (v=0; v < (ssize_t) (width-j); v++)
396 {
397 for (u=0; u < (ssize_t) (width-j); u++)
398 {
399 pixel+=(*k)*pixels[i];
400 gamma+=(*k);
401 k++;
402 pixels+=GetPixelChannels(image);
403 }
404 }
405 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000406 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000407 continue;
408 }
409 /*
410 Alpha blending.
411 */
412 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000413 {
cristy4c11c2b2011-09-05 20:17:07 +0000414 for (u=0; u < (ssize_t) (width-j); u++)
415 {
416 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
417 pixel+=(*k)*alpha*pixels[i];
418 gamma+=(*k)*alpha;
419 k++;
420 pixels+=GetPixelChannels(image);
421 }
cristy3ed852e2009-09-05 21:47:34 +0000422 }
cristy4c11c2b2011-09-05 20:17:07 +0000423 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000424 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000425 }
cristyed231572011-07-14 02:18:59 +0000426 q+=GetPixelChannels(blur_image);
427 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000428 }
429 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
430 status=MagickFalse;
431 if (image->progress_monitor != (MagickProgressMonitor) NULL)
432 {
433 MagickBooleanType
434 proceed;
435
cristyb5d5f722009-11-04 03:03:49 +0000436#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8ae632d2011-09-05 17:29:53 +0000437 #pragma omp critical (MagickCore_AdaptiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +0000438#endif
439 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
440 image->rows);
441 if (proceed == MagickFalse)
442 status=MagickFalse;
443 }
444 }
445 blur_image->type=image->type;
446 blur_view=DestroyCacheView(blur_view);
447 edge_view=DestroyCacheView(edge_view);
448 image_view=DestroyCacheView(image_view);
449 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000450 for (i=0; i < (ssize_t) width; i+=2)
cristyf5d0f3e2012-01-17 03:20:33 +0000451 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000452 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000453 if (status == MagickFalse)
454 blur_image=DestroyImage(blur_image);
455 return(blur_image);
456}
457
458/*
459%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460% %
461% %
462% %
463% A d a p t i v e S h a r p e n I m a g e %
464% %
465% %
466% %
467%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468%
469% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
470% intensely near image edges and less intensely far from edges. We sharpen the
471% image with a Gaussian operator of the given radius and standard deviation
472% (sigma). For reasonable results, radius should be larger than sigma. Use a
473% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
474%
475% The format of the AdaptiveSharpenImage method is:
476%
477% Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000478% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000479%
480% A description of each parameter follows:
481%
482% o image: the image.
483%
cristy3ed852e2009-09-05 21:47:34 +0000484% o radius: the radius of the Gaussian, in pixels, not counting the center
485% pixel.
486%
487% o sigma: the standard deviation of the Laplacian, in pixels.
488%
cristy4c11c2b2011-09-05 20:17:07 +0000489% o bias: the bias.
490%
cristy3ed852e2009-09-05 21:47:34 +0000491% o exception: return any errors or warnings in this structure.
492%
493*/
cristy3ed852e2009-09-05 21:47:34 +0000494MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000495 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000496{
cristy3ed852e2009-09-05 21:47:34 +0000497#define AdaptiveSharpenImageTag "Convolve/Image"
498#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
499
cristyc4c8d132010-01-07 01:58:38 +0000500 CacheView
501 *sharp_view,
502 *edge_view,
503 *image_view;
504
cristy3ed852e2009-09-05 21:47:34 +0000505 double
cristy47e00502009-12-17 19:19:57 +0000506 **kernel,
507 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000508
509 Image
510 *sharp_image,
511 *edge_image,
512 *gaussian_image;
513
cristy3ed852e2009-09-05 21:47:34 +0000514 MagickBooleanType
515 status;
516
cristybb503372010-05-27 20:51:26 +0000517 MagickOffsetType
518 progress;
519
cristybb503372010-05-27 20:51:26 +0000520 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000521 i;
cristy3ed852e2009-09-05 21:47:34 +0000522
cristybb503372010-05-27 20:51:26 +0000523 size_t
cristy3ed852e2009-09-05 21:47:34 +0000524 width;
525
cristybb503372010-05-27 20:51:26 +0000526 ssize_t
527 j,
528 k,
529 u,
530 v,
531 y;
532
cristy3ed852e2009-09-05 21:47:34 +0000533 assert(image != (const Image *) NULL);
534 assert(image->signature == MagickSignature);
535 if (image->debug != MagickFalse)
536 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
537 assert(exception != (ExceptionInfo *) NULL);
538 assert(exception->signature == MagickSignature);
539 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
540 if (sharp_image == (Image *) NULL)
541 return((Image *) NULL);
542 if (fabs(sigma) <= MagickEpsilon)
543 return(sharp_image);
cristy574cc262011-08-05 01:23:58 +0000544 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
cristy3ed852e2009-09-05 21:47:34 +0000546 sharp_image=DestroyImage(sharp_image);
547 return((Image *) NULL);
548 }
549 /*
550 Edge detect the image brighness channel, level, sharp, and level again.
551 */
cristy8ae632d2011-09-05 17:29:53 +0000552 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000553 if (edge_image == (Image *) NULL)
554 {
555 sharp_image=DestroyImage(sharp_image);
556 return((Image *) NULL);
557 }
cristy051718b2011-08-28 22:49:25 +0000558 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristyd89705a2012-01-20 02:52:24 +0000559 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000560 if (gaussian_image != (Image *) NULL)
561 {
562 edge_image=DestroyImage(edge_image);
563 edge_image=gaussian_image;
564 }
cristy051718b2011-08-28 22:49:25 +0000565 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000566 /*
567 Create a set of kernels from maximum (radius,sigma) to minimum.
568 */
569 width=GetOptimalKernelWidth2D(radius,sigma);
cristy219a63a2012-01-17 12:30:33 +0000570 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000571 if (kernel == (double **) NULL)
572 {
573 edge_image=DestroyImage(edge_image);
574 sharp_image=DestroyImage(sharp_image);
575 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
576 }
577 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000578 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000579 {
cristy59d1f6d2012-01-17 03:17:40 +0000580 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
cristy3ed852e2009-09-05 21:47:34 +0000581 sizeof(**kernel));
582 if (kernel[i] == (double *) NULL)
583 break;
cristy47e00502009-12-17 19:19:57 +0000584 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000585 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000586 k=0;
587 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000588 {
cristy47e00502009-12-17 19:19:57 +0000589 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000590 {
cristy4205a3c2010-09-12 20:19:59 +0000591 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
592 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000593 normalize+=kernel[i][k];
594 k++;
cristy3ed852e2009-09-05 21:47:34 +0000595 }
596 }
cristy3ed852e2009-09-05 21:47:34 +0000597 if (fabs(normalize) <= MagickEpsilon)
598 normalize=1.0;
599 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000600 for (k=0; k < (j*j); k++)
601 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000602 }
cristybb503372010-05-27 20:51:26 +0000603 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000604 {
605 for (i-=2; i >= 0; i-=2)
cristy59d1f6d2012-01-17 03:17:40 +0000606 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000607 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000608 edge_image=DestroyImage(edge_image);
609 sharp_image=DestroyImage(sharp_image);
610 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
611 }
612 /*
613 Adaptively sharpen image.
614 */
615 status=MagickTrue;
616 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000617 image_view=AcquireCacheView(image);
618 edge_view=AcquireCacheView(edge_image);
619 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000620#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000621 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000622#endif
cristybb503372010-05-27 20:51:26 +0000623 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000624 {
cristy4c08aed2011-07-01 19:47:50 +0000625 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000626 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000627
cristy4c08aed2011-07-01 19:47:50 +0000628 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000629 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000630
cristy117ff172010-08-15 21:35:32 +0000631 register ssize_t
632 x;
633
cristy3ed852e2009-09-05 21:47:34 +0000634 if (status == MagickFalse)
635 continue;
636 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
637 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
638 exception);
cristy4c08aed2011-07-01 19:47:50 +0000639 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000640 {
641 status=MagickFalse;
642 continue;
643 }
cristybb503372010-05-27 20:51:26 +0000644 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000645 {
cristy4c11c2b2011-09-05 20:17:07 +0000646 register const Quantum
647 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000648
cristybb503372010-05-27 20:51:26 +0000649 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000650 i;
cristy3ed852e2009-09-05 21:47:34 +0000651
cristy4c11c2b2011-09-05 20:17:07 +0000652 ssize_t
653 center,
654 j;
655
656 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000657 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000658 if (j < 0)
659 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000660 else
cristy4c11c2b2011-09-05 20:17:07 +0000661 if (j > (ssize_t) width)
662 j=(ssize_t) width;
663 if ((j & 0x01) != 0)
664 j--;
665 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
666 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000667 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000668 break;
cristy4c11c2b2011-09-05 20:17:07 +0000669 center=(ssize_t) GetPixelChannels(image)*(width-j)*
670 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2);
671 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000672 {
cristy4c11c2b2011-09-05 20:17:07 +0000673 MagickRealType
674 alpha,
675 gamma,
676 pixel;
677
678 PixelChannel
679 channel;
680
681 PixelTrait
682 sharp_traits,
683 traits;
684
685 register const double
686 *restrict k;
687
688 register const Quantum
689 *restrict pixels;
690
691 register ssize_t
692 u;
693
694 ssize_t
695 v;
696
cristye2a912b2011-12-05 20:02:07 +0000697 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000698 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000699 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
700 if ((traits == UndefinedPixelTrait) ||
701 (sharp_traits == UndefinedPixelTrait))
702 continue;
703 if ((sharp_traits & CopyPixelTrait) != 0)
704 {
cristy0beccfa2011-09-25 20:47:53 +0000705 SetPixelChannel(sharp_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000706 continue;
707 }
708 k=kernel[j];
709 pixels=p;
710 pixel=bias;
711 gamma=0.0;
712 if ((sharp_traits & BlendPixelTrait) == 0)
713 {
714 /*
715 No alpha blending.
716 */
717 for (v=0; v < (ssize_t) (width-j); v++)
718 {
719 for (u=0; u < (ssize_t) (width-j); u++)
720 {
721 pixel+=(*k)*pixels[i];
722 gamma+=(*k);
723 k++;
724 pixels+=GetPixelChannels(image);
725 }
726 }
727 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000728 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000729 continue;
730 }
731 /*
732 Alpha blending.
733 */
734 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000735 {
cristy4c11c2b2011-09-05 20:17:07 +0000736 for (u=0; u < (ssize_t) (width-j); u++)
737 {
738 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
739 pixel+=(*k)*alpha*pixels[i];
740 gamma+=(*k)*alpha;
741 k++;
742 pixels+=GetPixelChannels(image);
743 }
cristy3ed852e2009-09-05 21:47:34 +0000744 }
cristy4c11c2b2011-09-05 20:17:07 +0000745 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000746 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000747 }
cristyed231572011-07-14 02:18:59 +0000748 q+=GetPixelChannels(sharp_image);
749 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000750 }
751 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
752 status=MagickFalse;
753 if (image->progress_monitor != (MagickProgressMonitor) NULL)
754 {
755 MagickBooleanType
756 proceed;
757
cristyb5d5f722009-11-04 03:03:49 +0000758#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +0000759 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000760#endif
761 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
762 image->rows);
763 if (proceed == MagickFalse)
764 status=MagickFalse;
765 }
766 }
767 sharp_image->type=image->type;
768 sharp_view=DestroyCacheView(sharp_view);
769 edge_view=DestroyCacheView(edge_view);
770 image_view=DestroyCacheView(image_view);
771 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000772 for (i=0; i < (ssize_t) width; i+=2)
cristy59d1f6d2012-01-17 03:17:40 +0000773 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000774 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000775 if (status == MagickFalse)
776 sharp_image=DestroyImage(sharp_image);
777 return(sharp_image);
778}
779
780/*
781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782% %
783% %
784% %
785% B l u r I m a g e %
786% %
787% %
788% %
789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790%
791% BlurImage() blurs an image. We convolve the image with a Gaussian operator
792% of the given radius and standard deviation (sigma). For reasonable results,
793% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
794% selects a suitable radius for you.
795%
796% BlurImage() differs from GaussianBlurImage() in that it uses a separable
797% kernel which is faster but mathematically equivalent to the non-separable
798% kernel.
799%
800% The format of the BlurImage method is:
801%
802% Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000803% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000804%
805% A description of each parameter follows:
806%
807% o image: the image.
808%
cristy3ed852e2009-09-05 21:47:34 +0000809% o radius: the radius of the Gaussian, in pixels, not counting the center
810% pixel.
811%
812% o sigma: the standard deviation of the Gaussian, in pixels.
813%
cristy05c0c9a2011-09-05 23:16:13 +0000814% o bias: the bias.
815%
cristy3ed852e2009-09-05 21:47:34 +0000816% o exception: return any errors or warnings in this structure.
817%
818*/
819
cristybb503372010-05-27 20:51:26 +0000820static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000821{
cristy3ed852e2009-09-05 21:47:34 +0000822 double
cristy47e00502009-12-17 19:19:57 +0000823 *kernel,
824 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000825
cristy117ff172010-08-15 21:35:32 +0000826 register ssize_t
827 i;
828
cristybb503372010-05-27 20:51:26 +0000829 ssize_t
cristy47e00502009-12-17 19:19:57 +0000830 j,
831 k;
cristy3ed852e2009-09-05 21:47:34 +0000832
cristy3ed852e2009-09-05 21:47:34 +0000833 /*
834 Generate a 1-D convolution kernel.
835 */
836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +0000837 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000838 if (kernel == (double *) NULL)
839 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000840 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000841 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000842 i=0;
843 for (k=(-j); k <= j; k++)
844 {
cristy4205a3c2010-09-12 20:19:59 +0000845 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
846 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000847 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000848 i++;
849 }
cristybb503372010-05-27 20:51:26 +0000850 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000851 kernel[i]/=normalize;
852 return(kernel);
853}
854
cristyf4ad9df2011-07-08 16:49:03 +0000855MagickExport Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000856 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000857{
858#define BlurImageTag "Blur/Image"
859
cristyc4c8d132010-01-07 01:58:38 +0000860 CacheView
861 *blur_view,
862 *image_view;
863
cristy3ed852e2009-09-05 21:47:34 +0000864 double
865 *kernel;
866
867 Image
868 *blur_image;
869
cristy3ed852e2009-09-05 21:47:34 +0000870 MagickBooleanType
871 status;
872
cristybb503372010-05-27 20:51:26 +0000873 MagickOffsetType
874 progress;
875
cristybb503372010-05-27 20:51:26 +0000876 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000877 i;
878
cristybb503372010-05-27 20:51:26 +0000879 size_t
cristy3ed852e2009-09-05 21:47:34 +0000880 width;
881
cristybb503372010-05-27 20:51:26 +0000882 ssize_t
cristyb41a1172011-09-06 00:55:14 +0000883 center,
cristybb503372010-05-27 20:51:26 +0000884 x,
885 y;
886
cristy3ed852e2009-09-05 21:47:34 +0000887 /*
888 Initialize blur image attributes.
889 */
890 assert(image != (Image *) NULL);
891 assert(image->signature == MagickSignature);
892 if (image->debug != MagickFalse)
893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
894 assert(exception != (ExceptionInfo *) NULL);
895 assert(exception->signature == MagickSignature);
cristyd25c77e2011-09-06 00:10:24 +0000896 blur_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000897 if (blur_image == (Image *) NULL)
898 return((Image *) NULL);
899 if (fabs(sigma) <= MagickEpsilon)
900 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000901 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000902 {
cristy3ed852e2009-09-05 21:47:34 +0000903 blur_image=DestroyImage(blur_image);
904 return((Image *) NULL);
905 }
906 width=GetOptimalKernelWidth1D(radius,sigma);
907 kernel=GetBlurKernel(width,sigma);
908 if (kernel == (double *) NULL)
909 {
910 blur_image=DestroyImage(blur_image);
911 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
912 }
913 if (image->debug != MagickFalse)
914 {
915 char
916 format[MaxTextExtent],
917 *message;
918
919 register const double
920 *k;
921
922 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000923 " BlurImage with %.20g kernel:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000924 message=AcquireString("");
925 k=kernel;
cristybb503372010-05-27 20:51:26 +0000926 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000927 {
928 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000929 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000930 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000931 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000932 (void) ConcatenateString(&message,format);
933 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
934 }
935 message=DestroyString(message);
936 }
937 /*
938 Blur rows.
939 */
940 status=MagickTrue;
941 progress=0;
cristyb41a1172011-09-06 00:55:14 +0000942 center=(ssize_t) GetPixelChannels(image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +0000943 image_view=AcquireCacheView(image);
944 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000945#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000946 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000947#endif
cristyb41a1172011-09-06 00:55:14 +0000948 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000949 {
cristy4c08aed2011-07-01 19:47:50 +0000950 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000951 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000952
cristy4c08aed2011-07-01 19:47:50 +0000953 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000954 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000955
cristy117ff172010-08-15 21:35:32 +0000956 register ssize_t
957 x;
958
cristy3ed852e2009-09-05 21:47:34 +0000959 if (status == MagickFalse)
960 continue;
cristy117ff172010-08-15 21:35:32 +0000961 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
962 image->columns+width,1,exception);
cristy8b49f382012-01-17 03:11:03 +0000963 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000964 exception);
cristy4c08aed2011-07-01 19:47:50 +0000965 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000966 {
967 status=MagickFalse;
968 continue;
969 }
cristyb41a1172011-09-06 00:55:14 +0000970 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000971 {
cristybb503372010-05-27 20:51:26 +0000972 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000973 i;
974
cristyb41a1172011-09-06 00:55:14 +0000975 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
976 {
977 MagickRealType
978 alpha,
979 gamma,
980 pixel;
cristyd25c77e2011-09-06 00:10:24 +0000981
cristyb41a1172011-09-06 00:55:14 +0000982 PixelChannel
983 channel;
984
985 PixelTrait
986 blur_traits,
987 traits;
988
989 register const double
990 *restrict k;
991
992 register const Quantum
993 *restrict pixels;
994
995 register ssize_t
996 u;
997
cristye2a912b2011-12-05 20:02:07 +0000998 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000999 traits=GetPixelChannelMapTraits(image,channel);
cristyb41a1172011-09-06 00:55:14 +00001000 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1001 if ((traits == UndefinedPixelTrait) ||
1002 (blur_traits == UndefinedPixelTrait))
1003 continue;
1004 if ((blur_traits & CopyPixelTrait) != 0)
cristyd25c77e2011-09-06 00:10:24 +00001005 {
cristy0beccfa2011-09-25 20:47:53 +00001006 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001007 continue;
cristyd25c77e2011-09-06 00:10:24 +00001008 }
cristyb41a1172011-09-06 00:55:14 +00001009 k=kernel;
1010 pixels=p;
1011 pixel=0.0;
1012 if ((blur_traits & BlendPixelTrait) == 0)
1013 {
1014 /*
1015 No alpha blending.
1016 */
1017 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001018 {
cristyb41a1172011-09-06 00:55:14 +00001019 pixel+=(*k)*pixels[i];
1020 k++;
1021 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001022 }
cristy0beccfa2011-09-25 20:47:53 +00001023 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001024 continue;
1025 }
1026 /*
1027 Alpha blending.
1028 */
1029 gamma=0.0;
1030 for (u=0; u < (ssize_t) width; u++)
1031 {
1032 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1033 pixel+=(*k)*alpha*pixels[i];
1034 gamma+=(*k)*alpha;
1035 k++;
1036 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001037 }
cristyb41a1172011-09-06 00:55:14 +00001038 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001039 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001040 }
cristyed231572011-07-14 02:18:59 +00001041 p+=GetPixelChannels(image);
1042 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001043 }
1044 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1045 status=MagickFalse;
1046 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1047 {
1048 MagickBooleanType
1049 proceed;
1050
cristyb5d5f722009-11-04 03:03:49 +00001051#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001052 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001053#endif
1054 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1055 blur_image->columns);
1056 if (proceed == MagickFalse)
1057 status=MagickFalse;
1058 }
1059 }
1060 blur_view=DestroyCacheView(blur_view);
1061 image_view=DestroyCacheView(image_view);
1062 /*
1063 Blur columns.
1064 */
1065 image_view=AcquireCacheView(blur_image);
1066 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001067#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001068 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001069#endif
cristyb6dc4b72011-12-10 23:29:53 +00001070 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001071 {
cristy4c08aed2011-07-01 19:47:50 +00001072 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001073 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001074
cristy4c08aed2011-07-01 19:47:50 +00001075 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001076 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001077
cristy117ff172010-08-15 21:35:32 +00001078 register ssize_t
1079 y;
1080
cristy3ed852e2009-09-05 21:47:34 +00001081 if (status == MagickFalse)
1082 continue;
cristy117ff172010-08-15 21:35:32 +00001083 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
cristyb6dc4b72011-12-10 23:29:53 +00001084 blur_image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001085 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001086 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001087 {
1088 status=MagickFalse;
1089 continue;
1090 }
cristyb6dc4b72011-12-10 23:29:53 +00001091 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001092 {
cristybb503372010-05-27 20:51:26 +00001093 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001094 i;
1095
cristyb6dc4b72011-12-10 23:29:53 +00001096 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
cristyb41a1172011-09-06 00:55:14 +00001097 {
1098 MagickRealType
1099 alpha,
1100 gamma,
1101 pixel;
cristyd25c77e2011-09-06 00:10:24 +00001102
cristyb41a1172011-09-06 00:55:14 +00001103 PixelChannel
1104 channel;
1105
1106 PixelTrait
1107 blur_traits,
1108 traits;
1109
1110 register const double
1111 *restrict k;
1112
1113 register const Quantum
1114 *restrict pixels;
1115
1116 register ssize_t
1117 u;
1118
cristye2a912b2011-12-05 20:02:07 +00001119 channel=GetPixelChannelMapChannel(blur_image,i);
cristyabace412011-12-11 15:56:53 +00001120 traits=GetPixelChannelMapTraits(blur_image,channel);
cristyb41a1172011-09-06 00:55:14 +00001121 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1122 if ((traits == UndefinedPixelTrait) ||
1123 (blur_traits == UndefinedPixelTrait))
1124 continue;
1125 if ((blur_traits & CopyPixelTrait) != 0)
cristyd25c77e2011-09-06 00:10:24 +00001126 {
cristy0beccfa2011-09-25 20:47:53 +00001127 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001128 continue;
cristyd25c77e2011-09-06 00:10:24 +00001129 }
cristyb41a1172011-09-06 00:55:14 +00001130 k=kernel;
1131 pixels=p;
1132 pixel=0.0;
1133 if ((blur_traits & BlendPixelTrait) == 0)
1134 {
1135 /*
1136 No alpha blending.
1137 */
1138 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001139 {
cristyb41a1172011-09-06 00:55:14 +00001140 pixel+=(*k)*pixels[i];
1141 k++;
1142 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001143 }
cristy0beccfa2011-09-25 20:47:53 +00001144 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001145 continue;
1146 }
1147 /*
1148 Alpha blending.
1149 */
1150 gamma=0.0;
1151 for (u=0; u < (ssize_t) width; u++)
1152 {
cristyb6dc4b72011-12-10 23:29:53 +00001153 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1154 pixels));
cristyb41a1172011-09-06 00:55:14 +00001155 pixel+=(*k)*alpha*pixels[i];
1156 gamma+=(*k)*alpha;
1157 k++;
1158 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001159 }
cristyb41a1172011-09-06 00:55:14 +00001160 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001161 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001162 }
cristyd25c77e2011-09-06 00:10:24 +00001163 p+=GetPixelChannels(blur_image);
cristyed231572011-07-14 02:18:59 +00001164 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001165 }
1166 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1167 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001168 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001169 {
1170 MagickBooleanType
1171 proceed;
1172
cristyb5d5f722009-11-04 03:03:49 +00001173#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001174 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001175#endif
cristy4c08aed2011-07-01 19:47:50 +00001176 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1177 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001178 if (proceed == MagickFalse)
1179 status=MagickFalse;
1180 }
1181 }
1182 blur_view=DestroyCacheView(blur_view);
1183 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00001184 kernel=(double *) RelinquishAlignedMemory(kernel);
1185 blur_image->type=image->type;
cristy3ed852e2009-09-05 21:47:34 +00001186 if (status == MagickFalse)
1187 blur_image=DestroyImage(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001188 return(blur_image);
1189}
1190
1191/*
1192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193% %
1194% %
1195% %
cristyfccdab92009-11-30 16:43:57 +00001196% C o n v o l v e I m a g e %
1197% %
1198% %
1199% %
1200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201%
1202% ConvolveImage() applies a custom convolution kernel to the image.
1203%
1204% The format of the ConvolveImage method is:
1205%
cristy5e6be1e2011-07-16 01:23:39 +00001206% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1207% ExceptionInfo *exception)
1208%
cristyfccdab92009-11-30 16:43:57 +00001209% A description of each parameter follows:
1210%
1211% o image: the image.
1212%
cristy5e6be1e2011-07-16 01:23:39 +00001213% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001214%
1215% o exception: return any errors or warnings in this structure.
1216%
1217*/
cristy5e6be1e2011-07-16 01:23:39 +00001218MagickExport Image *ConvolveImage(const Image *image,
1219 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001220{
cristyfccdab92009-11-30 16:43:57 +00001221#define ConvolveImageTag "Convolve/Image"
1222
cristyc4c8d132010-01-07 01:58:38 +00001223 CacheView
1224 *convolve_view,
cristy105ba3c2011-07-18 02:28:38 +00001225 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00001226
cristyfccdab92009-11-30 16:43:57 +00001227 Image
1228 *convolve_image;
1229
cristyfccdab92009-11-30 16:43:57 +00001230 MagickBooleanType
1231 status;
1232
cristybb503372010-05-27 20:51:26 +00001233 MagickOffsetType
1234 progress;
1235
cristybb503372010-05-27 20:51:26 +00001236 ssize_t
cristy574cc262011-08-05 01:23:58 +00001237 center,
cristybb503372010-05-27 20:51:26 +00001238 y;
1239
cristyfccdab92009-11-30 16:43:57 +00001240 /*
1241 Initialize convolve image attributes.
1242 */
1243 assert(image != (Image *) NULL);
1244 assert(image->signature == MagickSignature);
1245 if (image->debug != MagickFalse)
1246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1247 assert(exception != (ExceptionInfo *) NULL);
1248 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001249 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001250 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001251 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1252 exception);
cristyfccdab92009-11-30 16:43:57 +00001253 if (convolve_image == (Image *) NULL)
1254 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001255 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001256 {
cristyfccdab92009-11-30 16:43:57 +00001257 convolve_image=DestroyImage(convolve_image);
1258 return((Image *) NULL);
1259 }
1260 if (image->debug != MagickFalse)
1261 {
1262 char
1263 format[MaxTextExtent],
1264 *message;
1265
cristya96f2492011-12-14 18:25:41 +00001266 register const MagickRealType
cristy117ff172010-08-15 21:35:32 +00001267 *k;
1268
cristy4e154852011-07-14 13:28:53 +00001269 register ssize_t
1270 u;
1271
cristybb503372010-05-27 20:51:26 +00001272 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001273 v;
1274
cristyfccdab92009-11-30 16:43:57 +00001275 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001276 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1277 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001278 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001279 k=kernel_info->values;
1280 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001281 {
1282 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001283 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001284 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001285 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001286 {
cristyb51dff52011-05-19 16:55:47 +00001287 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001288 (void) ConcatenateString(&message,format);
1289 }
1290 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1291 }
1292 message=DestroyString(message);
1293 }
cristy7ea27962011-09-12 18:12:49 +00001294 status=AccelerateConvolveImage(image,kernel_info,convolve_image,exception);
1295 if (status == MagickTrue)
1296 return(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001297 /*
cristyfccdab92009-11-30 16:43:57 +00001298 Convolve image.
anthonyfd706f92012-01-19 04:22:02 +00001299 FUTURE: Use Morphology Convolve instead.
cristyfccdab92009-11-30 16:43:57 +00001300 */
cristy574cc262011-08-05 01:23:58 +00001301 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
cristy075ff302011-09-07 01:51:24 +00001302 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2L);
cristyfccdab92009-11-30 16:43:57 +00001303 status=MagickTrue;
1304 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001305 image_view=AcquireCacheView(image);
1306 convolve_view=AcquireCacheView(convolve_image);
1307#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd36a25e2012-01-18 14:30:53 +00001308 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001309#endif
cristybb503372010-05-27 20:51:26 +00001310 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001311 {
cristy4c08aed2011-07-01 19:47:50 +00001312 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001313 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001314
cristy4c08aed2011-07-01 19:47:50 +00001315 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001316 *restrict q;
1317
cristy117ff172010-08-15 21:35:32 +00001318 register ssize_t
1319 x;
1320
cristyfccdab92009-11-30 16:43:57 +00001321 if (status == MagickFalse)
1322 continue;
cristy105ba3c2011-07-18 02:28:38 +00001323 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1324 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1325 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001326 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001327 exception);
cristy105ba3c2011-07-18 02:28:38 +00001328 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001329 {
1330 status=MagickFalse;
1331 continue;
1332 }
cristybb503372010-05-27 20:51:26 +00001333 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001334 {
cristybb503372010-05-27 20:51:26 +00001335 register ssize_t
cristyed231572011-07-14 02:18:59 +00001336 i;
cristyfccdab92009-11-30 16:43:57 +00001337
cristya30d9ba2011-07-23 21:00:48 +00001338 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001339 {
cristyed231572011-07-14 02:18:59 +00001340 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001341 alpha,
1342 gamma,
cristyed231572011-07-14 02:18:59 +00001343 pixel;
1344
1345 PixelChannel
1346 channel;
1347
1348 PixelTrait
1349 convolve_traits,
1350 traits;
1351
cristya96f2492011-12-14 18:25:41 +00001352 register const MagickRealType
cristyed231572011-07-14 02:18:59 +00001353 *restrict k;
1354
1355 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001356 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001357
1358 register ssize_t
1359 u;
1360
1361 ssize_t
1362 v;
1363
cristye2a912b2011-12-05 20:02:07 +00001364 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001365 traits=GetPixelChannelMapTraits(image,channel);
cristy4e154852011-07-14 13:28:53 +00001366 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001367 if ((traits == UndefinedPixelTrait) ||
1368 (convolve_traits == UndefinedPixelTrait))
cristy4e154852011-07-14 13:28:53 +00001369 continue;
1370 if ((convolve_traits & CopyPixelTrait) != 0)
1371 {
cristy0beccfa2011-09-25 20:47:53 +00001372 SetPixelChannel(convolve_image,channel,p[center+i],q);
cristy4e154852011-07-14 13:28:53 +00001373 continue;
1374 }
cristy5e6be1e2011-07-16 01:23:39 +00001375 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001376 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001377 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001378 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001379 {
cristyed231572011-07-14 02:18:59 +00001380 /*
cristy4e154852011-07-14 13:28:53 +00001381 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001382 */
cristyeb52cde2011-07-17 01:52:52 +00001383 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001384 {
cristyeb52cde2011-07-17 01:52:52 +00001385 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001386 {
cristyeb52cde2011-07-17 01:52:52 +00001387 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001388 k++;
cristya30d9ba2011-07-23 21:00:48 +00001389 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001390 }
cristya30d9ba2011-07-23 21:00:48 +00001391 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001392 }
cristy0beccfa2011-09-25 20:47:53 +00001393 SetPixelChannel(convolve_image,channel,ClampToQuantum(pixel),q);
cristy4e154852011-07-14 13:28:53 +00001394 continue;
cristyed231572011-07-14 02:18:59 +00001395 }
cristy4e154852011-07-14 13:28:53 +00001396 /*
1397 Alpha blending.
1398 */
1399 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001400 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001401 {
cristyeb52cde2011-07-17 01:52:52 +00001402 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001403 {
cristyeb52cde2011-07-17 01:52:52 +00001404 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1405 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001406 gamma+=(*k)*alpha;
1407 k++;
cristya30d9ba2011-07-23 21:00:48 +00001408 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001409 }
cristya30d9ba2011-07-23 21:00:48 +00001410 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001411 }
cristy1ce96d02011-07-14 17:57:24 +00001412 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001413 SetPixelChannel(convolve_image,channel,ClampToQuantum(gamma*pixel),q);
cristyed231572011-07-14 02:18:59 +00001414 }
cristya30d9ba2011-07-23 21:00:48 +00001415 p+=GetPixelChannels(image);
1416 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001417 }
cristyed231572011-07-14 02:18:59 +00001418 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001419 status=MagickFalse;
1420 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1421 {
1422 MagickBooleanType
1423 proceed;
1424
1425#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001426 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001427#endif
1428 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1429 if (proceed == MagickFalse)
1430 status=MagickFalse;
1431 }
1432 }
1433 convolve_image->type=image->type;
1434 convolve_view=DestroyCacheView(convolve_view);
1435 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001436 if (status == MagickFalse)
1437 convolve_image=DestroyImage(convolve_image);
1438 return(convolve_image);
1439}
1440
1441/*
1442%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1443% %
1444% %
1445% %
cristy3ed852e2009-09-05 21:47:34 +00001446% D e s p e c k l e I m a g e %
1447% %
1448% %
1449% %
1450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451%
1452% DespeckleImage() reduces the speckle noise in an image while perserving the
cristyf0ae7762012-01-15 22:04:03 +00001453% edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1454% neighbors, then complementarily lowering pixels that are brighter than their
1455% surrounding neighbors) to reduce the speckle index of that image (reference
1456% Crimmins speckle removal).
cristy3ed852e2009-09-05 21:47:34 +00001457%
1458% The format of the DespeckleImage method is:
1459%
1460% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1461%
1462% A description of each parameter follows:
1463%
1464% o image: the image.
1465%
1466% o exception: return any errors or warnings in this structure.
1467%
1468*/
1469
cristy5473bf02012-01-16 19:15:08 +00001470static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1471 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1472 Quantum *restrict g)
cristy3ed852e2009-09-05 21:47:34 +00001473{
cristy7b4b8862012-01-16 19:52:19 +00001474 register Quantum
cristy5473bf02012-01-16 19:15:08 +00001475 *p,
1476 *q,
1477 *r,
1478 *s;
cristy78f3de22012-01-16 00:43:18 +00001479
cristy5473bf02012-01-16 19:15:08 +00001480 ssize_t
1481 y;
1482
1483 assert(f != (Quantum *) NULL);
1484 assert(g != (Quantum *) NULL);
1485 p=f+(columns+2);
1486 q=g+(columns+2);
1487 r=p+(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001488#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8de86be2012-01-20 02:05:06 +00001489 #pragma omp parallel for schedule(static)
cristy5473bf02012-01-16 19:15:08 +00001490#endif
1491 for (y=0; y < (ssize_t) rows; y++)
1492 {
cristy7b4b8862012-01-16 19:52:19 +00001493 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001494 i,
1495 x;
1496
cristy7b4b8862012-01-16 19:52:19 +00001497 SignedQuantum
1498 v;
1499
cristy5473bf02012-01-16 19:15:08 +00001500 i=(2*y+1)+y*columns;
1501 if (polarity > 0)
1502 for (x=0; x < (ssize_t) columns; x++)
1503 {
1504 v=(SignedQuantum) p[i];
1505 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1506 v+=ScaleCharToQuantum(1);
1507 q[i]=(Quantum) v;
1508 i++;
1509 }
1510 else
1511 for (x=0; x < (ssize_t) columns; x++)
1512 {
1513 v=(SignedQuantum) p[i];
1514 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1515 v-=ScaleCharToQuantum(1);
1516 q[i]=(Quantum) v;
1517 i++;
1518 }
1519 }
1520 p=f+(columns+2);
1521 q=g+(columns+2);
1522 r=q+(y_offset*(columns+2)+x_offset);
1523 s=q-(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001524#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8de86be2012-01-20 02:05:06 +00001525 #pragma omp parallel for schedule(static)
cristy5473bf02012-01-16 19:15:08 +00001526#endif
1527 for (y=0; y < (ssize_t) rows; y++)
1528 {
cristy7b4b8862012-01-16 19:52:19 +00001529 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001530 i,
1531 x;
1532
cristy7b4b8862012-01-16 19:52:19 +00001533 SignedQuantum
1534 v;
1535
cristy5473bf02012-01-16 19:15:08 +00001536 i=(2*y+1)+y*columns;
1537 if (polarity > 0)
1538 for (x=0; x < (ssize_t) columns; x++)
1539 {
1540 v=(SignedQuantum) q[i];
1541 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1542 ((SignedQuantum) r[i] > v))
1543 v+=ScaleCharToQuantum(1);
1544 p[i]=(Quantum) v;
1545 i++;
1546 }
1547 else
1548 for (x=0; x < (ssize_t) columns; x++)
1549 {
1550 v=(SignedQuantum) q[i];
1551 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1552 ((SignedQuantum) r[i] < v))
1553 v-=ScaleCharToQuantum(1);
1554 p[i]=(Quantum) v;
1555 i++;
1556 }
1557 }
cristy3ed852e2009-09-05 21:47:34 +00001558}
1559
1560MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1561{
1562#define DespeckleImageTag "Despeckle/Image"
1563
cristy2407fc22009-09-11 00:55:25 +00001564 CacheView
1565 *despeckle_view,
1566 *image_view;
1567
cristy3ed852e2009-09-05 21:47:34 +00001568 Image
1569 *despeckle_image;
1570
cristy3ed852e2009-09-05 21:47:34 +00001571 MagickBooleanType
1572 status;
1573
cristy5473bf02012-01-16 19:15:08 +00001574 Quantum
1575 *restrict buffer,
1576 *restrict pixels;
cristya63e4a92011-09-09 00:47:59 +00001577
cristy5473bf02012-01-16 19:15:08 +00001578 register ssize_t
1579 i;
1580
1581 size_t
1582 length;
cristy117ff172010-08-15 21:35:32 +00001583
cristybb503372010-05-27 20:51:26 +00001584 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001585 X[4] = {0, 1, 1,-1},
1586 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001587
cristy3ed852e2009-09-05 21:47:34 +00001588 /*
1589 Allocate despeckled image.
1590 */
1591 assert(image != (const Image *) NULL);
1592 assert(image->signature == MagickSignature);
1593 if (image->debug != MagickFalse)
1594 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1595 assert(exception != (ExceptionInfo *) NULL);
1596 assert(exception->signature == MagickSignature);
cristy5473bf02012-01-16 19:15:08 +00001597 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001598 if (despeckle_image == (Image *) NULL)
1599 return((Image *) NULL);
cristya63e4a92011-09-09 00:47:59 +00001600 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1601 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001602 {
cristy3ed852e2009-09-05 21:47:34 +00001603 despeckle_image=DestroyImage(despeckle_image);
1604 return((Image *) NULL);
1605 }
1606 /*
cristy5473bf02012-01-16 19:15:08 +00001607 Allocate image buffer.
1608 */
1609 length=(size_t) ((image->columns+2)*(image->rows+2));
1610 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1611 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1612 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1613 {
1614 if (buffer != (Quantum *) NULL)
1615 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1616 if (pixels != (Quantum *) NULL)
1617 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1618 despeckle_image=DestroyImage(despeckle_image);
1619 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1620 }
1621 /*
1622 Reduce speckle in the image.
cristy3ed852e2009-09-05 21:47:34 +00001623 */
1624 status=MagickTrue;
cristyb06da092012-01-16 17:11:38 +00001625 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00001626 despeckle_view=AcquireCacheView(despeckle_image);
cristy5473bf02012-01-16 19:15:08 +00001627 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +00001628 {
cristy5473bf02012-01-16 19:15:08 +00001629 PixelChannel
1630 channel;
1631
1632 PixelTrait
1633 despeckle_traits,
1634 traits;
cristy3ed852e2009-09-05 21:47:34 +00001635
cristyc1488b52011-02-19 18:54:15 +00001636 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001637 k,
cristyf0ae7762012-01-15 22:04:03 +00001638 x;
cristyc1488b52011-02-19 18:54:15 +00001639
cristy5473bf02012-01-16 19:15:08 +00001640 ssize_t
1641 j,
1642 y;
1643
1644 if (status == MagickFalse)
1645 continue;
1646 channel=GetPixelChannelMapChannel(image,i);
1647 traits=GetPixelChannelMapTraits(image,channel);
1648 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1649 if ((traits == UndefinedPixelTrait) ||
1650 (despeckle_traits == UndefinedPixelTrait))
1651 continue;
1652 if ((despeckle_traits & CopyPixelTrait) != 0)
1653 continue;
1654 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1655 j=(ssize_t) image->columns+2;
1656 for (y=0; y < (ssize_t) image->rows; y++)
cristyfc830f42012-01-15 02:45:06 +00001657 {
cristy5473bf02012-01-16 19:15:08 +00001658 register const Quantum
1659 *restrict p;
cristyfc830f42012-01-15 02:45:06 +00001660
cristy5473bf02012-01-16 19:15:08 +00001661 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1662 if (p == (const Quantum *) NULL)
cristyf0ae7762012-01-15 22:04:03 +00001663 {
cristy5473bf02012-01-16 19:15:08 +00001664 status=MagickFalse;
1665 continue;
cristyf0ae7762012-01-15 22:04:03 +00001666 }
cristy5473bf02012-01-16 19:15:08 +00001667 j++;
1668 for (x=0; x < (ssize_t) image->columns; x++)
1669 {
1670 pixels[j++]=p[i];
1671 p+=GetPixelChannels(image);
cristyfc830f42012-01-15 02:45:06 +00001672 }
cristy5473bf02012-01-16 19:15:08 +00001673 j++;
cristy3ed852e2009-09-05 21:47:34 +00001674 }
cristy5473bf02012-01-16 19:15:08 +00001675 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1676 for (k=0; k < 4; k++)
1677 {
1678 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1679 Hull(-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1680 Hull(-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1681 Hull(X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1682 }
1683 j=(ssize_t) image->columns+2;
1684 for (y=0; y < (ssize_t) image->rows; y++)
1685 {
1686 MagickBooleanType
1687 sync;
1688
1689 register Quantum
1690 *restrict q;
1691
cristyd13e8eb2012-01-17 02:36:42 +00001692 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1693 despeckle_image->columns,1,exception);
cristy5473bf02012-01-16 19:15:08 +00001694 if (q == (Quantum *) NULL)
1695 {
1696 status=MagickFalse;
1697 continue;
1698 }
1699 j++;
1700 for (x=0; x < (ssize_t) image->columns; x++)
1701 {
1702 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1703 q+=GetPixelChannels(despeckle_image);
1704 }
1705 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1706 if (sync == MagickFalse)
1707 status=MagickFalse;
1708 j++;
1709 }
cristy3ed852e2009-09-05 21:47:34 +00001710 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1711 {
1712 MagickBooleanType
1713 proceed;
1714
cristy5473bf02012-01-16 19:15:08 +00001715 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1716 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00001717 if (proceed == MagickFalse)
1718 status=MagickFalse;
1719 }
1720 }
1721 despeckle_view=DestroyCacheView(despeckle_view);
1722 image_view=DestroyCacheView(image_view);
cristy5473bf02012-01-16 19:15:08 +00001723 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1724 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001725 despeckle_image->type=image->type;
1726 if (status == MagickFalse)
1727 despeckle_image=DestroyImage(despeckle_image);
1728 return(despeckle_image);
1729}
1730
1731/*
1732%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733% %
1734% %
1735% %
1736% E d g e I m a g e %
1737% %
1738% %
1739% %
1740%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1741%
1742% EdgeImage() finds edges in an image. Radius defines the radius of the
1743% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1744% radius for you.
1745%
1746% The format of the EdgeImage method is:
1747%
1748% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001749% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001750%
1751% A description of each parameter follows:
1752%
1753% o image: the image.
1754%
1755% o radius: the radius of the pixel neighborhood.
1756%
cristy8ae632d2011-09-05 17:29:53 +00001757% o sigma: the standard deviation of the Gaussian, in pixels.
1758%
cristy3ed852e2009-09-05 21:47:34 +00001759% o exception: return any errors or warnings in this structure.
1760%
1761*/
1762MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001763 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001764{
1765 Image
1766 *edge_image;
1767
cristy41cbe682011-07-15 19:12:37 +00001768 KernelInfo
1769 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001770
cristybb503372010-05-27 20:51:26 +00001771 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001772 i;
1773
cristybb503372010-05-27 20:51:26 +00001774 size_t
cristy3ed852e2009-09-05 21:47:34 +00001775 width;
1776
cristy41cbe682011-07-15 19:12:37 +00001777 ssize_t
1778 j,
1779 u,
1780 v;
1781
cristy3ed852e2009-09-05 21:47:34 +00001782 assert(image != (const Image *) NULL);
1783 assert(image->signature == MagickSignature);
1784 if (image->debug != MagickFalse)
1785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1786 assert(exception != (ExceptionInfo *) NULL);
1787 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001788 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001789 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001790 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001791 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001792 kernel_info->width=width;
1793 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001794 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1795 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1796 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001797 {
1798 kernel_info=DestroyKernelInfo(kernel_info);
1799 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1800 }
1801 j=(ssize_t) kernel_info->width/2;
1802 i=0;
1803 for (v=(-j); v <= j; v++)
1804 {
1805 for (u=(-j); u <= j; u++)
1806 {
1807 kernel_info->values[i]=(-1.0);
1808 i++;
1809 }
1810 }
1811 kernel_info->values[i/2]=(double) (width*width-1.0);
anthony736a1602011-10-06 12:38:17 +00001812 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
cristy5e6be1e2011-07-16 01:23:39 +00001813 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001814 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001815 return(edge_image);
1816}
1817
1818/*
1819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1820% %
1821% %
1822% %
1823% E m b o s s I m a g e %
1824% %
1825% %
1826% %
1827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1828%
1829% EmbossImage() returns a grayscale image with a three-dimensional effect.
1830% We convolve the image with a Gaussian operator of the given radius and
1831% standard deviation (sigma). For reasonable results, radius should be
1832% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1833% radius for you.
1834%
1835% The format of the EmbossImage method is:
1836%
1837% Image *EmbossImage(const Image *image,const double radius,
1838% const double sigma,ExceptionInfo *exception)
1839%
1840% A description of each parameter follows:
1841%
1842% o image: the image.
1843%
1844% o radius: the radius of the pixel neighborhood.
1845%
1846% o sigma: the standard deviation of the Gaussian, in pixels.
1847%
1848% o exception: return any errors or warnings in this structure.
1849%
1850*/
1851MagickExport Image *EmbossImage(const Image *image,const double radius,
1852 const double sigma,ExceptionInfo *exception)
1853{
cristy3ed852e2009-09-05 21:47:34 +00001854 Image
1855 *emboss_image;
1856
cristy41cbe682011-07-15 19:12:37 +00001857 KernelInfo
1858 *kernel_info;
1859
cristybb503372010-05-27 20:51:26 +00001860 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001861 i;
1862
cristybb503372010-05-27 20:51:26 +00001863 size_t
cristy3ed852e2009-09-05 21:47:34 +00001864 width;
1865
cristy117ff172010-08-15 21:35:32 +00001866 ssize_t
1867 j,
1868 k,
1869 u,
1870 v;
1871
cristy41cbe682011-07-15 19:12:37 +00001872 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001873 assert(image->signature == MagickSignature);
1874 if (image->debug != MagickFalse)
1875 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1876 assert(exception != (ExceptionInfo *) NULL);
1877 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001878 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001879 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001880 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001881 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001882 kernel_info->width=width;
1883 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001884 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1885 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1886 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001887 {
1888 kernel_info=DestroyKernelInfo(kernel_info);
1889 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1890 }
1891 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001892 k=j;
1893 i=0;
1894 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001895 {
cristy47e00502009-12-17 19:19:57 +00001896 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001897 {
cristy41cbe682011-07-15 19:12:37 +00001898 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001899 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001900 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001901 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001902 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001903 i++;
1904 }
cristy47e00502009-12-17 19:19:57 +00001905 k--;
cristy3ed852e2009-09-05 21:47:34 +00001906 }
anthony736a1602011-10-06 12:38:17 +00001907 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
cristy5e6be1e2011-07-16 01:23:39 +00001908 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001909 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001910 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001911 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001912 return(emboss_image);
1913}
1914
1915/*
1916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1917% %
1918% %
1919% %
1920% G a u s s i a n B l u r I m a g e %
1921% %
1922% %
1923% %
1924%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1925%
1926% GaussianBlurImage() blurs an image. We convolve the image with a
1927% Gaussian operator of the given radius and standard deviation (sigma).
1928% For reasonable results, the radius should be larger than sigma. Use a
1929% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1930%
1931% The format of the GaussianBlurImage method is:
1932%
1933% Image *GaussianBlurImage(const Image *image,onst double radius,
cristyd89705a2012-01-20 02:52:24 +00001934% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001935%
1936% A description of each parameter follows:
1937%
1938% o image: the image.
1939%
cristy3ed852e2009-09-05 21:47:34 +00001940% o radius: the radius of the Gaussian, in pixels, not counting the center
1941% pixel.
1942%
1943% o sigma: the standard deviation of the Gaussian, in pixels.
1944%
1945% o exception: return any errors or warnings in this structure.
1946%
1947*/
cristy41cbe682011-07-15 19:12:37 +00001948MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristyd89705a2012-01-20 02:52:24 +00001949 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001950{
cristy3ed852e2009-09-05 21:47:34 +00001951 Image
1952 *blur_image;
1953
cristy41cbe682011-07-15 19:12:37 +00001954 KernelInfo
1955 *kernel_info;
1956
cristybb503372010-05-27 20:51:26 +00001957 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001958 i;
1959
cristybb503372010-05-27 20:51:26 +00001960 size_t
cristy3ed852e2009-09-05 21:47:34 +00001961 width;
1962
cristy117ff172010-08-15 21:35:32 +00001963 ssize_t
1964 j,
1965 u,
1966 v;
1967
cristy3ed852e2009-09-05 21:47:34 +00001968 assert(image != (const Image *) NULL);
1969 assert(image->signature == MagickSignature);
1970 if (image->debug != MagickFalse)
1971 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1972 assert(exception != (ExceptionInfo *) NULL);
1973 assert(exception->signature == MagickSignature);
1974 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001975 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001976 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001977 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001978 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1979 kernel_info->width=width;
1980 kernel_info->height=width;
1981 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00001982 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1983 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1984 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001985 {
1986 kernel_info=DestroyKernelInfo(kernel_info);
1987 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1988 }
1989 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001990 i=0;
cristy47e00502009-12-17 19:19:57 +00001991 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001992 {
cristy47e00502009-12-17 19:19:57 +00001993 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001994 {
1995 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1996 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1997 i++;
1998 }
cristy3ed852e2009-09-05 21:47:34 +00001999 }
cristy5e6be1e2011-07-16 01:23:39 +00002000 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00002001 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00002002 return(blur_image);
2003}
2004
2005/*
2006%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2007% %
2008% %
2009% %
cristy3ed852e2009-09-05 21:47:34 +00002010% M o t i o n B l u r I m a g e %
2011% %
2012% %
2013% %
2014%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2015%
2016% MotionBlurImage() simulates motion blur. We convolve the image with a
2017% Gaussian operator of the given radius and standard deviation (sigma).
2018% For reasonable results, radius should be larger than sigma. Use a
2019% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2020% Angle gives the angle of the blurring motion.
2021%
2022% Andrew Protano contributed this effect.
2023%
2024% The format of the MotionBlurImage method is:
2025%
2026% Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002027% const double sigma,const double angle,const double bias,
2028% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002029%
2030% A description of each parameter follows:
2031%
2032% o image: the image.
2033%
cristy3ed852e2009-09-05 21:47:34 +00002034% o radius: the radius of the Gaussian, in pixels, not counting
2035% the center pixel.
2036%
2037% o sigma: the standard deviation of the Gaussian, in pixels.
2038%
cristycee97112010-05-28 00:44:52 +00002039% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002040%
cristyf7ef0252011-09-09 14:50:06 +00002041% o bias: the bias.
2042%
cristy3ed852e2009-09-05 21:47:34 +00002043% o exception: return any errors or warnings in this structure.
2044%
2045*/
2046
cristybb503372010-05-27 20:51:26 +00002047static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002048{
cristy3ed852e2009-09-05 21:47:34 +00002049 double
cristy47e00502009-12-17 19:19:57 +00002050 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002051 normalize;
2052
cristybb503372010-05-27 20:51:26 +00002053 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002054 i;
2055
2056 /*
cristy47e00502009-12-17 19:19:57 +00002057 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002058 */
2059 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +00002060 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00002061 if (kernel == (double *) NULL)
2062 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002063 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002064 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002065 {
cristy4205a3c2010-09-12 20:19:59 +00002066 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2067 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002068 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002069 }
cristybb503372010-05-27 20:51:26 +00002070 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002071 kernel[i]/=normalize;
2072 return(kernel);
2073}
2074
cristya63e4a92011-09-09 00:47:59 +00002075MagickExport Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002076 const double sigma,const double angle,const double bias,
2077 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002078{
cristyc4c8d132010-01-07 01:58:38 +00002079 CacheView
2080 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002081 *image_view,
2082 *motion_view;
cristyc4c8d132010-01-07 01:58:38 +00002083
cristy3ed852e2009-09-05 21:47:34 +00002084 double
2085 *kernel;
2086
2087 Image
2088 *blur_image;
2089
cristy3ed852e2009-09-05 21:47:34 +00002090 MagickBooleanType
2091 status;
2092
cristybb503372010-05-27 20:51:26 +00002093 MagickOffsetType
2094 progress;
2095
cristy3ed852e2009-09-05 21:47:34 +00002096 OffsetInfo
2097 *offset;
2098
2099 PointInfo
2100 point;
2101
cristybb503372010-05-27 20:51:26 +00002102 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002103 i;
2104
cristybb503372010-05-27 20:51:26 +00002105 size_t
cristy3ed852e2009-09-05 21:47:34 +00002106 width;
2107
cristybb503372010-05-27 20:51:26 +00002108 ssize_t
2109 y;
2110
cristy3ed852e2009-09-05 21:47:34 +00002111 assert(image != (Image *) NULL);
2112 assert(image->signature == MagickSignature);
2113 if (image->debug != MagickFalse)
2114 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2115 assert(exception != (ExceptionInfo *) NULL);
2116 width=GetOptimalKernelWidth1D(radius,sigma);
2117 kernel=GetMotionBlurKernel(width,sigma);
2118 if (kernel == (double *) NULL)
2119 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2120 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2121 if (offset == (OffsetInfo *) NULL)
2122 {
cristy8b49f382012-01-17 03:11:03 +00002123 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002124 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2125 }
cristy1e7aa312011-09-10 20:01:36 +00002126 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002127 if (blur_image == (Image *) NULL)
2128 {
cristy8b49f382012-01-17 03:11:03 +00002129 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002130 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2131 return((Image *) NULL);
2132 }
cristy574cc262011-08-05 01:23:58 +00002133 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002134 {
cristy8b49f382012-01-17 03:11:03 +00002135 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002136 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002137 blur_image=DestroyImage(blur_image);
2138 return((Image *) NULL);
2139 }
2140 point.x=(double) width*sin(DegreesToRadians(angle));
2141 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002142 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002143 {
cristybb503372010-05-27 20:51:26 +00002144 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2145 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002146 }
2147 /*
2148 Motion blur image.
2149 */
2150 status=MagickTrue;
2151 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002152 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002153 motion_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002154 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002155#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00002156 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002157#endif
cristybb503372010-05-27 20:51:26 +00002158 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002159 {
cristyf7ef0252011-09-09 14:50:06 +00002160 register const Quantum
2161 *restrict p;
2162
cristy4c08aed2011-07-01 19:47:50 +00002163 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002164 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002165
cristy117ff172010-08-15 21:35:32 +00002166 register ssize_t
2167 x;
2168
cristy3ed852e2009-09-05 21:47:34 +00002169 if (status == MagickFalse)
2170 continue;
cristy8b49f382012-01-17 03:11:03 +00002171 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2172 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002173 exception);
cristyf7ef0252011-09-09 14:50:06 +00002174 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002175 {
2176 status=MagickFalse;
2177 continue;
2178 }
cristybb503372010-05-27 20:51:26 +00002179 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002180 {
cristybb503372010-05-27 20:51:26 +00002181 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002182 i;
2183
cristyf7ef0252011-09-09 14:50:06 +00002184 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2185 {
2186 MagickRealType
2187 alpha,
2188 gamma,
2189 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002190
cristyf7ef0252011-09-09 14:50:06 +00002191 PixelChannel
2192 channel;
2193
2194 PixelTrait
2195 blur_traits,
2196 traits;
2197
2198 register const Quantum
2199 *restrict r;
2200
2201 register double
2202 *restrict k;
2203
2204 register ssize_t
2205 j;
2206
cristye2a912b2011-12-05 20:02:07 +00002207 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002208 traits=GetPixelChannelMapTraits(image,channel);
cristyf7ef0252011-09-09 14:50:06 +00002209 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2210 if ((traits == UndefinedPixelTrait) ||
2211 (blur_traits == UndefinedPixelTrait))
2212 continue;
2213 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002214 {
cristy0beccfa2011-09-25 20:47:53 +00002215 SetPixelChannel(blur_image,channel,p[i],q);
cristyf7ef0252011-09-09 14:50:06 +00002216 continue;
cristy3ed852e2009-09-05 21:47:34 +00002217 }
cristyf7ef0252011-09-09 14:50:06 +00002218 k=kernel;
2219 pixel=bias;
2220 if ((blur_traits & BlendPixelTrait) == 0)
2221 {
2222 for (j=0; j < (ssize_t) width; j++)
2223 {
cristy8b49f382012-01-17 03:11:03 +00002224 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
cristyf7ef0252011-09-09 14:50:06 +00002225 offset[j].y,1,1,exception);
2226 if (r == (const Quantum *) NULL)
2227 {
2228 status=MagickFalse;
2229 continue;
2230 }
2231 pixel+=(*k)*r[i];
2232 k++;
2233 }
cristy0beccfa2011-09-25 20:47:53 +00002234 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002235 continue;
2236 }
2237 alpha=0.0;
2238 gamma=0.0;
2239 for (j=0; j < (ssize_t) width; j++)
2240 {
cristy8b49f382012-01-17 03:11:03 +00002241 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
cristyf7ef0252011-09-09 14:50:06 +00002242 1,exception);
2243 if (r == (const Quantum *) NULL)
2244 {
2245 status=MagickFalse;
2246 continue;
2247 }
2248 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2249 pixel+=(*k)*alpha*r[i];
2250 gamma+=(*k)*alpha;
2251 k++;
cristy3ed852e2009-09-05 21:47:34 +00002252 }
cristyf7ef0252011-09-09 14:50:06 +00002253 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002254 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002255 }
2256 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002257 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002258 }
2259 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2260 status=MagickFalse;
2261 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2262 {
2263 MagickBooleanType
2264 proceed;
2265
cristyb557a152011-02-22 12:14:30 +00002266#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002267 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002268#endif
2269 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2270 if (proceed == MagickFalse)
2271 status=MagickFalse;
2272 }
2273 }
2274 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00002275 motion_view=DestroyCacheView(motion_view);
cristy3ed852e2009-09-05 21:47:34 +00002276 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00002277 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002278 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2279 if (status == MagickFalse)
2280 blur_image=DestroyImage(blur_image);
2281 return(blur_image);
2282}
2283
2284/*
2285%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2286% %
2287% %
2288% %
2289% P r e v i e w I m a g e %
2290% %
2291% %
2292% %
2293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2294%
2295% PreviewImage() tiles 9 thumbnails of the specified image with an image
2296% processing operation applied with varying parameters. This may be helpful
2297% pin-pointing an appropriate parameter for a particular image processing
2298% operation.
2299%
2300% The format of the PreviewImages method is:
2301%
2302% Image *PreviewImages(const Image *image,const PreviewType preview,
2303% ExceptionInfo *exception)
2304%
2305% A description of each parameter follows:
2306%
2307% o image: the image.
2308%
2309% o preview: the image processing operation.
2310%
2311% o exception: return any errors or warnings in this structure.
2312%
2313*/
2314MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2315 ExceptionInfo *exception)
2316{
2317#define NumberTiles 9
2318#define PreviewImageTag "Preview/Image"
2319#define DefaultPreviewGeometry "204x204+10+10"
2320
2321 char
2322 factor[MaxTextExtent],
2323 label[MaxTextExtent];
2324
2325 double
2326 degrees,
2327 gamma,
2328 percentage,
2329 radius,
2330 sigma,
2331 threshold;
2332
2333 Image
2334 *images,
2335 *montage_image,
2336 *preview_image,
2337 *thumbnail;
2338
2339 ImageInfo
2340 *preview_info;
2341
cristy3ed852e2009-09-05 21:47:34 +00002342 MagickBooleanType
2343 proceed;
2344
2345 MontageInfo
2346 *montage_info;
2347
2348 QuantizeInfo
2349 quantize_info;
2350
2351 RectangleInfo
2352 geometry;
2353
cristybb503372010-05-27 20:51:26 +00002354 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002355 i,
2356 x;
2357
cristybb503372010-05-27 20:51:26 +00002358 size_t
cristy3ed852e2009-09-05 21:47:34 +00002359 colors;
2360
cristy117ff172010-08-15 21:35:32 +00002361 ssize_t
2362 y;
2363
cristy3ed852e2009-09-05 21:47:34 +00002364 /*
2365 Open output image file.
2366 */
2367 assert(image != (Image *) NULL);
2368 assert(image->signature == MagickSignature);
2369 if (image->debug != MagickFalse)
2370 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2371 colors=2;
2372 degrees=0.0;
2373 gamma=(-0.2f);
2374 preview_info=AcquireImageInfo();
2375 SetGeometry(image,&geometry);
2376 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2377 &geometry.width,&geometry.height);
2378 images=NewImageList();
2379 percentage=12.5;
2380 GetQuantizeInfo(&quantize_info);
2381 radius=0.0;
2382 sigma=1.0;
2383 threshold=0.0;
2384 x=0;
2385 y=0;
2386 for (i=0; i < NumberTiles; i++)
2387 {
2388 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2389 if (thumbnail == (Image *) NULL)
2390 break;
2391 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2392 (void *) NULL);
cristyd15e6592011-10-15 00:13:06 +00002393 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002394 if (i == (NumberTiles/2))
2395 {
cristy9950d572011-10-01 18:22:35 +00002396 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2397 &thumbnail->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002398 AppendImageToList(&images,thumbnail);
2399 continue;
2400 }
2401 switch (preview)
2402 {
2403 case RotatePreview:
2404 {
2405 degrees+=45.0;
2406 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002407 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002408 break;
2409 }
2410 case ShearPreview:
2411 {
2412 degrees+=5.0;
2413 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002414 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002415 degrees,2.0*degrees);
2416 break;
2417 }
2418 case RollPreview:
2419 {
cristybb503372010-05-27 20:51:26 +00002420 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2421 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002422 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002423 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002424 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002425 break;
2426 }
2427 case HuePreview:
2428 {
2429 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2430 if (preview_image == (Image *) NULL)
2431 break;
cristyb51dff52011-05-19 16:55:47 +00002432 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002433 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002434 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002435 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002436 break;
2437 }
2438 case SaturationPreview:
2439 {
2440 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2441 if (preview_image == (Image *) NULL)
2442 break;
cristyb51dff52011-05-19 16:55:47 +00002443 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002444 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002445 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002446 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002447 break;
2448 }
2449 case BrightnessPreview:
2450 {
2451 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2452 if (preview_image == (Image *) NULL)
2453 break;
cristyb51dff52011-05-19 16:55:47 +00002454 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002455 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002456 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002457 break;
2458 }
2459 case GammaPreview:
2460 default:
2461 {
2462 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2463 if (preview_image == (Image *) NULL)
2464 break;
2465 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002466 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002467 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002468 break;
2469 }
2470 case SpiffPreview:
2471 {
2472 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2473 if (preview_image != (Image *) NULL)
2474 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002475 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002476 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002477 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002478 break;
2479 }
2480 case DullPreview:
2481 {
2482 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2483 if (preview_image == (Image *) NULL)
2484 break;
2485 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002486 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002487 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002488 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002489 break;
2490 }
2491 case GrayscalePreview:
2492 {
2493 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2494 if (preview_image == (Image *) NULL)
2495 break;
2496 colors<<=1;
2497 quantize_info.number_colors=colors;
2498 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002499 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002500 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002501 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002502 break;
2503 }
2504 case QuantizePreview:
2505 {
2506 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2507 if (preview_image == (Image *) NULL)
2508 break;
2509 colors<<=1;
2510 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002511 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002512 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002513 colors);
cristy3ed852e2009-09-05 21:47:34 +00002514 break;
2515 }
2516 case DespecklePreview:
2517 {
2518 for (x=0; x < (i-1); x++)
2519 {
2520 preview_image=DespeckleImage(thumbnail,exception);
2521 if (preview_image == (Image *) NULL)
2522 break;
2523 thumbnail=DestroyImage(thumbnail);
2524 thumbnail=preview_image;
2525 }
2526 preview_image=DespeckleImage(thumbnail,exception);
2527 if (preview_image == (Image *) NULL)
2528 break;
cristyb51dff52011-05-19 16:55:47 +00002529 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002530 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002531 break;
2532 }
2533 case ReduceNoisePreview:
2534 {
cristy95c38342011-03-18 22:39:51 +00002535 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2536 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002537 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002538 break;
2539 }
2540 case AddNoisePreview:
2541 {
2542 switch ((int) i)
2543 {
2544 case 0:
2545 {
2546 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2547 break;
2548 }
2549 case 1:
2550 {
2551 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2552 break;
2553 }
2554 case 2:
2555 {
2556 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2557 break;
2558 }
2559 case 3:
2560 {
2561 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2562 break;
2563 }
2564 case 4:
2565 {
2566 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2567 break;
2568 }
2569 case 5:
2570 {
2571 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2572 break;
2573 }
2574 default:
2575 {
2576 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2577 break;
2578 }
2579 }
cristyd76c51e2011-03-26 00:21:26 +00002580 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2581 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002582 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002583 break;
2584 }
2585 case SharpenPreview:
2586 {
anthony736a1602011-10-06 12:38:17 +00002587 /* FUTURE: user bias on sharpen! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002588 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2589 exception);
cristyb51dff52011-05-19 16:55:47 +00002590 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002591 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002592 break;
2593 }
2594 case BlurPreview:
2595 {
anthony736a1602011-10-06 12:38:17 +00002596 /* FUTURE: user bias on blur! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002597 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002598 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002599 sigma);
2600 break;
2601 }
2602 case ThresholdPreview:
2603 {
2604 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2605 if (preview_image == (Image *) NULL)
2606 break;
cristye941a752011-10-15 01:52:48 +00002607 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2608 QuantumRange+1.0))/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002609 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002610 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2611 break;
2612 }
2613 case EdgeDetectPreview:
2614 {
cristy8ae632d2011-09-05 17:29:53 +00002615 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002616 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002617 break;
2618 }
2619 case SpreadPreview:
2620 {
cristy5c4e2582011-09-11 19:21:03 +00002621 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2622 exception);
cristyb51dff52011-05-19 16:55:47 +00002623 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002624 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002625 break;
2626 }
2627 case SolarizePreview:
2628 {
2629 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2630 if (preview_image == (Image *) NULL)
2631 break;
2632 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002633 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002634 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002635 (QuantumRange*percentage)/100.0);
2636 break;
2637 }
2638 case ShadePreview:
2639 {
2640 degrees+=10.0;
2641 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2642 exception);
cristyb51dff52011-05-19 16:55:47 +00002643 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002644 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002645 break;
2646 }
2647 case RaisePreview:
2648 {
2649 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2650 if (preview_image == (Image *) NULL)
2651 break;
cristybb503372010-05-27 20:51:26 +00002652 geometry.width=(size_t) (2*i+2);
2653 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002654 geometry.x=i/2;
2655 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002656 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002657 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002658 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002659 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002660 break;
2661 }
2662 case SegmentPreview:
2663 {
2664 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2665 if (preview_image == (Image *) NULL)
2666 break;
2667 threshold+=0.4f;
2668 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002669 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002670 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002671 threshold,threshold);
2672 break;
2673 }
2674 case SwirlPreview:
2675 {
cristy76f512e2011-09-12 01:26:56 +00002676 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2677 exception);
cristyb51dff52011-05-19 16:55:47 +00002678 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002679 degrees+=45.0;
2680 break;
2681 }
2682 case ImplodePreview:
2683 {
2684 degrees+=0.1f;
cristy76f512e2011-09-12 01:26:56 +00002685 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2686 exception);
cristyb51dff52011-05-19 16:55:47 +00002687 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002688 break;
2689 }
2690 case WavePreview:
2691 {
2692 degrees+=5.0f;
cristy5c4e2582011-09-11 19:21:03 +00002693 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2694 image->interpolate,exception);
cristyb51dff52011-05-19 16:55:47 +00002695 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002696 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002697 break;
2698 }
2699 case OilPaintPreview:
2700 {
cristy14973ba2011-08-27 23:48:07 +00002701 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2702 exception);
2703 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2704 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002705 break;
2706 }
2707 case CharcoalDrawingPreview:
2708 {
anthony736a1602011-10-06 12:38:17 +00002709 /* FUTURE: user bias on charcoal! This is non-sensical! */
cristy3ed852e2009-09-05 21:47:34 +00002710 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristy05c0c9a2011-09-05 23:16:13 +00002711 image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002712 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002713 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002714 break;
2715 }
2716 case JPEGPreview:
2717 {
2718 char
2719 filename[MaxTextExtent];
2720
2721 int
2722 file;
2723
2724 MagickBooleanType
2725 status;
2726
2727 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2728 if (preview_image == (Image *) NULL)
2729 break;
cristybb503372010-05-27 20:51:26 +00002730 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002731 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002732 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002733 file=AcquireUniqueFileResource(filename);
2734 if (file != -1)
2735 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002736 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002737 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002738 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002739 if (status != MagickFalse)
2740 {
2741 Image
2742 *quality_image;
2743
2744 (void) CopyMagickString(preview_info->filename,
2745 preview_image->filename,MaxTextExtent);
2746 quality_image=ReadImage(preview_info,exception);
2747 if (quality_image != (Image *) NULL)
2748 {
2749 preview_image=DestroyImage(preview_image);
2750 preview_image=quality_image;
2751 }
2752 }
2753 (void) RelinquishUniqueFileResource(preview_image->filename);
2754 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002755 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002756 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2757 1024.0/1024.0);
2758 else
2759 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002760 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002761 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002762 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002763 else
cristyb51dff52011-05-19 16:55:47 +00002764 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002765 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002766 break;
2767 }
2768 }
2769 thumbnail=DestroyImage(thumbnail);
2770 percentage+=12.5;
2771 radius+=0.5;
2772 sigma+=0.25;
2773 if (preview_image == (Image *) NULL)
2774 break;
2775 (void) DeleteImageProperty(preview_image,"label");
cristyd15e6592011-10-15 00:13:06 +00002776 (void) SetImageProperty(preview_image,"label",label,exception);
cristy3ed852e2009-09-05 21:47:34 +00002777 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002778 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2779 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002780 if (proceed == MagickFalse)
2781 break;
2782 }
2783 if (images == (Image *) NULL)
2784 {
2785 preview_info=DestroyImageInfo(preview_info);
2786 return((Image *) NULL);
2787 }
2788 /*
2789 Create the montage.
2790 */
2791 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2792 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2793 montage_info->shadow=MagickTrue;
2794 (void) CloneString(&montage_info->tile,"3x3");
2795 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2796 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2797 montage_image=MontageImages(images,montage_info,exception);
2798 montage_info=DestroyMontageInfo(montage_info);
2799 images=DestroyImageList(images);
2800 if (montage_image == (Image *) NULL)
2801 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2802 if (montage_image->montage != (char *) NULL)
2803 {
2804 /*
2805 Free image directory.
2806 */
2807 montage_image->montage=(char *) RelinquishMagickMemory(
2808 montage_image->montage);
2809 if (image->directory != (char *) NULL)
2810 montage_image->directory=(char *) RelinquishMagickMemory(
2811 montage_image->directory);
2812 }
2813 preview_info=DestroyImageInfo(preview_info);
2814 return(montage_image);
2815}
2816
2817/*
2818%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2819% %
2820% %
2821% %
2822% R a d i a l B l u r I m a g e %
2823% %
2824% %
2825% %
2826%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2827%
2828% RadialBlurImage() applies a radial blur to the image.
2829%
2830% Andrew Protano contributed this effect.
2831%
2832% The format of the RadialBlurImage method is:
2833%
2834% Image *RadialBlurImage(const Image *image,const double angle,
cristy6435bd92011-09-10 02:10:07 +00002835% const double blur,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002836%
2837% A description of each parameter follows:
2838%
2839% o image: the image.
2840%
cristy3ed852e2009-09-05 21:47:34 +00002841% o angle: the angle of the radial blur.
2842%
cristy6435bd92011-09-10 02:10:07 +00002843% o blur: the blur.
2844%
cristy3ed852e2009-09-05 21:47:34 +00002845% o exception: return any errors or warnings in this structure.
2846%
2847*/
cristy4282c702011-11-21 00:01:06 +00002848MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2849 const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002850{
cristyc4c8d132010-01-07 01:58:38 +00002851 CacheView
2852 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002853 *image_view,
2854 *radial_view;
cristyc4c8d132010-01-07 01:58:38 +00002855
cristy3ed852e2009-09-05 21:47:34 +00002856 Image
2857 *blur_image;
2858
cristy3ed852e2009-09-05 21:47:34 +00002859 MagickBooleanType
2860 status;
2861
cristybb503372010-05-27 20:51:26 +00002862 MagickOffsetType
2863 progress;
2864
cristy3ed852e2009-09-05 21:47:34 +00002865 MagickRealType
2866 blur_radius,
2867 *cos_theta,
2868 offset,
2869 *sin_theta,
2870 theta;
2871
2872 PointInfo
2873 blur_center;
2874
cristybb503372010-05-27 20:51:26 +00002875 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002876 i;
2877
cristybb503372010-05-27 20:51:26 +00002878 size_t
cristy3ed852e2009-09-05 21:47:34 +00002879 n;
2880
cristybb503372010-05-27 20:51:26 +00002881 ssize_t
2882 y;
2883
cristy3ed852e2009-09-05 21:47:34 +00002884 /*
2885 Allocate blur image.
2886 */
2887 assert(image != (Image *) NULL);
2888 assert(image->signature == MagickSignature);
2889 if (image->debug != MagickFalse)
2890 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2891 assert(exception != (ExceptionInfo *) NULL);
2892 assert(exception->signature == MagickSignature);
cristy1e7aa312011-09-10 20:01:36 +00002893 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002894 if (blur_image == (Image *) NULL)
2895 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002896 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002897 {
cristy3ed852e2009-09-05 21:47:34 +00002898 blur_image=DestroyImage(blur_image);
2899 return((Image *) NULL);
2900 }
2901 blur_center.x=(double) image->columns/2.0;
2902 blur_center.y=(double) image->rows/2.0;
2903 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002904 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002905 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2906 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2907 sizeof(*cos_theta));
2908 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2909 sizeof(*sin_theta));
2910 if ((cos_theta == (MagickRealType *) NULL) ||
2911 (sin_theta == (MagickRealType *) NULL))
2912 {
2913 blur_image=DestroyImage(blur_image);
2914 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2915 }
2916 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002917 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002918 {
2919 cos_theta[i]=cos((double) (theta*i-offset));
2920 sin_theta[i]=sin((double) (theta*i-offset));
2921 }
2922 /*
2923 Radial blur image.
2924 */
2925 status=MagickTrue;
2926 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002927 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002928 radial_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002929 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002930#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002931 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002932#endif
cristy1e7aa312011-09-10 20:01:36 +00002933 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002934 {
cristy1e7aa312011-09-10 20:01:36 +00002935 register const Quantum
2936 *restrict p;
2937
cristy4c08aed2011-07-01 19:47:50 +00002938 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002939 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002940
cristy117ff172010-08-15 21:35:32 +00002941 register ssize_t
2942 x;
2943
cristy3ed852e2009-09-05 21:47:34 +00002944 if (status == MagickFalse)
2945 continue;
cristy8b49f382012-01-17 03:11:03 +00002946 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2947 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002948 exception);
cristy1e7aa312011-09-10 20:01:36 +00002949 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002950 {
2951 status=MagickFalse;
2952 continue;
2953 }
cristy1e7aa312011-09-10 20:01:36 +00002954 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002955 {
cristy3ed852e2009-09-05 21:47:34 +00002956 MagickRealType
cristy3ed852e2009-09-05 21:47:34 +00002957 radius;
2958
cristy3ed852e2009-09-05 21:47:34 +00002959 PointInfo
2960 center;
2961
cristybb503372010-05-27 20:51:26 +00002962 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002963 i;
2964
cristybb503372010-05-27 20:51:26 +00002965 size_t
cristy3ed852e2009-09-05 21:47:34 +00002966 step;
2967
2968 center.x=(double) x-blur_center.x;
2969 center.y=(double) y-blur_center.y;
2970 radius=hypot((double) center.x,center.y);
2971 if (radius == 0)
2972 step=1;
2973 else
2974 {
cristybb503372010-05-27 20:51:26 +00002975 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002976 if (step == 0)
2977 step=1;
2978 else
2979 if (step >= n)
2980 step=n-1;
2981 }
cristy1e7aa312011-09-10 20:01:36 +00002982 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2983 {
2984 MagickRealType
2985 gamma,
2986 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002987
cristy1e7aa312011-09-10 20:01:36 +00002988 PixelChannel
2989 channel;
2990
2991 PixelTrait
2992 blur_traits,
2993 traits;
2994
2995 register const Quantum
2996 *restrict r;
2997
2998 register ssize_t
2999 j;
3000
cristye2a912b2011-12-05 20:02:07 +00003001 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003002 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003003 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3004 if ((traits == UndefinedPixelTrait) ||
3005 (blur_traits == UndefinedPixelTrait))
3006 continue;
3007 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003008 {
cristy0beccfa2011-09-25 20:47:53 +00003009 SetPixelChannel(blur_image,channel,p[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003010 continue;
cristy3ed852e2009-09-05 21:47:34 +00003011 }
cristy1e7aa312011-09-10 20:01:36 +00003012 gamma=0.0;
3013 pixel=bias;
3014 if ((blur_traits & BlendPixelTrait) == 0)
3015 {
3016 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3017 {
cristy8b49f382012-01-17 03:11:03 +00003018 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003019 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3020 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3021 1,1,exception);
3022 if (r == (const Quantum *) NULL)
3023 {
3024 status=MagickFalse;
3025 continue;
3026 }
3027 pixel+=r[i];
3028 gamma++;
3029 }
3030 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003031 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003032 continue;
3033 }
3034 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3035 {
cristy8b49f382012-01-17 03:11:03 +00003036 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003037 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3038 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3039 1,1,exception);
3040 if (r == (const Quantum *) NULL)
3041 {
3042 status=MagickFalse;
3043 continue;
3044 }
3045 pixel+=GetPixelAlpha(image,r)*r[i];
3046 gamma+=GetPixelAlpha(image,r);
cristy3ed852e2009-09-05 21:47:34 +00003047 }
cristy1e7aa312011-09-10 20:01:36 +00003048 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003049 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003050 }
3051 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003052 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003053 }
3054 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3055 status=MagickFalse;
3056 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3057 {
3058 MagickBooleanType
3059 proceed;
3060
cristyb5d5f722009-11-04 03:03:49 +00003061#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003062 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003063#endif
3064 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3065 if (proceed == MagickFalse)
3066 status=MagickFalse;
3067 }
3068 }
3069 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00003070 radial_view=DestroyCacheView(radial_view);
cristy3ed852e2009-09-05 21:47:34 +00003071 image_view=DestroyCacheView(image_view);
3072 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3073 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3074 if (status == MagickFalse)
3075 blur_image=DestroyImage(blur_image);
3076 return(blur_image);
3077}
3078
3079/*
3080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3081% %
3082% %
3083% %
cristy3ed852e2009-09-05 21:47:34 +00003084% S e l e c t i v e B l u r I m a g e %
3085% %
3086% %
3087% %
3088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3089%
3090% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3091% It is similar to the unsharpen mask that sharpens everything with contrast
3092% above a certain threshold.
3093%
3094% The format of the SelectiveBlurImage method is:
3095%
3096% Image *SelectiveBlurImage(const Image *image,const double radius,
cristy1e7aa312011-09-10 20:01:36 +00003097% const double sigma,const double threshold,const double bias,
3098% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003099%
3100% A description of each parameter follows:
3101%
3102% o image: the image.
3103%
cristy3ed852e2009-09-05 21:47:34 +00003104% o radius: the radius of the Gaussian, in pixels, not counting the center
3105% pixel.
3106%
3107% o sigma: the standard deviation of the Gaussian, in pixels.
3108%
3109% o threshold: only pixels within this contrast threshold are included
3110% in the blur operation.
3111%
cristy1e7aa312011-09-10 20:01:36 +00003112% o bias: the bias.
3113%
cristy3ed852e2009-09-05 21:47:34 +00003114% o exception: return any errors or warnings in this structure.
3115%
3116*/
cristy4282c702011-11-21 00:01:06 +00003117MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3118 const double sigma,const double threshold,const double bias,
3119 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003120{
3121#define SelectiveBlurImageTag "SelectiveBlur/Image"
3122
cristy47e00502009-12-17 19:19:57 +00003123 CacheView
3124 *blur_view,
3125 *image_view;
3126
cristy3ed852e2009-09-05 21:47:34 +00003127 double
cristy3ed852e2009-09-05 21:47:34 +00003128 *kernel;
3129
3130 Image
3131 *blur_image;
3132
cristy3ed852e2009-09-05 21:47:34 +00003133 MagickBooleanType
3134 status;
3135
cristybb503372010-05-27 20:51:26 +00003136 MagickOffsetType
3137 progress;
3138
cristybb503372010-05-27 20:51:26 +00003139 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003140 i;
cristy3ed852e2009-09-05 21:47:34 +00003141
cristybb503372010-05-27 20:51:26 +00003142 size_t
cristy3ed852e2009-09-05 21:47:34 +00003143 width;
3144
cristybb503372010-05-27 20:51:26 +00003145 ssize_t
cristyc8523c12011-09-13 00:02:53 +00003146 center,
cristybb503372010-05-27 20:51:26 +00003147 j,
3148 u,
3149 v,
3150 y;
3151
cristy3ed852e2009-09-05 21:47:34 +00003152 /*
3153 Initialize blur image attributes.
3154 */
3155 assert(image != (Image *) NULL);
3156 assert(image->signature == MagickSignature);
3157 if (image->debug != MagickFalse)
3158 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3159 assert(exception != (ExceptionInfo *) NULL);
3160 assert(exception->signature == MagickSignature);
3161 width=GetOptimalKernelWidth1D(radius,sigma);
cristy8b49f382012-01-17 03:11:03 +00003162 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00003163 if (kernel == (double *) NULL)
3164 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003165 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003166 i=0;
cristy47e00502009-12-17 19:19:57 +00003167 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003168 {
cristy47e00502009-12-17 19:19:57 +00003169 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003170 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3171 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003172 }
3173 if (image->debug != MagickFalse)
3174 {
3175 char
3176 format[MaxTextExtent],
3177 *message;
3178
cristy117ff172010-08-15 21:35:32 +00003179 register const double
3180 *k;
3181
cristybb503372010-05-27 20:51:26 +00003182 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003183 u,
3184 v;
3185
cristy3ed852e2009-09-05 21:47:34 +00003186 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003187 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3188 width);
cristy3ed852e2009-09-05 21:47:34 +00003189 message=AcquireString("");
3190 k=kernel;
cristybb503372010-05-27 20:51:26 +00003191 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003192 {
3193 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003194 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003195 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003196 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003197 {
cristyb51dff52011-05-19 16:55:47 +00003198 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003199 (void) ConcatenateString(&message,format);
3200 }
3201 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3202 }
3203 message=DestroyString(message);
3204 }
cristy1e7aa312011-09-10 20:01:36 +00003205 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003206 if (blur_image == (Image *) NULL)
3207 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003208 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003209 {
cristy3ed852e2009-09-05 21:47:34 +00003210 blur_image=DestroyImage(blur_image);
3211 return((Image *) NULL);
3212 }
3213 /*
3214 Threshold blur image.
3215 */
3216 status=MagickTrue;
3217 progress=0;
cristyc8523c12011-09-13 00:02:53 +00003218 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3219 GetPixelChannels(image)*(width/2L));
cristy3ed852e2009-09-05 21:47:34 +00003220 image_view=AcquireCacheView(image);
3221 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003222#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003223 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003224#endif
cristybb503372010-05-27 20:51:26 +00003225 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003226 {
cristy4c08aed2011-07-01 19:47:50 +00003227 double
3228 contrast;
3229
cristy3ed852e2009-09-05 21:47:34 +00003230 MagickBooleanType
3231 sync;
3232
cristy4c08aed2011-07-01 19:47:50 +00003233 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003234 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003235
cristy4c08aed2011-07-01 19:47:50 +00003236 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003237 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003238
cristy117ff172010-08-15 21:35:32 +00003239 register ssize_t
3240 x;
3241
cristy3ed852e2009-09-05 21:47:34 +00003242 if (status == MagickFalse)
3243 continue;
cristy117ff172010-08-15 21:35:32 +00003244 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3245 (width/2L),image->columns+width,width,exception);
cristy8b49f382012-01-17 03:11:03 +00003246 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003247 exception);
cristy4c08aed2011-07-01 19:47:50 +00003248 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003249 {
3250 status=MagickFalse;
3251 continue;
3252 }
cristybb503372010-05-27 20:51:26 +00003253 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003254 {
cristybb503372010-05-27 20:51:26 +00003255 register ssize_t
cristy1e7aa312011-09-10 20:01:36 +00003256 i;
cristy3ed852e2009-09-05 21:47:34 +00003257
cristy1e7aa312011-09-10 20:01:36 +00003258 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3259 {
3260 MagickRealType
3261 alpha,
3262 gamma,
3263 intensity,
3264 pixel;
cristy117ff172010-08-15 21:35:32 +00003265
cristy1e7aa312011-09-10 20:01:36 +00003266 PixelChannel
3267 channel;
3268
3269 PixelTrait
3270 blur_traits,
3271 traits;
3272
3273 register const double
3274 *restrict k;
3275
3276 register const Quantum
3277 *restrict pixels;
3278
3279 register ssize_t
3280 u;
3281
3282 ssize_t
3283 v;
3284
cristye2a912b2011-12-05 20:02:07 +00003285 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003286 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003287 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3288 if ((traits == UndefinedPixelTrait) ||
3289 (blur_traits == UndefinedPixelTrait))
3290 continue;
3291 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003292 {
cristy0beccfa2011-09-25 20:47:53 +00003293 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003294 continue;
cristy3ed852e2009-09-05 21:47:34 +00003295 }
cristy1e7aa312011-09-10 20:01:36 +00003296 k=kernel;
3297 pixel=bias;
3298 pixels=p;
cristyc8523c12011-09-13 00:02:53 +00003299 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
cristy1e7aa312011-09-10 20:01:36 +00003300 gamma=0.0;
3301 if ((blur_traits & BlendPixelTrait) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003302 {
cristy1e7aa312011-09-10 20:01:36 +00003303 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003304 {
cristy1e7aa312011-09-10 20:01:36 +00003305 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003306 {
cristy1e7aa312011-09-10 20:01:36 +00003307 contrast=GetPixelIntensity(image,pixels)-intensity;
3308 if (fabs(contrast) < threshold)
3309 {
3310 pixel+=(*k)*pixels[i];
3311 gamma+=(*k);
3312 }
3313 k++;
3314 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003315 }
cristy1e7aa312011-09-10 20:01:36 +00003316 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003317 }
cristy1e7aa312011-09-10 20:01:36 +00003318 if (fabs((double) gamma) < MagickEpsilon)
3319 {
cristy0beccfa2011-09-25 20:47:53 +00003320 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003321 continue;
3322 }
3323 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003324 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003325 continue;
3326 }
3327 for (v=0; v < (ssize_t) width; v++)
3328 {
3329 for (u=0; u < (ssize_t) width; u++)
3330 {
3331 contrast=GetPixelIntensity(image,pixels)-intensity;
3332 if (fabs(contrast) < threshold)
3333 {
3334 alpha=(MagickRealType) (QuantumScale*
3335 GetPixelAlpha(image,pixels));
3336 pixel+=(*k)*alpha*pixels[i];
3337 gamma+=(*k)*alpha;
3338 }
3339 k++;
3340 pixels+=GetPixelChannels(image);
3341 }
3342 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003343 }
cristy1e7aa312011-09-10 20:01:36 +00003344 if (fabs((double) gamma) < MagickEpsilon)
3345 {
cristy0beccfa2011-09-25 20:47:53 +00003346 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003347 continue;
3348 }
3349 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003350 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003351 }
cristyed231572011-07-14 02:18:59 +00003352 p+=GetPixelChannels(image);
3353 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003354 }
3355 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3356 if (sync == MagickFalse)
3357 status=MagickFalse;
3358 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3359 {
3360 MagickBooleanType
3361 proceed;
3362
cristyb5d5f722009-11-04 03:03:49 +00003363#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003364 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003365#endif
3366 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3367 image->rows);
3368 if (proceed == MagickFalse)
3369 status=MagickFalse;
3370 }
3371 }
3372 blur_image->type=image->type;
3373 blur_view=DestroyCacheView(blur_view);
3374 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00003375 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00003376 if (status == MagickFalse)
3377 blur_image=DestroyImage(blur_image);
3378 return(blur_image);
3379}
3380
3381/*
3382%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3383% %
3384% %
3385% %
3386% S h a d e I m a g e %
3387% %
3388% %
3389% %
3390%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3391%
3392% ShadeImage() shines a distant light on an image to create a
3393% three-dimensional effect. You control the positioning of the light with
3394% azimuth and elevation; azimuth is measured in degrees off the x axis
3395% and elevation is measured in pixels above the Z axis.
3396%
3397% The format of the ShadeImage method is:
3398%
3399% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3400% const double azimuth,const double elevation,ExceptionInfo *exception)
3401%
3402% A description of each parameter follows:
3403%
3404% o image: the image.
3405%
3406% o gray: A value other than zero shades the intensity of each pixel.
3407%
3408% o azimuth, elevation: Define the light source direction.
3409%
3410% o exception: return any errors or warnings in this structure.
3411%
3412*/
3413MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3414 const double azimuth,const double elevation,ExceptionInfo *exception)
3415{
3416#define ShadeImageTag "Shade/Image"
3417
cristyc4c8d132010-01-07 01:58:38 +00003418 CacheView
3419 *image_view,
3420 *shade_view;
3421
cristy3ed852e2009-09-05 21:47:34 +00003422 Image
3423 *shade_image;
3424
cristy3ed852e2009-09-05 21:47:34 +00003425 MagickBooleanType
3426 status;
3427
cristybb503372010-05-27 20:51:26 +00003428 MagickOffsetType
3429 progress;
3430
cristy3ed852e2009-09-05 21:47:34 +00003431 PrimaryInfo
3432 light;
3433
cristybb503372010-05-27 20:51:26 +00003434 ssize_t
3435 y;
3436
cristy3ed852e2009-09-05 21:47:34 +00003437 /*
3438 Initialize shaded image attributes.
3439 */
3440 assert(image != (const Image *) NULL);
3441 assert(image->signature == MagickSignature);
3442 if (image->debug != MagickFalse)
3443 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3444 assert(exception != (ExceptionInfo *) NULL);
3445 assert(exception->signature == MagickSignature);
3446 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3447 if (shade_image == (Image *) NULL)
3448 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003449 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003450 {
cristy3ed852e2009-09-05 21:47:34 +00003451 shade_image=DestroyImage(shade_image);
3452 return((Image *) NULL);
3453 }
3454 /*
3455 Compute the light vector.
3456 */
3457 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3458 cos(DegreesToRadians(elevation));
3459 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3460 cos(DegreesToRadians(elevation));
3461 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3462 /*
3463 Shade image.
3464 */
3465 status=MagickTrue;
3466 progress=0;
3467 image_view=AcquireCacheView(image);
3468 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003469#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003470 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003471#endif
cristybb503372010-05-27 20:51:26 +00003472 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003473 {
3474 MagickRealType
3475 distance,
3476 normal_distance,
3477 shade;
3478
3479 PrimaryInfo
3480 normal;
3481
cristy4c08aed2011-07-01 19:47:50 +00003482 register const Quantum
cristy1e7aa312011-09-10 20:01:36 +00003483 *restrict center,
cristyc47d1f82009-11-26 01:44:43 +00003484 *restrict p,
cristy1e7aa312011-09-10 20:01:36 +00003485 *restrict post,
3486 *restrict pre;
cristy3ed852e2009-09-05 21:47:34 +00003487
cristy4c08aed2011-07-01 19:47:50 +00003488 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003489 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003490
cristy117ff172010-08-15 21:35:32 +00003491 register ssize_t
3492 x;
3493
cristy3ed852e2009-09-05 21:47:34 +00003494 if (status == MagickFalse)
3495 continue;
3496 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3497 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3498 exception);
cristy4c08aed2011-07-01 19:47:50 +00003499 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003500 {
3501 status=MagickFalse;
3502 continue;
3503 }
3504 /*
3505 Shade this row of pixels.
3506 */
3507 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristy1e7aa312011-09-10 20:01:36 +00003508 pre=p+GetPixelChannels(image);
3509 center=pre+(image->columns+2)*GetPixelChannels(image);
3510 post=center+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003511 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003512 {
cristy1e7aa312011-09-10 20:01:36 +00003513 register ssize_t
3514 i;
3515
cristy3ed852e2009-09-05 21:47:34 +00003516 /*
3517 Determine the surface normal and compute shading.
3518 */
cristy1e7aa312011-09-10 20:01:36 +00003519 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3520 GetPixelIntensity(image,center-GetPixelChannels(image))+
3521 GetPixelIntensity(image,post-GetPixelChannels(image))-
3522 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3523 GetPixelIntensity(image,center+GetPixelChannels(image))-
3524 GetPixelIntensity(image,post+GetPixelChannels(image)));
3525 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3526 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3527 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3528 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3529 GetPixelIntensity(image,pre+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003530 if ((normal.x == 0.0) && (normal.y == 0.0))
3531 shade=light.z;
3532 else
3533 {
3534 shade=0.0;
3535 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3536 if (distance > MagickEpsilon)
3537 {
3538 normal_distance=
3539 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3540 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3541 shade=distance/sqrt((double) normal_distance);
3542 }
3543 }
cristy1e7aa312011-09-10 20:01:36 +00003544 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3545 {
3546 PixelChannel
3547 channel;
3548
3549 PixelTrait
3550 shade_traits,
3551 traits;
3552
cristye2a912b2011-12-05 20:02:07 +00003553 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003554 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003555 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3556 if ((traits == UndefinedPixelTrait) ||
3557 (shade_traits == UndefinedPixelTrait))
3558 continue;
3559 if ((shade_traits & CopyPixelTrait) != 0)
3560 {
cristy0beccfa2011-09-25 20:47:53 +00003561 SetPixelChannel(shade_image,channel,center[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003562 continue;
3563 }
3564 if (gray != MagickFalse)
3565 {
cristy0beccfa2011-09-25 20:47:53 +00003566 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
cristy1e7aa312011-09-10 20:01:36 +00003567 continue;
3568 }
cristy0beccfa2011-09-25 20:47:53 +00003569 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3570 center[i]),q);
cristy1e7aa312011-09-10 20:01:36 +00003571 }
3572 pre+=GetPixelChannels(image);
3573 center+=GetPixelChannels(image);
3574 post+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003575 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003576 }
3577 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3578 status=MagickFalse;
3579 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3580 {
3581 MagickBooleanType
3582 proceed;
3583
cristyb5d5f722009-11-04 03:03:49 +00003584#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003585 #pragma omp critical (MagickCore_ShadeImage)
3586#endif
3587 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3588 if (proceed == MagickFalse)
3589 status=MagickFalse;
3590 }
3591 }
3592 shade_view=DestroyCacheView(shade_view);
3593 image_view=DestroyCacheView(image_view);
3594 if (status == MagickFalse)
3595 shade_image=DestroyImage(shade_image);
3596 return(shade_image);
3597}
3598
3599/*
3600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3601% %
3602% %
3603% %
3604% S h a r p e n I m a g e %
3605% %
3606% %
3607% %
3608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3609%
3610% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3611% operator of the given radius and standard deviation (sigma). For
3612% reasonable results, radius should be larger than sigma. Use a radius of 0
3613% and SharpenImage() selects a suitable radius for you.
3614%
3615% Using a separable kernel would be faster, but the negative weights cancel
3616% out on the corners of the kernel producing often undesirable ringing in the
3617% filtered result; this can be avoided by using a 2D gaussian shaped image
3618% sharpening kernel instead.
3619%
3620% The format of the SharpenImage method is:
3621%
3622% Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003623% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003624%
3625% A description of each parameter follows:
3626%
3627% o image: the image.
3628%
cristy3ed852e2009-09-05 21:47:34 +00003629% o radius: the radius of the Gaussian, in pixels, not counting the center
3630% pixel.
3631%
3632% o sigma: the standard deviation of the Laplacian, in pixels.
3633%
cristy05c0c9a2011-09-05 23:16:13 +00003634% o bias: bias.
3635%
cristy3ed852e2009-09-05 21:47:34 +00003636% o exception: return any errors or warnings in this structure.
3637%
3638*/
cristy3ed852e2009-09-05 21:47:34 +00003639MagickExport Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003640 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003641{
cristy3ed852e2009-09-05 21:47:34 +00003642 double
cristy47e00502009-12-17 19:19:57 +00003643 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003644
3645 Image
3646 *sharp_image;
3647
cristy41cbe682011-07-15 19:12:37 +00003648 KernelInfo
3649 *kernel_info;
3650
cristybb503372010-05-27 20:51:26 +00003651 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003652 i;
3653
cristybb503372010-05-27 20:51:26 +00003654 size_t
cristy3ed852e2009-09-05 21:47:34 +00003655 width;
3656
cristy117ff172010-08-15 21:35:32 +00003657 ssize_t
3658 j,
3659 u,
3660 v;
3661
cristy3ed852e2009-09-05 21:47:34 +00003662 assert(image != (const Image *) NULL);
3663 assert(image->signature == MagickSignature);
3664 if (image->debug != MagickFalse)
3665 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3666 assert(exception != (ExceptionInfo *) NULL);
3667 assert(exception->signature == MagickSignature);
3668 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003669 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003670 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003671 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003672 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3673 kernel_info->width=width;
3674 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00003675 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
cristy41cbe682011-07-15 19:12:37 +00003676 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00003677 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3678 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3679 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00003680 {
3681 kernel_info=DestroyKernelInfo(kernel_info);
3682 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3683 }
cristy3ed852e2009-09-05 21:47:34 +00003684 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003685 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003686 i=0;
3687 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003688 {
cristy47e00502009-12-17 19:19:57 +00003689 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003690 {
cristy41cbe682011-07-15 19:12:37 +00003691 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3692 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3693 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003694 i++;
3695 }
3696 }
cristy41cbe682011-07-15 19:12:37 +00003697 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003698 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003699 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003700 return(sharp_image);
3701}
3702
3703/*
3704%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3705% %
3706% %
3707% %
3708% S p r e a d I m a g e %
3709% %
3710% %
3711% %
3712%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3713%
3714% SpreadImage() is a special effects method that randomly displaces each
3715% pixel in a block defined by the radius parameter.
3716%
3717% The format of the SpreadImage method is:
3718%
3719% Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003720% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003721%
3722% A description of each parameter follows:
3723%
3724% o image: the image.
3725%
cristy5c4e2582011-09-11 19:21:03 +00003726% o radius: choose a random pixel in a neighborhood of this extent.
3727%
3728% o method: the pixel interpolation method.
cristy3ed852e2009-09-05 21:47:34 +00003729%
3730% o exception: return any errors or warnings in this structure.
3731%
3732*/
3733MagickExport Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003734 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003735{
3736#define SpreadImageTag "Spread/Image"
3737
cristyfa112112010-01-04 17:48:07 +00003738 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003739 *image_view,
3740 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003741
cristy3ed852e2009-09-05 21:47:34 +00003742 Image
3743 *spread_image;
3744
cristy3ed852e2009-09-05 21:47:34 +00003745 MagickBooleanType
3746 status;
3747
cristybb503372010-05-27 20:51:26 +00003748 MagickOffsetType
3749 progress;
3750
cristy3ed852e2009-09-05 21:47:34 +00003751 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003752 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003753
cristybb503372010-05-27 20:51:26 +00003754 size_t
cristy3ed852e2009-09-05 21:47:34 +00003755 width;
3756
cristybb503372010-05-27 20:51:26 +00003757 ssize_t
3758 y;
3759
cristy3ed852e2009-09-05 21:47:34 +00003760 /*
3761 Initialize spread image attributes.
3762 */
3763 assert(image != (Image *) NULL);
3764 assert(image->signature == MagickSignature);
3765 if (image->debug != MagickFalse)
3766 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3767 assert(exception != (ExceptionInfo *) NULL);
3768 assert(exception->signature == MagickSignature);
3769 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3770 exception);
3771 if (spread_image == (Image *) NULL)
3772 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003773 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003774 {
cristy3ed852e2009-09-05 21:47:34 +00003775 spread_image=DestroyImage(spread_image);
3776 return((Image *) NULL);
3777 }
3778 /*
3779 Spread image.
3780 */
3781 status=MagickTrue;
3782 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003783 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003784 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003785 image_view=AcquireCacheView(image);
3786 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003787#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy78778cb2012-01-17 14:48:47 +00003788 #pragma omp parallel for schedule(static,8) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003789#endif
cristybe82ad52011-09-06 01:17:20 +00003790 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003791 {
cristy5c9e6f22010-09-17 17:31:01 +00003792 const int
3793 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003794
cristybe82ad52011-09-06 01:17:20 +00003795 register const Quantum
3796 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003797
cristy4c08aed2011-07-01 19:47:50 +00003798 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003799 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003800
cristy117ff172010-08-15 21:35:32 +00003801 register ssize_t
3802 x;
3803
cristy3ed852e2009-09-05 21:47:34 +00003804 if (status == MagickFalse)
3805 continue;
cristybe82ad52011-09-06 01:17:20 +00003806 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy9f7e7cb2011-03-26 00:49:57 +00003807 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003808 exception);
cristybe82ad52011-09-06 01:17:20 +00003809 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003810 {
3811 status=MagickFalse;
3812 continue;
3813 }
cristybe82ad52011-09-06 01:17:20 +00003814 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003815 {
cristybe82ad52011-09-06 01:17:20 +00003816 PointInfo
3817 point;
3818
cristybe82ad52011-09-06 01:17:20 +00003819 point.x=GetPseudoRandomValue(random_info[id]);
3820 point.y=GetPseudoRandomValue(random_info[id]);
cristy5c4e2582011-09-11 19:21:03 +00003821 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3822 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
cristyed231572011-07-14 02:18:59 +00003823 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003824 }
cristy9f7e7cb2011-03-26 00:49:57 +00003825 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003826 status=MagickFalse;
3827 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3828 {
3829 MagickBooleanType
3830 proceed;
3831
cristyb557a152011-02-22 12:14:30 +00003832#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003833 #pragma omp critical (MagickCore_SpreadImage)
3834#endif
3835 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3836 if (proceed == MagickFalse)
3837 status=MagickFalse;
3838 }
3839 }
cristy9f7e7cb2011-03-26 00:49:57 +00003840 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003841 image_view=DestroyCacheView(image_view);
3842 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003843 return(spread_image);
3844}
3845
3846/*
3847%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3848% %
3849% %
3850% %
3851% U n s h a r p M a s k I m a g e %
3852% %
3853% %
3854% %
3855%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3856%
3857% UnsharpMaskImage() sharpens one or more image channels. We convolve the
3858% image with a Gaussian operator of the given radius and standard deviation
3859% (sigma). For reasonable results, radius should be larger than sigma. Use a
3860% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3861%
3862% The format of the UnsharpMaskImage method is:
3863%
3864% Image *UnsharpMaskImage(const Image *image,const double radius,
3865% const double sigma,const double amount,const double threshold,
3866% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003867%
3868% A description of each parameter follows:
3869%
3870% o image: the image.
3871%
cristy3ed852e2009-09-05 21:47:34 +00003872% o radius: the radius of the Gaussian, in pixels, not counting the center
3873% pixel.
3874%
3875% o sigma: the standard deviation of the Gaussian, in pixels.
3876%
3877% o amount: the percentage of the difference between the original and the
3878% blur image that is added back into the original.
3879%
3880% o threshold: the threshold in pixels needed to apply the diffence amount.
3881%
3882% o exception: return any errors or warnings in this structure.
3883%
3884*/
cristy31bb6272011-11-20 23:57:25 +00003885MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3886 const double sigma,const double amount,const double threshold,
3887 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003888{
3889#define SharpenImageTag "Sharpen/Image"
3890
cristyc4c8d132010-01-07 01:58:38 +00003891 CacheView
3892 *image_view,
3893 *unsharp_view;
3894
cristy3ed852e2009-09-05 21:47:34 +00003895 Image
3896 *unsharp_image;
3897
cristy3ed852e2009-09-05 21:47:34 +00003898 MagickBooleanType
3899 status;
3900
cristybb503372010-05-27 20:51:26 +00003901 MagickOffsetType
3902 progress;
3903
cristy3ed852e2009-09-05 21:47:34 +00003904 MagickRealType
3905 quantum_threshold;
3906
cristybb503372010-05-27 20:51:26 +00003907 ssize_t
3908 y;
3909
cristy3ed852e2009-09-05 21:47:34 +00003910 assert(image != (const Image *) NULL);
3911 assert(image->signature == MagickSignature);
3912 if (image->debug != MagickFalse)
3913 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3914 assert(exception != (ExceptionInfo *) NULL);
cristy05c0c9a2011-09-05 23:16:13 +00003915 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00003916 if (unsharp_image == (Image *) NULL)
3917 return((Image *) NULL);
3918 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3919 /*
3920 Unsharp-mask image.
3921 */
3922 status=MagickTrue;
3923 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003924 image_view=AcquireCacheView(image);
3925 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00003926#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003927 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003928#endif
cristybb503372010-05-27 20:51:26 +00003929 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003930 {
cristy4c08aed2011-07-01 19:47:50 +00003931 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003932 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003933
cristy4c08aed2011-07-01 19:47:50 +00003934 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003935 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003936
cristy117ff172010-08-15 21:35:32 +00003937 register ssize_t
3938 x;
3939
cristy3ed852e2009-09-05 21:47:34 +00003940 if (status == MagickFalse)
3941 continue;
3942 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy8b49f382012-01-17 03:11:03 +00003943 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003944 exception);
cristy4c08aed2011-07-01 19:47:50 +00003945 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003946 {
3947 status=MagickFalse;
3948 continue;
3949 }
cristybb503372010-05-27 20:51:26 +00003950 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003951 {
cristy7f3a0d12011-09-05 23:27:59 +00003952 register ssize_t
3953 i;
3954
3955 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3956 {
3957 MagickRealType
3958 pixel;
3959
3960 PixelChannel
3961 channel;
3962
3963 PixelTrait
3964 traits,
3965 unsharp_traits;
3966
cristye2a912b2011-12-05 20:02:07 +00003967 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003968 traits=GetPixelChannelMapTraits(image,channel);
cristy7f3a0d12011-09-05 23:27:59 +00003969 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3970 if ((traits == UndefinedPixelTrait) ||
3971 (unsharp_traits == UndefinedPixelTrait))
3972 continue;
3973 if ((unsharp_traits & CopyPixelTrait) != 0)
3974 {
cristy0beccfa2011-09-25 20:47:53 +00003975 SetPixelChannel(unsharp_image,channel,p[i],q);
cristy7f3a0d12011-09-05 23:27:59 +00003976 continue;
3977 }
cristy0beccfa2011-09-25 20:47:53 +00003978 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
cristy7f3a0d12011-09-05 23:27:59 +00003979 if (fabs(2.0*pixel) < quantum_threshold)
3980 pixel=(MagickRealType) p[i];
3981 else
3982 pixel=(MagickRealType) p[i]+amount*pixel;
cristy0beccfa2011-09-25 20:47:53 +00003983 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
cristy7f3a0d12011-09-05 23:27:59 +00003984 }
cristyed231572011-07-14 02:18:59 +00003985 p+=GetPixelChannels(image);
3986 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00003987 }
3988 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3989 status=MagickFalse;
3990 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3991 {
3992 MagickBooleanType
3993 proceed;
3994
cristyb5d5f722009-11-04 03:03:49 +00003995#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003996 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00003997#endif
3998 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3999 if (proceed == MagickFalse)
4000 status=MagickFalse;
4001 }
4002 }
4003 unsharp_image->type=image->type;
4004 unsharp_view=DestroyCacheView(unsharp_view);
4005 image_view=DestroyCacheView(image_view);
4006 if (status == MagickFalse)
4007 unsharp_image=DestroyImage(unsharp_image);
4008 return(unsharp_image);
4009}