blob: 3127dcd4c0f2d6cb363b2b929c3dda27bc7bf95d [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;
cristyd09f8802012-02-04 16:44:10 +0000347 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
348 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;
cristyd09f8802012-02-04 16:44:10 +0000381 if (((blur_traits & CopyPixelTrait) != 0) ||
382 (GetPixelMask(image,q) != 0))
cristy4c11c2b2011-09-05 20:17:07 +0000383 {
cristy0beccfa2011-09-25 20:47:53 +0000384 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000385 continue;
386 }
387 k=kernel[j];
388 pixels=p;
389 pixel=bias;
390 gamma=0.0;
391 if ((blur_traits & BlendPixelTrait) == 0)
392 {
393 /*
394 No alpha blending.
395 */
396 for (v=0; v < (ssize_t) (width-j); v++)
397 {
398 for (u=0; u < (ssize_t) (width-j); u++)
399 {
400 pixel+=(*k)*pixels[i];
401 gamma+=(*k);
402 k++;
403 pixels+=GetPixelChannels(image);
404 }
405 }
406 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000407 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000408 continue;
409 }
410 /*
411 Alpha blending.
412 */
413 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000414 {
cristy4c11c2b2011-09-05 20:17:07 +0000415 for (u=0; u < (ssize_t) (width-j); u++)
416 {
417 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
418 pixel+=(*k)*alpha*pixels[i];
419 gamma+=(*k)*alpha;
420 k++;
421 pixels+=GetPixelChannels(image);
422 }
cristy3ed852e2009-09-05 21:47:34 +0000423 }
cristy4c11c2b2011-09-05 20:17:07 +0000424 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000425 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000426 }
cristyed231572011-07-14 02:18:59 +0000427 q+=GetPixelChannels(blur_image);
428 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000429 }
430 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
431 status=MagickFalse;
432 if (image->progress_monitor != (MagickProgressMonitor) NULL)
433 {
434 MagickBooleanType
435 proceed;
436
cristyb5d5f722009-11-04 03:03:49 +0000437#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +0000438 #pragma omp critical (MagickCore_AdaptiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +0000439#endif
440 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
441 image->rows);
442 if (proceed == MagickFalse)
443 status=MagickFalse;
444 }
445 }
446 blur_image->type=image->type;
447 blur_view=DestroyCacheView(blur_view);
448 edge_view=DestroyCacheView(edge_view);
449 image_view=DestroyCacheView(image_view);
450 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000451 for (i=0; i < (ssize_t) width; i+=2)
cristyf5d0f3e2012-01-17 03:20:33 +0000452 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000453 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000454 if (status == MagickFalse)
455 blur_image=DestroyImage(blur_image);
456 return(blur_image);
457}
458
459/*
460%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
461% %
462% %
463% %
464% A d a p t i v e S h a r p e n I m a g e %
465% %
466% %
467% %
468%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469%
470% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
471% intensely near image edges and less intensely far from edges. We sharpen the
472% image with a Gaussian operator of the given radius and standard deviation
473% (sigma). For reasonable results, radius should be larger than sigma. Use a
474% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
475%
476% The format of the AdaptiveSharpenImage method is:
477%
478% Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000479% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000480%
481% A description of each parameter follows:
482%
483% o image: the image.
484%
cristy3ed852e2009-09-05 21:47:34 +0000485% o radius: the radius of the Gaussian, in pixels, not counting the center
486% pixel.
487%
488% o sigma: the standard deviation of the Laplacian, in pixels.
489%
cristy4c11c2b2011-09-05 20:17:07 +0000490% o bias: the bias.
491%
cristy3ed852e2009-09-05 21:47:34 +0000492% o exception: return any errors or warnings in this structure.
493%
494*/
cristy3ed852e2009-09-05 21:47:34 +0000495MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000496 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000497{
cristy3ed852e2009-09-05 21:47:34 +0000498#define AdaptiveSharpenImageTag "Convolve/Image"
499#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
500
cristyc4c8d132010-01-07 01:58:38 +0000501 CacheView
502 *sharp_view,
503 *edge_view,
504 *image_view;
505
cristy3ed852e2009-09-05 21:47:34 +0000506 double
cristy47e00502009-12-17 19:19:57 +0000507 **kernel,
508 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000509
510 Image
511 *sharp_image,
512 *edge_image,
513 *gaussian_image;
514
cristy3ed852e2009-09-05 21:47:34 +0000515 MagickBooleanType
516 status;
517
cristybb503372010-05-27 20:51:26 +0000518 MagickOffsetType
519 progress;
520
cristybb503372010-05-27 20:51:26 +0000521 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000522 i;
cristy3ed852e2009-09-05 21:47:34 +0000523
cristybb503372010-05-27 20:51:26 +0000524 size_t
cristy3ed852e2009-09-05 21:47:34 +0000525 width;
526
cristybb503372010-05-27 20:51:26 +0000527 ssize_t
528 j,
529 k,
530 u,
531 v,
532 y;
533
cristy3ed852e2009-09-05 21:47:34 +0000534 assert(image != (const Image *) NULL);
535 assert(image->signature == MagickSignature);
536 if (image->debug != MagickFalse)
537 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
538 assert(exception != (ExceptionInfo *) NULL);
539 assert(exception->signature == MagickSignature);
540 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
541 if (sharp_image == (Image *) NULL)
542 return((Image *) NULL);
543 if (fabs(sigma) <= MagickEpsilon)
544 return(sharp_image);
cristy574cc262011-08-05 01:23:58 +0000545 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000546 {
cristy3ed852e2009-09-05 21:47:34 +0000547 sharp_image=DestroyImage(sharp_image);
548 return((Image *) NULL);
549 }
550 /*
551 Edge detect the image brighness channel, level, sharp, and level again.
552 */
cristy8ae632d2011-09-05 17:29:53 +0000553 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000554 if (edge_image == (Image *) NULL)
555 {
556 sharp_image=DestroyImage(sharp_image);
557 return((Image *) NULL);
558 }
cristy051718b2011-08-28 22:49:25 +0000559 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristyd89705a2012-01-20 02:52:24 +0000560 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000561 if (gaussian_image != (Image *) NULL)
562 {
563 edge_image=DestroyImage(edge_image);
564 edge_image=gaussian_image;
565 }
cristy051718b2011-08-28 22:49:25 +0000566 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000567 /*
568 Create a set of kernels from maximum (radius,sigma) to minimum.
569 */
570 width=GetOptimalKernelWidth2D(radius,sigma);
cristy219a63a2012-01-17 12:30:33 +0000571 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000572 if (kernel == (double **) NULL)
573 {
574 edge_image=DestroyImage(edge_image);
575 sharp_image=DestroyImage(sharp_image);
576 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
577 }
578 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000579 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000580 {
cristy59d1f6d2012-01-17 03:17:40 +0000581 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
cristy3ed852e2009-09-05 21:47:34 +0000582 sizeof(**kernel));
583 if (kernel[i] == (double *) NULL)
584 break;
cristy47e00502009-12-17 19:19:57 +0000585 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000586 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000587 k=0;
588 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000589 {
cristy47e00502009-12-17 19:19:57 +0000590 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000591 {
cristy4205a3c2010-09-12 20:19:59 +0000592 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
593 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000594 normalize+=kernel[i][k];
595 k++;
cristy3ed852e2009-09-05 21:47:34 +0000596 }
597 }
cristy3ed852e2009-09-05 21:47:34 +0000598 if (fabs(normalize) <= MagickEpsilon)
599 normalize=1.0;
600 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000601 for (k=0; k < (j*j); k++)
602 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000603 }
cristybb503372010-05-27 20:51:26 +0000604 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000605 {
606 for (i-=2; i >= 0; i-=2)
cristy59d1f6d2012-01-17 03:17:40 +0000607 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000608 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000609 edge_image=DestroyImage(edge_image);
610 sharp_image=DestroyImage(sharp_image);
611 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
612 }
613 /*
614 Adaptively sharpen image.
615 */
616 status=MagickTrue;
617 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000618 image_view=AcquireCacheView(image);
619 edge_view=AcquireCacheView(edge_image);
620 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000621#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000622 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000623#endif
cristybb503372010-05-27 20:51:26 +0000624 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000625 {
cristy4c08aed2011-07-01 19:47:50 +0000626 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000627 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000628
cristy4c08aed2011-07-01 19:47:50 +0000629 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000630 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000631
cristy117ff172010-08-15 21:35:32 +0000632 register ssize_t
633 x;
634
cristy3ed852e2009-09-05 21:47:34 +0000635 if (status == MagickFalse)
636 continue;
637 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
638 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
639 exception);
cristy4c08aed2011-07-01 19:47:50 +0000640 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000641 {
642 status=MagickFalse;
643 continue;
644 }
cristybb503372010-05-27 20:51:26 +0000645 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000646 {
cristy4c11c2b2011-09-05 20:17:07 +0000647 register const Quantum
648 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000649
cristybb503372010-05-27 20:51:26 +0000650 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000651 i;
cristy3ed852e2009-09-05 21:47:34 +0000652
cristy4c11c2b2011-09-05 20:17:07 +0000653 ssize_t
654 center,
655 j;
656
657 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000658 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000659 if (j < 0)
660 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000661 else
cristy4c11c2b2011-09-05 20:17:07 +0000662 if (j > (ssize_t) width)
663 j=(ssize_t) width;
664 if ((j & 0x01) != 0)
665 j--;
666 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
667 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000668 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000669 break;
cristyd09f8802012-02-04 16:44:10 +0000670 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
671 GetPixelChannels(image)*((width-j)/2);
cristyc94ba6f2012-01-29 23:19:58 +0000672 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000673 {
cristy4c11c2b2011-09-05 20:17:07 +0000674 MagickRealType
675 alpha,
676 gamma,
677 pixel;
678
679 PixelChannel
680 channel;
681
682 PixelTrait
683 sharp_traits,
684 traits;
685
686 register const double
687 *restrict k;
688
689 register const Quantum
690 *restrict pixels;
691
692 register ssize_t
693 u;
694
695 ssize_t
696 v;
697
cristye2a912b2011-12-05 20:02:07 +0000698 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000699 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000700 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
701 if ((traits == UndefinedPixelTrait) ||
702 (sharp_traits == UndefinedPixelTrait))
703 continue;
cristyd09f8802012-02-04 16:44:10 +0000704 if (((sharp_traits & CopyPixelTrait) != 0) ||
705 (GetPixelMask(image,q) != 0))
cristy4c11c2b2011-09-05 20:17:07 +0000706 {
cristy0beccfa2011-09-25 20:47:53 +0000707 SetPixelChannel(sharp_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000708 continue;
709 }
710 k=kernel[j];
711 pixels=p;
712 pixel=bias;
713 gamma=0.0;
714 if ((sharp_traits & BlendPixelTrait) == 0)
715 {
716 /*
717 No alpha blending.
718 */
719 for (v=0; v < (ssize_t) (width-j); v++)
720 {
721 for (u=0; u < (ssize_t) (width-j); u++)
722 {
723 pixel+=(*k)*pixels[i];
724 gamma+=(*k);
725 k++;
726 pixels+=GetPixelChannels(image);
727 }
728 }
729 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000730 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000731 continue;
732 }
733 /*
734 Alpha blending.
735 */
736 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000737 {
cristy4c11c2b2011-09-05 20:17:07 +0000738 for (u=0; u < (ssize_t) (width-j); u++)
739 {
740 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
741 pixel+=(*k)*alpha*pixels[i];
742 gamma+=(*k)*alpha;
743 k++;
744 pixels+=GetPixelChannels(image);
745 }
cristy3ed852e2009-09-05 21:47:34 +0000746 }
cristy4c11c2b2011-09-05 20:17:07 +0000747 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000748 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000749 }
cristyed231572011-07-14 02:18:59 +0000750 q+=GetPixelChannels(sharp_image);
751 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000752 }
753 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
754 status=MagickFalse;
755 if (image->progress_monitor != (MagickProgressMonitor) NULL)
756 {
757 MagickBooleanType
758 proceed;
759
cristyb5d5f722009-11-04 03:03:49 +0000760#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +0000761 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000762#endif
763 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
764 image->rows);
765 if (proceed == MagickFalse)
766 status=MagickFalse;
767 }
768 }
769 sharp_image->type=image->type;
770 sharp_view=DestroyCacheView(sharp_view);
771 edge_view=DestroyCacheView(edge_view);
772 image_view=DestroyCacheView(image_view);
773 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000774 for (i=0; i < (ssize_t) width; i+=2)
cristy59d1f6d2012-01-17 03:17:40 +0000775 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
cristy219a63a2012-01-17 12:30:33 +0000776 kernel=(double **) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +0000777 if (status == MagickFalse)
778 sharp_image=DestroyImage(sharp_image);
779 return(sharp_image);
780}
781
782/*
783%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784% %
785% %
786% %
787% B l u r I m a g e %
788% %
789% %
790% %
791%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792%
793% BlurImage() blurs an image. We convolve the image with a Gaussian operator
794% of the given radius and standard deviation (sigma). For reasonable results,
795% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
796% selects a suitable radius for you.
797%
798% BlurImage() differs from GaussianBlurImage() in that it uses a separable
799% kernel which is faster but mathematically equivalent to the non-separable
800% kernel.
801%
802% The format of the BlurImage method is:
803%
804% Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000805% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000806%
807% A description of each parameter follows:
808%
809% o image: the image.
810%
cristy3ed852e2009-09-05 21:47:34 +0000811% o radius: the radius of the Gaussian, in pixels, not counting the center
812% pixel.
813%
814% o sigma: the standard deviation of the Gaussian, in pixels.
815%
cristy05c0c9a2011-09-05 23:16:13 +0000816% o bias: the bias.
817%
cristy3ed852e2009-09-05 21:47:34 +0000818% o exception: return any errors or warnings in this structure.
819%
820*/
821
cristybb503372010-05-27 20:51:26 +0000822static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000823{
cristy3ed852e2009-09-05 21:47:34 +0000824 double
cristy47e00502009-12-17 19:19:57 +0000825 *kernel,
826 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000827
cristy117ff172010-08-15 21:35:32 +0000828 register ssize_t
829 i;
830
cristybb503372010-05-27 20:51:26 +0000831 ssize_t
cristy47e00502009-12-17 19:19:57 +0000832 j,
833 k;
cristy3ed852e2009-09-05 21:47:34 +0000834
cristy3ed852e2009-09-05 21:47:34 +0000835 /*
836 Generate a 1-D convolution kernel.
837 */
838 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +0000839 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +0000840 if (kernel == (double *) NULL)
841 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000842 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000843 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000844 i=0;
845 for (k=(-j); k <= j; k++)
846 {
cristy4205a3c2010-09-12 20:19:59 +0000847 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
848 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000849 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000850 i++;
851 }
cristybb503372010-05-27 20:51:26 +0000852 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000853 kernel[i]/=normalize;
854 return(kernel);
855}
856
cristyf4ad9df2011-07-08 16:49:03 +0000857MagickExport Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000858 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000859{
860#define BlurImageTag "Blur/Image"
861
cristyc4c8d132010-01-07 01:58:38 +0000862 CacheView
863 *blur_view,
864 *image_view;
865
cristy3ed852e2009-09-05 21:47:34 +0000866 double
867 *kernel;
868
869 Image
870 *blur_image;
871
cristy3ed852e2009-09-05 21:47:34 +0000872 MagickBooleanType
873 status;
874
cristybb503372010-05-27 20:51:26 +0000875 MagickOffsetType
876 progress;
877
cristybb503372010-05-27 20:51:26 +0000878 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000879 i;
880
cristybb503372010-05-27 20:51:26 +0000881 size_t
cristy3ed852e2009-09-05 21:47:34 +0000882 width;
883
cristybb503372010-05-27 20:51:26 +0000884 ssize_t
cristyb41a1172011-09-06 00:55:14 +0000885 center,
cristybb503372010-05-27 20:51:26 +0000886 x,
887 y;
888
cristy3ed852e2009-09-05 21:47:34 +0000889 /*
890 Initialize blur image attributes.
891 */
892 assert(image != (Image *) NULL);
893 assert(image->signature == MagickSignature);
894 if (image->debug != MagickFalse)
895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
896 assert(exception != (ExceptionInfo *) NULL);
897 assert(exception->signature == MagickSignature);
cristyd25c77e2011-09-06 00:10:24 +0000898 blur_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000899 if (blur_image == (Image *) NULL)
900 return((Image *) NULL);
901 if (fabs(sigma) <= MagickEpsilon)
902 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000903 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000904 {
cristy3ed852e2009-09-05 21:47:34 +0000905 blur_image=DestroyImage(blur_image);
906 return((Image *) NULL);
907 }
908 width=GetOptimalKernelWidth1D(radius,sigma);
909 kernel=GetBlurKernel(width,sigma);
910 if (kernel == (double *) NULL)
911 {
912 blur_image=DestroyImage(blur_image);
913 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
914 }
915 if (image->debug != MagickFalse)
916 {
917 char
918 format[MaxTextExtent],
919 *message;
920
921 register const double
922 *k;
923
924 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristyef1f9072012-01-20 14:43:06 +0000925 " blur image with kernel width %.20g:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000926 message=AcquireString("");
927 k=kernel;
cristybb503372010-05-27 20:51:26 +0000928 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000929 {
930 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000931 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000932 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000933 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000934 (void) ConcatenateString(&message,format);
935 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
936 }
937 message=DestroyString(message);
938 }
939 /*
940 Blur rows.
941 */
942 status=MagickTrue;
943 progress=0;
cristyb41a1172011-09-06 00:55:14 +0000944 center=(ssize_t) GetPixelChannels(image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +0000945 image_view=AcquireCacheView(image);
946 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000947#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000948 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000949#endif
cristyb41a1172011-09-06 00:55:14 +0000950 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000951 {
cristy4c08aed2011-07-01 19:47:50 +0000952 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000953 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000954
cristy4c08aed2011-07-01 19:47:50 +0000955 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000956 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000957
cristy117ff172010-08-15 21:35:32 +0000958 register ssize_t
959 x;
960
cristy3ed852e2009-09-05 21:47:34 +0000961 if (status == MagickFalse)
962 continue;
cristy117ff172010-08-15 21:35:32 +0000963 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
964 image->columns+width,1,exception);
cristy8b49f382012-01-17 03:11:03 +0000965 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000966 exception);
cristy4c08aed2011-07-01 19:47:50 +0000967 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000968 {
969 status=MagickFalse;
970 continue;
971 }
cristyb41a1172011-09-06 00:55:14 +0000972 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000973 {
cristybb503372010-05-27 20:51:26 +0000974 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000975 i;
976
cristyb41a1172011-09-06 00:55:14 +0000977 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
978 {
979 MagickRealType
980 alpha,
981 gamma,
982 pixel;
cristyd25c77e2011-09-06 00:10:24 +0000983
cristyb41a1172011-09-06 00:55:14 +0000984 PixelChannel
985 channel;
986
987 PixelTrait
988 blur_traits,
989 traits;
990
991 register const double
992 *restrict k;
993
994 register const Quantum
995 *restrict pixels;
996
997 register ssize_t
998 u;
999
cristye2a912b2011-12-05 20:02:07 +00001000 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001001 traits=GetPixelChannelMapTraits(image,channel);
cristyb41a1172011-09-06 00:55:14 +00001002 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1003 if ((traits == UndefinedPixelTrait) ||
1004 (blur_traits == UndefinedPixelTrait))
1005 continue;
cristyd09f8802012-02-04 16:44:10 +00001006 if (((blur_traits & CopyPixelTrait) != 0) ||
1007 (GetPixelMask(image,p) != 0))
cristyd25c77e2011-09-06 00:10:24 +00001008 {
cristy0beccfa2011-09-25 20:47:53 +00001009 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001010 continue;
cristyd25c77e2011-09-06 00:10:24 +00001011 }
cristyb41a1172011-09-06 00:55:14 +00001012 k=kernel;
1013 pixels=p;
1014 pixel=0.0;
1015 if ((blur_traits & BlendPixelTrait) == 0)
1016 {
1017 /*
1018 No alpha blending.
1019 */
1020 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001021 {
cristyb41a1172011-09-06 00:55:14 +00001022 pixel+=(*k)*pixels[i];
1023 k++;
1024 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001025 }
cristy0beccfa2011-09-25 20:47:53 +00001026 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001027 continue;
1028 }
1029 /*
1030 Alpha blending.
1031 */
1032 gamma=0.0;
1033 for (u=0; u < (ssize_t) width; u++)
1034 {
1035 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1036 pixel+=(*k)*alpha*pixels[i];
1037 gamma+=(*k)*alpha;
1038 k++;
1039 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001040 }
cristyb41a1172011-09-06 00:55:14 +00001041 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001042 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001043 }
cristyed231572011-07-14 02:18:59 +00001044 p+=GetPixelChannels(image);
1045 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001046 }
1047 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1048 status=MagickFalse;
1049 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1050 {
1051 MagickBooleanType
1052 proceed;
1053
cristyb5d5f722009-11-04 03:03:49 +00001054#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyef1f9072012-01-20 14:43:06 +00001055 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001056#endif
1057 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1058 blur_image->columns);
1059 if (proceed == MagickFalse)
1060 status=MagickFalse;
1061 }
1062 }
1063 blur_view=DestroyCacheView(blur_view);
1064 image_view=DestroyCacheView(image_view);
1065 /*
1066 Blur columns.
1067 */
cristyef1f9072012-01-20 14:43:06 +00001068 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +00001069 image_view=AcquireCacheView(blur_image);
1070 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001071#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001072 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001073#endif
cristyb6dc4b72011-12-10 23:29:53 +00001074 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001075 {
cristy4c08aed2011-07-01 19:47:50 +00001076 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001077 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001078
cristy4c08aed2011-07-01 19:47:50 +00001079 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001080 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001081
cristy117ff172010-08-15 21:35:32 +00001082 register ssize_t
1083 y;
1084
cristy3ed852e2009-09-05 21:47:34 +00001085 if (status == MagickFalse)
1086 continue;
cristy117ff172010-08-15 21:35:32 +00001087 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
cristyb6dc4b72011-12-10 23:29:53 +00001088 blur_image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001089 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001090 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001091 {
1092 status=MagickFalse;
1093 continue;
1094 }
cristyb6dc4b72011-12-10 23:29:53 +00001095 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001096 {
cristybb503372010-05-27 20:51:26 +00001097 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001098 i;
1099
cristyb6dc4b72011-12-10 23:29:53 +00001100 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
cristyb41a1172011-09-06 00:55:14 +00001101 {
1102 MagickRealType
1103 alpha,
1104 gamma,
1105 pixel;
cristyd25c77e2011-09-06 00:10:24 +00001106
cristyb41a1172011-09-06 00:55:14 +00001107 PixelChannel
1108 channel;
1109
1110 PixelTrait
1111 blur_traits,
1112 traits;
1113
1114 register const double
1115 *restrict k;
1116
1117 register const Quantum
1118 *restrict pixels;
1119
1120 register ssize_t
1121 u;
1122
cristye2a912b2011-12-05 20:02:07 +00001123 channel=GetPixelChannelMapChannel(blur_image,i);
cristyabace412011-12-11 15:56:53 +00001124 traits=GetPixelChannelMapTraits(blur_image,channel);
cristyb41a1172011-09-06 00:55:14 +00001125 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1126 if ((traits == UndefinedPixelTrait) ||
1127 (blur_traits == UndefinedPixelTrait))
1128 continue;
cristyd09f8802012-02-04 16:44:10 +00001129 if (((blur_traits & CopyPixelTrait) != 0) ||
1130 (GetPixelMask(blur_image,p) != 0))
cristyd25c77e2011-09-06 00:10:24 +00001131 {
cristy0beccfa2011-09-25 20:47:53 +00001132 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001133 continue;
cristyd25c77e2011-09-06 00:10:24 +00001134 }
cristyb41a1172011-09-06 00:55:14 +00001135 k=kernel;
1136 pixels=p;
1137 pixel=0.0;
1138 if ((blur_traits & BlendPixelTrait) == 0)
1139 {
1140 /*
1141 No alpha blending.
1142 */
1143 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001144 {
cristyb41a1172011-09-06 00:55:14 +00001145 pixel+=(*k)*pixels[i];
1146 k++;
1147 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001148 }
cristy0beccfa2011-09-25 20:47:53 +00001149 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001150 continue;
1151 }
1152 /*
1153 Alpha blending.
1154 */
1155 gamma=0.0;
1156 for (u=0; u < (ssize_t) width; u++)
1157 {
cristyb6dc4b72011-12-10 23:29:53 +00001158 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1159 pixels));
cristyb41a1172011-09-06 00:55:14 +00001160 pixel+=(*k)*alpha*pixels[i];
1161 gamma+=(*k)*alpha;
1162 k++;
1163 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001164 }
cristyb41a1172011-09-06 00:55:14 +00001165 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001166 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001167 }
cristyd25c77e2011-09-06 00:10:24 +00001168 p+=GetPixelChannels(blur_image);
cristyed231572011-07-14 02:18:59 +00001169 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001170 }
1171 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1172 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001173 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001174 {
1175 MagickBooleanType
1176 proceed;
1177
cristyb5d5f722009-11-04 03:03:49 +00001178#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyef1f9072012-01-20 14:43:06 +00001179 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001180#endif
cristy4c08aed2011-07-01 19:47:50 +00001181 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1182 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001183 if (proceed == MagickFalse)
1184 status=MagickFalse;
1185 }
1186 }
1187 blur_view=DestroyCacheView(blur_view);
1188 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00001189 kernel=(double *) RelinquishAlignedMemory(kernel);
1190 blur_image->type=image->type;
cristy3ed852e2009-09-05 21:47:34 +00001191 if (status == MagickFalse)
1192 blur_image=DestroyImage(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001193 return(blur_image);
1194}
1195
1196/*
1197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1198% %
1199% %
1200% %
cristyfccdab92009-11-30 16:43:57 +00001201% C o n v o l v e I m a g e %
1202% %
1203% %
1204% %
1205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1206%
1207% ConvolveImage() applies a custom convolution kernel to the image.
1208%
1209% The format of the ConvolveImage method is:
1210%
cristy5e6be1e2011-07-16 01:23:39 +00001211% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1212% ExceptionInfo *exception)
1213%
cristyfccdab92009-11-30 16:43:57 +00001214% A description of each parameter follows:
1215%
1216% o image: the image.
1217%
cristy5e6be1e2011-07-16 01:23:39 +00001218% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001219%
1220% o exception: return any errors or warnings in this structure.
1221%
1222*/
cristy5e6be1e2011-07-16 01:23:39 +00001223MagickExport Image *ConvolveImage(const Image *image,
1224 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001225{
cristyfccdab92009-11-30 16:43:57 +00001226#define ConvolveImageTag "Convolve/Image"
1227
cristyc4c8d132010-01-07 01:58:38 +00001228 CacheView
1229 *convolve_view,
cristy105ba3c2011-07-18 02:28:38 +00001230 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00001231
cristyfccdab92009-11-30 16:43:57 +00001232 Image
1233 *convolve_image;
1234
cristyfccdab92009-11-30 16:43:57 +00001235 MagickBooleanType
1236 status;
1237
cristybb503372010-05-27 20:51:26 +00001238 MagickOffsetType
1239 progress;
1240
cristybb503372010-05-27 20:51:26 +00001241 ssize_t
cristy574cc262011-08-05 01:23:58 +00001242 center,
cristybb503372010-05-27 20:51:26 +00001243 y;
1244
cristyfccdab92009-11-30 16:43:57 +00001245 /*
1246 Initialize convolve image attributes.
1247 */
1248 assert(image != (Image *) NULL);
1249 assert(image->signature == MagickSignature);
1250 if (image->debug != MagickFalse)
1251 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1252 assert(exception != (ExceptionInfo *) NULL);
1253 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001254 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001255 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001256 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1257 exception);
cristyfccdab92009-11-30 16:43:57 +00001258 if (convolve_image == (Image *) NULL)
1259 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001260 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001261 {
cristyfccdab92009-11-30 16:43:57 +00001262 convolve_image=DestroyImage(convolve_image);
1263 return((Image *) NULL);
1264 }
1265 if (image->debug != MagickFalse)
1266 {
1267 char
1268 format[MaxTextExtent],
1269 *message;
1270
cristya96f2492011-12-14 18:25:41 +00001271 register const MagickRealType
cristy117ff172010-08-15 21:35:32 +00001272 *k;
1273
cristy4e154852011-07-14 13:28:53 +00001274 register ssize_t
1275 u;
1276
cristybb503372010-05-27 20:51:26 +00001277 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001278 v;
1279
cristyfccdab92009-11-30 16:43:57 +00001280 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001281 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1282 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001283 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001284 k=kernel_info->values;
1285 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001286 {
1287 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001288 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001289 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001290 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001291 {
cristyb51dff52011-05-19 16:55:47 +00001292 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001293 (void) ConcatenateString(&message,format);
1294 }
1295 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1296 }
1297 message=DestroyString(message);
1298 }
cristy7ea27962011-09-12 18:12:49 +00001299 status=AccelerateConvolveImage(image,kernel_info,convolve_image,exception);
1300 if (status == MagickTrue)
1301 return(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001302 /*
cristyfccdab92009-11-30 16:43:57 +00001303 Convolve image.
anthonyfd706f92012-01-19 04:22:02 +00001304 FUTURE: Use Morphology Convolve instead.
cristyfccdab92009-11-30 16:43:57 +00001305 */
cristy574cc262011-08-05 01:23:58 +00001306 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
cristy075ff302011-09-07 01:51:24 +00001307 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2L);
cristyfccdab92009-11-30 16:43:57 +00001308 status=MagickTrue;
1309 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001310 image_view=AcquireCacheView(image);
1311 convolve_view=AcquireCacheView(convolve_image);
1312#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd36a25e2012-01-18 14:30:53 +00001313 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001314#endif
cristybb503372010-05-27 20:51:26 +00001315 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001316 {
cristy4c08aed2011-07-01 19:47:50 +00001317 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001318 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001319
cristy4c08aed2011-07-01 19:47:50 +00001320 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001321 *restrict q;
1322
cristy117ff172010-08-15 21:35:32 +00001323 register ssize_t
1324 x;
1325
cristyfccdab92009-11-30 16:43:57 +00001326 if (status == MagickFalse)
1327 continue;
cristy105ba3c2011-07-18 02:28:38 +00001328 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1329 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1330 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001331 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001332 exception);
cristy105ba3c2011-07-18 02:28:38 +00001333 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001334 {
1335 status=MagickFalse;
1336 continue;
1337 }
cristybb503372010-05-27 20:51:26 +00001338 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001339 {
cristybb503372010-05-27 20:51:26 +00001340 register ssize_t
cristyed231572011-07-14 02:18:59 +00001341 i;
cristyfccdab92009-11-30 16:43:57 +00001342
cristya30d9ba2011-07-23 21:00:48 +00001343 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001344 {
cristyed231572011-07-14 02:18:59 +00001345 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001346 alpha,
1347 gamma,
cristyed231572011-07-14 02:18:59 +00001348 pixel;
1349
1350 PixelChannel
1351 channel;
1352
1353 PixelTrait
1354 convolve_traits,
1355 traits;
1356
cristya96f2492011-12-14 18:25:41 +00001357 register const MagickRealType
cristyed231572011-07-14 02:18:59 +00001358 *restrict k;
1359
1360 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001361 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001362
1363 register ssize_t
1364 u;
1365
1366 ssize_t
1367 v;
1368
cristye2a912b2011-12-05 20:02:07 +00001369 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001370 traits=GetPixelChannelMapTraits(image,channel);
cristy4e154852011-07-14 13:28:53 +00001371 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001372 if ((traits == UndefinedPixelTrait) ||
1373 (convolve_traits == UndefinedPixelTrait))
cristy4e154852011-07-14 13:28:53 +00001374 continue;
cristyd09f8802012-02-04 16:44:10 +00001375 if (((convolve_traits & CopyPixelTrait) != 0) ||
1376 (GetPixelMask(image,p) != 0))
cristy4e154852011-07-14 13:28:53 +00001377 {
cristy0beccfa2011-09-25 20:47:53 +00001378 SetPixelChannel(convolve_image,channel,p[center+i],q);
cristy4e154852011-07-14 13:28:53 +00001379 continue;
1380 }
cristy5e6be1e2011-07-16 01:23:39 +00001381 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001382 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001383 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001384 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001385 {
cristyed231572011-07-14 02:18:59 +00001386 /*
cristy4e154852011-07-14 13:28:53 +00001387 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001388 */
cristyeb52cde2011-07-17 01:52:52 +00001389 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001390 {
cristyeb52cde2011-07-17 01:52:52 +00001391 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001392 {
cristyeb52cde2011-07-17 01:52:52 +00001393 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001394 k++;
cristya30d9ba2011-07-23 21:00:48 +00001395 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001396 }
cristya30d9ba2011-07-23 21:00:48 +00001397 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001398 }
cristy0beccfa2011-09-25 20:47:53 +00001399 SetPixelChannel(convolve_image,channel,ClampToQuantum(pixel),q);
cristy4e154852011-07-14 13:28:53 +00001400 continue;
cristyed231572011-07-14 02:18:59 +00001401 }
cristy4e154852011-07-14 13:28:53 +00001402 /*
1403 Alpha blending.
1404 */
1405 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001406 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001407 {
cristyeb52cde2011-07-17 01:52:52 +00001408 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001409 {
cristyeb52cde2011-07-17 01:52:52 +00001410 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1411 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001412 gamma+=(*k)*alpha;
1413 k++;
cristya30d9ba2011-07-23 21:00:48 +00001414 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001415 }
cristya30d9ba2011-07-23 21:00:48 +00001416 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001417 }
cristy1ce96d02011-07-14 17:57:24 +00001418 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001419 SetPixelChannel(convolve_image,channel,ClampToQuantum(gamma*pixel),q);
cristyed231572011-07-14 02:18:59 +00001420 }
cristya30d9ba2011-07-23 21:00:48 +00001421 p+=GetPixelChannels(image);
1422 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001423 }
cristyed231572011-07-14 02:18:59 +00001424 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001425 status=MagickFalse;
1426 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1427 {
1428 MagickBooleanType
1429 proceed;
1430
1431#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyef1f9072012-01-20 14:43:06 +00001432 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001433#endif
1434 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1435 if (proceed == MagickFalse)
1436 status=MagickFalse;
1437 }
1438 }
1439 convolve_image->type=image->type;
1440 convolve_view=DestroyCacheView(convolve_view);
1441 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001442 if (status == MagickFalse)
1443 convolve_image=DestroyImage(convolve_image);
1444 return(convolve_image);
1445}
1446
1447/*
1448%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1449% %
1450% %
1451% %
cristy3ed852e2009-09-05 21:47:34 +00001452% D e s p e c k l e I m a g e %
1453% %
1454% %
1455% %
1456%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457%
1458% DespeckleImage() reduces the speckle noise in an image while perserving the
cristyf0ae7762012-01-15 22:04:03 +00001459% edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1460% neighbors, then complementarily lowering pixels that are brighter than their
1461% surrounding neighbors) to reduce the speckle index of that image (reference
1462% Crimmins speckle removal).
cristy3ed852e2009-09-05 21:47:34 +00001463%
1464% The format of the DespeckleImage method is:
1465%
1466% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1467%
1468% A description of each parameter follows:
1469%
1470% o image: the image.
1471%
1472% o exception: return any errors or warnings in this structure.
1473%
1474*/
1475
cristy5473bf02012-01-16 19:15:08 +00001476static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1477 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1478 Quantum *restrict g)
cristy3ed852e2009-09-05 21:47:34 +00001479{
cristy7b4b8862012-01-16 19:52:19 +00001480 register Quantum
cristy5473bf02012-01-16 19:15:08 +00001481 *p,
1482 *q,
1483 *r,
1484 *s;
cristy78f3de22012-01-16 00:43:18 +00001485
cristy5473bf02012-01-16 19:15:08 +00001486 ssize_t
1487 y;
1488
1489 assert(f != (Quantum *) NULL);
1490 assert(g != (Quantum *) NULL);
1491 p=f+(columns+2);
1492 q=g+(columns+2);
1493 r=p+(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001494#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8de86be2012-01-20 02:05:06 +00001495 #pragma omp parallel for schedule(static)
cristy5473bf02012-01-16 19:15:08 +00001496#endif
1497 for (y=0; y < (ssize_t) rows; y++)
1498 {
cristy7b4b8862012-01-16 19:52:19 +00001499 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001500 i,
1501 x;
1502
cristy7b4b8862012-01-16 19:52:19 +00001503 SignedQuantum
1504 v;
1505
cristy5473bf02012-01-16 19:15:08 +00001506 i=(2*y+1)+y*columns;
1507 if (polarity > 0)
1508 for (x=0; x < (ssize_t) columns; x++)
1509 {
1510 v=(SignedQuantum) p[i];
1511 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1512 v+=ScaleCharToQuantum(1);
1513 q[i]=(Quantum) v;
1514 i++;
1515 }
1516 else
1517 for (x=0; x < (ssize_t) columns; x++)
1518 {
1519 v=(SignedQuantum) p[i];
1520 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1521 v-=ScaleCharToQuantum(1);
1522 q[i]=(Quantum) v;
1523 i++;
1524 }
1525 }
1526 p=f+(columns+2);
1527 q=g+(columns+2);
1528 r=q+(y_offset*(columns+2)+x_offset);
1529 s=q-(y_offset*(columns+2)+x_offset);
cristyff8e85a2012-01-18 13:36:20 +00001530#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8de86be2012-01-20 02:05:06 +00001531 #pragma omp parallel for schedule(static)
cristy5473bf02012-01-16 19:15:08 +00001532#endif
1533 for (y=0; y < (ssize_t) rows; y++)
1534 {
cristy7b4b8862012-01-16 19:52:19 +00001535 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001536 i,
1537 x;
1538
cristy7b4b8862012-01-16 19:52:19 +00001539 SignedQuantum
1540 v;
1541
cristy5473bf02012-01-16 19:15:08 +00001542 i=(2*y+1)+y*columns;
1543 if (polarity > 0)
1544 for (x=0; x < (ssize_t) columns; x++)
1545 {
1546 v=(SignedQuantum) q[i];
1547 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1548 ((SignedQuantum) r[i] > v))
1549 v+=ScaleCharToQuantum(1);
1550 p[i]=(Quantum) v;
1551 i++;
1552 }
1553 else
1554 for (x=0; x < (ssize_t) columns; x++)
1555 {
1556 v=(SignedQuantum) q[i];
1557 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1558 ((SignedQuantum) r[i] < v))
1559 v-=ScaleCharToQuantum(1);
1560 p[i]=(Quantum) v;
1561 i++;
1562 }
1563 }
cristy3ed852e2009-09-05 21:47:34 +00001564}
1565
1566MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1567{
1568#define DespeckleImageTag "Despeckle/Image"
1569
cristy2407fc22009-09-11 00:55:25 +00001570 CacheView
1571 *despeckle_view,
1572 *image_view;
1573
cristy3ed852e2009-09-05 21:47:34 +00001574 Image
1575 *despeckle_image;
1576
cristy3ed852e2009-09-05 21:47:34 +00001577 MagickBooleanType
1578 status;
1579
cristy5473bf02012-01-16 19:15:08 +00001580 Quantum
1581 *restrict buffer,
1582 *restrict pixels;
cristya63e4a92011-09-09 00:47:59 +00001583
cristy5473bf02012-01-16 19:15:08 +00001584 register ssize_t
1585 i;
1586
1587 size_t
1588 length;
cristy117ff172010-08-15 21:35:32 +00001589
cristybb503372010-05-27 20:51:26 +00001590 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001591 X[4] = {0, 1, 1,-1},
1592 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001593
cristy3ed852e2009-09-05 21:47:34 +00001594 /*
1595 Allocate despeckled image.
1596 */
1597 assert(image != (const Image *) NULL);
1598 assert(image->signature == MagickSignature);
1599 if (image->debug != MagickFalse)
1600 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1601 assert(exception != (ExceptionInfo *) NULL);
1602 assert(exception->signature == MagickSignature);
cristy5473bf02012-01-16 19:15:08 +00001603 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001604 if (despeckle_image == (Image *) NULL)
1605 return((Image *) NULL);
cristya63e4a92011-09-09 00:47:59 +00001606 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1607 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001608 {
cristy3ed852e2009-09-05 21:47:34 +00001609 despeckle_image=DestroyImage(despeckle_image);
1610 return((Image *) NULL);
1611 }
1612 /*
cristy5473bf02012-01-16 19:15:08 +00001613 Allocate image buffer.
1614 */
1615 length=(size_t) ((image->columns+2)*(image->rows+2));
1616 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1617 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1618 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1619 {
1620 if (buffer != (Quantum *) NULL)
1621 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1622 if (pixels != (Quantum *) NULL)
1623 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1624 despeckle_image=DestroyImage(despeckle_image);
1625 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1626 }
1627 /*
1628 Reduce speckle in the image.
cristy3ed852e2009-09-05 21:47:34 +00001629 */
1630 status=MagickTrue;
cristyb06da092012-01-16 17:11:38 +00001631 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00001632 despeckle_view=AcquireCacheView(despeckle_image);
cristy5473bf02012-01-16 19:15:08 +00001633 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +00001634 {
cristy5473bf02012-01-16 19:15:08 +00001635 PixelChannel
1636 channel;
1637
1638 PixelTrait
1639 despeckle_traits,
1640 traits;
cristy3ed852e2009-09-05 21:47:34 +00001641
cristyc1488b52011-02-19 18:54:15 +00001642 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001643 k,
cristyf0ae7762012-01-15 22:04:03 +00001644 x;
cristyc1488b52011-02-19 18:54:15 +00001645
cristy5473bf02012-01-16 19:15:08 +00001646 ssize_t
1647 j,
1648 y;
1649
1650 if (status == MagickFalse)
1651 continue;
1652 channel=GetPixelChannelMapChannel(image,i);
1653 traits=GetPixelChannelMapTraits(image,channel);
1654 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1655 if ((traits == UndefinedPixelTrait) ||
1656 (despeckle_traits == UndefinedPixelTrait))
1657 continue;
1658 if ((despeckle_traits & CopyPixelTrait) != 0)
1659 continue;
1660 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1661 j=(ssize_t) image->columns+2;
1662 for (y=0; y < (ssize_t) image->rows; y++)
cristyfc830f42012-01-15 02:45:06 +00001663 {
cristy5473bf02012-01-16 19:15:08 +00001664 register const Quantum
1665 *restrict p;
cristyfc830f42012-01-15 02:45:06 +00001666
cristy5473bf02012-01-16 19:15:08 +00001667 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1668 if (p == (const Quantum *) NULL)
cristyf0ae7762012-01-15 22:04:03 +00001669 {
cristy5473bf02012-01-16 19:15:08 +00001670 status=MagickFalse;
1671 continue;
cristyf0ae7762012-01-15 22:04:03 +00001672 }
cristy5473bf02012-01-16 19:15:08 +00001673 j++;
1674 for (x=0; x < (ssize_t) image->columns; x++)
1675 {
1676 pixels[j++]=p[i];
1677 p+=GetPixelChannels(image);
cristyfc830f42012-01-15 02:45:06 +00001678 }
cristy5473bf02012-01-16 19:15:08 +00001679 j++;
cristy3ed852e2009-09-05 21:47:34 +00001680 }
cristy5473bf02012-01-16 19:15:08 +00001681 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1682 for (k=0; k < 4; k++)
1683 {
1684 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1685 Hull(-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1686 Hull(-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1687 Hull(X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1688 }
1689 j=(ssize_t) image->columns+2;
1690 for (y=0; y < (ssize_t) image->rows; y++)
1691 {
1692 MagickBooleanType
1693 sync;
1694
1695 register Quantum
1696 *restrict q;
1697
cristyd13e8eb2012-01-17 02:36:42 +00001698 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1699 despeckle_image->columns,1,exception);
cristy5473bf02012-01-16 19:15:08 +00001700 if (q == (Quantum *) NULL)
1701 {
1702 status=MagickFalse;
1703 continue;
1704 }
1705 j++;
1706 for (x=0; x < (ssize_t) image->columns; x++)
1707 {
1708 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1709 q+=GetPixelChannels(despeckle_image);
1710 }
1711 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1712 if (sync == MagickFalse)
1713 status=MagickFalse;
1714 j++;
1715 }
cristy3ed852e2009-09-05 21:47:34 +00001716 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1717 {
1718 MagickBooleanType
1719 proceed;
1720
cristy5473bf02012-01-16 19:15:08 +00001721 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1722 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00001723 if (proceed == MagickFalse)
1724 status=MagickFalse;
1725 }
1726 }
1727 despeckle_view=DestroyCacheView(despeckle_view);
1728 image_view=DestroyCacheView(image_view);
cristy5473bf02012-01-16 19:15:08 +00001729 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1730 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001731 despeckle_image->type=image->type;
1732 if (status == MagickFalse)
1733 despeckle_image=DestroyImage(despeckle_image);
1734 return(despeckle_image);
1735}
1736
1737/*
1738%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1739% %
1740% %
1741% %
1742% E d g e I m a g e %
1743% %
1744% %
1745% %
1746%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1747%
1748% EdgeImage() finds edges in an image. Radius defines the radius of the
1749% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1750% radius for you.
1751%
1752% The format of the EdgeImage method is:
1753%
1754% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001755% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001756%
1757% A description of each parameter follows:
1758%
1759% o image: the image.
1760%
1761% o radius: the radius of the pixel neighborhood.
1762%
cristy8ae632d2011-09-05 17:29:53 +00001763% o sigma: the standard deviation of the Gaussian, in pixels.
1764%
cristy3ed852e2009-09-05 21:47:34 +00001765% o exception: return any errors or warnings in this structure.
1766%
1767*/
1768MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001769 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001770{
1771 Image
1772 *edge_image;
1773
cristy41cbe682011-07-15 19:12:37 +00001774 KernelInfo
1775 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001776
cristybb503372010-05-27 20:51:26 +00001777 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001778 i;
1779
cristybb503372010-05-27 20:51:26 +00001780 size_t
cristy3ed852e2009-09-05 21:47:34 +00001781 width;
1782
cristy41cbe682011-07-15 19:12:37 +00001783 ssize_t
1784 j,
1785 u,
1786 v;
1787
cristy3ed852e2009-09-05 21:47:34 +00001788 assert(image != (const Image *) NULL);
1789 assert(image->signature == MagickSignature);
1790 if (image->debug != MagickFalse)
1791 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1792 assert(exception != (ExceptionInfo *) NULL);
1793 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001794 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001795 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001796 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001797 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001798 kernel_info->width=width;
1799 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001800 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1801 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1802 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001803 {
1804 kernel_info=DestroyKernelInfo(kernel_info);
1805 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1806 }
1807 j=(ssize_t) kernel_info->width/2;
1808 i=0;
1809 for (v=(-j); v <= j; v++)
1810 {
1811 for (u=(-j); u <= j; u++)
1812 {
1813 kernel_info->values[i]=(-1.0);
1814 i++;
1815 }
1816 }
1817 kernel_info->values[i/2]=(double) (width*width-1.0);
anthony736a1602011-10-06 12:38:17 +00001818 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
cristy5e6be1e2011-07-16 01:23:39 +00001819 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001820 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001821 return(edge_image);
1822}
1823
1824/*
1825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1826% %
1827% %
1828% %
1829% E m b o s s I m a g e %
1830% %
1831% %
1832% %
1833%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1834%
1835% EmbossImage() returns a grayscale image with a three-dimensional effect.
1836% We convolve the image with a Gaussian operator of the given radius and
1837% standard deviation (sigma). For reasonable results, radius should be
1838% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1839% radius for you.
1840%
1841% The format of the EmbossImage method is:
1842%
1843% Image *EmbossImage(const Image *image,const double radius,
1844% const double sigma,ExceptionInfo *exception)
1845%
1846% A description of each parameter follows:
1847%
1848% o image: the image.
1849%
1850% o radius: the radius of the pixel neighborhood.
1851%
1852% o sigma: the standard deviation of the Gaussian, in pixels.
1853%
1854% o exception: return any errors or warnings in this structure.
1855%
1856*/
1857MagickExport Image *EmbossImage(const Image *image,const double radius,
1858 const double sigma,ExceptionInfo *exception)
1859{
cristy3ed852e2009-09-05 21:47:34 +00001860 Image
1861 *emboss_image;
1862
cristy41cbe682011-07-15 19:12:37 +00001863 KernelInfo
1864 *kernel_info;
1865
cristybb503372010-05-27 20:51:26 +00001866 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001867 i;
1868
cristybb503372010-05-27 20:51:26 +00001869 size_t
cristy3ed852e2009-09-05 21:47:34 +00001870 width;
1871
cristy117ff172010-08-15 21:35:32 +00001872 ssize_t
1873 j,
1874 k,
1875 u,
1876 v;
1877
cristy41cbe682011-07-15 19:12:37 +00001878 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001879 assert(image->signature == MagickSignature);
1880 if (image->debug != MagickFalse)
1881 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1882 assert(exception != (ExceptionInfo *) NULL);
1883 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001884 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001885 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001886 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001887 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001888 kernel_info->width=width;
1889 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001890 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1891 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1892 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001893 {
1894 kernel_info=DestroyKernelInfo(kernel_info);
1895 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1896 }
1897 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001898 k=j;
1899 i=0;
1900 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001901 {
cristy47e00502009-12-17 19:19:57 +00001902 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001903 {
cristy41cbe682011-07-15 19:12:37 +00001904 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001905 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001906 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001907 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001908 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001909 i++;
1910 }
cristy47e00502009-12-17 19:19:57 +00001911 k--;
cristy3ed852e2009-09-05 21:47:34 +00001912 }
anthony736a1602011-10-06 12:38:17 +00001913 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
cristy5e6be1e2011-07-16 01:23:39 +00001914 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001915 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001916 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001917 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001918 return(emboss_image);
1919}
1920
1921/*
1922%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1923% %
1924% %
1925% %
1926% G a u s s i a n B l u r I m a g e %
1927% %
1928% %
1929% %
1930%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1931%
1932% GaussianBlurImage() blurs an image. We convolve the image with a
1933% Gaussian operator of the given radius and standard deviation (sigma).
1934% For reasonable results, the radius should be larger than sigma. Use a
1935% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1936%
1937% The format of the GaussianBlurImage method is:
1938%
1939% Image *GaussianBlurImage(const Image *image,onst double radius,
cristyd89705a2012-01-20 02:52:24 +00001940% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001941%
1942% A description of each parameter follows:
1943%
1944% o image: the image.
1945%
cristy3ed852e2009-09-05 21:47:34 +00001946% o radius: the radius of the Gaussian, in pixels, not counting the center
1947% pixel.
1948%
1949% o sigma: the standard deviation of the Gaussian, in pixels.
1950%
1951% o exception: return any errors or warnings in this structure.
1952%
1953*/
cristy41cbe682011-07-15 19:12:37 +00001954MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristyd89705a2012-01-20 02:52:24 +00001955 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001956{
cristy3ed852e2009-09-05 21:47:34 +00001957 Image
1958 *blur_image;
1959
cristy41cbe682011-07-15 19:12:37 +00001960 KernelInfo
1961 *kernel_info;
1962
cristybb503372010-05-27 20:51:26 +00001963 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001964 i;
1965
cristybb503372010-05-27 20:51:26 +00001966 size_t
cristy3ed852e2009-09-05 21:47:34 +00001967 width;
1968
cristy117ff172010-08-15 21:35:32 +00001969 ssize_t
1970 j,
1971 u,
1972 v;
1973
cristy3ed852e2009-09-05 21:47:34 +00001974 assert(image != (const Image *) NULL);
1975 assert(image->signature == MagickSignature);
1976 if (image->debug != MagickFalse)
1977 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1978 assert(exception != (ExceptionInfo *) NULL);
1979 assert(exception->signature == MagickSignature);
1980 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001981 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001982 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001983 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001984 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1985 kernel_info->width=width;
1986 kernel_info->height=width;
1987 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00001988 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1989 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1990 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001991 {
1992 kernel_info=DestroyKernelInfo(kernel_info);
1993 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1994 }
1995 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001996 i=0;
cristy47e00502009-12-17 19:19:57 +00001997 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001998 {
cristy47e00502009-12-17 19:19:57 +00001999 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00002000 {
2001 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
2002 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2003 i++;
2004 }
cristy3ed852e2009-09-05 21:47:34 +00002005 }
cristy5e6be1e2011-07-16 01:23:39 +00002006 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00002007 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00002008 return(blur_image);
2009}
2010
2011/*
2012%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2013% %
2014% %
2015% %
cristy3ed852e2009-09-05 21:47:34 +00002016% M o t i o n B l u r I m a g e %
2017% %
2018% %
2019% %
2020%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2021%
2022% MotionBlurImage() simulates motion blur. We convolve the image with a
2023% Gaussian operator of the given radius and standard deviation (sigma).
2024% For reasonable results, radius should be larger than sigma. Use a
2025% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2026% Angle gives the angle of the blurring motion.
2027%
2028% Andrew Protano contributed this effect.
2029%
2030% The format of the MotionBlurImage method is:
2031%
2032% Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002033% const double sigma,const double angle,const double bias,
2034% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002035%
2036% A description of each parameter follows:
2037%
2038% o image: the image.
2039%
cristy3ed852e2009-09-05 21:47:34 +00002040% o radius: the radius of the Gaussian, in pixels, not counting
2041% the center pixel.
2042%
2043% o sigma: the standard deviation of the Gaussian, in pixels.
2044%
cristycee97112010-05-28 00:44:52 +00002045% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002046%
cristyf7ef0252011-09-09 14:50:06 +00002047% o bias: the bias.
2048%
cristy3ed852e2009-09-05 21:47:34 +00002049% o exception: return any errors or warnings in this structure.
2050%
2051*/
2052
cristybb503372010-05-27 20:51:26 +00002053static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002054{
cristy3ed852e2009-09-05 21:47:34 +00002055 double
cristy47e00502009-12-17 19:19:57 +00002056 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002057 normalize;
2058
cristybb503372010-05-27 20:51:26 +00002059 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002060 i;
2061
2062 /*
cristy47e00502009-12-17 19:19:57 +00002063 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002064 */
2065 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy8b49f382012-01-17 03:11:03 +00002066 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00002067 if (kernel == (double *) NULL)
2068 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002069 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002070 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002071 {
cristy4205a3c2010-09-12 20:19:59 +00002072 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2073 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002074 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002075 }
cristybb503372010-05-27 20:51:26 +00002076 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002077 kernel[i]/=normalize;
2078 return(kernel);
2079}
2080
cristya63e4a92011-09-09 00:47:59 +00002081MagickExport Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002082 const double sigma,const double angle,const double bias,
2083 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002084{
cristyc4c8d132010-01-07 01:58:38 +00002085 CacheView
2086 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002087 *image_view,
2088 *motion_view;
cristyc4c8d132010-01-07 01:58:38 +00002089
cristy3ed852e2009-09-05 21:47:34 +00002090 double
2091 *kernel;
2092
2093 Image
2094 *blur_image;
2095
cristy3ed852e2009-09-05 21:47:34 +00002096 MagickBooleanType
2097 status;
2098
cristybb503372010-05-27 20:51:26 +00002099 MagickOffsetType
2100 progress;
2101
cristy3ed852e2009-09-05 21:47:34 +00002102 OffsetInfo
2103 *offset;
2104
2105 PointInfo
2106 point;
2107
cristybb503372010-05-27 20:51:26 +00002108 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002109 i;
2110
cristybb503372010-05-27 20:51:26 +00002111 size_t
cristy3ed852e2009-09-05 21:47:34 +00002112 width;
2113
cristybb503372010-05-27 20:51:26 +00002114 ssize_t
2115 y;
2116
cristy3ed852e2009-09-05 21:47:34 +00002117 assert(image != (Image *) NULL);
2118 assert(image->signature == MagickSignature);
2119 if (image->debug != MagickFalse)
2120 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2121 assert(exception != (ExceptionInfo *) NULL);
2122 width=GetOptimalKernelWidth1D(radius,sigma);
2123 kernel=GetMotionBlurKernel(width,sigma);
2124 if (kernel == (double *) NULL)
2125 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2126 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2127 if (offset == (OffsetInfo *) NULL)
2128 {
cristy8b49f382012-01-17 03:11:03 +00002129 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002130 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2131 }
cristy1e7aa312011-09-10 20:01:36 +00002132 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002133 if (blur_image == (Image *) NULL)
2134 {
cristy8b49f382012-01-17 03:11:03 +00002135 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002136 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2137 return((Image *) NULL);
2138 }
cristy574cc262011-08-05 01:23:58 +00002139 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002140 {
cristy8b49f382012-01-17 03:11:03 +00002141 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002142 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002143 blur_image=DestroyImage(blur_image);
2144 return((Image *) NULL);
2145 }
2146 point.x=(double) width*sin(DegreesToRadians(angle));
2147 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002148 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002149 {
cristybb503372010-05-27 20:51:26 +00002150 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2151 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002152 }
2153 /*
2154 Motion blur image.
2155 */
2156 status=MagickTrue;
2157 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002158 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002159 motion_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002160 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002161#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00002162 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002163#endif
cristybb503372010-05-27 20:51:26 +00002164 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002165 {
cristyf7ef0252011-09-09 14:50:06 +00002166 register const Quantum
2167 *restrict p;
2168
cristy4c08aed2011-07-01 19:47:50 +00002169 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002170 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002171
cristy117ff172010-08-15 21:35:32 +00002172 register ssize_t
2173 x;
2174
cristy3ed852e2009-09-05 21:47:34 +00002175 if (status == MagickFalse)
2176 continue;
cristy8b49f382012-01-17 03:11:03 +00002177 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2178 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002179 exception);
cristyf7ef0252011-09-09 14:50:06 +00002180 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002181 {
2182 status=MagickFalse;
2183 continue;
2184 }
cristybb503372010-05-27 20:51:26 +00002185 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002186 {
cristybb503372010-05-27 20:51:26 +00002187 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002188 i;
2189
cristyf7ef0252011-09-09 14:50:06 +00002190 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2191 {
2192 MagickRealType
2193 alpha,
2194 gamma,
2195 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002196
cristyf7ef0252011-09-09 14:50:06 +00002197 PixelChannel
2198 channel;
2199
2200 PixelTrait
2201 blur_traits,
2202 traits;
2203
2204 register const Quantum
2205 *restrict r;
2206
2207 register double
2208 *restrict k;
2209
2210 register ssize_t
2211 j;
2212
cristye2a912b2011-12-05 20:02:07 +00002213 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002214 traits=GetPixelChannelMapTraits(image,channel);
cristyf7ef0252011-09-09 14:50:06 +00002215 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2216 if ((traits == UndefinedPixelTrait) ||
2217 (blur_traits == UndefinedPixelTrait))
2218 continue;
cristyd09f8802012-02-04 16:44:10 +00002219 if (((blur_traits & CopyPixelTrait) != 0) ||
2220 (GetPixelMask(image,p) != 0))
cristy3ed852e2009-09-05 21:47:34 +00002221 {
cristy0beccfa2011-09-25 20:47:53 +00002222 SetPixelChannel(blur_image,channel,p[i],q);
cristyf7ef0252011-09-09 14:50:06 +00002223 continue;
cristy3ed852e2009-09-05 21:47:34 +00002224 }
cristyf7ef0252011-09-09 14:50:06 +00002225 k=kernel;
2226 pixel=bias;
2227 if ((blur_traits & BlendPixelTrait) == 0)
2228 {
2229 for (j=0; j < (ssize_t) width; j++)
2230 {
cristy8b49f382012-01-17 03:11:03 +00002231 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
cristyf7ef0252011-09-09 14:50:06 +00002232 offset[j].y,1,1,exception);
2233 if (r == (const Quantum *) NULL)
2234 {
2235 status=MagickFalse;
2236 continue;
2237 }
2238 pixel+=(*k)*r[i];
2239 k++;
2240 }
cristy0beccfa2011-09-25 20:47:53 +00002241 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002242 continue;
2243 }
2244 alpha=0.0;
2245 gamma=0.0;
2246 for (j=0; j < (ssize_t) width; j++)
2247 {
cristy8b49f382012-01-17 03:11:03 +00002248 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
cristyf7ef0252011-09-09 14:50:06 +00002249 1,exception);
2250 if (r == (const Quantum *) NULL)
2251 {
2252 status=MagickFalse;
2253 continue;
2254 }
2255 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2256 pixel+=(*k)*alpha*r[i];
2257 gamma+=(*k)*alpha;
2258 k++;
cristy3ed852e2009-09-05 21:47:34 +00002259 }
cristyf7ef0252011-09-09 14:50:06 +00002260 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002261 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002262 }
2263 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002264 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002265 }
2266 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2267 status=MagickFalse;
2268 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2269 {
2270 MagickBooleanType
2271 proceed;
2272
cristyb557a152011-02-22 12:14:30 +00002273#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00002274 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002275#endif
2276 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2277 if (proceed == MagickFalse)
2278 status=MagickFalse;
2279 }
2280 }
2281 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00002282 motion_view=DestroyCacheView(motion_view);
cristy3ed852e2009-09-05 21:47:34 +00002283 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00002284 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002285 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2286 if (status == MagickFalse)
2287 blur_image=DestroyImage(blur_image);
2288 return(blur_image);
2289}
2290
2291/*
2292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2293% %
2294% %
2295% %
2296% P r e v i e w I m a g e %
2297% %
2298% %
2299% %
2300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2301%
2302% PreviewImage() tiles 9 thumbnails of the specified image with an image
2303% processing operation applied with varying parameters. This may be helpful
2304% pin-pointing an appropriate parameter for a particular image processing
2305% operation.
2306%
2307% The format of the PreviewImages method is:
2308%
2309% Image *PreviewImages(const Image *image,const PreviewType preview,
2310% ExceptionInfo *exception)
2311%
2312% A description of each parameter follows:
2313%
2314% o image: the image.
2315%
2316% o preview: the image processing operation.
2317%
2318% o exception: return any errors or warnings in this structure.
2319%
2320*/
2321MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2322 ExceptionInfo *exception)
2323{
2324#define NumberTiles 9
2325#define PreviewImageTag "Preview/Image"
2326#define DefaultPreviewGeometry "204x204+10+10"
2327
2328 char
2329 factor[MaxTextExtent],
2330 label[MaxTextExtent];
2331
2332 double
2333 degrees,
2334 gamma,
2335 percentage,
2336 radius,
2337 sigma,
2338 threshold;
2339
2340 Image
2341 *images,
2342 *montage_image,
2343 *preview_image,
2344 *thumbnail;
2345
2346 ImageInfo
2347 *preview_info;
2348
cristy3ed852e2009-09-05 21:47:34 +00002349 MagickBooleanType
2350 proceed;
2351
2352 MontageInfo
2353 *montage_info;
2354
2355 QuantizeInfo
2356 quantize_info;
2357
2358 RectangleInfo
2359 geometry;
2360
cristybb503372010-05-27 20:51:26 +00002361 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002362 i,
2363 x;
2364
cristybb503372010-05-27 20:51:26 +00002365 size_t
cristy3ed852e2009-09-05 21:47:34 +00002366 colors;
2367
cristy117ff172010-08-15 21:35:32 +00002368 ssize_t
2369 y;
2370
cristy3ed852e2009-09-05 21:47:34 +00002371 /*
2372 Open output image file.
2373 */
2374 assert(image != (Image *) NULL);
2375 assert(image->signature == MagickSignature);
2376 if (image->debug != MagickFalse)
2377 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2378 colors=2;
2379 degrees=0.0;
2380 gamma=(-0.2f);
2381 preview_info=AcquireImageInfo();
2382 SetGeometry(image,&geometry);
2383 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2384 &geometry.width,&geometry.height);
2385 images=NewImageList();
2386 percentage=12.5;
2387 GetQuantizeInfo(&quantize_info);
2388 radius=0.0;
2389 sigma=1.0;
2390 threshold=0.0;
2391 x=0;
2392 y=0;
2393 for (i=0; i < NumberTiles; i++)
2394 {
2395 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2396 if (thumbnail == (Image *) NULL)
2397 break;
2398 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2399 (void *) NULL);
cristyd15e6592011-10-15 00:13:06 +00002400 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002401 if (i == (NumberTiles/2))
2402 {
cristy9950d572011-10-01 18:22:35 +00002403 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2404 &thumbnail->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002405 AppendImageToList(&images,thumbnail);
2406 continue;
2407 }
2408 switch (preview)
2409 {
2410 case RotatePreview:
2411 {
2412 degrees+=45.0;
2413 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002414 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002415 break;
2416 }
2417 case ShearPreview:
2418 {
2419 degrees+=5.0;
2420 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002421 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002422 degrees,2.0*degrees);
2423 break;
2424 }
2425 case RollPreview:
2426 {
cristybb503372010-05-27 20:51:26 +00002427 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2428 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002429 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002430 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002431 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002432 break;
2433 }
2434 case HuePreview:
2435 {
2436 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2437 if (preview_image == (Image *) NULL)
2438 break;
cristyb51dff52011-05-19 16:55:47 +00002439 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002440 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002441 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002442 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002443 break;
2444 }
2445 case SaturationPreview:
2446 {
2447 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2448 if (preview_image == (Image *) NULL)
2449 break;
cristyb51dff52011-05-19 16:55:47 +00002450 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002451 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002452 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002453 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002454 break;
2455 }
2456 case BrightnessPreview:
2457 {
2458 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2459 if (preview_image == (Image *) NULL)
2460 break;
cristyb51dff52011-05-19 16:55:47 +00002461 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002462 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002463 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002464 break;
2465 }
2466 case GammaPreview:
2467 default:
2468 {
2469 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2470 if (preview_image == (Image *) NULL)
2471 break;
2472 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002473 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002474 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002475 break;
2476 }
2477 case SpiffPreview:
2478 {
2479 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2480 if (preview_image != (Image *) NULL)
2481 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002482 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002483 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002484 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002485 break;
2486 }
2487 case DullPreview:
2488 {
2489 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2490 if (preview_image == (Image *) NULL)
2491 break;
2492 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002493 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002494 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002495 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002496 break;
2497 }
2498 case GrayscalePreview:
2499 {
2500 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2501 if (preview_image == (Image *) NULL)
2502 break;
2503 colors<<=1;
2504 quantize_info.number_colors=colors;
2505 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002506 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002507 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002508 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002509 break;
2510 }
2511 case QuantizePreview:
2512 {
2513 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2514 if (preview_image == (Image *) NULL)
2515 break;
2516 colors<<=1;
2517 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002518 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002519 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002520 colors);
cristy3ed852e2009-09-05 21:47:34 +00002521 break;
2522 }
2523 case DespecklePreview:
2524 {
2525 for (x=0; x < (i-1); x++)
2526 {
2527 preview_image=DespeckleImage(thumbnail,exception);
2528 if (preview_image == (Image *) NULL)
2529 break;
2530 thumbnail=DestroyImage(thumbnail);
2531 thumbnail=preview_image;
2532 }
2533 preview_image=DespeckleImage(thumbnail,exception);
2534 if (preview_image == (Image *) NULL)
2535 break;
cristyb51dff52011-05-19 16:55:47 +00002536 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002537 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002538 break;
2539 }
2540 case ReduceNoisePreview:
2541 {
cristy95c38342011-03-18 22:39:51 +00002542 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2543 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002544 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002545 break;
2546 }
2547 case AddNoisePreview:
2548 {
2549 switch ((int) i)
2550 {
2551 case 0:
2552 {
2553 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2554 break;
2555 }
2556 case 1:
2557 {
2558 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2559 break;
2560 }
2561 case 2:
2562 {
2563 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2564 break;
2565 }
2566 case 3:
2567 {
2568 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2569 break;
2570 }
2571 case 4:
2572 {
2573 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2574 break;
2575 }
2576 case 5:
2577 {
2578 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2579 break;
2580 }
2581 default:
2582 {
2583 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2584 break;
2585 }
2586 }
cristyd76c51e2011-03-26 00:21:26 +00002587 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2588 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002589 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002590 break;
2591 }
2592 case SharpenPreview:
2593 {
anthony736a1602011-10-06 12:38:17 +00002594 /* FUTURE: user bias on sharpen! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002595 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2596 exception);
cristyb51dff52011-05-19 16:55:47 +00002597 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002598 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002599 break;
2600 }
2601 case BlurPreview:
2602 {
anthony736a1602011-10-06 12:38:17 +00002603 /* FUTURE: user bias on blur! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002604 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002605 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002606 sigma);
2607 break;
2608 }
2609 case ThresholdPreview:
2610 {
2611 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2612 if (preview_image == (Image *) NULL)
2613 break;
cristye941a752011-10-15 01:52:48 +00002614 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2615 QuantumRange+1.0))/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002616 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002617 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2618 break;
2619 }
2620 case EdgeDetectPreview:
2621 {
cristy8ae632d2011-09-05 17:29:53 +00002622 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002623 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002624 break;
2625 }
2626 case SpreadPreview:
2627 {
cristy5c4e2582011-09-11 19:21:03 +00002628 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2629 exception);
cristyb51dff52011-05-19 16:55:47 +00002630 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002631 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002632 break;
2633 }
2634 case SolarizePreview:
2635 {
2636 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2637 if (preview_image == (Image *) NULL)
2638 break;
2639 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002640 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002641 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002642 (QuantumRange*percentage)/100.0);
2643 break;
2644 }
2645 case ShadePreview:
2646 {
2647 degrees+=10.0;
2648 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2649 exception);
cristyb51dff52011-05-19 16:55:47 +00002650 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002651 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002652 break;
2653 }
2654 case RaisePreview:
2655 {
2656 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2657 if (preview_image == (Image *) NULL)
2658 break;
cristybb503372010-05-27 20:51:26 +00002659 geometry.width=(size_t) (2*i+2);
2660 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002661 geometry.x=i/2;
2662 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002663 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002664 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002665 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002666 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002667 break;
2668 }
2669 case SegmentPreview:
2670 {
2671 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2672 if (preview_image == (Image *) NULL)
2673 break;
2674 threshold+=0.4f;
2675 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002676 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002677 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002678 threshold,threshold);
2679 break;
2680 }
2681 case SwirlPreview:
2682 {
cristy76f512e2011-09-12 01:26:56 +00002683 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2684 exception);
cristyb51dff52011-05-19 16:55:47 +00002685 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002686 degrees+=45.0;
2687 break;
2688 }
2689 case ImplodePreview:
2690 {
2691 degrees+=0.1f;
cristy76f512e2011-09-12 01:26:56 +00002692 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2693 exception);
cristyb51dff52011-05-19 16:55:47 +00002694 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002695 break;
2696 }
2697 case WavePreview:
2698 {
2699 degrees+=5.0f;
cristy5c4e2582011-09-11 19:21:03 +00002700 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2701 image->interpolate,exception);
cristyb51dff52011-05-19 16:55:47 +00002702 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002703 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002704 break;
2705 }
2706 case OilPaintPreview:
2707 {
cristy14973ba2011-08-27 23:48:07 +00002708 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2709 exception);
2710 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2711 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002712 break;
2713 }
2714 case CharcoalDrawingPreview:
2715 {
anthony736a1602011-10-06 12:38:17 +00002716 /* FUTURE: user bias on charcoal! This is non-sensical! */
cristy3ed852e2009-09-05 21:47:34 +00002717 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristy05c0c9a2011-09-05 23:16:13 +00002718 image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002719 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002720 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002721 break;
2722 }
2723 case JPEGPreview:
2724 {
2725 char
2726 filename[MaxTextExtent];
2727
2728 int
2729 file;
2730
2731 MagickBooleanType
2732 status;
2733
2734 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2735 if (preview_image == (Image *) NULL)
2736 break;
cristybb503372010-05-27 20:51:26 +00002737 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002738 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002739 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002740 file=AcquireUniqueFileResource(filename);
2741 if (file != -1)
2742 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002743 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002744 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002745 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002746 if (status != MagickFalse)
2747 {
2748 Image
2749 *quality_image;
2750
2751 (void) CopyMagickString(preview_info->filename,
2752 preview_image->filename,MaxTextExtent);
2753 quality_image=ReadImage(preview_info,exception);
2754 if (quality_image != (Image *) NULL)
2755 {
2756 preview_image=DestroyImage(preview_image);
2757 preview_image=quality_image;
2758 }
2759 }
2760 (void) RelinquishUniqueFileResource(preview_image->filename);
2761 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002762 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002763 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2764 1024.0/1024.0);
2765 else
2766 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002767 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002768 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002769 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002770 else
cristyb51dff52011-05-19 16:55:47 +00002771 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002772 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002773 break;
2774 }
2775 }
2776 thumbnail=DestroyImage(thumbnail);
2777 percentage+=12.5;
2778 radius+=0.5;
2779 sigma+=0.25;
2780 if (preview_image == (Image *) NULL)
2781 break;
2782 (void) DeleteImageProperty(preview_image,"label");
cristyd15e6592011-10-15 00:13:06 +00002783 (void) SetImageProperty(preview_image,"label",label,exception);
cristy3ed852e2009-09-05 21:47:34 +00002784 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002785 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2786 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002787 if (proceed == MagickFalse)
2788 break;
2789 }
2790 if (images == (Image *) NULL)
2791 {
2792 preview_info=DestroyImageInfo(preview_info);
2793 return((Image *) NULL);
2794 }
2795 /*
2796 Create the montage.
2797 */
2798 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2799 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2800 montage_info->shadow=MagickTrue;
2801 (void) CloneString(&montage_info->tile,"3x3");
2802 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2803 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2804 montage_image=MontageImages(images,montage_info,exception);
2805 montage_info=DestroyMontageInfo(montage_info);
2806 images=DestroyImageList(images);
2807 if (montage_image == (Image *) NULL)
2808 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2809 if (montage_image->montage != (char *) NULL)
2810 {
2811 /*
2812 Free image directory.
2813 */
2814 montage_image->montage=(char *) RelinquishMagickMemory(
2815 montage_image->montage);
2816 if (image->directory != (char *) NULL)
2817 montage_image->directory=(char *) RelinquishMagickMemory(
2818 montage_image->directory);
2819 }
2820 preview_info=DestroyImageInfo(preview_info);
2821 return(montage_image);
2822}
2823
2824/*
2825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2826% %
2827% %
2828% %
2829% R a d i a l B l u r I m a g e %
2830% %
2831% %
2832% %
2833%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2834%
2835% RadialBlurImage() applies a radial blur to the image.
2836%
2837% Andrew Protano contributed this effect.
2838%
2839% The format of the RadialBlurImage method is:
2840%
2841% Image *RadialBlurImage(const Image *image,const double angle,
cristy6435bd92011-09-10 02:10:07 +00002842% const double blur,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002843%
2844% A description of each parameter follows:
2845%
2846% o image: the image.
2847%
cristy3ed852e2009-09-05 21:47:34 +00002848% o angle: the angle of the radial blur.
2849%
cristy6435bd92011-09-10 02:10:07 +00002850% o blur: the blur.
2851%
cristy3ed852e2009-09-05 21:47:34 +00002852% o exception: return any errors or warnings in this structure.
2853%
2854*/
cristy4282c702011-11-21 00:01:06 +00002855MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2856 const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002857{
cristyc4c8d132010-01-07 01:58:38 +00002858 CacheView
2859 *blur_view,
cristy8b49f382012-01-17 03:11:03 +00002860 *image_view,
2861 *radial_view;
cristyc4c8d132010-01-07 01:58:38 +00002862
cristy3ed852e2009-09-05 21:47:34 +00002863 Image
2864 *blur_image;
2865
cristy3ed852e2009-09-05 21:47:34 +00002866 MagickBooleanType
2867 status;
2868
cristybb503372010-05-27 20:51:26 +00002869 MagickOffsetType
2870 progress;
2871
cristy3ed852e2009-09-05 21:47:34 +00002872 MagickRealType
2873 blur_radius,
2874 *cos_theta,
2875 offset,
2876 *sin_theta,
2877 theta;
2878
2879 PointInfo
2880 blur_center;
2881
cristybb503372010-05-27 20:51:26 +00002882 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002883 i;
2884
cristybb503372010-05-27 20:51:26 +00002885 size_t
cristy3ed852e2009-09-05 21:47:34 +00002886 n;
2887
cristybb503372010-05-27 20:51:26 +00002888 ssize_t
2889 y;
2890
cristy3ed852e2009-09-05 21:47:34 +00002891 /*
2892 Allocate blur image.
2893 */
2894 assert(image != (Image *) NULL);
2895 assert(image->signature == MagickSignature);
2896 if (image->debug != MagickFalse)
2897 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2898 assert(exception != (ExceptionInfo *) NULL);
2899 assert(exception->signature == MagickSignature);
cristy1e7aa312011-09-10 20:01:36 +00002900 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002901 if (blur_image == (Image *) NULL)
2902 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002903 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002904 {
cristy3ed852e2009-09-05 21:47:34 +00002905 blur_image=DestroyImage(blur_image);
2906 return((Image *) NULL);
2907 }
2908 blur_center.x=(double) image->columns/2.0;
2909 blur_center.y=(double) image->rows/2.0;
2910 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002911 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002912 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2913 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2914 sizeof(*cos_theta));
2915 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2916 sizeof(*sin_theta));
2917 if ((cos_theta == (MagickRealType *) NULL) ||
2918 (sin_theta == (MagickRealType *) NULL))
2919 {
2920 blur_image=DestroyImage(blur_image);
2921 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2922 }
2923 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002924 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002925 {
2926 cos_theta[i]=cos((double) (theta*i-offset));
2927 sin_theta[i]=sin((double) (theta*i-offset));
2928 }
2929 /*
2930 Radial blur image.
2931 */
2932 status=MagickTrue;
2933 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002934 image_view=AcquireCacheView(image);
cristy8b49f382012-01-17 03:11:03 +00002935 radial_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00002936 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002937#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002938 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002939#endif
cristy1e7aa312011-09-10 20:01:36 +00002940 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002941 {
cristy1e7aa312011-09-10 20:01:36 +00002942 register const Quantum
2943 *restrict p;
2944
cristy4c08aed2011-07-01 19:47:50 +00002945 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002946 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002947
cristy117ff172010-08-15 21:35:32 +00002948 register ssize_t
2949 x;
2950
cristy3ed852e2009-09-05 21:47:34 +00002951 if (status == MagickFalse)
2952 continue;
cristy8b49f382012-01-17 03:11:03 +00002953 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2954 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00002955 exception);
cristy1e7aa312011-09-10 20:01:36 +00002956 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002957 {
2958 status=MagickFalse;
2959 continue;
2960 }
cristy1e7aa312011-09-10 20:01:36 +00002961 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002962 {
cristy3ed852e2009-09-05 21:47:34 +00002963 MagickRealType
cristy3ed852e2009-09-05 21:47:34 +00002964 radius;
2965
cristy3ed852e2009-09-05 21:47:34 +00002966 PointInfo
2967 center;
2968
cristybb503372010-05-27 20:51:26 +00002969 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002970 i;
2971
cristybb503372010-05-27 20:51:26 +00002972 size_t
cristy3ed852e2009-09-05 21:47:34 +00002973 step;
2974
2975 center.x=(double) x-blur_center.x;
2976 center.y=(double) y-blur_center.y;
2977 radius=hypot((double) center.x,center.y);
2978 if (radius == 0)
2979 step=1;
2980 else
2981 {
cristybb503372010-05-27 20:51:26 +00002982 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002983 if (step == 0)
2984 step=1;
2985 else
2986 if (step >= n)
2987 step=n-1;
2988 }
cristy1e7aa312011-09-10 20:01:36 +00002989 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2990 {
2991 MagickRealType
2992 gamma,
2993 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002994
cristy1e7aa312011-09-10 20:01:36 +00002995 PixelChannel
2996 channel;
2997
2998 PixelTrait
2999 blur_traits,
3000 traits;
3001
3002 register const Quantum
3003 *restrict r;
3004
3005 register ssize_t
3006 j;
3007
cristye2a912b2011-12-05 20:02:07 +00003008 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003009 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003010 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3011 if ((traits == UndefinedPixelTrait) ||
3012 (blur_traits == UndefinedPixelTrait))
3013 continue;
cristyd09f8802012-02-04 16:44:10 +00003014 if (((blur_traits & CopyPixelTrait) != 0) ||
3015 (GetPixelMask(image,p) != 0))
cristy3ed852e2009-09-05 21:47:34 +00003016 {
cristy0beccfa2011-09-25 20:47:53 +00003017 SetPixelChannel(blur_image,channel,p[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003018 continue;
cristy3ed852e2009-09-05 21:47:34 +00003019 }
cristy1e7aa312011-09-10 20:01:36 +00003020 gamma=0.0;
3021 pixel=bias;
3022 if ((blur_traits & BlendPixelTrait) == 0)
3023 {
3024 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3025 {
cristy8b49f382012-01-17 03:11:03 +00003026 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003027 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3028 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3029 1,1,exception);
3030 if (r == (const Quantum *) NULL)
3031 {
3032 status=MagickFalse;
3033 continue;
3034 }
3035 pixel+=r[i];
3036 gamma++;
3037 }
3038 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003039 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003040 continue;
3041 }
3042 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3043 {
cristy8b49f382012-01-17 03:11:03 +00003044 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
cristy1e7aa312011-09-10 20:01:36 +00003045 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3046 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3047 1,1,exception);
3048 if (r == (const Quantum *) NULL)
3049 {
3050 status=MagickFalse;
3051 continue;
3052 }
3053 pixel+=GetPixelAlpha(image,r)*r[i];
3054 gamma+=GetPixelAlpha(image,r);
cristy3ed852e2009-09-05 21:47:34 +00003055 }
cristy1e7aa312011-09-10 20:01:36 +00003056 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003057 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003058 }
3059 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003060 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003061 }
3062 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3063 status=MagickFalse;
3064 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3065 {
3066 MagickBooleanType
3067 proceed;
3068
cristyb5d5f722009-11-04 03:03:49 +00003069#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00003070 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003071#endif
3072 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3073 if (proceed == MagickFalse)
3074 status=MagickFalse;
3075 }
3076 }
3077 blur_view=DestroyCacheView(blur_view);
cristy8b49f382012-01-17 03:11:03 +00003078 radial_view=DestroyCacheView(radial_view);
cristy3ed852e2009-09-05 21:47:34 +00003079 image_view=DestroyCacheView(image_view);
3080 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3081 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3082 if (status == MagickFalse)
3083 blur_image=DestroyImage(blur_image);
3084 return(blur_image);
3085}
3086
3087/*
3088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3089% %
3090% %
3091% %
cristy3ed852e2009-09-05 21:47:34 +00003092% S e l e c t i v e B l u r I m a g e %
3093% %
3094% %
3095% %
3096%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3097%
3098% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3099% It is similar to the unsharpen mask that sharpens everything with contrast
3100% above a certain threshold.
3101%
3102% The format of the SelectiveBlurImage method is:
3103%
3104% Image *SelectiveBlurImage(const Image *image,const double radius,
cristy1e7aa312011-09-10 20:01:36 +00003105% const double sigma,const double threshold,const double bias,
3106% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003107%
3108% A description of each parameter follows:
3109%
3110% o image: the image.
3111%
cristy3ed852e2009-09-05 21:47:34 +00003112% o radius: the radius of the Gaussian, in pixels, not counting the center
3113% pixel.
3114%
3115% o sigma: the standard deviation of the Gaussian, in pixels.
3116%
3117% o threshold: only pixels within this contrast threshold are included
3118% in the blur operation.
3119%
cristy1e7aa312011-09-10 20:01:36 +00003120% o bias: the bias.
3121%
cristy3ed852e2009-09-05 21:47:34 +00003122% o exception: return any errors or warnings in this structure.
3123%
3124*/
cristy4282c702011-11-21 00:01:06 +00003125MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3126 const double sigma,const double threshold,const double bias,
3127 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003128{
3129#define SelectiveBlurImageTag "SelectiveBlur/Image"
3130
cristy47e00502009-12-17 19:19:57 +00003131 CacheView
3132 *blur_view,
3133 *image_view;
3134
cristy3ed852e2009-09-05 21:47:34 +00003135 double
cristy3ed852e2009-09-05 21:47:34 +00003136 *kernel;
3137
3138 Image
3139 *blur_image;
3140
cristy3ed852e2009-09-05 21:47:34 +00003141 MagickBooleanType
3142 status;
3143
cristybb503372010-05-27 20:51:26 +00003144 MagickOffsetType
3145 progress;
3146
cristybb503372010-05-27 20:51:26 +00003147 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003148 i;
cristy3ed852e2009-09-05 21:47:34 +00003149
cristybb503372010-05-27 20:51:26 +00003150 size_t
cristy3ed852e2009-09-05 21:47:34 +00003151 width;
3152
cristybb503372010-05-27 20:51:26 +00003153 ssize_t
cristyc8523c12011-09-13 00:02:53 +00003154 center,
cristybb503372010-05-27 20:51:26 +00003155 j,
3156 u,
3157 v,
3158 y;
3159
cristy3ed852e2009-09-05 21:47:34 +00003160 /*
3161 Initialize blur image attributes.
3162 */
3163 assert(image != (Image *) NULL);
3164 assert(image->signature == MagickSignature);
3165 if (image->debug != MagickFalse)
3166 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3167 assert(exception != (ExceptionInfo *) NULL);
3168 assert(exception->signature == MagickSignature);
3169 width=GetOptimalKernelWidth1D(radius,sigma);
cristy8b49f382012-01-17 03:11:03 +00003170 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
cristy3ed852e2009-09-05 21:47:34 +00003171 if (kernel == (double *) NULL)
3172 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003173 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003174 i=0;
cristy47e00502009-12-17 19:19:57 +00003175 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003176 {
cristy47e00502009-12-17 19:19:57 +00003177 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003178 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3179 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003180 }
3181 if (image->debug != MagickFalse)
3182 {
3183 char
3184 format[MaxTextExtent],
3185 *message;
3186
cristy117ff172010-08-15 21:35:32 +00003187 register const double
3188 *k;
3189
cristybb503372010-05-27 20:51:26 +00003190 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003191 u,
3192 v;
3193
cristy3ed852e2009-09-05 21:47:34 +00003194 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003195 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3196 width);
cristy3ed852e2009-09-05 21:47:34 +00003197 message=AcquireString("");
3198 k=kernel;
cristybb503372010-05-27 20:51:26 +00003199 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003200 {
3201 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003202 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003203 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003204 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003205 {
cristyb51dff52011-05-19 16:55:47 +00003206 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003207 (void) ConcatenateString(&message,format);
3208 }
3209 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3210 }
3211 message=DestroyString(message);
3212 }
cristy1e7aa312011-09-10 20:01:36 +00003213 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003214 if (blur_image == (Image *) NULL)
3215 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003216 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003217 {
cristy3ed852e2009-09-05 21:47:34 +00003218 blur_image=DestroyImage(blur_image);
3219 return((Image *) NULL);
3220 }
3221 /*
3222 Threshold blur image.
3223 */
3224 status=MagickTrue;
3225 progress=0;
cristyc8523c12011-09-13 00:02:53 +00003226 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3227 GetPixelChannels(image)*(width/2L));
cristy3ed852e2009-09-05 21:47:34 +00003228 image_view=AcquireCacheView(image);
3229 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003230#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003231 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003232#endif
cristybb503372010-05-27 20:51:26 +00003233 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003234 {
cristy4c08aed2011-07-01 19:47:50 +00003235 double
3236 contrast;
3237
cristy3ed852e2009-09-05 21:47:34 +00003238 MagickBooleanType
3239 sync;
3240
cristy4c08aed2011-07-01 19:47:50 +00003241 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003242 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003243
cristy4c08aed2011-07-01 19:47:50 +00003244 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003245 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003246
cristy117ff172010-08-15 21:35:32 +00003247 register ssize_t
3248 x;
3249
cristy3ed852e2009-09-05 21:47:34 +00003250 if (status == MagickFalse)
3251 continue;
cristy117ff172010-08-15 21:35:32 +00003252 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3253 (width/2L),image->columns+width,width,exception);
cristy8b49f382012-01-17 03:11:03 +00003254 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003255 exception);
cristy4c08aed2011-07-01 19:47:50 +00003256 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003257 {
3258 status=MagickFalse;
3259 continue;
3260 }
cristybb503372010-05-27 20:51:26 +00003261 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003262 {
cristybb503372010-05-27 20:51:26 +00003263 register ssize_t
cristy1e7aa312011-09-10 20:01:36 +00003264 i;
cristy3ed852e2009-09-05 21:47:34 +00003265
cristy1e7aa312011-09-10 20:01:36 +00003266 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3267 {
3268 MagickRealType
3269 alpha,
3270 gamma,
3271 intensity,
3272 pixel;
cristy117ff172010-08-15 21:35:32 +00003273
cristy1e7aa312011-09-10 20:01:36 +00003274 PixelChannel
3275 channel;
3276
3277 PixelTrait
3278 blur_traits,
3279 traits;
3280
3281 register const double
3282 *restrict k;
3283
3284 register const Quantum
3285 *restrict pixels;
3286
3287 register ssize_t
3288 u;
3289
3290 ssize_t
3291 v;
3292
cristye2a912b2011-12-05 20:02:07 +00003293 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003294 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003295 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3296 if ((traits == UndefinedPixelTrait) ||
3297 (blur_traits == UndefinedPixelTrait))
3298 continue;
cristyd09f8802012-02-04 16:44:10 +00003299 if (((blur_traits & CopyPixelTrait) != 0) ||
3300 (GetPixelMask(image,p) != 0))
cristy3ed852e2009-09-05 21:47:34 +00003301 {
cristy0beccfa2011-09-25 20:47:53 +00003302 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003303 continue;
cristy3ed852e2009-09-05 21:47:34 +00003304 }
cristy1e7aa312011-09-10 20:01:36 +00003305 k=kernel;
3306 pixel=bias;
3307 pixels=p;
cristyc8523c12011-09-13 00:02:53 +00003308 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
cristy1e7aa312011-09-10 20:01:36 +00003309 gamma=0.0;
3310 if ((blur_traits & BlendPixelTrait) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003311 {
cristy1e7aa312011-09-10 20:01:36 +00003312 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003313 {
cristy1e7aa312011-09-10 20:01:36 +00003314 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003315 {
cristy1e7aa312011-09-10 20:01:36 +00003316 contrast=GetPixelIntensity(image,pixels)-intensity;
3317 if (fabs(contrast) < threshold)
3318 {
3319 pixel+=(*k)*pixels[i];
3320 gamma+=(*k);
3321 }
3322 k++;
3323 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003324 }
cristy1e7aa312011-09-10 20:01:36 +00003325 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003326 }
cristy1e7aa312011-09-10 20:01:36 +00003327 if (fabs((double) gamma) < MagickEpsilon)
3328 {
cristy0beccfa2011-09-25 20:47:53 +00003329 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003330 continue;
3331 }
3332 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003333 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003334 continue;
3335 }
3336 for (v=0; v < (ssize_t) width; v++)
3337 {
3338 for (u=0; u < (ssize_t) width; u++)
3339 {
3340 contrast=GetPixelIntensity(image,pixels)-intensity;
3341 if (fabs(contrast) < threshold)
3342 {
3343 alpha=(MagickRealType) (QuantumScale*
3344 GetPixelAlpha(image,pixels));
3345 pixel+=(*k)*alpha*pixels[i];
3346 gamma+=(*k)*alpha;
3347 }
3348 k++;
3349 pixels+=GetPixelChannels(image);
3350 }
3351 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003352 }
cristy1e7aa312011-09-10 20:01:36 +00003353 if (fabs((double) gamma) < MagickEpsilon)
3354 {
cristy0beccfa2011-09-25 20:47:53 +00003355 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003356 continue;
3357 }
3358 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003359 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003360 }
cristyed231572011-07-14 02:18:59 +00003361 p+=GetPixelChannels(image);
3362 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003363 }
3364 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3365 if (sync == MagickFalse)
3366 status=MagickFalse;
3367 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3368 {
3369 MagickBooleanType
3370 proceed;
3371
cristyb5d5f722009-11-04 03:03:49 +00003372#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00003373 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003374#endif
3375 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3376 image->rows);
3377 if (proceed == MagickFalse)
3378 status=MagickFalse;
3379 }
3380 }
3381 blur_image->type=image->type;
3382 blur_view=DestroyCacheView(blur_view);
3383 image_view=DestroyCacheView(image_view);
cristy8b49f382012-01-17 03:11:03 +00003384 kernel=(double *) RelinquishAlignedMemory(kernel);
cristy3ed852e2009-09-05 21:47:34 +00003385 if (status == MagickFalse)
3386 blur_image=DestroyImage(blur_image);
3387 return(blur_image);
3388}
3389
3390/*
3391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3392% %
3393% %
3394% %
3395% S h a d e I m a g e %
3396% %
3397% %
3398% %
3399%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3400%
3401% ShadeImage() shines a distant light on an image to create a
3402% three-dimensional effect. You control the positioning of the light with
3403% azimuth and elevation; azimuth is measured in degrees off the x axis
3404% and elevation is measured in pixels above the Z axis.
3405%
3406% The format of the ShadeImage method is:
3407%
3408% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3409% const double azimuth,const double elevation,ExceptionInfo *exception)
3410%
3411% A description of each parameter follows:
3412%
3413% o image: the image.
3414%
3415% o gray: A value other than zero shades the intensity of each pixel.
3416%
3417% o azimuth, elevation: Define the light source direction.
3418%
3419% o exception: return any errors or warnings in this structure.
3420%
3421*/
3422MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3423 const double azimuth,const double elevation,ExceptionInfo *exception)
3424{
3425#define ShadeImageTag "Shade/Image"
3426
cristyc4c8d132010-01-07 01:58:38 +00003427 CacheView
3428 *image_view,
3429 *shade_view;
3430
cristy3ed852e2009-09-05 21:47:34 +00003431 Image
3432 *shade_image;
3433
cristy3ed852e2009-09-05 21:47:34 +00003434 MagickBooleanType
3435 status;
3436
cristybb503372010-05-27 20:51:26 +00003437 MagickOffsetType
3438 progress;
3439
cristy3ed852e2009-09-05 21:47:34 +00003440 PrimaryInfo
3441 light;
3442
cristybb503372010-05-27 20:51:26 +00003443 ssize_t
3444 y;
3445
cristy3ed852e2009-09-05 21:47:34 +00003446 /*
3447 Initialize shaded image attributes.
3448 */
3449 assert(image != (const Image *) NULL);
3450 assert(image->signature == MagickSignature);
3451 if (image->debug != MagickFalse)
3452 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3453 assert(exception != (ExceptionInfo *) NULL);
3454 assert(exception->signature == MagickSignature);
3455 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3456 if (shade_image == (Image *) NULL)
3457 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003458 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003459 {
cristy3ed852e2009-09-05 21:47:34 +00003460 shade_image=DestroyImage(shade_image);
3461 return((Image *) NULL);
3462 }
3463 /*
3464 Compute the light vector.
3465 */
3466 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3467 cos(DegreesToRadians(elevation));
3468 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3469 cos(DegreesToRadians(elevation));
3470 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3471 /*
3472 Shade image.
3473 */
3474 status=MagickTrue;
3475 progress=0;
3476 image_view=AcquireCacheView(image);
3477 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003478#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003479 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003480#endif
cristybb503372010-05-27 20:51:26 +00003481 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003482 {
3483 MagickRealType
3484 distance,
3485 normal_distance,
3486 shade;
3487
3488 PrimaryInfo
3489 normal;
3490
cristy4c08aed2011-07-01 19:47:50 +00003491 register const Quantum
cristy1e7aa312011-09-10 20:01:36 +00003492 *restrict center,
cristyc47d1f82009-11-26 01:44:43 +00003493 *restrict p,
cristy1e7aa312011-09-10 20:01:36 +00003494 *restrict post,
3495 *restrict pre;
cristy3ed852e2009-09-05 21:47:34 +00003496
cristy4c08aed2011-07-01 19:47:50 +00003497 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003498 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003499
cristy117ff172010-08-15 21:35:32 +00003500 register ssize_t
3501 x;
3502
cristy3ed852e2009-09-05 21:47:34 +00003503 if (status == MagickFalse)
3504 continue;
3505 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3506 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3507 exception);
cristy4c08aed2011-07-01 19:47:50 +00003508 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003509 {
3510 status=MagickFalse;
3511 continue;
3512 }
3513 /*
3514 Shade this row of pixels.
3515 */
3516 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristy1e7aa312011-09-10 20:01:36 +00003517 pre=p+GetPixelChannels(image);
3518 center=pre+(image->columns+2)*GetPixelChannels(image);
3519 post=center+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003520 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003521 {
cristy1e7aa312011-09-10 20:01:36 +00003522 register ssize_t
3523 i;
3524
cristy3ed852e2009-09-05 21:47:34 +00003525 /*
3526 Determine the surface normal and compute shading.
3527 */
cristy1e7aa312011-09-10 20:01:36 +00003528 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3529 GetPixelIntensity(image,center-GetPixelChannels(image))+
3530 GetPixelIntensity(image,post-GetPixelChannels(image))-
3531 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3532 GetPixelIntensity(image,center+GetPixelChannels(image))-
3533 GetPixelIntensity(image,post+GetPixelChannels(image)));
3534 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3535 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3536 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3537 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3538 GetPixelIntensity(image,pre+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003539 if ((normal.x == 0.0) && (normal.y == 0.0))
3540 shade=light.z;
3541 else
3542 {
3543 shade=0.0;
3544 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3545 if (distance > MagickEpsilon)
3546 {
3547 normal_distance=
3548 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3549 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3550 shade=distance/sqrt((double) normal_distance);
3551 }
3552 }
cristy1e7aa312011-09-10 20:01:36 +00003553 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3554 {
3555 PixelChannel
3556 channel;
3557
3558 PixelTrait
3559 shade_traits,
3560 traits;
3561
cristye2a912b2011-12-05 20:02:07 +00003562 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003563 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003564 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3565 if ((traits == UndefinedPixelTrait) ||
3566 (shade_traits == UndefinedPixelTrait))
3567 continue;
cristyd09f8802012-02-04 16:44:10 +00003568 if (((shade_traits & CopyPixelTrait) != 0) ||
3569 (GetPixelMask(image,pre) != 0))
cristy1e7aa312011-09-10 20:01:36 +00003570 {
cristy0beccfa2011-09-25 20:47:53 +00003571 SetPixelChannel(shade_image,channel,center[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003572 continue;
3573 }
3574 if (gray != MagickFalse)
3575 {
cristy0beccfa2011-09-25 20:47:53 +00003576 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
cristy1e7aa312011-09-10 20:01:36 +00003577 continue;
3578 }
cristy0beccfa2011-09-25 20:47:53 +00003579 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3580 center[i]),q);
cristy1e7aa312011-09-10 20:01:36 +00003581 }
3582 pre+=GetPixelChannels(image);
3583 center+=GetPixelChannels(image);
3584 post+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003585 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003586 }
3587 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3588 status=MagickFalse;
3589 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3590 {
3591 MagickBooleanType
3592 proceed;
3593
cristyb5d5f722009-11-04 03:03:49 +00003594#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00003595 #pragma omp critical (MagickCore_ShadeImage)
cristy3ed852e2009-09-05 21:47:34 +00003596#endif
3597 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3598 if (proceed == MagickFalse)
3599 status=MagickFalse;
3600 }
3601 }
3602 shade_view=DestroyCacheView(shade_view);
3603 image_view=DestroyCacheView(image_view);
3604 if (status == MagickFalse)
3605 shade_image=DestroyImage(shade_image);
3606 return(shade_image);
3607}
3608
3609/*
3610%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3611% %
3612% %
3613% %
3614% S h a r p e n I m a g e %
3615% %
3616% %
3617% %
3618%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3619%
3620% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3621% operator of the given radius and standard deviation (sigma). For
3622% reasonable results, radius should be larger than sigma. Use a radius of 0
3623% and SharpenImage() selects a suitable radius for you.
3624%
3625% Using a separable kernel would be faster, but the negative weights cancel
3626% out on the corners of the kernel producing often undesirable ringing in the
3627% filtered result; this can be avoided by using a 2D gaussian shaped image
3628% sharpening kernel instead.
3629%
3630% The format of the SharpenImage method is:
3631%
3632% Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003633% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003634%
3635% A description of each parameter follows:
3636%
3637% o image: the image.
3638%
cristy3ed852e2009-09-05 21:47:34 +00003639% o radius: the radius of the Gaussian, in pixels, not counting the center
3640% pixel.
3641%
3642% o sigma: the standard deviation of the Laplacian, in pixels.
3643%
cristy05c0c9a2011-09-05 23:16:13 +00003644% o bias: bias.
3645%
cristy3ed852e2009-09-05 21:47:34 +00003646% o exception: return any errors or warnings in this structure.
3647%
3648*/
cristy3ed852e2009-09-05 21:47:34 +00003649MagickExport Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003650 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003651{
cristy3ed852e2009-09-05 21:47:34 +00003652 double
cristy47e00502009-12-17 19:19:57 +00003653 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003654
3655 Image
3656 *sharp_image;
3657
cristy41cbe682011-07-15 19:12:37 +00003658 KernelInfo
3659 *kernel_info;
3660
cristybb503372010-05-27 20:51:26 +00003661 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003662 i;
3663
cristybb503372010-05-27 20:51:26 +00003664 size_t
cristy3ed852e2009-09-05 21:47:34 +00003665 width;
3666
cristy117ff172010-08-15 21:35:32 +00003667 ssize_t
3668 j,
3669 u,
3670 v;
3671
cristy3ed852e2009-09-05 21:47:34 +00003672 assert(image != (const Image *) NULL);
3673 assert(image->signature == MagickSignature);
3674 if (image->debug != MagickFalse)
3675 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3676 assert(exception != (ExceptionInfo *) NULL);
3677 assert(exception->signature == MagickSignature);
3678 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003679 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003680 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003681 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003682 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3683 kernel_info->width=width;
3684 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00003685 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
cristy41cbe682011-07-15 19:12:37 +00003686 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00003687 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3688 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3689 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00003690 {
3691 kernel_info=DestroyKernelInfo(kernel_info);
3692 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3693 }
cristy3ed852e2009-09-05 21:47:34 +00003694 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003695 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003696 i=0;
3697 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003698 {
cristy47e00502009-12-17 19:19:57 +00003699 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003700 {
cristy41cbe682011-07-15 19:12:37 +00003701 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3702 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3703 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003704 i++;
3705 }
3706 }
cristy41cbe682011-07-15 19:12:37 +00003707 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003708 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003709 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003710 return(sharp_image);
3711}
3712
3713/*
3714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3715% %
3716% %
3717% %
3718% S p r e a d I m a g e %
3719% %
3720% %
3721% %
3722%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3723%
3724% SpreadImage() is a special effects method that randomly displaces each
3725% pixel in a block defined by the radius parameter.
3726%
3727% The format of the SpreadImage method is:
3728%
3729% Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003730% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003731%
3732% A description of each parameter follows:
3733%
3734% o image: the image.
3735%
cristy5c4e2582011-09-11 19:21:03 +00003736% o radius: choose a random pixel in a neighborhood of this extent.
3737%
3738% o method: the pixel interpolation method.
cristy3ed852e2009-09-05 21:47:34 +00003739%
3740% o exception: return any errors or warnings in this structure.
3741%
3742*/
3743MagickExport Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003744 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003745{
3746#define SpreadImageTag "Spread/Image"
3747
cristyfa112112010-01-04 17:48:07 +00003748 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003749 *image_view,
3750 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003751
cristy3ed852e2009-09-05 21:47:34 +00003752 Image
3753 *spread_image;
3754
cristy3ed852e2009-09-05 21:47:34 +00003755 MagickBooleanType
3756 status;
3757
cristybb503372010-05-27 20:51:26 +00003758 MagickOffsetType
3759 progress;
3760
cristy3ed852e2009-09-05 21:47:34 +00003761 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003762 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003763
cristybb503372010-05-27 20:51:26 +00003764 size_t
cristy3ed852e2009-09-05 21:47:34 +00003765 width;
3766
cristybb503372010-05-27 20:51:26 +00003767 ssize_t
3768 y;
3769
cristy3ed852e2009-09-05 21:47:34 +00003770 /*
3771 Initialize spread image attributes.
3772 */
3773 assert(image != (Image *) NULL);
3774 assert(image->signature == MagickSignature);
3775 if (image->debug != MagickFalse)
3776 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3777 assert(exception != (ExceptionInfo *) NULL);
3778 assert(exception->signature == MagickSignature);
3779 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3780 exception);
3781 if (spread_image == (Image *) NULL)
3782 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003783 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003784 {
cristy3ed852e2009-09-05 21:47:34 +00003785 spread_image=DestroyImage(spread_image);
3786 return((Image *) NULL);
3787 }
3788 /*
3789 Spread image.
3790 */
3791 status=MagickTrue;
3792 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003793 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003794 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003795 image_view=AcquireCacheView(image);
3796 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003797#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy78778cb2012-01-17 14:48:47 +00003798 #pragma omp parallel for schedule(static,8) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003799#endif
cristybe82ad52011-09-06 01:17:20 +00003800 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003801 {
cristy5c9e6f22010-09-17 17:31:01 +00003802 const int
3803 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003804
cristybe82ad52011-09-06 01:17:20 +00003805 register const Quantum
3806 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003807
cristy4c08aed2011-07-01 19:47:50 +00003808 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003809 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003810
cristy117ff172010-08-15 21:35:32 +00003811 register ssize_t
3812 x;
3813
cristy3ed852e2009-09-05 21:47:34 +00003814 if (status == MagickFalse)
3815 continue;
cristybe82ad52011-09-06 01:17:20 +00003816 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy9f7e7cb2011-03-26 00:49:57 +00003817 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003818 exception);
cristybe82ad52011-09-06 01:17:20 +00003819 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003820 {
3821 status=MagickFalse;
3822 continue;
3823 }
cristybe82ad52011-09-06 01:17:20 +00003824 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003825 {
cristybe82ad52011-09-06 01:17:20 +00003826 PointInfo
3827 point;
3828
cristybe82ad52011-09-06 01:17:20 +00003829 point.x=GetPseudoRandomValue(random_info[id]);
3830 point.y=GetPseudoRandomValue(random_info[id]);
cristy5c4e2582011-09-11 19:21:03 +00003831 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3832 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
cristyed231572011-07-14 02:18:59 +00003833 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003834 }
cristy9f7e7cb2011-03-26 00:49:57 +00003835 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003836 status=MagickFalse;
3837 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3838 {
3839 MagickBooleanType
3840 proceed;
3841
cristyb557a152011-02-22 12:14:30 +00003842#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00003843 #pragma omp critical (MagickCore_SpreadImage)
cristy3ed852e2009-09-05 21:47:34 +00003844#endif
3845 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3846 if (proceed == MagickFalse)
3847 status=MagickFalse;
3848 }
3849 }
cristy9f7e7cb2011-03-26 00:49:57 +00003850 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003851 image_view=DestroyCacheView(image_view);
3852 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003853 return(spread_image);
3854}
3855
3856/*
3857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3858% %
3859% %
3860% %
3861% U n s h a r p M a s k I m a g e %
3862% %
3863% %
3864% %
3865%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3866%
3867% UnsharpMaskImage() sharpens one or more image channels. We convolve the
3868% image with a Gaussian operator of the given radius and standard deviation
3869% (sigma). For reasonable results, radius should be larger than sigma. Use a
3870% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3871%
3872% The format of the UnsharpMaskImage method is:
3873%
3874% Image *UnsharpMaskImage(const Image *image,const double radius,
3875% const double sigma,const double amount,const double threshold,
3876% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003877%
3878% A description of each parameter follows:
3879%
3880% o image: the image.
3881%
cristy3ed852e2009-09-05 21:47:34 +00003882% o radius: the radius of the Gaussian, in pixels, not counting the center
3883% pixel.
3884%
3885% o sigma: the standard deviation of the Gaussian, in pixels.
3886%
3887% o amount: the percentage of the difference between the original and the
3888% blur image that is added back into the original.
3889%
3890% o threshold: the threshold in pixels needed to apply the diffence amount.
3891%
3892% o exception: return any errors or warnings in this structure.
3893%
3894*/
cristy31bb6272011-11-20 23:57:25 +00003895MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3896 const double sigma,const double amount,const double threshold,
3897 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003898{
3899#define SharpenImageTag "Sharpen/Image"
3900
cristyc4c8d132010-01-07 01:58:38 +00003901 CacheView
3902 *image_view,
3903 *unsharp_view;
3904
cristy3ed852e2009-09-05 21:47:34 +00003905 Image
3906 *unsharp_image;
3907
cristy3ed852e2009-09-05 21:47:34 +00003908 MagickBooleanType
3909 status;
3910
cristybb503372010-05-27 20:51:26 +00003911 MagickOffsetType
3912 progress;
3913
cristy3ed852e2009-09-05 21:47:34 +00003914 MagickRealType
3915 quantum_threshold;
3916
cristybb503372010-05-27 20:51:26 +00003917 ssize_t
3918 y;
3919
cristy3ed852e2009-09-05 21:47:34 +00003920 assert(image != (const Image *) NULL);
3921 assert(image->signature == MagickSignature);
3922 if (image->debug != MagickFalse)
3923 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3924 assert(exception != (ExceptionInfo *) NULL);
cristy05c0c9a2011-09-05 23:16:13 +00003925 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00003926 if (unsharp_image == (Image *) NULL)
3927 return((Image *) NULL);
3928 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3929 /*
3930 Unsharp-mask image.
3931 */
3932 status=MagickTrue;
3933 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003934 image_view=AcquireCacheView(image);
3935 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00003936#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003937 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003938#endif
cristybb503372010-05-27 20:51:26 +00003939 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003940 {
cristy4c08aed2011-07-01 19:47:50 +00003941 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003942 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003943
cristy4c08aed2011-07-01 19:47:50 +00003944 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003945 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003946
cristy117ff172010-08-15 21:35:32 +00003947 register ssize_t
3948 x;
3949
cristy3ed852e2009-09-05 21:47:34 +00003950 if (status == MagickFalse)
3951 continue;
3952 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy8b49f382012-01-17 03:11:03 +00003953 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003954 exception);
cristy4c08aed2011-07-01 19:47:50 +00003955 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003956 {
3957 status=MagickFalse;
3958 continue;
3959 }
cristybb503372010-05-27 20:51:26 +00003960 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003961 {
cristy7f3a0d12011-09-05 23:27:59 +00003962 register ssize_t
3963 i;
3964
3965 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3966 {
3967 MagickRealType
3968 pixel;
3969
3970 PixelChannel
3971 channel;
3972
3973 PixelTrait
3974 traits,
3975 unsharp_traits;
3976
cristye2a912b2011-12-05 20:02:07 +00003977 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003978 traits=GetPixelChannelMapTraits(image,channel);
cristy7f3a0d12011-09-05 23:27:59 +00003979 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3980 if ((traits == UndefinedPixelTrait) ||
3981 (unsharp_traits == UndefinedPixelTrait))
3982 continue;
cristyd09f8802012-02-04 16:44:10 +00003983 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3984 (GetPixelMask(image,p) != 0))
cristy7f3a0d12011-09-05 23:27:59 +00003985 {
cristy0beccfa2011-09-25 20:47:53 +00003986 SetPixelChannel(unsharp_image,channel,p[i],q);
cristy7f3a0d12011-09-05 23:27:59 +00003987 continue;
3988 }
cristy0beccfa2011-09-25 20:47:53 +00003989 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
cristy7f3a0d12011-09-05 23:27:59 +00003990 if (fabs(2.0*pixel) < quantum_threshold)
3991 pixel=(MagickRealType) p[i];
3992 else
3993 pixel=(MagickRealType) p[i]+amount*pixel;
cristy0beccfa2011-09-25 20:47:53 +00003994 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
cristy7f3a0d12011-09-05 23:27:59 +00003995 }
cristyed231572011-07-14 02:18:59 +00003996 p+=GetPixelChannels(image);
3997 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00003998 }
3999 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
4000 status=MagickFalse;
4001 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4002 {
4003 MagickBooleanType
4004 proceed;
4005
cristyb5d5f722009-11-04 03:03:49 +00004006#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf8561542012-01-24 00:26:46 +00004007 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00004008#endif
4009 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4010 if (proceed == MagickFalse)
4011 status=MagickFalse;
4012 }
4013 }
4014 unsharp_image->type=image->type;
4015 unsharp_view=DestroyCacheView(unsharp_view);
4016 image_view=DestroyCacheView(image_view);
4017 if (status == MagickFalse)
4018 unsharp_image=DestroyImage(unsharp_image);
4019 return(unsharp_image);
4020}