blob: 0ce9717ec753f290164153276ffb99f7de7f1b47 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% EEEEE FFFFF FFFFF EEEEE CCCC TTTTT %
7% E F F E C T %
8% EEE FFF FFF EEE C T %
9% E F F E C T %
10% EEEEE F F EEEEE CCCC T %
11% %
12% %
13% MagickCore Image Effects Methods %
14% %
15% Software Design %
16% John Cristy %
17% October 1996 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/accelerate.h"
45#include "MagickCore/blob.h"
46#include "MagickCore/cache-view.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colorspace.h"
50#include "MagickCore/constitute.h"
51#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000052#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/draw.h"
54#include "MagickCore/enhance.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/exception-private.h"
57#include "MagickCore/effect.h"
58#include "MagickCore/fx.h"
59#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000060#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000061#include "MagickCore/geometry.h"
62#include "MagickCore/image-private.h"
63#include "MagickCore/list.h"
64#include "MagickCore/log.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/monitor.h"
67#include "MagickCore/monitor-private.h"
68#include "MagickCore/montage.h"
69#include "MagickCore/morphology.h"
70#include "MagickCore/paint.h"
71#include "MagickCore/pixel-accessor.h"
72#include "MagickCore/property.h"
73#include "MagickCore/quantize.h"
74#include "MagickCore/quantum.h"
75#include "MagickCore/quantum-private.h"
76#include "MagickCore/random_.h"
77#include "MagickCore/random-private.h"
78#include "MagickCore/resample.h"
79#include "MagickCore/resample-private.h"
80#include "MagickCore/resize.h"
81#include "MagickCore/resource_.h"
82#include "MagickCore/segment.h"
cristy31bbf2f2011-11-17 13:19:37 +000083#include "MagickCore/shear.h"
cristy4c08aed2011-07-01 19:47:50 +000084#include "MagickCore/signature-private.h"
cristy99bd5232011-12-07 14:38:20 +000085#include "MagickCore/statistic.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/string_.h"
87#include "MagickCore/thread-private.h"
88#include "MagickCore/transform.h"
89#include "MagickCore/threshold.h"
cristy3ed852e2009-09-05 21:47:34 +000090
91/*
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93% %
94% %
95% %
96% A d a p t i v e B l u r I m a g e %
97% %
98% %
99% %
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101%
102% AdaptiveBlurImage() adaptively blurs the image by blurring less
103% intensely near image edges and more intensely far from edges. We blur the
104% image with a Gaussian operator of the given radius and standard deviation
105% (sigma). For reasonable results, radius should be larger than sigma. Use a
106% radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
107%
108% The format of the AdaptiveBlurImage method is:
109%
110% Image *AdaptiveBlurImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000111% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000112%
113% A description of each parameter follows:
114%
115% o image: the image.
116%
cristy3ed852e2009-09-05 21:47:34 +0000117% o radius: the radius of the Gaussian, in pixels, not counting the center
118% pixel.
119%
120% o sigma: the standard deviation of the Laplacian, in pixels.
121%
cristy4c11c2b2011-09-05 20:17:07 +0000122% o bias: the bias.
123%
cristy3ed852e2009-09-05 21:47:34 +0000124% o exception: return any errors or warnings in this structure.
125%
126*/
127
cristyf89cb1d2011-07-07 01:24:37 +0000128MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
cristy051718b2011-08-28 22:49:25 +0000129 const char *levels,ExceptionInfo *exception)
cristyf89cb1d2011-07-07 01:24:37 +0000130{
131 double
132 black_point,
133 gamma,
134 white_point;
135
136 GeometryInfo
137 geometry_info;
138
139 MagickBooleanType
140 status;
141
142 MagickStatusType
143 flags;
144
145 /*
146 Parse levels.
147 */
148 if (levels == (char *) NULL)
149 return(MagickFalse);
150 flags=ParseGeometry(levels,&geometry_info);
151 black_point=geometry_info.rho;
152 white_point=(double) QuantumRange;
153 if ((flags & SigmaValue) != 0)
154 white_point=geometry_info.sigma;
155 gamma=1.0;
156 if ((flags & XiValue) != 0)
157 gamma=geometry_info.xi;
158 if ((flags & PercentValue) != 0)
159 {
160 black_point*=(double) image->columns*image->rows/100.0;
161 white_point*=(double) image->columns*image->rows/100.0;
162 }
163 if ((flags & SigmaValue) == 0)
164 white_point=(double) QuantumRange-black_point;
165 if ((flags & AspectValue ) == 0)
cristy7c0a0a42011-08-23 17:57:25 +0000166 status=LevelImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000167 else
cristy7c0a0a42011-08-23 17:57:25 +0000168 status=LevelizeImage(image,black_point,white_point,gamma,exception);
cristyf89cb1d2011-07-07 01:24:37 +0000169 return(status);
170}
171
cristy4282c702011-11-21 00:01:06 +0000172MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
173 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000174{
175#define AdaptiveBlurImageTag "Convolve/Image"
176#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
177
cristyc4c8d132010-01-07 01:58:38 +0000178 CacheView
179 *blur_view,
180 *edge_view,
181 *image_view;
182
cristy3ed852e2009-09-05 21:47:34 +0000183 double
cristy47e00502009-12-17 19:19:57 +0000184 **kernel,
185 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000186
187 Image
188 *blur_image,
189 *edge_image,
190 *gaussian_image;
191
cristy3ed852e2009-09-05 21:47:34 +0000192 MagickBooleanType
193 status;
194
cristybb503372010-05-27 20:51:26 +0000195 MagickOffsetType
196 progress;
197
cristybb503372010-05-27 20:51:26 +0000198 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000199 i;
cristy3ed852e2009-09-05 21:47:34 +0000200
cristybb503372010-05-27 20:51:26 +0000201 size_t
cristy3ed852e2009-09-05 21:47:34 +0000202 width;
203
cristybb503372010-05-27 20:51:26 +0000204 ssize_t
205 j,
206 k,
207 u,
208 v,
209 y;
210
cristy3ed852e2009-09-05 21:47:34 +0000211 assert(image != (const Image *) NULL);
212 assert(image->signature == MagickSignature);
213 if (image->debug != MagickFalse)
214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
215 assert(exception != (ExceptionInfo *) NULL);
216 assert(exception->signature == MagickSignature);
217 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
218 if (blur_image == (Image *) NULL)
219 return((Image *) NULL);
220 if (fabs(sigma) <= MagickEpsilon)
221 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000222 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000223 {
cristy3ed852e2009-09-05 21:47:34 +0000224 blur_image=DestroyImage(blur_image);
225 return((Image *) NULL);
226 }
227 /*
228 Edge detect the image brighness channel, level, blur, and level again.
229 */
cristy8ae632d2011-09-05 17:29:53 +0000230 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000231 if (edge_image == (Image *) NULL)
232 {
233 blur_image=DestroyImage(blur_image);
234 return((Image *) NULL);
235 }
cristy051718b2011-08-28 22:49:25 +0000236 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristy05c0c9a2011-09-05 23:16:13 +0000237 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +0000238 if (gaussian_image != (Image *) NULL)
239 {
240 edge_image=DestroyImage(edge_image);
241 edge_image=gaussian_image;
242 }
cristy051718b2011-08-28 22:49:25 +0000243 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000244 /*
245 Create a set of kernels from maximum (radius,sigma) to minimum.
246 */
247 width=GetOptimalKernelWidth2D(radius,sigma);
248 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
249 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 {
258 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
259 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)
284 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
285 kernel=(double **) RelinquishMagickMemory(kernel);
286 edge_image=DestroyImage(edge_image);
287 blur_image=DestroyImage(blur_image);
288 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
289 }
290 /*
291 Adaptively blur image.
292 */
293 status=MagickTrue;
294 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000295 image_view=AcquireCacheView(image);
296 edge_view=AcquireCacheView(edge_image);
297 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000298#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000299 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000300#endif
cristybb503372010-05-27 20:51:26 +0000301 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
cristy4c08aed2011-07-01 19:47:50 +0000303 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000304 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000305
cristy4c08aed2011-07-01 19:47:50 +0000306 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000307 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000308
cristy117ff172010-08-15 21:35:32 +0000309 register ssize_t
310 x;
311
cristy3ed852e2009-09-05 21:47:34 +0000312 if (status == MagickFalse)
313 continue;
314 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
315 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
316 exception);
cristyacd2ed22011-08-30 01:44:23 +0000317 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000318 {
319 status=MagickFalse;
320 continue;
321 }
cristybb503372010-05-27 20:51:26 +0000322 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000323 {
cristy4c11c2b2011-09-05 20:17:07 +0000324 register const Quantum
325 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000326
cristybb503372010-05-27 20:51:26 +0000327 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000328 i;
cristy3ed852e2009-09-05 21:47:34 +0000329
cristy4c11c2b2011-09-05 20:17:07 +0000330 ssize_t
331 center,
332 j;
333
334 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000335 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000336 if (j < 0)
337 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000338 else
cristy4c11c2b2011-09-05 20:17:07 +0000339 if (j > (ssize_t) width)
340 j=(ssize_t) width;
341 if ((j & 0x01) != 0)
342 j--;
343 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
344 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000345 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000346 break;
cristy4c11c2b2011-09-05 20:17:07 +0000347 center=(ssize_t) GetPixelChannels(image)*(width-j)*
cristy075ff302011-09-07 01:51:24 +0000348 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2L);
cristy4c11c2b2011-09-05 20:17:07 +0000349 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristy4c11c2b2011-09-05 20:17:07 +0000351 MagickRealType
352 alpha,
353 gamma,
354 pixel;
355
356 PixelChannel
357 channel;
358
359 PixelTrait
360 blur_traits,
361 traits;
362
363 register const double
364 *restrict k;
365
366 register const Quantum
367 *restrict pixels;
368
369 register ssize_t
370 u;
371
372 ssize_t
373 v;
374
cristye2a912b2011-12-05 20:02:07 +0000375 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000376 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000377 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
378 if ((traits == UndefinedPixelTrait) ||
379 (blur_traits == UndefinedPixelTrait))
380 continue;
381 if ((blur_traits & CopyPixelTrait) != 0)
382 {
cristy0beccfa2011-09-25 20:47:53 +0000383 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000384 continue;
385 }
386 k=kernel[j];
387 pixels=p;
388 pixel=bias;
389 gamma=0.0;
390 if ((blur_traits & BlendPixelTrait) == 0)
391 {
392 /*
393 No alpha blending.
394 */
395 for (v=0; v < (ssize_t) (width-j); v++)
396 {
397 for (u=0; u < (ssize_t) (width-j); u++)
398 {
399 pixel+=(*k)*pixels[i];
400 gamma+=(*k);
401 k++;
402 pixels+=GetPixelChannels(image);
403 }
404 }
405 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000406 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000407 continue;
408 }
409 /*
410 Alpha blending.
411 */
412 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000413 {
cristy4c11c2b2011-09-05 20:17:07 +0000414 for (u=0; u < (ssize_t) (width-j); u++)
415 {
416 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
417 pixel+=(*k)*alpha*pixels[i];
418 gamma+=(*k)*alpha;
419 k++;
420 pixels+=GetPixelChannels(image);
421 }
cristy3ed852e2009-09-05 21:47:34 +0000422 }
cristy4c11c2b2011-09-05 20:17:07 +0000423 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000424 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000425 }
cristyed231572011-07-14 02:18:59 +0000426 q+=GetPixelChannels(blur_image);
427 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000428 }
429 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
430 status=MagickFalse;
431 if (image->progress_monitor != (MagickProgressMonitor) NULL)
432 {
433 MagickBooleanType
434 proceed;
435
cristyb5d5f722009-11-04 03:03:49 +0000436#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy8ae632d2011-09-05 17:29:53 +0000437 #pragma omp critical (MagickCore_AdaptiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +0000438#endif
439 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
440 image->rows);
441 if (proceed == MagickFalse)
442 status=MagickFalse;
443 }
444 }
445 blur_image->type=image->type;
446 blur_view=DestroyCacheView(blur_view);
447 edge_view=DestroyCacheView(edge_view);
448 image_view=DestroyCacheView(image_view);
449 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000450 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000451 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
452 kernel=(double **) RelinquishMagickMemory(kernel);
453 if (status == MagickFalse)
454 blur_image=DestroyImage(blur_image);
455 return(blur_image);
456}
457
458/*
459%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460% %
461% %
462% %
463% A d a p t i v e S h a r p e n I m a g e %
464% %
465% %
466% %
467%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468%
469% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
470% intensely near image edges and less intensely far from edges. We sharpen the
471% image with a Gaussian operator of the given radius and standard deviation
472% (sigma). For reasonable results, radius should be larger than sigma. Use a
473% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
474%
475% The format of the AdaptiveSharpenImage method is:
476%
477% Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000478% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000479%
480% A description of each parameter follows:
481%
482% o image: the image.
483%
cristy3ed852e2009-09-05 21:47:34 +0000484% o radius: the radius of the Gaussian, in pixels, not counting the center
485% pixel.
486%
487% o sigma: the standard deviation of the Laplacian, in pixels.
488%
cristy4c11c2b2011-09-05 20:17:07 +0000489% o bias: the bias.
490%
cristy3ed852e2009-09-05 21:47:34 +0000491% o exception: return any errors or warnings in this structure.
492%
493*/
cristy3ed852e2009-09-05 21:47:34 +0000494MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
cristy4c11c2b2011-09-05 20:17:07 +0000495 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000496{
cristy3ed852e2009-09-05 21:47:34 +0000497#define AdaptiveSharpenImageTag "Convolve/Image"
498#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
499
cristyc4c8d132010-01-07 01:58:38 +0000500 CacheView
501 *sharp_view,
502 *edge_view,
503 *image_view;
504
cristy3ed852e2009-09-05 21:47:34 +0000505 double
cristy47e00502009-12-17 19:19:57 +0000506 **kernel,
507 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000508
509 Image
510 *sharp_image,
511 *edge_image,
512 *gaussian_image;
513
cristy3ed852e2009-09-05 21:47:34 +0000514 MagickBooleanType
515 status;
516
cristybb503372010-05-27 20:51:26 +0000517 MagickOffsetType
518 progress;
519
cristybb503372010-05-27 20:51:26 +0000520 register ssize_t
cristy47e00502009-12-17 19:19:57 +0000521 i;
cristy3ed852e2009-09-05 21:47:34 +0000522
cristybb503372010-05-27 20:51:26 +0000523 size_t
cristy3ed852e2009-09-05 21:47:34 +0000524 width;
525
cristybb503372010-05-27 20:51:26 +0000526 ssize_t
527 j,
528 k,
529 u,
530 v,
531 y;
532
cristy3ed852e2009-09-05 21:47:34 +0000533 assert(image != (const Image *) NULL);
534 assert(image->signature == MagickSignature);
535 if (image->debug != MagickFalse)
536 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
537 assert(exception != (ExceptionInfo *) NULL);
538 assert(exception->signature == MagickSignature);
539 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
540 if (sharp_image == (Image *) NULL)
541 return((Image *) NULL);
542 if (fabs(sigma) <= MagickEpsilon)
543 return(sharp_image);
cristy574cc262011-08-05 01:23:58 +0000544 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
cristy3ed852e2009-09-05 21:47:34 +0000546 sharp_image=DestroyImage(sharp_image);
547 return((Image *) NULL);
548 }
549 /*
550 Edge detect the image brighness channel, level, sharp, and level again.
551 */
cristy8ae632d2011-09-05 17:29:53 +0000552 edge_image=EdgeImage(image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000553 if (edge_image == (Image *) NULL)
554 {
555 sharp_image=DestroyImage(sharp_image);
556 return((Image *) NULL);
557 }
cristy051718b2011-08-28 22:49:25 +0000558 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
cristy05c0c9a2011-09-05 23:16:13 +0000559 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +0000560 if (gaussian_image != (Image *) NULL)
561 {
562 edge_image=DestroyImage(edge_image);
563 edge_image=gaussian_image;
564 }
cristy051718b2011-08-28 22:49:25 +0000565 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
cristy3ed852e2009-09-05 21:47:34 +0000566 /*
567 Create a set of kernels from maximum (radius,sigma) to minimum.
568 */
569 width=GetOptimalKernelWidth2D(radius,sigma);
570 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
571 if (kernel == (double **) NULL)
572 {
573 edge_image=DestroyImage(edge_image);
574 sharp_image=DestroyImage(sharp_image);
575 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
576 }
577 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
cristybb503372010-05-27 20:51:26 +0000578 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000579 {
580 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
581 sizeof(**kernel));
582 if (kernel[i] == (double *) NULL)
583 break;
cristy47e00502009-12-17 19:19:57 +0000584 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000585 j=(ssize_t) (width-i)/2;
cristy47e00502009-12-17 19:19:57 +0000586 k=0;
587 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000588 {
cristy47e00502009-12-17 19:19:57 +0000589 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000590 {
cristy4205a3c2010-09-12 20:19:59 +0000591 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
592 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +0000593 normalize+=kernel[i][k];
594 k++;
cristy3ed852e2009-09-05 21:47:34 +0000595 }
596 }
cristy3ed852e2009-09-05 21:47:34 +0000597 if (fabs(normalize) <= MagickEpsilon)
598 normalize=1.0;
599 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000600 for (k=0; k < (j*j); k++)
601 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000602 }
cristybb503372010-05-27 20:51:26 +0000603 if (i < (ssize_t) width)
cristy3ed852e2009-09-05 21:47:34 +0000604 {
605 for (i-=2; i >= 0; i-=2)
606 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
607 kernel=(double **) RelinquishMagickMemory(kernel);
608 edge_image=DestroyImage(edge_image);
609 sharp_image=DestroyImage(sharp_image);
610 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
611 }
612 /*
613 Adaptively sharpen image.
614 */
615 status=MagickTrue;
616 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000617 image_view=AcquireCacheView(image);
618 edge_view=AcquireCacheView(edge_image);
619 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000620#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000621 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000622#endif
cristybb503372010-05-27 20:51:26 +0000623 for (y=0; y < (ssize_t) sharp_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000624 {
cristy4c08aed2011-07-01 19:47:50 +0000625 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000626 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000627
cristy4c08aed2011-07-01 19:47:50 +0000628 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000629 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000630
cristy117ff172010-08-15 21:35:32 +0000631 register ssize_t
632 x;
633
cristy3ed852e2009-09-05 21:47:34 +0000634 if (status == MagickFalse)
635 continue;
636 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
637 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
638 exception);
cristy4c08aed2011-07-01 19:47:50 +0000639 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000640 {
641 status=MagickFalse;
642 continue;
643 }
cristybb503372010-05-27 20:51:26 +0000644 for (x=0; x < (ssize_t) sharp_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000645 {
cristy4c11c2b2011-09-05 20:17:07 +0000646 register const Quantum
647 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000648
cristybb503372010-05-27 20:51:26 +0000649 register ssize_t
cristy4c11c2b2011-09-05 20:17:07 +0000650 i;
cristy3ed852e2009-09-05 21:47:34 +0000651
cristy4c11c2b2011-09-05 20:17:07 +0000652 ssize_t
653 center,
654 j;
655
656 j=(ssize_t) ceil((double) width*QuantumScale*
cristy4c08aed2011-07-01 19:47:50 +0000657 GetPixelIntensity(edge_image,r)-0.5);
cristy4c11c2b2011-09-05 20:17:07 +0000658 if (j < 0)
659 j=0;
cristy3ed852e2009-09-05 21:47:34 +0000660 else
cristy4c11c2b2011-09-05 20:17:07 +0000661 if (j > (ssize_t) width)
662 j=(ssize_t) width;
663 if ((j & 0x01) != 0)
664 j--;
665 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
666 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
cristy4c08aed2011-07-01 19:47:50 +0000667 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000668 break;
cristy4c11c2b2011-09-05 20:17:07 +0000669 center=(ssize_t) GetPixelChannels(image)*(width-j)*
670 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2);
671 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000672 {
cristy4c11c2b2011-09-05 20:17:07 +0000673 MagickRealType
674 alpha,
675 gamma,
676 pixel;
677
678 PixelChannel
679 channel;
680
681 PixelTrait
682 sharp_traits,
683 traits;
684
685 register const double
686 *restrict k;
687
688 register const Quantum
689 *restrict pixels;
690
691 register ssize_t
692 u;
693
694 ssize_t
695 v;
696
cristye2a912b2011-12-05 20:02:07 +0000697 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000698 traits=GetPixelChannelMapTraits(image,channel);
cristy4c11c2b2011-09-05 20:17:07 +0000699 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
700 if ((traits == UndefinedPixelTrait) ||
701 (sharp_traits == UndefinedPixelTrait))
702 continue;
703 if ((sharp_traits & CopyPixelTrait) != 0)
704 {
cristy0beccfa2011-09-25 20:47:53 +0000705 SetPixelChannel(sharp_image,channel,p[center+i],q);
cristy4c11c2b2011-09-05 20:17:07 +0000706 continue;
707 }
708 k=kernel[j];
709 pixels=p;
710 pixel=bias;
711 gamma=0.0;
712 if ((sharp_traits & BlendPixelTrait) == 0)
713 {
714 /*
715 No alpha blending.
716 */
717 for (v=0; v < (ssize_t) (width-j); v++)
718 {
719 for (u=0; u < (ssize_t) (width-j); u++)
720 {
721 pixel+=(*k)*pixels[i];
722 gamma+=(*k);
723 k++;
724 pixels+=GetPixelChannels(image);
725 }
726 }
727 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000728 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy4c11c2b2011-09-05 20:17:07 +0000729 continue;
730 }
731 /*
732 Alpha blending.
733 */
734 for (v=0; v < (ssize_t) (width-j); v++)
cristy3ed852e2009-09-05 21:47:34 +0000735 {
cristy4c11c2b2011-09-05 20:17:07 +0000736 for (u=0; u < (ssize_t) (width-j); u++)
737 {
738 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
739 pixel+=(*k)*alpha*pixels[i];
740 gamma+=(*k)*alpha;
741 k++;
742 pixels+=GetPixelChannels(image);
743 }
cristy3ed852e2009-09-05 21:47:34 +0000744 }
cristy4c11c2b2011-09-05 20:17:07 +0000745 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +0000746 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000747 }
cristyed231572011-07-14 02:18:59 +0000748 q+=GetPixelChannels(sharp_image);
749 r+=GetPixelChannels(edge_image);
cristy3ed852e2009-09-05 21:47:34 +0000750 }
751 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
752 status=MagickFalse;
753 if (image->progress_monitor != (MagickProgressMonitor) NULL)
754 {
755 MagickBooleanType
756 proceed;
757
cristyb5d5f722009-11-04 03:03:49 +0000758#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +0000759 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
cristy3ed852e2009-09-05 21:47:34 +0000760#endif
761 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
762 image->rows);
763 if (proceed == MagickFalse)
764 status=MagickFalse;
765 }
766 }
767 sharp_image->type=image->type;
768 sharp_view=DestroyCacheView(sharp_view);
769 edge_view=DestroyCacheView(edge_view);
770 image_view=DestroyCacheView(image_view);
771 edge_image=DestroyImage(edge_image);
cristybb503372010-05-27 20:51:26 +0000772 for (i=0; i < (ssize_t) width; i+=2)
cristy3ed852e2009-09-05 21:47:34 +0000773 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
774 kernel=(double **) RelinquishMagickMemory(kernel);
775 if (status == MagickFalse)
776 sharp_image=DestroyImage(sharp_image);
777 return(sharp_image);
778}
779
780/*
781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782% %
783% %
784% %
785% B l u r I m a g e %
786% %
787% %
788% %
789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790%
791% BlurImage() blurs an image. We convolve the image with a Gaussian operator
792% of the given radius and standard deviation (sigma). For reasonable results,
793% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
794% selects a suitable radius for you.
795%
796% BlurImage() differs from GaussianBlurImage() in that it uses a separable
797% kernel which is faster but mathematically equivalent to the non-separable
798% kernel.
799%
800% The format of the BlurImage method is:
801%
802% Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000803% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000804%
805% A description of each parameter follows:
806%
807% o image: the image.
808%
cristy3ed852e2009-09-05 21:47:34 +0000809% o radius: the radius of the Gaussian, in pixels, not counting the center
810% pixel.
811%
812% o sigma: the standard deviation of the Gaussian, in pixels.
813%
cristy05c0c9a2011-09-05 23:16:13 +0000814% o bias: the bias.
815%
cristy3ed852e2009-09-05 21:47:34 +0000816% o exception: return any errors or warnings in this structure.
817%
818*/
819
cristybb503372010-05-27 20:51:26 +0000820static double *GetBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000821{
cristy3ed852e2009-09-05 21:47:34 +0000822 double
cristy47e00502009-12-17 19:19:57 +0000823 *kernel,
824 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000825
cristy117ff172010-08-15 21:35:32 +0000826 register ssize_t
827 i;
828
cristybb503372010-05-27 20:51:26 +0000829 ssize_t
cristy47e00502009-12-17 19:19:57 +0000830 j,
831 k;
cristy3ed852e2009-09-05 21:47:34 +0000832
cristy3ed852e2009-09-05 21:47:34 +0000833 /*
834 Generate a 1-D convolution kernel.
835 */
836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
837 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
838 if (kernel == (double *) NULL)
839 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000840 normalize=0.0;
cristybb503372010-05-27 20:51:26 +0000841 j=(ssize_t) width/2;
cristy47e00502009-12-17 19:19:57 +0000842 i=0;
843 for (k=(-j); k <= j; k++)
844 {
cristy4205a3c2010-09-12 20:19:59 +0000845 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
846 (MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +0000847 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000848 i++;
849 }
cristybb503372010-05-27 20:51:26 +0000850 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000851 kernel[i]/=normalize;
852 return(kernel);
853}
854
cristyf4ad9df2011-07-08 16:49:03 +0000855MagickExport Image *BlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000856 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000857{
858#define BlurImageTag "Blur/Image"
859
cristyc4c8d132010-01-07 01:58:38 +0000860 CacheView
861 *blur_view,
862 *image_view;
863
cristy3ed852e2009-09-05 21:47:34 +0000864 double
865 *kernel;
866
867 Image
868 *blur_image;
869
cristy3ed852e2009-09-05 21:47:34 +0000870 MagickBooleanType
871 status;
872
cristybb503372010-05-27 20:51:26 +0000873 MagickOffsetType
874 progress;
875
cristybb503372010-05-27 20:51:26 +0000876 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000877 i;
878
cristybb503372010-05-27 20:51:26 +0000879 size_t
cristy3ed852e2009-09-05 21:47:34 +0000880 width;
881
cristybb503372010-05-27 20:51:26 +0000882 ssize_t
cristyb41a1172011-09-06 00:55:14 +0000883 center,
cristybb503372010-05-27 20:51:26 +0000884 x,
885 y;
886
cristy3ed852e2009-09-05 21:47:34 +0000887 /*
888 Initialize blur image attributes.
889 */
890 assert(image != (Image *) NULL);
891 assert(image->signature == MagickSignature);
892 if (image->debug != MagickFalse)
893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
894 assert(exception != (ExceptionInfo *) NULL);
895 assert(exception->signature == MagickSignature);
cristyd25c77e2011-09-06 00:10:24 +0000896 blur_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000897 if (blur_image == (Image *) NULL)
898 return((Image *) NULL);
899 if (fabs(sigma) <= MagickEpsilon)
900 return(blur_image);
cristy574cc262011-08-05 01:23:58 +0000901 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000902 {
cristy3ed852e2009-09-05 21:47:34 +0000903 blur_image=DestroyImage(blur_image);
904 return((Image *) NULL);
905 }
906 width=GetOptimalKernelWidth1D(radius,sigma);
907 kernel=GetBlurKernel(width,sigma);
908 if (kernel == (double *) NULL)
909 {
910 blur_image=DestroyImage(blur_image);
911 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
912 }
913 if (image->debug != MagickFalse)
914 {
915 char
916 format[MaxTextExtent],
917 *message;
918
919 register const double
920 *k;
921
922 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000923 " BlurImage with %.20g kernel:",(double) width);
cristy3ed852e2009-09-05 21:47:34 +0000924 message=AcquireString("");
925 k=kernel;
cristybb503372010-05-27 20:51:26 +0000926 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +0000927 {
928 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000929 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000930 (void) ConcatenateString(&message,format);
cristyb51dff52011-05-19 16:55:47 +0000931 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000932 (void) ConcatenateString(&message,format);
933 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
934 }
935 message=DestroyString(message);
936 }
937 /*
938 Blur rows.
939 */
940 status=MagickTrue;
941 progress=0;
cristyb41a1172011-09-06 00:55:14 +0000942 center=(ssize_t) GetPixelChannels(image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +0000943 image_view=AcquireCacheView(image);
944 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000945#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000946 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000947#endif
cristyb41a1172011-09-06 00:55:14 +0000948 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000949 {
cristy4c08aed2011-07-01 19:47:50 +0000950 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000951 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000952
cristy4c08aed2011-07-01 19:47:50 +0000953 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000954 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000955
cristy117ff172010-08-15 21:35:32 +0000956 register ssize_t
957 x;
958
cristy3ed852e2009-09-05 21:47:34 +0000959 if (status == MagickFalse)
960 continue;
cristy117ff172010-08-15 21:35:32 +0000961 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
962 image->columns+width,1,exception);
cristy3ed852e2009-09-05 21:47:34 +0000963 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
964 exception);
cristy4c08aed2011-07-01 19:47:50 +0000965 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000966 {
967 status=MagickFalse;
968 continue;
969 }
cristyb41a1172011-09-06 00:55:14 +0000970 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000971 {
cristybb503372010-05-27 20:51:26 +0000972 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000973 i;
974
cristyb41a1172011-09-06 00:55:14 +0000975 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
976 {
977 MagickRealType
978 alpha,
979 gamma,
980 pixel;
cristyd25c77e2011-09-06 00:10:24 +0000981
cristyb41a1172011-09-06 00:55:14 +0000982 PixelChannel
983 channel;
984
985 PixelTrait
986 blur_traits,
987 traits;
988
989 register const double
990 *restrict k;
991
992 register const Quantum
993 *restrict pixels;
994
995 register ssize_t
996 u;
997
cristye2a912b2011-12-05 20:02:07 +0000998 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000999 traits=GetPixelChannelMapTraits(image,channel);
cristyb41a1172011-09-06 00:55:14 +00001000 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1001 if ((traits == UndefinedPixelTrait) ||
1002 (blur_traits == UndefinedPixelTrait))
1003 continue;
1004 if ((blur_traits & CopyPixelTrait) != 0)
cristyd25c77e2011-09-06 00:10:24 +00001005 {
cristy0beccfa2011-09-25 20:47:53 +00001006 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001007 continue;
cristyd25c77e2011-09-06 00:10:24 +00001008 }
cristyb41a1172011-09-06 00:55:14 +00001009 k=kernel;
1010 pixels=p;
1011 pixel=0.0;
1012 if ((blur_traits & BlendPixelTrait) == 0)
1013 {
1014 /*
1015 No alpha blending.
1016 */
1017 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001018 {
cristyb41a1172011-09-06 00:55:14 +00001019 pixel+=(*k)*pixels[i];
1020 k++;
1021 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001022 }
cristy0beccfa2011-09-25 20:47:53 +00001023 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001024 continue;
1025 }
1026 /*
1027 Alpha blending.
1028 */
1029 gamma=0.0;
1030 for (u=0; u < (ssize_t) width; u++)
1031 {
1032 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1033 pixel+=(*k)*alpha*pixels[i];
1034 gamma+=(*k)*alpha;
1035 k++;
1036 pixels+=GetPixelChannels(image);
cristyd25c77e2011-09-06 00:10:24 +00001037 }
cristyb41a1172011-09-06 00:55:14 +00001038 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001039 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001040 }
cristyed231572011-07-14 02:18:59 +00001041 p+=GetPixelChannels(image);
1042 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001043 }
1044 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1045 status=MagickFalse;
1046 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1047 {
1048 MagickBooleanType
1049 proceed;
1050
cristyb5d5f722009-11-04 03:03:49 +00001051#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001052 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001053#endif
1054 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1055 blur_image->columns);
1056 if (proceed == MagickFalse)
1057 status=MagickFalse;
1058 }
1059 }
1060 blur_view=DestroyCacheView(blur_view);
1061 image_view=DestroyCacheView(image_view);
1062 /*
1063 Blur columns.
1064 */
1065 image_view=AcquireCacheView(blur_image);
1066 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001067#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001068 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001069#endif
cristyb6dc4b72011-12-10 23:29:53 +00001070 for (x=0; x < (ssize_t) blur_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001071 {
cristy4c08aed2011-07-01 19:47:50 +00001072 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001073 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001074
cristy4c08aed2011-07-01 19:47:50 +00001075 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001076 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001077
cristy117ff172010-08-15 21:35:32 +00001078 register ssize_t
1079 y;
1080
cristy3ed852e2009-09-05 21:47:34 +00001081 if (status == MagickFalse)
1082 continue;
cristy117ff172010-08-15 21:35:32 +00001083 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
cristyb6dc4b72011-12-10 23:29:53 +00001084 blur_image->rows+width,exception);
cristy3ed852e2009-09-05 21:47:34 +00001085 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001086 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001087 {
1088 status=MagickFalse;
1089 continue;
1090 }
cristyb6dc4b72011-12-10 23:29:53 +00001091 for (y=0; y < (ssize_t) blur_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001092 {
cristybb503372010-05-27 20:51:26 +00001093 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001094 i;
1095
cristyb6dc4b72011-12-10 23:29:53 +00001096 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
cristyb41a1172011-09-06 00:55:14 +00001097 {
1098 MagickRealType
1099 alpha,
1100 gamma,
1101 pixel;
cristyd25c77e2011-09-06 00:10:24 +00001102
cristyb41a1172011-09-06 00:55:14 +00001103 PixelChannel
1104 channel;
1105
1106 PixelTrait
1107 blur_traits,
1108 traits;
1109
1110 register const double
1111 *restrict k;
1112
1113 register const Quantum
1114 *restrict pixels;
1115
1116 register ssize_t
1117 u;
1118
cristye2a912b2011-12-05 20:02:07 +00001119 channel=GetPixelChannelMapChannel(blur_image,i);
cristyabace412011-12-11 15:56:53 +00001120 traits=GetPixelChannelMapTraits(blur_image,channel);
cristyb41a1172011-09-06 00:55:14 +00001121 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1122 if ((traits == UndefinedPixelTrait) ||
1123 (blur_traits == UndefinedPixelTrait))
1124 continue;
1125 if ((blur_traits & CopyPixelTrait) != 0)
cristyd25c77e2011-09-06 00:10:24 +00001126 {
cristy0beccfa2011-09-25 20:47:53 +00001127 SetPixelChannel(blur_image,channel,p[center+i],q);
cristyb41a1172011-09-06 00:55:14 +00001128 continue;
cristyd25c77e2011-09-06 00:10:24 +00001129 }
cristyb41a1172011-09-06 00:55:14 +00001130 k=kernel;
1131 pixels=p;
1132 pixel=0.0;
1133 if ((blur_traits & BlendPixelTrait) == 0)
1134 {
1135 /*
1136 No alpha blending.
1137 */
1138 for (u=0; u < (ssize_t) width; u++)
cristyd25c77e2011-09-06 00:10:24 +00001139 {
cristyb41a1172011-09-06 00:55:14 +00001140 pixel+=(*k)*pixels[i];
1141 k++;
1142 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001143 }
cristy0beccfa2011-09-25 20:47:53 +00001144 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001145 continue;
1146 }
1147 /*
1148 Alpha blending.
1149 */
1150 gamma=0.0;
1151 for (u=0; u < (ssize_t) width; u++)
1152 {
cristyb6dc4b72011-12-10 23:29:53 +00001153 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1154 pixels));
cristyb41a1172011-09-06 00:55:14 +00001155 pixel+=(*k)*alpha*pixels[i];
1156 gamma+=(*k)*alpha;
1157 k++;
1158 pixels+=GetPixelChannels(blur_image);
cristyd25c77e2011-09-06 00:10:24 +00001159 }
cristyb41a1172011-09-06 00:55:14 +00001160 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001161 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyb41a1172011-09-06 00:55:14 +00001162 }
cristyd25c77e2011-09-06 00:10:24 +00001163 p+=GetPixelChannels(blur_image);
cristyed231572011-07-14 02:18:59 +00001164 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00001165 }
1166 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1167 status=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +00001168 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001169 {
1170 MagickBooleanType
1171 proceed;
1172
cristyb5d5f722009-11-04 03:03:49 +00001173#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001174 #pragma omp critical (MagickCore_BlurImage)
cristy3ed852e2009-09-05 21:47:34 +00001175#endif
cristy4c08aed2011-07-01 19:47:50 +00001176 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1177 blur_image->rows+blur_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001178 if (proceed == MagickFalse)
1179 status=MagickFalse;
1180 }
1181 }
1182 blur_view=DestroyCacheView(blur_view);
1183 image_view=DestroyCacheView(image_view);
1184 kernel=(double *) RelinquishMagickMemory(kernel);
1185 if (status == MagickFalse)
1186 blur_image=DestroyImage(blur_image);
1187 blur_image->type=image->type;
1188 return(blur_image);
1189}
1190
1191/*
1192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193% %
1194% %
1195% %
cristyfccdab92009-11-30 16:43:57 +00001196% C o n v o l v e I m a g e %
1197% %
1198% %
1199% %
1200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201%
1202% ConvolveImage() applies a custom convolution kernel to the image.
1203%
1204% The format of the ConvolveImage method is:
1205%
cristy5e6be1e2011-07-16 01:23:39 +00001206% Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1207% ExceptionInfo *exception)
1208%
cristyfccdab92009-11-30 16:43:57 +00001209% A description of each parameter follows:
1210%
1211% o image: the image.
1212%
cristy5e6be1e2011-07-16 01:23:39 +00001213% o kernel: the filtering kernel.
cristyfccdab92009-11-30 16:43:57 +00001214%
1215% o exception: return any errors or warnings in this structure.
1216%
1217*/
cristy5e6be1e2011-07-16 01:23:39 +00001218MagickExport Image *ConvolveImage(const Image *image,
1219 const KernelInfo *kernel_info,ExceptionInfo *exception)
cristyfccdab92009-11-30 16:43:57 +00001220{
cristyfccdab92009-11-30 16:43:57 +00001221#define ConvolveImageTag "Convolve/Image"
1222
cristyc4c8d132010-01-07 01:58:38 +00001223 CacheView
1224 *convolve_view,
cristy105ba3c2011-07-18 02:28:38 +00001225 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00001226
cristyfccdab92009-11-30 16:43:57 +00001227 Image
1228 *convolve_image;
1229
cristyfccdab92009-11-30 16:43:57 +00001230 MagickBooleanType
1231 status;
1232
cristybb503372010-05-27 20:51:26 +00001233 MagickOffsetType
1234 progress;
1235
cristybb503372010-05-27 20:51:26 +00001236 ssize_t
cristy574cc262011-08-05 01:23:58 +00001237 center,
cristybb503372010-05-27 20:51:26 +00001238 y;
1239
cristyfccdab92009-11-30 16:43:57 +00001240 /*
1241 Initialize convolve image attributes.
1242 */
1243 assert(image != (Image *) NULL);
1244 assert(image->signature == MagickSignature);
1245 if (image->debug != MagickFalse)
1246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1247 assert(exception != (ExceptionInfo *) NULL);
1248 assert(exception->signature == MagickSignature);
cristy5e6be1e2011-07-16 01:23:39 +00001249 if ((kernel_info->width % 2) == 0)
cristyfccdab92009-11-30 16:43:57 +00001250 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
cristy08429172011-07-14 17:18:16 +00001251 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1252 exception);
cristyfccdab92009-11-30 16:43:57 +00001253 if (convolve_image == (Image *) NULL)
1254 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001255 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001256 {
cristyfccdab92009-11-30 16:43:57 +00001257 convolve_image=DestroyImage(convolve_image);
1258 return((Image *) NULL);
1259 }
1260 if (image->debug != MagickFalse)
1261 {
1262 char
1263 format[MaxTextExtent],
1264 *message;
1265
cristya96f2492011-12-14 18:25:41 +00001266 register const MagickRealType
cristy117ff172010-08-15 21:35:32 +00001267 *k;
1268
cristy4e154852011-07-14 13:28:53 +00001269 register ssize_t
1270 u;
1271
cristybb503372010-05-27 20:51:26 +00001272 ssize_t
cristyfccdab92009-11-30 16:43:57 +00001273 v;
1274
cristyfccdab92009-11-30 16:43:57 +00001275 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristy5e6be1e2011-07-16 01:23:39 +00001276 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1277 (double) kernel_info->height);
cristyfccdab92009-11-30 16:43:57 +00001278 message=AcquireString("");
cristy5e6be1e2011-07-16 01:23:39 +00001279 k=kernel_info->values;
1280 for (v=0; v < (ssize_t) kernel_info->width; v++)
cristyfccdab92009-11-30 16:43:57 +00001281 {
1282 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001283 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristyfccdab92009-11-30 16:43:57 +00001284 (void) ConcatenateString(&message,format);
cristy5e6be1e2011-07-16 01:23:39 +00001285 for (u=0; u < (ssize_t) kernel_info->height; u++)
cristyfccdab92009-11-30 16:43:57 +00001286 {
cristyb51dff52011-05-19 16:55:47 +00001287 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001288 (void) ConcatenateString(&message,format);
1289 }
1290 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1291 }
1292 message=DestroyString(message);
1293 }
cristy7ea27962011-09-12 18:12:49 +00001294 status=AccelerateConvolveImage(image,kernel_info,convolve_image,exception);
1295 if (status == MagickTrue)
1296 return(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001297 /*
cristyfccdab92009-11-30 16:43:57 +00001298 Convolve image.
1299 */
cristy574cc262011-08-05 01:23:58 +00001300 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
cristy075ff302011-09-07 01:51:24 +00001301 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2L);
cristyfccdab92009-11-30 16:43:57 +00001302 status=MagickTrue;
1303 progress=0;
cristyfccdab92009-11-30 16:43:57 +00001304 image_view=AcquireCacheView(image);
1305 convolve_view=AcquireCacheView(convolve_image);
1306#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001307 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyfccdab92009-11-30 16:43:57 +00001308#endif
cristybb503372010-05-27 20:51:26 +00001309 for (y=0; y < (ssize_t) image->rows; y++)
cristyfccdab92009-11-30 16:43:57 +00001310 {
cristy4c08aed2011-07-01 19:47:50 +00001311 register const Quantum
cristy105ba3c2011-07-18 02:28:38 +00001312 *restrict p;
cristyfccdab92009-11-30 16:43:57 +00001313
cristy4c08aed2011-07-01 19:47:50 +00001314 register Quantum
cristyfccdab92009-11-30 16:43:57 +00001315 *restrict q;
1316
cristy117ff172010-08-15 21:35:32 +00001317 register ssize_t
1318 x;
1319
cristyfccdab92009-11-30 16:43:57 +00001320 if (status == MagickFalse)
1321 continue;
cristy105ba3c2011-07-18 02:28:38 +00001322 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1323 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1324 kernel_info->height,exception);
cristy08429172011-07-14 17:18:16 +00001325 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
cristyfccdab92009-11-30 16:43:57 +00001326 exception);
cristy105ba3c2011-07-18 02:28:38 +00001327 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristyfccdab92009-11-30 16:43:57 +00001328 {
1329 status=MagickFalse;
1330 continue;
1331 }
cristybb503372010-05-27 20:51:26 +00001332 for (x=0; x < (ssize_t) image->columns; x++)
cristyfccdab92009-11-30 16:43:57 +00001333 {
cristybb503372010-05-27 20:51:26 +00001334 register ssize_t
cristyed231572011-07-14 02:18:59 +00001335 i;
cristyfccdab92009-11-30 16:43:57 +00001336
cristya30d9ba2011-07-23 21:00:48 +00001337 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyed231572011-07-14 02:18:59 +00001338 {
cristyed231572011-07-14 02:18:59 +00001339 MagickRealType
cristy4e154852011-07-14 13:28:53 +00001340 alpha,
1341 gamma,
cristyed231572011-07-14 02:18:59 +00001342 pixel;
1343
1344 PixelChannel
1345 channel;
1346
1347 PixelTrait
1348 convolve_traits,
1349 traits;
1350
cristya96f2492011-12-14 18:25:41 +00001351 register const MagickRealType
cristyed231572011-07-14 02:18:59 +00001352 *restrict k;
1353
1354 register const Quantum
cristyeb52cde2011-07-17 01:52:52 +00001355 *restrict pixels;
cristyed231572011-07-14 02:18:59 +00001356
1357 register ssize_t
1358 u;
1359
1360 ssize_t
1361 v;
1362
cristye2a912b2011-12-05 20:02:07 +00001363 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001364 traits=GetPixelChannelMapTraits(image,channel);
cristy4e154852011-07-14 13:28:53 +00001365 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001366 if ((traits == UndefinedPixelTrait) ||
1367 (convolve_traits == UndefinedPixelTrait))
cristy4e154852011-07-14 13:28:53 +00001368 continue;
1369 if ((convolve_traits & CopyPixelTrait) != 0)
1370 {
cristy0beccfa2011-09-25 20:47:53 +00001371 SetPixelChannel(convolve_image,channel,p[center+i],q);
cristy4e154852011-07-14 13:28:53 +00001372 continue;
1373 }
cristy5e6be1e2011-07-16 01:23:39 +00001374 k=kernel_info->values;
cristy105ba3c2011-07-18 02:28:38 +00001375 pixels=p;
cristy0a922382011-07-16 15:30:34 +00001376 pixel=kernel_info->bias;
cristy222b19c2011-08-04 01:35:11 +00001377 if ((convolve_traits & BlendPixelTrait) == 0)
cristyfccdab92009-11-30 16:43:57 +00001378 {
cristyed231572011-07-14 02:18:59 +00001379 /*
cristy4e154852011-07-14 13:28:53 +00001380 No alpha blending.
cristyed231572011-07-14 02:18:59 +00001381 */
cristyeb52cde2011-07-17 01:52:52 +00001382 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristyfccdab92009-11-30 16:43:57 +00001383 {
cristyeb52cde2011-07-17 01:52:52 +00001384 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy175653e2011-07-10 23:13:34 +00001385 {
cristyeb52cde2011-07-17 01:52:52 +00001386 pixel+=(*k)*pixels[i];
cristyed231572011-07-14 02:18:59 +00001387 k++;
cristya30d9ba2011-07-23 21:00:48 +00001388 pixels+=GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001389 }
cristya30d9ba2011-07-23 21:00:48 +00001390 pixels+=image->columns*GetPixelChannels(image);
cristy175653e2011-07-10 23:13:34 +00001391 }
cristy0beccfa2011-09-25 20:47:53 +00001392 SetPixelChannel(convolve_image,channel,ClampToQuantum(pixel),q);
cristy4e154852011-07-14 13:28:53 +00001393 continue;
cristyed231572011-07-14 02:18:59 +00001394 }
cristy4e154852011-07-14 13:28:53 +00001395 /*
1396 Alpha blending.
1397 */
1398 gamma=0.0;
cristyeb52cde2011-07-17 01:52:52 +00001399 for (v=0; v < (ssize_t) kernel_info->height; v++)
cristy4e154852011-07-14 13:28:53 +00001400 {
cristyeb52cde2011-07-17 01:52:52 +00001401 for (u=0; u < (ssize_t) kernel_info->width; u++)
cristy4e154852011-07-14 13:28:53 +00001402 {
cristyeb52cde2011-07-17 01:52:52 +00001403 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1404 pixel+=(*k)*alpha*pixels[i];
cristy4e154852011-07-14 13:28:53 +00001405 gamma+=(*k)*alpha;
1406 k++;
cristya30d9ba2011-07-23 21:00:48 +00001407 pixels+=GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001408 }
cristya30d9ba2011-07-23 21:00:48 +00001409 pixels+=image->columns*GetPixelChannels(image);
cristy4e154852011-07-14 13:28:53 +00001410 }
cristy1ce96d02011-07-14 17:57:24 +00001411 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00001412 SetPixelChannel(convolve_image,channel,ClampToQuantum(gamma*pixel),q);
cristyed231572011-07-14 02:18:59 +00001413 }
cristya30d9ba2011-07-23 21:00:48 +00001414 p+=GetPixelChannels(image);
1415 q+=GetPixelChannels(convolve_image);
cristyfccdab92009-11-30 16:43:57 +00001416 }
cristyed231572011-07-14 02:18:59 +00001417 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
cristyfccdab92009-11-30 16:43:57 +00001418 status=MagickFalse;
1419 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1420 {
1421 MagickBooleanType
1422 proceed;
1423
1424#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00001425 #pragma omp critical (MagickCore_ConvolveImage)
cristyfccdab92009-11-30 16:43:57 +00001426#endif
1427 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1428 if (proceed == MagickFalse)
1429 status=MagickFalse;
1430 }
1431 }
1432 convolve_image->type=image->type;
1433 convolve_view=DestroyCacheView(convolve_view);
1434 image_view=DestroyCacheView(image_view);
cristyfccdab92009-11-30 16:43:57 +00001435 if (status == MagickFalse)
1436 convolve_image=DestroyImage(convolve_image);
1437 return(convolve_image);
1438}
1439
1440/*
1441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442% %
1443% %
1444% %
cristy3ed852e2009-09-05 21:47:34 +00001445% D e s p e c k l e I m a g e %
1446% %
1447% %
1448% %
1449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450%
1451% DespeckleImage() reduces the speckle noise in an image while perserving the
cristyf0ae7762012-01-15 22:04:03 +00001452% edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1453% neighbors, then complementarily lowering pixels that are brighter than their
1454% surrounding neighbors) to reduce the speckle index of that image (reference
1455% Crimmins speckle removal).
cristy3ed852e2009-09-05 21:47:34 +00001456%
1457% The format of the DespeckleImage method is:
1458%
1459% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1460%
1461% A description of each parameter follows:
1462%
1463% o image: the image.
1464%
1465% o exception: return any errors or warnings in this structure.
1466%
1467*/
1468
cristy5473bf02012-01-16 19:15:08 +00001469static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1470 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1471 Quantum *restrict g)
cristy3ed852e2009-09-05 21:47:34 +00001472{
cristy7b4b8862012-01-16 19:52:19 +00001473 register Quantum
cristy5473bf02012-01-16 19:15:08 +00001474 *p,
1475 *q,
1476 *r,
1477 *s;
cristy78f3de22012-01-16 00:43:18 +00001478
cristy5473bf02012-01-16 19:15:08 +00001479 ssize_t
1480 y;
1481
1482 assert(f != (Quantum *) NULL);
1483 assert(g != (Quantum *) NULL);
1484 p=f+(columns+2);
1485 q=g+(columns+2);
1486 r=p+(y_offset*(columns+2)+x_offset);
1487#if defined(HAVE_OPENMP)
1488 #pragma omp parallel for schedule(guided)
1489#endif
1490 for (y=0; y < (ssize_t) rows; y++)
1491 {
cristy7b4b8862012-01-16 19:52:19 +00001492 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001493 i,
1494 x;
1495
cristy7b4b8862012-01-16 19:52:19 +00001496 SignedQuantum
1497 v;
1498
cristy5473bf02012-01-16 19:15:08 +00001499 i=(2*y+1)+y*columns;
1500 if (polarity > 0)
1501 for (x=0; x < (ssize_t) columns; x++)
1502 {
1503 v=(SignedQuantum) p[i];
1504 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1505 v+=ScaleCharToQuantum(1);
1506 q[i]=(Quantum) v;
1507 i++;
1508 }
1509 else
1510 for (x=0; x < (ssize_t) columns; x++)
1511 {
1512 v=(SignedQuantum) p[i];
1513 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1514 v-=ScaleCharToQuantum(1);
1515 q[i]=(Quantum) v;
1516 i++;
1517 }
1518 }
1519 p=f+(columns+2);
1520 q=g+(columns+2);
1521 r=q+(y_offset*(columns+2)+x_offset);
1522 s=q-(y_offset*(columns+2)+x_offset);
1523#if defined(HAVE_OPENMP)
1524 #pragma omp parallel for schedule(guided)
1525#endif
1526 for (y=0; y < (ssize_t) rows; y++)
1527 {
cristy7b4b8862012-01-16 19:52:19 +00001528 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001529 i,
1530 x;
1531
cristy7b4b8862012-01-16 19:52:19 +00001532 SignedQuantum
1533 v;
1534
cristy5473bf02012-01-16 19:15:08 +00001535 i=(2*y+1)+y*columns;
1536 if (polarity > 0)
1537 for (x=0; x < (ssize_t) columns; x++)
1538 {
1539 v=(SignedQuantum) q[i];
1540 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1541 ((SignedQuantum) r[i] > v))
1542 v+=ScaleCharToQuantum(1);
1543 p[i]=(Quantum) v;
1544 i++;
1545 }
1546 else
1547 for (x=0; x < (ssize_t) columns; x++)
1548 {
1549 v=(SignedQuantum) q[i];
1550 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1551 ((SignedQuantum) r[i] < v))
1552 v-=ScaleCharToQuantum(1);
1553 p[i]=(Quantum) v;
1554 i++;
1555 }
1556 }
cristy3ed852e2009-09-05 21:47:34 +00001557}
1558
1559MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1560{
1561#define DespeckleImageTag "Despeckle/Image"
1562
cristy2407fc22009-09-11 00:55:25 +00001563 CacheView
1564 *despeckle_view,
1565 *image_view;
1566
cristy3ed852e2009-09-05 21:47:34 +00001567 Image
1568 *despeckle_image;
1569
cristy3ed852e2009-09-05 21:47:34 +00001570 MagickBooleanType
1571 status;
1572
cristy5473bf02012-01-16 19:15:08 +00001573 Quantum
1574 *restrict buffer,
1575 *restrict pixels;
cristya63e4a92011-09-09 00:47:59 +00001576
cristy5473bf02012-01-16 19:15:08 +00001577 register ssize_t
1578 i;
1579
1580 size_t
1581 length;
cristy117ff172010-08-15 21:35:32 +00001582
cristybb503372010-05-27 20:51:26 +00001583 static const ssize_t
cristy691a29e2009-09-11 00:44:10 +00001584 X[4] = {0, 1, 1,-1},
1585 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001586
cristy3ed852e2009-09-05 21:47:34 +00001587 /*
1588 Allocate despeckled image.
1589 */
1590 assert(image != (const Image *) NULL);
1591 assert(image->signature == MagickSignature);
1592 if (image->debug != MagickFalse)
1593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1594 assert(exception != (ExceptionInfo *) NULL);
1595 assert(exception->signature == MagickSignature);
cristy5473bf02012-01-16 19:15:08 +00001596 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001597 if (despeckle_image == (Image *) NULL)
1598 return((Image *) NULL);
cristya63e4a92011-09-09 00:47:59 +00001599 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1600 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001601 {
cristy3ed852e2009-09-05 21:47:34 +00001602 despeckle_image=DestroyImage(despeckle_image);
1603 return((Image *) NULL);
1604 }
1605 /*
cristy5473bf02012-01-16 19:15:08 +00001606 Allocate image buffer.
1607 */
1608 length=(size_t) ((image->columns+2)*(image->rows+2));
1609 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1610 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1611 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1612 {
1613 if (buffer != (Quantum *) NULL)
1614 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1615 if (pixels != (Quantum *) NULL)
1616 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1617 despeckle_image=DestroyImage(despeckle_image);
1618 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1619 }
1620 /*
1621 Reduce speckle in the image.
cristy3ed852e2009-09-05 21:47:34 +00001622 */
1623 status=MagickTrue;
cristyb06da092012-01-16 17:11:38 +00001624 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00001625 despeckle_view=AcquireCacheView(despeckle_image);
cristy5473bf02012-01-16 19:15:08 +00001626 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +00001627 {
cristy5473bf02012-01-16 19:15:08 +00001628 PixelChannel
1629 channel;
1630
1631 PixelTrait
1632 despeckle_traits,
1633 traits;
cristy3ed852e2009-09-05 21:47:34 +00001634
cristyc1488b52011-02-19 18:54:15 +00001635 register ssize_t
cristy5473bf02012-01-16 19:15:08 +00001636 k,
cristyf0ae7762012-01-15 22:04:03 +00001637 x;
cristyc1488b52011-02-19 18:54:15 +00001638
cristy5473bf02012-01-16 19:15:08 +00001639 ssize_t
1640 j,
1641 y;
1642
1643 if (status == MagickFalse)
1644 continue;
1645 channel=GetPixelChannelMapChannel(image,i);
1646 traits=GetPixelChannelMapTraits(image,channel);
1647 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1648 if ((traits == UndefinedPixelTrait) ||
1649 (despeckle_traits == UndefinedPixelTrait))
1650 continue;
1651 if ((despeckle_traits & CopyPixelTrait) != 0)
1652 continue;
1653 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1654 j=(ssize_t) image->columns+2;
1655 for (y=0; y < (ssize_t) image->rows; y++)
cristyfc830f42012-01-15 02:45:06 +00001656 {
cristy5473bf02012-01-16 19:15:08 +00001657 register const Quantum
1658 *restrict p;
cristyfc830f42012-01-15 02:45:06 +00001659
cristy5473bf02012-01-16 19:15:08 +00001660 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1661 if (p == (const Quantum *) NULL)
cristyf0ae7762012-01-15 22:04:03 +00001662 {
cristy5473bf02012-01-16 19:15:08 +00001663 status=MagickFalse;
1664 continue;
cristyf0ae7762012-01-15 22:04:03 +00001665 }
cristy5473bf02012-01-16 19:15:08 +00001666 j++;
1667 for (x=0; x < (ssize_t) image->columns; x++)
1668 {
1669 pixels[j++]=p[i];
1670 p+=GetPixelChannels(image);
cristyfc830f42012-01-15 02:45:06 +00001671 }
cristy5473bf02012-01-16 19:15:08 +00001672 j++;
cristy3ed852e2009-09-05 21:47:34 +00001673 }
cristy5473bf02012-01-16 19:15:08 +00001674 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1675 for (k=0; k < 4; k++)
1676 {
1677 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1678 Hull(-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1679 Hull(-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1680 Hull(X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1681 }
1682 j=(ssize_t) image->columns+2;
1683 for (y=0; y < (ssize_t) image->rows; y++)
1684 {
1685 MagickBooleanType
1686 sync;
1687
1688 register Quantum
1689 *restrict q;
1690
cristyd13e8eb2012-01-17 02:36:42 +00001691 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1692 despeckle_image->columns,1,exception);
cristy5473bf02012-01-16 19:15:08 +00001693 if (q == (Quantum *) NULL)
1694 {
1695 status=MagickFalse;
1696 continue;
1697 }
1698 j++;
1699 for (x=0; x < (ssize_t) image->columns; x++)
1700 {
1701 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1702 q+=GetPixelChannels(despeckle_image);
1703 }
1704 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1705 if (sync == MagickFalse)
1706 status=MagickFalse;
1707 j++;
1708 }
cristy3ed852e2009-09-05 21:47:34 +00001709 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1710 {
1711 MagickBooleanType
1712 proceed;
1713
cristy5473bf02012-01-16 19:15:08 +00001714 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1715 GetPixelChannels(image));
cristy3ed852e2009-09-05 21:47:34 +00001716 if (proceed == MagickFalse)
1717 status=MagickFalse;
1718 }
1719 }
1720 despeckle_view=DestroyCacheView(despeckle_view);
1721 image_view=DestroyCacheView(image_view);
cristy5473bf02012-01-16 19:15:08 +00001722 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1723 pixels=(Quantum *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001724 despeckle_image->type=image->type;
1725 if (status == MagickFalse)
1726 despeckle_image=DestroyImage(despeckle_image);
1727 return(despeckle_image);
1728}
1729
1730/*
1731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1732% %
1733% %
1734% %
1735% E d g e I m a g e %
1736% %
1737% %
1738% %
1739%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1740%
1741% EdgeImage() finds edges in an image. Radius defines the radius of the
1742% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1743% radius for you.
1744%
1745% The format of the EdgeImage method is:
1746%
1747% Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001748% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001749%
1750% A description of each parameter follows:
1751%
1752% o image: the image.
1753%
1754% o radius: the radius of the pixel neighborhood.
1755%
cristy8ae632d2011-09-05 17:29:53 +00001756% o sigma: the standard deviation of the Gaussian, in pixels.
1757%
cristy3ed852e2009-09-05 21:47:34 +00001758% o exception: return any errors or warnings in this structure.
1759%
1760*/
1761MagickExport Image *EdgeImage(const Image *image,const double radius,
cristy8ae632d2011-09-05 17:29:53 +00001762 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001763{
1764 Image
1765 *edge_image;
1766
cristy41cbe682011-07-15 19:12:37 +00001767 KernelInfo
1768 *kernel_info;
cristy3ed852e2009-09-05 21:47:34 +00001769
cristybb503372010-05-27 20:51:26 +00001770 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001771 i;
1772
cristybb503372010-05-27 20:51:26 +00001773 size_t
cristy3ed852e2009-09-05 21:47:34 +00001774 width;
1775
cristy41cbe682011-07-15 19:12:37 +00001776 ssize_t
1777 j,
1778 u,
1779 v;
1780
cristy3ed852e2009-09-05 21:47:34 +00001781 assert(image != (const Image *) NULL);
1782 assert(image->signature == MagickSignature);
1783 if (image->debug != MagickFalse)
1784 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1785 assert(exception != (ExceptionInfo *) NULL);
1786 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001787 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001788 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001789 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001790 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001791 kernel_info->width=width;
1792 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001793 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1794 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1795 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001796 {
1797 kernel_info=DestroyKernelInfo(kernel_info);
1798 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1799 }
1800 j=(ssize_t) kernel_info->width/2;
1801 i=0;
1802 for (v=(-j); v <= j; v++)
1803 {
1804 for (u=(-j); u <= j; u++)
1805 {
1806 kernel_info->values[i]=(-1.0);
1807 i++;
1808 }
1809 }
1810 kernel_info->values[i/2]=(double) (width*width-1.0);
anthony736a1602011-10-06 12:38:17 +00001811 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
cristy5e6be1e2011-07-16 01:23:39 +00001812 edge_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001813 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001814 return(edge_image);
1815}
1816
1817/*
1818%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1819% %
1820% %
1821% %
1822% E m b o s s I m a g e %
1823% %
1824% %
1825% %
1826%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1827%
1828% EmbossImage() returns a grayscale image with a three-dimensional effect.
1829% We convolve the image with a Gaussian operator of the given radius and
1830% standard deviation (sigma). For reasonable results, radius should be
1831% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1832% radius for you.
1833%
1834% The format of the EmbossImage method is:
1835%
1836% Image *EmbossImage(const Image *image,const double radius,
1837% const double sigma,ExceptionInfo *exception)
1838%
1839% A description of each parameter follows:
1840%
1841% o image: the image.
1842%
1843% o radius: the radius of the pixel neighborhood.
1844%
1845% o sigma: the standard deviation of the Gaussian, in pixels.
1846%
1847% o exception: return any errors or warnings in this structure.
1848%
1849*/
1850MagickExport Image *EmbossImage(const Image *image,const double radius,
1851 const double sigma,ExceptionInfo *exception)
1852{
cristy3ed852e2009-09-05 21:47:34 +00001853 Image
1854 *emboss_image;
1855
cristy41cbe682011-07-15 19:12:37 +00001856 KernelInfo
1857 *kernel_info;
1858
cristybb503372010-05-27 20:51:26 +00001859 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001860 i;
1861
cristybb503372010-05-27 20:51:26 +00001862 size_t
cristy3ed852e2009-09-05 21:47:34 +00001863 width;
1864
cristy117ff172010-08-15 21:35:32 +00001865 ssize_t
1866 j,
1867 k,
1868 u,
1869 v;
1870
cristy41cbe682011-07-15 19:12:37 +00001871 assert(image != (const Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001872 assert(image->signature == MagickSignature);
1873 if (image->debug != MagickFalse)
1874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1875 assert(exception != (ExceptionInfo *) NULL);
1876 assert(exception->signature == MagickSignature);
cristyfc0ae532011-12-06 15:14:45 +00001877 width=GetOptimalKernelWidth1D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001878 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001879 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001880 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001881 kernel_info->width=width;
1882 kernel_info->height=width;
cristya96f2492011-12-14 18:25:41 +00001883 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1884 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1885 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001886 {
1887 kernel_info=DestroyKernelInfo(kernel_info);
1888 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1889 }
1890 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00001891 k=j;
1892 i=0;
1893 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001894 {
cristy47e00502009-12-17 19:19:57 +00001895 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00001896 {
cristy41cbe682011-07-15 19:12:37 +00001897 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
cristy47e00502009-12-17 19:19:57 +00001898 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
cristy4205a3c2010-09-12 20:19:59 +00001899 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy47e00502009-12-17 19:19:57 +00001900 if (u != k)
cristy41cbe682011-07-15 19:12:37 +00001901 kernel_info->values[i]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001902 i++;
1903 }
cristy47e00502009-12-17 19:19:57 +00001904 k--;
cristy3ed852e2009-09-05 21:47:34 +00001905 }
anthony736a1602011-10-06 12:38:17 +00001906 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
cristy5e6be1e2011-07-16 01:23:39 +00001907 emboss_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00001908 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00001909 if (emboss_image != (Image *) NULL)
cristy6d8c3d72011-08-22 01:20:01 +00001910 (void) EqualizeImage(emboss_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001911 return(emboss_image);
1912}
1913
1914/*
1915%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1916% %
1917% %
1918% %
1919% G a u s s i a n B l u r I m a g e %
1920% %
1921% %
1922% %
1923%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1924%
1925% GaussianBlurImage() blurs an image. We convolve the image with a
1926% Gaussian operator of the given radius and standard deviation (sigma).
1927% For reasonable results, the radius should be larger than sigma. Use a
1928% radius of 0 and GaussianBlurImage() selects a suitable radius for you
1929%
1930% The format of the GaussianBlurImage method is:
1931%
1932% Image *GaussianBlurImage(const Image *image,onst double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001933% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001934%
1935% A description of each parameter follows:
1936%
1937% o image: the image.
1938%
cristy3ed852e2009-09-05 21:47:34 +00001939% o radius: the radius of the Gaussian, in pixels, not counting the center
1940% pixel.
1941%
1942% o sigma: the standard deviation of the Gaussian, in pixels.
1943%
cristy05c0c9a2011-09-05 23:16:13 +00001944% o bias: the bias.
1945%
cristy3ed852e2009-09-05 21:47:34 +00001946% o exception: return any errors or warnings in this structure.
1947%
1948*/
cristy41cbe682011-07-15 19:12:37 +00001949MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00001950 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001951{
cristy3ed852e2009-09-05 21:47:34 +00001952 Image
1953 *blur_image;
1954
cristy41cbe682011-07-15 19:12:37 +00001955 KernelInfo
1956 *kernel_info;
1957
cristybb503372010-05-27 20:51:26 +00001958 register ssize_t
cristy47e00502009-12-17 19:19:57 +00001959 i;
1960
cristybb503372010-05-27 20:51:26 +00001961 size_t
cristy3ed852e2009-09-05 21:47:34 +00001962 width;
1963
cristy117ff172010-08-15 21:35:32 +00001964 ssize_t
1965 j,
1966 u,
1967 v;
1968
cristy3ed852e2009-09-05 21:47:34 +00001969 assert(image != (const Image *) NULL);
1970 assert(image->signature == MagickSignature);
1971 if (image->debug != MagickFalse)
1972 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1973 assert(exception != (ExceptionInfo *) NULL);
1974 assert(exception->signature == MagickSignature);
1975 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00001976 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00001977 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001978 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00001979 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1980 kernel_info->width=width;
1981 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00001982 kernel_info->bias=bias; /* FUTURE: user bias on Gaussian Blur! non-sense */
cristy41cbe682011-07-15 19:12:37 +00001983 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00001984 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1985 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1986 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00001987 {
1988 kernel_info=DestroyKernelInfo(kernel_info);
1989 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1990 }
1991 j=(ssize_t) kernel_info->width/2;
cristy3ed852e2009-09-05 21:47:34 +00001992 i=0;
cristy47e00502009-12-17 19:19:57 +00001993 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00001994 {
cristy47e00502009-12-17 19:19:57 +00001995 for (u=(-j); u <= j; u++)
cristy41cbe682011-07-15 19:12:37 +00001996 {
1997 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1998 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1999 i++;
2000 }
cristy3ed852e2009-09-05 21:47:34 +00002001 }
cristy5e6be1e2011-07-16 01:23:39 +00002002 blur_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00002003 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00002004 return(blur_image);
2005}
2006
2007/*
2008%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2009% %
2010% %
2011% %
cristy3ed852e2009-09-05 21:47:34 +00002012% M o t i o n B l u r I m a g e %
2013% %
2014% %
2015% %
2016%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017%
2018% MotionBlurImage() simulates motion blur. We convolve the image with a
2019% Gaussian operator of the given radius and standard deviation (sigma).
2020% For reasonable results, radius should be larger than sigma. Use a
2021% radius of 0 and MotionBlurImage() selects a suitable radius for you.
2022% Angle gives the angle of the blurring motion.
2023%
2024% Andrew Protano contributed this effect.
2025%
2026% The format of the MotionBlurImage method is:
2027%
2028% Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002029% const double sigma,const double angle,const double bias,
2030% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002031%
2032% A description of each parameter follows:
2033%
2034% o image: the image.
2035%
cristy3ed852e2009-09-05 21:47:34 +00002036% o radius: the radius of the Gaussian, in pixels, not counting
2037% the center pixel.
2038%
2039% o sigma: the standard deviation of the Gaussian, in pixels.
2040%
cristycee97112010-05-28 00:44:52 +00002041% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00002042%
cristyf7ef0252011-09-09 14:50:06 +00002043% o bias: the bias.
2044%
cristy3ed852e2009-09-05 21:47:34 +00002045% o exception: return any errors or warnings in this structure.
2046%
2047*/
2048
cristybb503372010-05-27 20:51:26 +00002049static double *GetMotionBlurKernel(const size_t width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00002050{
cristy3ed852e2009-09-05 21:47:34 +00002051 double
cristy47e00502009-12-17 19:19:57 +00002052 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00002053 normalize;
2054
cristybb503372010-05-27 20:51:26 +00002055 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002056 i;
2057
2058 /*
cristy47e00502009-12-17 19:19:57 +00002059 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00002060 */
2061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2062 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2063 if (kernel == (double *) NULL)
2064 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00002065 normalize=0.0;
cristybb503372010-05-27 20:51:26 +00002066 for (i=0; i < (ssize_t) width; i++)
cristy47e00502009-12-17 19:19:57 +00002067 {
cristy4205a3c2010-09-12 20:19:59 +00002068 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2069 MagickSigma)))/(MagickSQ2PI*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00002070 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00002071 }
cristybb503372010-05-27 20:51:26 +00002072 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002073 kernel[i]/=normalize;
2074 return(kernel);
2075}
2076
cristya63e4a92011-09-09 00:47:59 +00002077MagickExport Image *MotionBlurImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00002078 const double sigma,const double angle,const double bias,
2079 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002080{
cristyc4c8d132010-01-07 01:58:38 +00002081 CacheView
2082 *blur_view,
2083 *image_view;
2084
cristy3ed852e2009-09-05 21:47:34 +00002085 double
2086 *kernel;
2087
2088 Image
2089 *blur_image;
2090
cristy3ed852e2009-09-05 21:47:34 +00002091 MagickBooleanType
2092 status;
2093
cristybb503372010-05-27 20:51:26 +00002094 MagickOffsetType
2095 progress;
2096
cristy3ed852e2009-09-05 21:47:34 +00002097 OffsetInfo
2098 *offset;
2099
2100 PointInfo
2101 point;
2102
cristybb503372010-05-27 20:51:26 +00002103 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002104 i;
2105
cristybb503372010-05-27 20:51:26 +00002106 size_t
cristy3ed852e2009-09-05 21:47:34 +00002107 width;
2108
cristybb503372010-05-27 20:51:26 +00002109 ssize_t
2110 y;
2111
cristy3ed852e2009-09-05 21:47:34 +00002112 assert(image != (Image *) NULL);
2113 assert(image->signature == MagickSignature);
2114 if (image->debug != MagickFalse)
2115 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2116 assert(exception != (ExceptionInfo *) NULL);
2117 width=GetOptimalKernelWidth1D(radius,sigma);
2118 kernel=GetMotionBlurKernel(width,sigma);
2119 if (kernel == (double *) NULL)
2120 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2121 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2122 if (offset == (OffsetInfo *) NULL)
2123 {
2124 kernel=(double *) RelinquishMagickMemory(kernel);
2125 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2126 }
cristy1e7aa312011-09-10 20:01:36 +00002127 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002128 if (blur_image == (Image *) NULL)
2129 {
2130 kernel=(double *) RelinquishMagickMemory(kernel);
2131 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2132 return((Image *) NULL);
2133 }
cristy574cc262011-08-05 01:23:58 +00002134 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002135 {
2136 kernel=(double *) RelinquishMagickMemory(kernel);
2137 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
cristy3ed852e2009-09-05 21:47:34 +00002138 blur_image=DestroyImage(blur_image);
2139 return((Image *) NULL);
2140 }
2141 point.x=(double) width*sin(DegreesToRadians(angle));
2142 point.y=(double) width*cos(DegreesToRadians(angle));
cristybb503372010-05-27 20:51:26 +00002143 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00002144 {
cristybb503372010-05-27 20:51:26 +00002145 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2146 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002147 }
2148 /*
2149 Motion blur image.
2150 */
2151 status=MagickTrue;
2152 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002153 image_view=AcquireCacheView(image);
2154 blur_view=AcquireCacheView(blur_image);
cristyb557a152011-02-22 12:14:30 +00002155#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00002156 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002157#endif
cristybb503372010-05-27 20:51:26 +00002158 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002159 {
cristyf7ef0252011-09-09 14:50:06 +00002160 register const Quantum
2161 *restrict p;
2162
cristy4c08aed2011-07-01 19:47:50 +00002163 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002164 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002165
cristy117ff172010-08-15 21:35:32 +00002166 register ssize_t
2167 x;
2168
cristy3ed852e2009-09-05 21:47:34 +00002169 if (status == MagickFalse)
2170 continue;
cristyf7ef0252011-09-09 14:50:06 +00002171 p=GetCacheViewVirtualPixels(blur_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00002172 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2173 exception);
cristyf7ef0252011-09-09 14:50:06 +00002174 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002175 {
2176 status=MagickFalse;
2177 continue;
2178 }
cristybb503372010-05-27 20:51:26 +00002179 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002180 {
cristybb503372010-05-27 20:51:26 +00002181 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002182 i;
2183
cristyf7ef0252011-09-09 14:50:06 +00002184 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2185 {
2186 MagickRealType
2187 alpha,
2188 gamma,
2189 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002190
cristyf7ef0252011-09-09 14:50:06 +00002191 PixelChannel
2192 channel;
2193
2194 PixelTrait
2195 blur_traits,
2196 traits;
2197
2198 register const Quantum
2199 *restrict r;
2200
2201 register double
2202 *restrict k;
2203
2204 register ssize_t
2205 j;
2206
cristye2a912b2011-12-05 20:02:07 +00002207 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002208 traits=GetPixelChannelMapTraits(image,channel);
cristyf7ef0252011-09-09 14:50:06 +00002209 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2210 if ((traits == UndefinedPixelTrait) ||
2211 (blur_traits == UndefinedPixelTrait))
2212 continue;
2213 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002214 {
cristy0beccfa2011-09-25 20:47:53 +00002215 SetPixelChannel(blur_image,channel,p[i],q);
cristyf7ef0252011-09-09 14:50:06 +00002216 continue;
cristy3ed852e2009-09-05 21:47:34 +00002217 }
cristyf7ef0252011-09-09 14:50:06 +00002218 k=kernel;
2219 pixel=bias;
2220 if ((blur_traits & BlendPixelTrait) == 0)
2221 {
2222 for (j=0; j < (ssize_t) width; j++)
2223 {
2224 r=GetCacheViewVirtualPixels(image_view,x+offset[j].x,y+
2225 offset[j].y,1,1,exception);
2226 if (r == (const Quantum *) NULL)
2227 {
2228 status=MagickFalse;
2229 continue;
2230 }
2231 pixel+=(*k)*r[i];
2232 k++;
2233 }
cristy0beccfa2011-09-25 20:47:53 +00002234 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002235 continue;
2236 }
2237 alpha=0.0;
2238 gamma=0.0;
2239 for (j=0; j < (ssize_t) width; j++)
2240 {
2241 r=GetCacheViewVirtualPixels(image_view,x+offset[j].x,y+offset[j].y,1,
2242 1,exception);
2243 if (r == (const Quantum *) NULL)
2244 {
2245 status=MagickFalse;
2246 continue;
2247 }
2248 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2249 pixel+=(*k)*alpha*r[i];
2250 gamma+=(*k)*alpha;
2251 k++;
cristy3ed852e2009-09-05 21:47:34 +00002252 }
cristyf7ef0252011-09-09 14:50:06 +00002253 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00002254 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristyf7ef0252011-09-09 14:50:06 +00002255 }
2256 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00002257 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00002258 }
2259 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2260 status=MagickFalse;
2261 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2262 {
2263 MagickBooleanType
2264 proceed;
2265
cristyb557a152011-02-22 12:14:30 +00002266#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00002267 #pragma omp critical (MagickCore_MotionBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00002268#endif
2269 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2270 if (proceed == MagickFalse)
2271 status=MagickFalse;
2272 }
2273 }
2274 blur_view=DestroyCacheView(blur_view);
2275 image_view=DestroyCacheView(image_view);
2276 kernel=(double *) RelinquishMagickMemory(kernel);
2277 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2278 if (status == MagickFalse)
2279 blur_image=DestroyImage(blur_image);
2280 return(blur_image);
2281}
2282
2283/*
2284%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2285% %
2286% %
2287% %
2288% P r e v i e w I m a g e %
2289% %
2290% %
2291% %
2292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2293%
2294% PreviewImage() tiles 9 thumbnails of the specified image with an image
2295% processing operation applied with varying parameters. This may be helpful
2296% pin-pointing an appropriate parameter for a particular image processing
2297% operation.
2298%
2299% The format of the PreviewImages method is:
2300%
2301% Image *PreviewImages(const Image *image,const PreviewType preview,
2302% ExceptionInfo *exception)
2303%
2304% A description of each parameter follows:
2305%
2306% o image: the image.
2307%
2308% o preview: the image processing operation.
2309%
2310% o exception: return any errors or warnings in this structure.
2311%
2312*/
2313MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2314 ExceptionInfo *exception)
2315{
2316#define NumberTiles 9
2317#define PreviewImageTag "Preview/Image"
2318#define DefaultPreviewGeometry "204x204+10+10"
2319
2320 char
2321 factor[MaxTextExtent],
2322 label[MaxTextExtent];
2323
2324 double
2325 degrees,
2326 gamma,
2327 percentage,
2328 radius,
2329 sigma,
2330 threshold;
2331
2332 Image
2333 *images,
2334 *montage_image,
2335 *preview_image,
2336 *thumbnail;
2337
2338 ImageInfo
2339 *preview_info;
2340
cristy3ed852e2009-09-05 21:47:34 +00002341 MagickBooleanType
2342 proceed;
2343
2344 MontageInfo
2345 *montage_info;
2346
2347 QuantizeInfo
2348 quantize_info;
2349
2350 RectangleInfo
2351 geometry;
2352
cristybb503372010-05-27 20:51:26 +00002353 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002354 i,
2355 x;
2356
cristybb503372010-05-27 20:51:26 +00002357 size_t
cristy3ed852e2009-09-05 21:47:34 +00002358 colors;
2359
cristy117ff172010-08-15 21:35:32 +00002360 ssize_t
2361 y;
2362
cristy3ed852e2009-09-05 21:47:34 +00002363 /*
2364 Open output image file.
2365 */
2366 assert(image != (Image *) NULL);
2367 assert(image->signature == MagickSignature);
2368 if (image->debug != MagickFalse)
2369 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2370 colors=2;
2371 degrees=0.0;
2372 gamma=(-0.2f);
2373 preview_info=AcquireImageInfo();
2374 SetGeometry(image,&geometry);
2375 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2376 &geometry.width,&geometry.height);
2377 images=NewImageList();
2378 percentage=12.5;
2379 GetQuantizeInfo(&quantize_info);
2380 radius=0.0;
2381 sigma=1.0;
2382 threshold=0.0;
2383 x=0;
2384 y=0;
2385 for (i=0; i < NumberTiles; i++)
2386 {
2387 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2388 if (thumbnail == (Image *) NULL)
2389 break;
2390 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2391 (void *) NULL);
cristyd15e6592011-10-15 00:13:06 +00002392 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002393 if (i == (NumberTiles/2))
2394 {
cristy9950d572011-10-01 18:22:35 +00002395 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2396 &thumbnail->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002397 AppendImageToList(&images,thumbnail);
2398 continue;
2399 }
2400 switch (preview)
2401 {
2402 case RotatePreview:
2403 {
2404 degrees+=45.0;
2405 preview_image=RotateImage(thumbnail,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002406 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002407 break;
2408 }
2409 case ShearPreview:
2410 {
2411 degrees+=5.0;
2412 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristyb51dff52011-05-19 16:55:47 +00002413 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002414 degrees,2.0*degrees);
2415 break;
2416 }
2417 case RollPreview:
2418 {
cristybb503372010-05-27 20:51:26 +00002419 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2420 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
cristy3ed852e2009-09-05 21:47:34 +00002421 preview_image=RollImage(thumbnail,x,y,exception);
cristyb51dff52011-05-19 16:55:47 +00002422 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
cristye8c25f92010-06-03 00:53:06 +00002423 (double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00002424 break;
2425 }
2426 case HuePreview:
2427 {
2428 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2429 if (preview_image == (Image *) NULL)
2430 break;
cristyb51dff52011-05-19 16:55:47 +00002431 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
cristy3ed852e2009-09-05 21:47:34 +00002432 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002433 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002434 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002435 break;
2436 }
2437 case SaturationPreview:
2438 {
2439 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2440 if (preview_image == (Image *) NULL)
2441 break;
cristyb51dff52011-05-19 16:55:47 +00002442 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
cristy8cd5b312010-01-07 01:10:24 +00002443 2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002444 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002445 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002446 break;
2447 }
2448 case BrightnessPreview:
2449 {
2450 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2451 if (preview_image == (Image *) NULL)
2452 break;
cristyb51dff52011-05-19 16:55:47 +00002453 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
cristy33bd5152011-08-24 01:42:24 +00002454 (void) ModulateImage(preview_image,factor,exception);
cristyb51dff52011-05-19 16:55:47 +00002455 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002456 break;
2457 }
2458 case GammaPreview:
2459 default:
2460 {
2461 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2462 if (preview_image == (Image *) NULL)
2463 break;
2464 gamma+=0.4f;
cristyb3e7c6c2011-07-24 01:43:55 +00002465 (void) GammaImage(preview_image,gamma,exception);
cristyb51dff52011-05-19 16:55:47 +00002466 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00002467 break;
2468 }
2469 case SpiffPreview:
2470 {
2471 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2472 if (preview_image != (Image *) NULL)
2473 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002474 (void) ContrastImage(preview_image,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002475 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002476 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002477 break;
2478 }
2479 case DullPreview:
2480 {
2481 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2482 if (preview_image == (Image *) NULL)
2483 break;
2484 for (x=0; x < i; x++)
cristye23ec9d2011-08-16 18:15:40 +00002485 (void) ContrastImage(preview_image,MagickFalse,exception);
cristyb51dff52011-05-19 16:55:47 +00002486 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002487 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002488 break;
2489 }
2490 case GrayscalePreview:
2491 {
2492 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2493 if (preview_image == (Image *) NULL)
2494 break;
2495 colors<<=1;
2496 quantize_info.number_colors=colors;
2497 quantize_info.colorspace=GRAYColorspace;
cristy018f07f2011-09-04 21:15:19 +00002498 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002499 (void) FormatLocaleString(label,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002500 "-colorspace gray -colors %.20g",(double) colors);
cristy3ed852e2009-09-05 21:47:34 +00002501 break;
2502 }
2503 case QuantizePreview:
2504 {
2505 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2506 if (preview_image == (Image *) NULL)
2507 break;
2508 colors<<=1;
2509 quantize_info.number_colors=colors;
cristy018f07f2011-09-04 21:15:19 +00002510 (void) QuantizeImage(&quantize_info,preview_image,exception);
cristyb51dff52011-05-19 16:55:47 +00002511 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002512 colors);
cristy3ed852e2009-09-05 21:47:34 +00002513 break;
2514 }
2515 case DespecklePreview:
2516 {
2517 for (x=0; x < (i-1); x++)
2518 {
2519 preview_image=DespeckleImage(thumbnail,exception);
2520 if (preview_image == (Image *) NULL)
2521 break;
2522 thumbnail=DestroyImage(thumbnail);
2523 thumbnail=preview_image;
2524 }
2525 preview_image=DespeckleImage(thumbnail,exception);
2526 if (preview_image == (Image *) NULL)
2527 break;
cristyb51dff52011-05-19 16:55:47 +00002528 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
cristye8c25f92010-06-03 00:53:06 +00002529 (double) i+1);
cristy3ed852e2009-09-05 21:47:34 +00002530 break;
2531 }
2532 case ReduceNoisePreview:
2533 {
cristy95c38342011-03-18 22:39:51 +00002534 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2535 (size_t) radius,exception);
cristyb51dff52011-05-19 16:55:47 +00002536 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002537 break;
2538 }
2539 case AddNoisePreview:
2540 {
2541 switch ((int) i)
2542 {
2543 case 0:
2544 {
2545 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2546 break;
2547 }
2548 case 1:
2549 {
2550 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2551 break;
2552 }
2553 case 2:
2554 {
2555 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2556 break;
2557 }
2558 case 3:
2559 {
2560 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2561 break;
2562 }
2563 case 4:
2564 {
2565 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2566 break;
2567 }
2568 case 5:
2569 {
2570 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2571 break;
2572 }
2573 default:
2574 {
2575 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2576 break;
2577 }
2578 }
cristyd76c51e2011-03-26 00:21:26 +00002579 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2580 (size_t) i,exception);
cristyb51dff52011-05-19 16:55:47 +00002581 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
cristy3ed852e2009-09-05 21:47:34 +00002582 break;
2583 }
2584 case SharpenPreview:
2585 {
anthony736a1602011-10-06 12:38:17 +00002586 /* FUTURE: user bias on sharpen! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002587 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2588 exception);
cristyb51dff52011-05-19 16:55:47 +00002589 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002590 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002591 break;
2592 }
2593 case BlurPreview:
2594 {
anthony736a1602011-10-06 12:38:17 +00002595 /* FUTURE: user bias on blur! This is non-sensical! */
cristy05c0c9a2011-09-05 23:16:13 +00002596 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002597 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
cristy3ed852e2009-09-05 21:47:34 +00002598 sigma);
2599 break;
2600 }
2601 case ThresholdPreview:
2602 {
2603 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2604 if (preview_image == (Image *) NULL)
2605 break;
cristye941a752011-10-15 01:52:48 +00002606 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2607 QuantumRange+1.0))/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002608 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
cristy3ed852e2009-09-05 21:47:34 +00002609 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2610 break;
2611 }
2612 case EdgeDetectPreview:
2613 {
cristy8ae632d2011-09-05 17:29:53 +00002614 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
cristyb51dff52011-05-19 16:55:47 +00002615 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
cristy3ed852e2009-09-05 21:47:34 +00002616 break;
2617 }
2618 case SpreadPreview:
2619 {
cristy5c4e2582011-09-11 19:21:03 +00002620 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2621 exception);
cristyb51dff52011-05-19 16:55:47 +00002622 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
cristy8cd5b312010-01-07 01:10:24 +00002623 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002624 break;
2625 }
2626 case SolarizePreview:
2627 {
2628 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2629 if (preview_image == (Image *) NULL)
2630 break;
2631 (void) SolarizeImage(preview_image,(double) QuantumRange*
cristy5cbc0162011-08-29 00:36:28 +00002632 percentage/100.0,exception);
cristyb51dff52011-05-19 16:55:47 +00002633 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
cristy3ed852e2009-09-05 21:47:34 +00002634 (QuantumRange*percentage)/100.0);
2635 break;
2636 }
2637 case ShadePreview:
2638 {
2639 degrees+=10.0;
2640 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2641 exception);
cristyb51dff52011-05-19 16:55:47 +00002642 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002643 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00002644 break;
2645 }
2646 case RaisePreview:
2647 {
2648 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2649 if (preview_image == (Image *) NULL)
2650 break;
cristybb503372010-05-27 20:51:26 +00002651 geometry.width=(size_t) (2*i+2);
2652 geometry.height=(size_t) (2*i+2);
cristy3ed852e2009-09-05 21:47:34 +00002653 geometry.x=i/2;
2654 geometry.y=i/2;
cristy6170ac32011-08-28 14:15:37 +00002655 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
cristyb51dff52011-05-19 16:55:47 +00002656 (void) FormatLocaleString(label,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002657 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002658 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00002659 break;
2660 }
2661 case SegmentPreview:
2662 {
2663 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2664 if (preview_image == (Image *) NULL)
2665 break;
2666 threshold+=0.4f;
2667 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
cristy018f07f2011-09-04 21:15:19 +00002668 threshold,exception);
cristyb51dff52011-05-19 16:55:47 +00002669 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
cristy3ed852e2009-09-05 21:47:34 +00002670 threshold,threshold);
2671 break;
2672 }
2673 case SwirlPreview:
2674 {
cristy76f512e2011-09-12 01:26:56 +00002675 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2676 exception);
cristyb51dff52011-05-19 16:55:47 +00002677 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002678 degrees+=45.0;
2679 break;
2680 }
2681 case ImplodePreview:
2682 {
2683 degrees+=0.1f;
cristy76f512e2011-09-12 01:26:56 +00002684 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2685 exception);
cristyb51dff52011-05-19 16:55:47 +00002686 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00002687 break;
2688 }
2689 case WavePreview:
2690 {
2691 degrees+=5.0f;
cristy5c4e2582011-09-11 19:21:03 +00002692 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2693 image->interpolate,exception);
cristyb51dff52011-05-19 16:55:47 +00002694 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002695 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00002696 break;
2697 }
2698 case OilPaintPreview:
2699 {
cristy14973ba2011-08-27 23:48:07 +00002700 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2701 exception);
2702 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2703 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002704 break;
2705 }
2706 case CharcoalDrawingPreview:
2707 {
anthony736a1602011-10-06 12:38:17 +00002708 /* FUTURE: user bias on charcoal! This is non-sensical! */
cristy3ed852e2009-09-05 21:47:34 +00002709 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
cristy05c0c9a2011-09-05 23:16:13 +00002710 image->bias,exception);
cristyb51dff52011-05-19 16:55:47 +00002711 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
cristy8cd5b312010-01-07 01:10:24 +00002712 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00002713 break;
2714 }
2715 case JPEGPreview:
2716 {
2717 char
2718 filename[MaxTextExtent];
2719
2720 int
2721 file;
2722
2723 MagickBooleanType
2724 status;
2725
2726 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2727 if (preview_image == (Image *) NULL)
2728 break;
cristybb503372010-05-27 20:51:26 +00002729 preview_info->quality=(size_t) percentage;
cristyb51dff52011-05-19 16:55:47 +00002730 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
cristye8c25f92010-06-03 00:53:06 +00002731 preview_info->quality);
cristy3ed852e2009-09-05 21:47:34 +00002732 file=AcquireUniqueFileResource(filename);
2733 if (file != -1)
2734 file=close(file)-1;
cristyb51dff52011-05-19 16:55:47 +00002735 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00002736 "jpeg:%s",filename);
cristy6f9e0d32011-08-28 16:32:09 +00002737 status=WriteImage(preview_info,preview_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002738 if (status != MagickFalse)
2739 {
2740 Image
2741 *quality_image;
2742
2743 (void) CopyMagickString(preview_info->filename,
2744 preview_image->filename,MaxTextExtent);
2745 quality_image=ReadImage(preview_info,exception);
2746 if (quality_image != (Image *) NULL)
2747 {
2748 preview_image=DestroyImage(preview_image);
2749 preview_image=quality_image;
2750 }
2751 }
2752 (void) RelinquishUniqueFileResource(preview_image->filename);
2753 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002754 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
cristy3ed852e2009-09-05 21:47:34 +00002755 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2756 1024.0/1024.0);
2757 else
2758 if (GetBlobSize(preview_image) >= 1024)
cristyb51dff52011-05-19 16:55:47 +00002759 (void) FormatLocaleString(label,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002760 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
cristy8cd5b312010-01-07 01:10:24 +00002761 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00002762 else
cristyb51dff52011-05-19 16:55:47 +00002763 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
cristy54ea5732011-06-10 12:39:53 +00002764 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
cristy3ed852e2009-09-05 21:47:34 +00002765 break;
2766 }
2767 }
2768 thumbnail=DestroyImage(thumbnail);
2769 percentage+=12.5;
2770 radius+=0.5;
2771 sigma+=0.25;
2772 if (preview_image == (Image *) NULL)
2773 break;
2774 (void) DeleteImageProperty(preview_image,"label");
cristyd15e6592011-10-15 00:13:06 +00002775 (void) SetImageProperty(preview_image,"label",label,exception);
cristy3ed852e2009-09-05 21:47:34 +00002776 AppendImageToList(&images,preview_image);
cristybb503372010-05-27 20:51:26 +00002777 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2778 NumberTiles);
cristy3ed852e2009-09-05 21:47:34 +00002779 if (proceed == MagickFalse)
2780 break;
2781 }
2782 if (images == (Image *) NULL)
2783 {
2784 preview_info=DestroyImageInfo(preview_info);
2785 return((Image *) NULL);
2786 }
2787 /*
2788 Create the montage.
2789 */
2790 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2791 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2792 montage_info->shadow=MagickTrue;
2793 (void) CloneString(&montage_info->tile,"3x3");
2794 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2795 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2796 montage_image=MontageImages(images,montage_info,exception);
2797 montage_info=DestroyMontageInfo(montage_info);
2798 images=DestroyImageList(images);
2799 if (montage_image == (Image *) NULL)
2800 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2801 if (montage_image->montage != (char *) NULL)
2802 {
2803 /*
2804 Free image directory.
2805 */
2806 montage_image->montage=(char *) RelinquishMagickMemory(
2807 montage_image->montage);
2808 if (image->directory != (char *) NULL)
2809 montage_image->directory=(char *) RelinquishMagickMemory(
2810 montage_image->directory);
2811 }
2812 preview_info=DestroyImageInfo(preview_info);
2813 return(montage_image);
2814}
2815
2816/*
2817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2818% %
2819% %
2820% %
2821% R a d i a l B l u r I m a g e %
2822% %
2823% %
2824% %
2825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2826%
2827% RadialBlurImage() applies a radial blur to the image.
2828%
2829% Andrew Protano contributed this effect.
2830%
2831% The format of the RadialBlurImage method is:
2832%
2833% Image *RadialBlurImage(const Image *image,const double angle,
cristy6435bd92011-09-10 02:10:07 +00002834% const double blur,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002835%
2836% A description of each parameter follows:
2837%
2838% o image: the image.
2839%
cristy3ed852e2009-09-05 21:47:34 +00002840% o angle: the angle of the radial blur.
2841%
cristy6435bd92011-09-10 02:10:07 +00002842% o blur: the blur.
2843%
cristy3ed852e2009-09-05 21:47:34 +00002844% o exception: return any errors or warnings in this structure.
2845%
2846*/
cristy4282c702011-11-21 00:01:06 +00002847MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2848 const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002849{
cristyc4c8d132010-01-07 01:58:38 +00002850 CacheView
2851 *blur_view,
2852 *image_view;
2853
cristy3ed852e2009-09-05 21:47:34 +00002854 Image
2855 *blur_image;
2856
cristy3ed852e2009-09-05 21:47:34 +00002857 MagickBooleanType
2858 status;
2859
cristybb503372010-05-27 20:51:26 +00002860 MagickOffsetType
2861 progress;
2862
cristy3ed852e2009-09-05 21:47:34 +00002863 MagickRealType
2864 blur_radius,
2865 *cos_theta,
2866 offset,
2867 *sin_theta,
2868 theta;
2869
2870 PointInfo
2871 blur_center;
2872
cristybb503372010-05-27 20:51:26 +00002873 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002874 i;
2875
cristybb503372010-05-27 20:51:26 +00002876 size_t
cristy3ed852e2009-09-05 21:47:34 +00002877 n;
2878
cristybb503372010-05-27 20:51:26 +00002879 ssize_t
2880 y;
2881
cristy3ed852e2009-09-05 21:47:34 +00002882 /*
2883 Allocate blur image.
2884 */
2885 assert(image != (Image *) NULL);
2886 assert(image->signature == MagickSignature);
2887 if (image->debug != MagickFalse)
2888 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2889 assert(exception != (ExceptionInfo *) NULL);
2890 assert(exception->signature == MagickSignature);
cristy1e7aa312011-09-10 20:01:36 +00002891 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002892 if (blur_image == (Image *) NULL)
2893 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002894 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002895 {
cristy3ed852e2009-09-05 21:47:34 +00002896 blur_image=DestroyImage(blur_image);
2897 return((Image *) NULL);
2898 }
2899 blur_center.x=(double) image->columns/2.0;
2900 blur_center.y=(double) image->rows/2.0;
2901 blur_radius=hypot(blur_center.x,blur_center.y);
cristy117ff172010-08-15 21:35:32 +00002902 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
cristy3ed852e2009-09-05 21:47:34 +00002903 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2904 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2905 sizeof(*cos_theta));
2906 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2907 sizeof(*sin_theta));
2908 if ((cos_theta == (MagickRealType *) NULL) ||
2909 (sin_theta == (MagickRealType *) NULL))
2910 {
2911 blur_image=DestroyImage(blur_image);
2912 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2913 }
2914 offset=theta*(MagickRealType) (n-1)/2.0;
cristybb503372010-05-27 20:51:26 +00002915 for (i=0; i < (ssize_t) n; i++)
cristy3ed852e2009-09-05 21:47:34 +00002916 {
2917 cos_theta[i]=cos((double) (theta*i-offset));
2918 sin_theta[i]=sin((double) (theta*i-offset));
2919 }
2920 /*
2921 Radial blur image.
2922 */
2923 status=MagickTrue;
2924 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002925 image_view=AcquireCacheView(image);
2926 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00002927#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002928 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002929#endif
cristy1e7aa312011-09-10 20:01:36 +00002930 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002931 {
cristy1e7aa312011-09-10 20:01:36 +00002932 register const Quantum
2933 *restrict p;
2934
cristy4c08aed2011-07-01 19:47:50 +00002935 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002936 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002937
cristy117ff172010-08-15 21:35:32 +00002938 register ssize_t
2939 x;
2940
cristy3ed852e2009-09-05 21:47:34 +00002941 if (status == MagickFalse)
2942 continue;
cristy1e7aa312011-09-10 20:01:36 +00002943 p=GetCacheViewVirtualPixels(blur_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00002944 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2945 exception);
cristy1e7aa312011-09-10 20:01:36 +00002946 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00002947 {
2948 status=MagickFalse;
2949 continue;
2950 }
cristy1e7aa312011-09-10 20:01:36 +00002951 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002952 {
cristy3ed852e2009-09-05 21:47:34 +00002953 MagickRealType
cristy3ed852e2009-09-05 21:47:34 +00002954 radius;
2955
cristy3ed852e2009-09-05 21:47:34 +00002956 PointInfo
2957 center;
2958
cristybb503372010-05-27 20:51:26 +00002959 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002960 i;
2961
cristybb503372010-05-27 20:51:26 +00002962 size_t
cristy3ed852e2009-09-05 21:47:34 +00002963 step;
2964
2965 center.x=(double) x-blur_center.x;
2966 center.y=(double) y-blur_center.y;
2967 radius=hypot((double) center.x,center.y);
2968 if (radius == 0)
2969 step=1;
2970 else
2971 {
cristybb503372010-05-27 20:51:26 +00002972 step=(size_t) (blur_radius/radius);
cristy3ed852e2009-09-05 21:47:34 +00002973 if (step == 0)
2974 step=1;
2975 else
2976 if (step >= n)
2977 step=n-1;
2978 }
cristy1e7aa312011-09-10 20:01:36 +00002979 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2980 {
2981 MagickRealType
2982 gamma,
2983 pixel;
cristy3ed852e2009-09-05 21:47:34 +00002984
cristy1e7aa312011-09-10 20:01:36 +00002985 PixelChannel
2986 channel;
2987
2988 PixelTrait
2989 blur_traits,
2990 traits;
2991
2992 register const Quantum
2993 *restrict r;
2994
2995 register ssize_t
2996 j;
2997
cristye2a912b2011-12-05 20:02:07 +00002998 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00002999 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003000 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3001 if ((traits == UndefinedPixelTrait) ||
3002 (blur_traits == UndefinedPixelTrait))
3003 continue;
3004 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003005 {
cristy0beccfa2011-09-25 20:47:53 +00003006 SetPixelChannel(blur_image,channel,p[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003007 continue;
cristy3ed852e2009-09-05 21:47:34 +00003008 }
cristy1e7aa312011-09-10 20:01:36 +00003009 gamma=0.0;
3010 pixel=bias;
3011 if ((blur_traits & BlendPixelTrait) == 0)
3012 {
3013 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3014 {
3015 r=GetCacheViewVirtualPixels(image_view, (ssize_t) (blur_center.x+
3016 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3017 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3018 1,1,exception);
3019 if (r == (const Quantum *) NULL)
3020 {
3021 status=MagickFalse;
3022 continue;
3023 }
3024 pixel+=r[i];
3025 gamma++;
3026 }
3027 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003028 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003029 continue;
3030 }
3031 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3032 {
3033 r=GetCacheViewVirtualPixels(image_view, (ssize_t) (blur_center.x+
3034 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3035 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3036 1,1,exception);
3037 if (r == (const Quantum *) NULL)
3038 {
3039 status=MagickFalse;
3040 continue;
3041 }
3042 pixel+=GetPixelAlpha(image,r)*r[i];
3043 gamma+=GetPixelAlpha(image,r);
cristy3ed852e2009-09-05 21:47:34 +00003044 }
cristy1e7aa312011-09-10 20:01:36 +00003045 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003046 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003047 }
3048 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003049 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003050 }
3051 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3052 status=MagickFalse;
3053 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3054 {
3055 MagickBooleanType
3056 proceed;
3057
cristyb5d5f722009-11-04 03:03:49 +00003058#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003059 #pragma omp critical (MagickCore_RadialBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003060#endif
3061 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3062 if (proceed == MagickFalse)
3063 status=MagickFalse;
3064 }
3065 }
3066 blur_view=DestroyCacheView(blur_view);
3067 image_view=DestroyCacheView(image_view);
3068 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3069 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3070 if (status == MagickFalse)
3071 blur_image=DestroyImage(blur_image);
3072 return(blur_image);
3073}
3074
3075/*
3076%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3077% %
3078% %
3079% %
cristy3ed852e2009-09-05 21:47:34 +00003080% S e l e c t i v e B l u r I m a g e %
3081% %
3082% %
3083% %
3084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3085%
3086% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3087% It is similar to the unsharpen mask that sharpens everything with contrast
3088% above a certain threshold.
3089%
3090% The format of the SelectiveBlurImage method is:
3091%
3092% Image *SelectiveBlurImage(const Image *image,const double radius,
cristy1e7aa312011-09-10 20:01:36 +00003093% const double sigma,const double threshold,const double bias,
3094% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003095%
3096% A description of each parameter follows:
3097%
3098% o image: the image.
3099%
cristy3ed852e2009-09-05 21:47:34 +00003100% o radius: the radius of the Gaussian, in pixels, not counting the center
3101% pixel.
3102%
3103% o sigma: the standard deviation of the Gaussian, in pixels.
3104%
3105% o threshold: only pixels within this contrast threshold are included
3106% in the blur operation.
3107%
cristy1e7aa312011-09-10 20:01:36 +00003108% o bias: the bias.
3109%
cristy3ed852e2009-09-05 21:47:34 +00003110% o exception: return any errors or warnings in this structure.
3111%
3112*/
cristy4282c702011-11-21 00:01:06 +00003113MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3114 const double sigma,const double threshold,const double bias,
3115 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003116{
3117#define SelectiveBlurImageTag "SelectiveBlur/Image"
3118
cristy47e00502009-12-17 19:19:57 +00003119 CacheView
3120 *blur_view,
3121 *image_view;
3122
cristy3ed852e2009-09-05 21:47:34 +00003123 double
cristy3ed852e2009-09-05 21:47:34 +00003124 *kernel;
3125
3126 Image
3127 *blur_image;
3128
cristy3ed852e2009-09-05 21:47:34 +00003129 MagickBooleanType
3130 status;
3131
cristybb503372010-05-27 20:51:26 +00003132 MagickOffsetType
3133 progress;
3134
cristybb503372010-05-27 20:51:26 +00003135 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003136 i;
cristy3ed852e2009-09-05 21:47:34 +00003137
cristybb503372010-05-27 20:51:26 +00003138 size_t
cristy3ed852e2009-09-05 21:47:34 +00003139 width;
3140
cristybb503372010-05-27 20:51:26 +00003141 ssize_t
cristyc8523c12011-09-13 00:02:53 +00003142 center,
cristybb503372010-05-27 20:51:26 +00003143 j,
3144 u,
3145 v,
3146 y;
3147
cristy3ed852e2009-09-05 21:47:34 +00003148 /*
3149 Initialize blur image attributes.
3150 */
3151 assert(image != (Image *) NULL);
3152 assert(image->signature == MagickSignature);
3153 if (image->debug != MagickFalse)
3154 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3155 assert(exception != (ExceptionInfo *) NULL);
3156 assert(exception->signature == MagickSignature);
3157 width=GetOptimalKernelWidth1D(radius,sigma);
3158 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3159 if (kernel == (double *) NULL)
3160 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00003161 j=(ssize_t) width/2;
cristy3ed852e2009-09-05 21:47:34 +00003162 i=0;
cristy47e00502009-12-17 19:19:57 +00003163 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003164 {
cristy47e00502009-12-17 19:19:57 +00003165 for (u=(-j); u <= j; u++)
cristy4205a3c2010-09-12 20:19:59 +00003166 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3167 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00003168 }
3169 if (image->debug != MagickFalse)
3170 {
3171 char
3172 format[MaxTextExtent],
3173 *message;
3174
cristy117ff172010-08-15 21:35:32 +00003175 register const double
3176 *k;
3177
cristybb503372010-05-27 20:51:26 +00003178 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003179 u,
3180 v;
3181
cristy3ed852e2009-09-05 21:47:34 +00003182 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003183 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3184 width);
cristy3ed852e2009-09-05 21:47:34 +00003185 message=AcquireString("");
3186 k=kernel;
cristybb503372010-05-27 20:51:26 +00003187 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003188 {
3189 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00003190 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristy3ed852e2009-09-05 21:47:34 +00003191 (void) ConcatenateString(&message,format);
cristybb503372010-05-27 20:51:26 +00003192 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003193 {
cristyb51dff52011-05-19 16:55:47 +00003194 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
cristy3ed852e2009-09-05 21:47:34 +00003195 (void) ConcatenateString(&message,format);
3196 }
3197 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3198 }
3199 message=DestroyString(message);
3200 }
cristy1e7aa312011-09-10 20:01:36 +00003201 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003202 if (blur_image == (Image *) NULL)
3203 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003204 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003205 {
cristy3ed852e2009-09-05 21:47:34 +00003206 blur_image=DestroyImage(blur_image);
3207 return((Image *) NULL);
3208 }
3209 /*
3210 Threshold blur image.
3211 */
3212 status=MagickTrue;
3213 progress=0;
cristyc8523c12011-09-13 00:02:53 +00003214 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3215 GetPixelChannels(image)*(width/2L));
cristy3ed852e2009-09-05 21:47:34 +00003216 image_view=AcquireCacheView(image);
3217 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003218#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003219 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003220#endif
cristybb503372010-05-27 20:51:26 +00003221 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003222 {
cristy4c08aed2011-07-01 19:47:50 +00003223 double
3224 contrast;
3225
cristy3ed852e2009-09-05 21:47:34 +00003226 MagickBooleanType
3227 sync;
3228
cristy4c08aed2011-07-01 19:47:50 +00003229 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003230 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003231
cristy4c08aed2011-07-01 19:47:50 +00003232 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003233 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003234
cristy117ff172010-08-15 21:35:32 +00003235 register ssize_t
3236 x;
3237
cristy3ed852e2009-09-05 21:47:34 +00003238 if (status == MagickFalse)
3239 continue;
cristy117ff172010-08-15 21:35:32 +00003240 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3241 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +00003242 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3243 exception);
cristy4c08aed2011-07-01 19:47:50 +00003244 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003245 {
3246 status=MagickFalse;
3247 continue;
3248 }
cristybb503372010-05-27 20:51:26 +00003249 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003250 {
cristybb503372010-05-27 20:51:26 +00003251 register ssize_t
cristy1e7aa312011-09-10 20:01:36 +00003252 i;
cristy3ed852e2009-09-05 21:47:34 +00003253
cristy1e7aa312011-09-10 20:01:36 +00003254 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3255 {
3256 MagickRealType
3257 alpha,
3258 gamma,
3259 intensity,
3260 pixel;
cristy117ff172010-08-15 21:35:32 +00003261
cristy1e7aa312011-09-10 20:01:36 +00003262 PixelChannel
3263 channel;
3264
3265 PixelTrait
3266 blur_traits,
3267 traits;
3268
3269 register const double
3270 *restrict k;
3271
3272 register const Quantum
3273 *restrict pixels;
3274
3275 register ssize_t
3276 u;
3277
3278 ssize_t
3279 v;
3280
cristye2a912b2011-12-05 20:02:07 +00003281 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003282 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003283 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3284 if ((traits == UndefinedPixelTrait) ||
3285 (blur_traits == UndefinedPixelTrait))
3286 continue;
3287 if ((blur_traits & CopyPixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003288 {
cristy0beccfa2011-09-25 20:47:53 +00003289 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003290 continue;
cristy3ed852e2009-09-05 21:47:34 +00003291 }
cristy1e7aa312011-09-10 20:01:36 +00003292 k=kernel;
3293 pixel=bias;
3294 pixels=p;
cristyc8523c12011-09-13 00:02:53 +00003295 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
cristy1e7aa312011-09-10 20:01:36 +00003296 gamma=0.0;
3297 if ((blur_traits & BlendPixelTrait) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003298 {
cristy1e7aa312011-09-10 20:01:36 +00003299 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +00003300 {
cristy1e7aa312011-09-10 20:01:36 +00003301 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +00003302 {
cristy1e7aa312011-09-10 20:01:36 +00003303 contrast=GetPixelIntensity(image,pixels)-intensity;
3304 if (fabs(contrast) < threshold)
3305 {
3306 pixel+=(*k)*pixels[i];
3307 gamma+=(*k);
3308 }
3309 k++;
3310 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003311 }
cristy1e7aa312011-09-10 20:01:36 +00003312 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003313 }
cristy1e7aa312011-09-10 20:01:36 +00003314 if (fabs((double) gamma) < MagickEpsilon)
3315 {
cristy0beccfa2011-09-25 20:47:53 +00003316 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003317 continue;
3318 }
3319 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003320 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003321 continue;
3322 }
3323 for (v=0; v < (ssize_t) width; v++)
3324 {
3325 for (u=0; u < (ssize_t) width; u++)
3326 {
3327 contrast=GetPixelIntensity(image,pixels)-intensity;
3328 if (fabs(contrast) < threshold)
3329 {
3330 alpha=(MagickRealType) (QuantumScale*
3331 GetPixelAlpha(image,pixels));
3332 pixel+=(*k)*alpha*pixels[i];
3333 gamma+=(*k)*alpha;
3334 }
3335 k++;
3336 pixels+=GetPixelChannels(image);
3337 }
3338 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003339 }
cristy1e7aa312011-09-10 20:01:36 +00003340 if (fabs((double) gamma) < MagickEpsilon)
3341 {
cristy0beccfa2011-09-25 20:47:53 +00003342 SetPixelChannel(blur_image,channel,p[center+i],q);
cristy1e7aa312011-09-10 20:01:36 +00003343 continue;
3344 }
3345 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy0beccfa2011-09-25 20:47:53 +00003346 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
cristy1e7aa312011-09-10 20:01:36 +00003347 }
cristyed231572011-07-14 02:18:59 +00003348 p+=GetPixelChannels(image);
3349 q+=GetPixelChannels(blur_image);
cristy3ed852e2009-09-05 21:47:34 +00003350 }
3351 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3352 if (sync == MagickFalse)
3353 status=MagickFalse;
3354 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3355 {
3356 MagickBooleanType
3357 proceed;
3358
cristyb5d5f722009-11-04 03:03:49 +00003359#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003360 #pragma omp critical (MagickCore_SelectiveBlurImage)
cristy3ed852e2009-09-05 21:47:34 +00003361#endif
3362 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3363 image->rows);
3364 if (proceed == MagickFalse)
3365 status=MagickFalse;
3366 }
3367 }
3368 blur_image->type=image->type;
3369 blur_view=DestroyCacheView(blur_view);
3370 image_view=DestroyCacheView(image_view);
3371 kernel=(double *) RelinquishMagickMemory(kernel);
3372 if (status == MagickFalse)
3373 blur_image=DestroyImage(blur_image);
3374 return(blur_image);
3375}
3376
3377/*
3378%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3379% %
3380% %
3381% %
3382% S h a d e I m a g e %
3383% %
3384% %
3385% %
3386%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3387%
3388% ShadeImage() shines a distant light on an image to create a
3389% three-dimensional effect. You control the positioning of the light with
3390% azimuth and elevation; azimuth is measured in degrees off the x axis
3391% and elevation is measured in pixels above the Z axis.
3392%
3393% The format of the ShadeImage method is:
3394%
3395% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3396% const double azimuth,const double elevation,ExceptionInfo *exception)
3397%
3398% A description of each parameter follows:
3399%
3400% o image: the image.
3401%
3402% o gray: A value other than zero shades the intensity of each pixel.
3403%
3404% o azimuth, elevation: Define the light source direction.
3405%
3406% o exception: return any errors or warnings in this structure.
3407%
3408*/
3409MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3410 const double azimuth,const double elevation,ExceptionInfo *exception)
3411{
3412#define ShadeImageTag "Shade/Image"
3413
cristyc4c8d132010-01-07 01:58:38 +00003414 CacheView
3415 *image_view,
3416 *shade_view;
3417
cristy3ed852e2009-09-05 21:47:34 +00003418 Image
3419 *shade_image;
3420
cristy3ed852e2009-09-05 21:47:34 +00003421 MagickBooleanType
3422 status;
3423
cristybb503372010-05-27 20:51:26 +00003424 MagickOffsetType
3425 progress;
3426
cristy3ed852e2009-09-05 21:47:34 +00003427 PrimaryInfo
3428 light;
3429
cristybb503372010-05-27 20:51:26 +00003430 ssize_t
3431 y;
3432
cristy3ed852e2009-09-05 21:47:34 +00003433 /*
3434 Initialize shaded image attributes.
3435 */
3436 assert(image != (const Image *) NULL);
3437 assert(image->signature == MagickSignature);
3438 if (image->debug != MagickFalse)
3439 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3440 assert(exception != (ExceptionInfo *) NULL);
3441 assert(exception->signature == MagickSignature);
3442 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3443 if (shade_image == (Image *) NULL)
3444 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003445 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003446 {
cristy3ed852e2009-09-05 21:47:34 +00003447 shade_image=DestroyImage(shade_image);
3448 return((Image *) NULL);
3449 }
3450 /*
3451 Compute the light vector.
3452 */
3453 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3454 cos(DegreesToRadians(elevation));
3455 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3456 cos(DegreesToRadians(elevation));
3457 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3458 /*
3459 Shade image.
3460 */
3461 status=MagickTrue;
3462 progress=0;
3463 image_view=AcquireCacheView(image);
3464 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00003465#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003466 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003467#endif
cristybb503372010-05-27 20:51:26 +00003468 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003469 {
3470 MagickRealType
3471 distance,
3472 normal_distance,
3473 shade;
3474
3475 PrimaryInfo
3476 normal;
3477
cristy4c08aed2011-07-01 19:47:50 +00003478 register const Quantum
cristy1e7aa312011-09-10 20:01:36 +00003479 *restrict center,
cristyc47d1f82009-11-26 01:44:43 +00003480 *restrict p,
cristy1e7aa312011-09-10 20:01:36 +00003481 *restrict post,
3482 *restrict pre;
cristy3ed852e2009-09-05 21:47:34 +00003483
cristy4c08aed2011-07-01 19:47:50 +00003484 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003485 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003486
cristy117ff172010-08-15 21:35:32 +00003487 register ssize_t
3488 x;
3489
cristy3ed852e2009-09-05 21:47:34 +00003490 if (status == MagickFalse)
3491 continue;
3492 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3493 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3494 exception);
cristy4c08aed2011-07-01 19:47:50 +00003495 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003496 {
3497 status=MagickFalse;
3498 continue;
3499 }
3500 /*
3501 Shade this row of pixels.
3502 */
3503 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
cristy1e7aa312011-09-10 20:01:36 +00003504 pre=p+GetPixelChannels(image);
3505 center=pre+(image->columns+2)*GetPixelChannels(image);
3506 post=center+(image->columns+2)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00003507 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003508 {
cristy1e7aa312011-09-10 20:01:36 +00003509 register ssize_t
3510 i;
3511
cristy3ed852e2009-09-05 21:47:34 +00003512 /*
3513 Determine the surface normal and compute shading.
3514 */
cristy1e7aa312011-09-10 20:01:36 +00003515 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3516 GetPixelIntensity(image,center-GetPixelChannels(image))+
3517 GetPixelIntensity(image,post-GetPixelChannels(image))-
3518 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3519 GetPixelIntensity(image,center+GetPixelChannels(image))-
3520 GetPixelIntensity(image,post+GetPixelChannels(image)));
3521 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3522 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3523 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3524 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3525 GetPixelIntensity(image,pre+GetPixelChannels(image)));
cristy3ed852e2009-09-05 21:47:34 +00003526 if ((normal.x == 0.0) && (normal.y == 0.0))
3527 shade=light.z;
3528 else
3529 {
3530 shade=0.0;
3531 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3532 if (distance > MagickEpsilon)
3533 {
3534 normal_distance=
3535 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3536 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3537 shade=distance/sqrt((double) normal_distance);
3538 }
3539 }
cristy1e7aa312011-09-10 20:01:36 +00003540 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3541 {
3542 PixelChannel
3543 channel;
3544
3545 PixelTrait
3546 shade_traits,
3547 traits;
3548
cristye2a912b2011-12-05 20:02:07 +00003549 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003550 traits=GetPixelChannelMapTraits(image,channel);
cristy1e7aa312011-09-10 20:01:36 +00003551 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3552 if ((traits == UndefinedPixelTrait) ||
3553 (shade_traits == UndefinedPixelTrait))
3554 continue;
3555 if ((shade_traits & CopyPixelTrait) != 0)
3556 {
cristy0beccfa2011-09-25 20:47:53 +00003557 SetPixelChannel(shade_image,channel,center[i],q);
cristy1e7aa312011-09-10 20:01:36 +00003558 continue;
3559 }
3560 if (gray != MagickFalse)
3561 {
cristy0beccfa2011-09-25 20:47:53 +00003562 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
cristy1e7aa312011-09-10 20:01:36 +00003563 continue;
3564 }
cristy0beccfa2011-09-25 20:47:53 +00003565 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3566 center[i]),q);
cristy1e7aa312011-09-10 20:01:36 +00003567 }
3568 pre+=GetPixelChannels(image);
3569 center+=GetPixelChannels(image);
3570 post+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003571 q+=GetPixelChannels(shade_image);
cristy3ed852e2009-09-05 21:47:34 +00003572 }
3573 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3574 status=MagickFalse;
3575 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3576 {
3577 MagickBooleanType
3578 proceed;
3579
cristyb5d5f722009-11-04 03:03:49 +00003580#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003581 #pragma omp critical (MagickCore_ShadeImage)
3582#endif
3583 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3584 if (proceed == MagickFalse)
3585 status=MagickFalse;
3586 }
3587 }
3588 shade_view=DestroyCacheView(shade_view);
3589 image_view=DestroyCacheView(image_view);
3590 if (status == MagickFalse)
3591 shade_image=DestroyImage(shade_image);
3592 return(shade_image);
3593}
3594
3595/*
3596%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3597% %
3598% %
3599% %
3600% S h a r p e n I m a g e %
3601% %
3602% %
3603% %
3604%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3605%
3606% SharpenImage() sharpens the image. We convolve the image with a Gaussian
3607% operator of the given radius and standard deviation (sigma). For
3608% reasonable results, radius should be larger than sigma. Use a radius of 0
3609% and SharpenImage() selects a suitable radius for you.
3610%
3611% Using a separable kernel would be faster, but the negative weights cancel
3612% out on the corners of the kernel producing often undesirable ringing in the
3613% filtered result; this can be avoided by using a 2D gaussian shaped image
3614% sharpening kernel instead.
3615%
3616% The format of the SharpenImage method is:
3617%
3618% Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003619% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003620%
3621% A description of each parameter follows:
3622%
3623% o image: the image.
3624%
cristy3ed852e2009-09-05 21:47:34 +00003625% o radius: the radius of the Gaussian, in pixels, not counting the center
3626% pixel.
3627%
3628% o sigma: the standard deviation of the Laplacian, in pixels.
3629%
cristy05c0c9a2011-09-05 23:16:13 +00003630% o bias: bias.
3631%
cristy3ed852e2009-09-05 21:47:34 +00003632% o exception: return any errors or warnings in this structure.
3633%
3634*/
cristy3ed852e2009-09-05 21:47:34 +00003635MagickExport Image *SharpenImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00003636 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003637{
cristy3ed852e2009-09-05 21:47:34 +00003638 double
cristy47e00502009-12-17 19:19:57 +00003639 normalize;
cristy3ed852e2009-09-05 21:47:34 +00003640
3641 Image
3642 *sharp_image;
3643
cristy41cbe682011-07-15 19:12:37 +00003644 KernelInfo
3645 *kernel_info;
3646
cristybb503372010-05-27 20:51:26 +00003647 register ssize_t
cristy47e00502009-12-17 19:19:57 +00003648 i;
3649
cristybb503372010-05-27 20:51:26 +00003650 size_t
cristy3ed852e2009-09-05 21:47:34 +00003651 width;
3652
cristy117ff172010-08-15 21:35:32 +00003653 ssize_t
3654 j,
3655 u,
3656 v;
3657
cristy3ed852e2009-09-05 21:47:34 +00003658 assert(image != (const Image *) NULL);
3659 assert(image->signature == MagickSignature);
3660 if (image->debug != MagickFalse)
3661 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3662 assert(exception != (ExceptionInfo *) NULL);
3663 assert(exception->signature == MagickSignature);
3664 width=GetOptimalKernelWidth2D(radius,sigma);
cristy5e6be1e2011-07-16 01:23:39 +00003665 kernel_info=AcquireKernelInfo((const char *) NULL);
cristy41cbe682011-07-15 19:12:37 +00003666 if (kernel_info == (KernelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003667 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy41cbe682011-07-15 19:12:37 +00003668 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3669 kernel_info->width=width;
3670 kernel_info->height=width;
anthony736a1602011-10-06 12:38:17 +00003671 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
cristy41cbe682011-07-15 19:12:37 +00003672 kernel_info->signature=MagickSignature;
cristya96f2492011-12-14 18:25:41 +00003673 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3674 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3675 if (kernel_info->values == (MagickRealType *) NULL)
cristy41cbe682011-07-15 19:12:37 +00003676 {
3677 kernel_info=DestroyKernelInfo(kernel_info);
3678 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3679 }
cristy3ed852e2009-09-05 21:47:34 +00003680 normalize=0.0;
cristy41cbe682011-07-15 19:12:37 +00003681 j=(ssize_t) kernel_info->width/2;
cristy47e00502009-12-17 19:19:57 +00003682 i=0;
3683 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00003684 {
cristy47e00502009-12-17 19:19:57 +00003685 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00003686 {
cristy41cbe682011-07-15 19:12:37 +00003687 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3688 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3689 normalize+=kernel_info->values[i];
cristy3ed852e2009-09-05 21:47:34 +00003690 i++;
3691 }
3692 }
cristy41cbe682011-07-15 19:12:37 +00003693 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
cristy5e6be1e2011-07-16 01:23:39 +00003694 sharp_image=ConvolveImage(image,kernel_info,exception);
cristy41cbe682011-07-15 19:12:37 +00003695 kernel_info=DestroyKernelInfo(kernel_info);
cristy3ed852e2009-09-05 21:47:34 +00003696 return(sharp_image);
3697}
3698
3699/*
3700%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3701% %
3702% %
3703% %
3704% S p r e a d I m a g e %
3705% %
3706% %
3707% %
3708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3709%
3710% SpreadImage() is a special effects method that randomly displaces each
3711% pixel in a block defined by the radius parameter.
3712%
3713% The format of the SpreadImage method is:
3714%
3715% Image *SpreadImage(const Image *image,const double radius,
cristy5c4e2582011-09-11 19:21:03 +00003716% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003717%
3718% A description of each parameter follows:
3719%
3720% o image: the image.
3721%
cristy5c4e2582011-09-11 19:21:03 +00003722% o radius: choose a random pixel in a neighborhood of this extent.
3723%
3724% o method: the pixel interpolation method.
cristy3ed852e2009-09-05 21:47:34 +00003725%
3726% o exception: return any errors or warnings in this structure.
3727%
3728*/
3729MagickExport 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#define SpreadImageTag "Spread/Image"
3733
cristyfa112112010-01-04 17:48:07 +00003734 CacheView
cristy9f7e7cb2011-03-26 00:49:57 +00003735 *image_view,
3736 *spread_view;
cristyfa112112010-01-04 17:48:07 +00003737
cristy3ed852e2009-09-05 21:47:34 +00003738 Image
3739 *spread_image;
3740
cristy3ed852e2009-09-05 21:47:34 +00003741 MagickBooleanType
3742 status;
3743
cristybb503372010-05-27 20:51:26 +00003744 MagickOffsetType
3745 progress;
3746
cristy3ed852e2009-09-05 21:47:34 +00003747 RandomInfo
cristyfa112112010-01-04 17:48:07 +00003748 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00003749
cristybb503372010-05-27 20:51:26 +00003750 size_t
cristy3ed852e2009-09-05 21:47:34 +00003751 width;
3752
cristybb503372010-05-27 20:51:26 +00003753 ssize_t
3754 y;
3755
cristy3ed852e2009-09-05 21:47:34 +00003756 /*
3757 Initialize spread image attributes.
3758 */
3759 assert(image != (Image *) NULL);
3760 assert(image->signature == MagickSignature);
3761 if (image->debug != MagickFalse)
3762 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3763 assert(exception != (ExceptionInfo *) NULL);
3764 assert(exception->signature == MagickSignature);
3765 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3766 exception);
3767 if (spread_image == (Image *) NULL)
3768 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003769 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003770 {
cristy3ed852e2009-09-05 21:47:34 +00003771 spread_image=DestroyImage(spread_image);
3772 return((Image *) NULL);
3773 }
3774 /*
3775 Spread image.
3776 */
3777 status=MagickTrue;
3778 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003779 width=GetOptimalKernelWidth1D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +00003780 random_info=AcquireRandomInfoThreadSet();
cristy9f7e7cb2011-03-26 00:49:57 +00003781 image_view=AcquireCacheView(image);
3782 spread_view=AcquireCacheView(spread_image);
cristyb557a152011-02-22 12:14:30 +00003783#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13e98362012-01-12 02:08:08 +00003784 #pragma omp parallel for schedule(static,1) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003785#endif
cristybe82ad52011-09-06 01:17:20 +00003786 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003787 {
cristy5c9e6f22010-09-17 17:31:01 +00003788 const int
3789 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003790
cristybe82ad52011-09-06 01:17:20 +00003791 register const Quantum
3792 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003793
cristy4c08aed2011-07-01 19:47:50 +00003794 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003795 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003796
cristy117ff172010-08-15 21:35:32 +00003797 register ssize_t
3798 x;
3799
cristy3ed852e2009-09-05 21:47:34 +00003800 if (status == MagickFalse)
3801 continue;
cristybe82ad52011-09-06 01:17:20 +00003802 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy9f7e7cb2011-03-26 00:49:57 +00003803 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003804 exception);
cristybe82ad52011-09-06 01:17:20 +00003805 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003806 {
3807 status=MagickFalse;
3808 continue;
3809 }
cristybe82ad52011-09-06 01:17:20 +00003810 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003811 {
cristybe82ad52011-09-06 01:17:20 +00003812 PointInfo
3813 point;
3814
cristybe82ad52011-09-06 01:17:20 +00003815 point.x=GetPseudoRandomValue(random_info[id]);
3816 point.y=GetPseudoRandomValue(random_info[id]);
cristy5c4e2582011-09-11 19:21:03 +00003817 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3818 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
cristyed231572011-07-14 02:18:59 +00003819 q+=GetPixelChannels(spread_image);
cristy3ed852e2009-09-05 21:47:34 +00003820 }
cristy9f7e7cb2011-03-26 00:49:57 +00003821 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003822 status=MagickFalse;
3823 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3824 {
3825 MagickBooleanType
3826 proceed;
3827
cristyb557a152011-02-22 12:14:30 +00003828#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003829 #pragma omp critical (MagickCore_SpreadImage)
3830#endif
3831 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3832 if (proceed == MagickFalse)
3833 status=MagickFalse;
3834 }
3835 }
cristy9f7e7cb2011-03-26 00:49:57 +00003836 spread_view=DestroyCacheView(spread_view);
cristy3ed852e2009-09-05 21:47:34 +00003837 image_view=DestroyCacheView(image_view);
3838 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00003839 return(spread_image);
3840}
3841
3842/*
3843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3844% %
3845% %
3846% %
3847% U n s h a r p M a s k I m a g e %
3848% %
3849% %
3850% %
3851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3852%
3853% UnsharpMaskImage() sharpens one or more image channels. We convolve the
3854% image with a Gaussian operator of the given radius and standard deviation
3855% (sigma). For reasonable results, radius should be larger than sigma. Use a
3856% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3857%
3858% The format of the UnsharpMaskImage method is:
3859%
3860% Image *UnsharpMaskImage(const Image *image,const double radius,
3861% const double sigma,const double amount,const double threshold,
3862% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003863%
3864% A description of each parameter follows:
3865%
3866% o image: the image.
3867%
cristy3ed852e2009-09-05 21:47:34 +00003868% o radius: the radius of the Gaussian, in pixels, not counting the center
3869% pixel.
3870%
3871% o sigma: the standard deviation of the Gaussian, in pixels.
3872%
3873% o amount: the percentage of the difference between the original and the
3874% blur image that is added back into the original.
3875%
3876% o threshold: the threshold in pixels needed to apply the diffence amount.
3877%
3878% o exception: return any errors or warnings in this structure.
3879%
3880*/
cristy31bb6272011-11-20 23:57:25 +00003881MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3882 const double sigma,const double amount,const double threshold,
3883 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003884{
3885#define SharpenImageTag "Sharpen/Image"
3886
cristyc4c8d132010-01-07 01:58:38 +00003887 CacheView
3888 *image_view,
3889 *unsharp_view;
3890
cristy3ed852e2009-09-05 21:47:34 +00003891 Image
3892 *unsharp_image;
3893
cristy3ed852e2009-09-05 21:47:34 +00003894 MagickBooleanType
3895 status;
3896
cristybb503372010-05-27 20:51:26 +00003897 MagickOffsetType
3898 progress;
3899
cristy3ed852e2009-09-05 21:47:34 +00003900 MagickRealType
3901 quantum_threshold;
3902
cristybb503372010-05-27 20:51:26 +00003903 ssize_t
3904 y;
3905
cristy3ed852e2009-09-05 21:47:34 +00003906 assert(image != (const Image *) NULL);
3907 assert(image->signature == MagickSignature);
3908 if (image->debug != MagickFalse)
3909 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3910 assert(exception != (ExceptionInfo *) NULL);
anthony736a1602011-10-06 12:38:17 +00003911
3912
3913 /* FUTURE: use of bias on sharpen is non-sensical */
cristy05c0c9a2011-09-05 23:16:13 +00003914 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
anthony736a1602011-10-06 12:38:17 +00003915
cristy3ed852e2009-09-05 21:47:34 +00003916 if (unsharp_image == (Image *) NULL)
3917 return((Image *) NULL);
3918 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3919 /*
3920 Unsharp-mask image.
3921 */
3922 status=MagickTrue;
3923 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003924 image_view=AcquireCacheView(image);
3925 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00003926#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003927 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003928#endif
cristybb503372010-05-27 20:51:26 +00003929 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003930 {
cristy4c08aed2011-07-01 19:47:50 +00003931 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003932 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003933
cristy4c08aed2011-07-01 19:47:50 +00003934 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003935 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003936
cristy117ff172010-08-15 21:35:32 +00003937 register ssize_t
3938 x;
3939
cristy3ed852e2009-09-05 21:47:34 +00003940 if (status == MagickFalse)
3941 continue;
3942 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3943 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3944 exception);
cristy4c08aed2011-07-01 19:47:50 +00003945 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003946 {
3947 status=MagickFalse;
3948 continue;
3949 }
cristybb503372010-05-27 20:51:26 +00003950 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003951 {
cristy7f3a0d12011-09-05 23:27:59 +00003952 register ssize_t
3953 i;
3954
3955 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3956 {
3957 MagickRealType
3958 pixel;
3959
3960 PixelChannel
3961 channel;
3962
3963 PixelTrait
3964 traits,
3965 unsharp_traits;
3966
cristye2a912b2011-12-05 20:02:07 +00003967 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003968 traits=GetPixelChannelMapTraits(image,channel);
cristy7f3a0d12011-09-05 23:27:59 +00003969 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3970 if ((traits == UndefinedPixelTrait) ||
3971 (unsharp_traits == UndefinedPixelTrait))
3972 continue;
3973 if ((unsharp_traits & CopyPixelTrait) != 0)
3974 {
cristy0beccfa2011-09-25 20:47:53 +00003975 SetPixelChannel(unsharp_image,channel,p[i],q);
cristy7f3a0d12011-09-05 23:27:59 +00003976 continue;
3977 }
cristy0beccfa2011-09-25 20:47:53 +00003978 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
cristy7f3a0d12011-09-05 23:27:59 +00003979 if (fabs(2.0*pixel) < quantum_threshold)
3980 pixel=(MagickRealType) p[i];
3981 else
3982 pixel=(MagickRealType) p[i]+amount*pixel;
cristy0beccfa2011-09-25 20:47:53 +00003983 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
cristy7f3a0d12011-09-05 23:27:59 +00003984 }
cristyed231572011-07-14 02:18:59 +00003985 p+=GetPixelChannels(image);
3986 q+=GetPixelChannels(unsharp_image);
cristy3ed852e2009-09-05 21:47:34 +00003987 }
3988 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3989 status=MagickFalse;
3990 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3991 {
3992 MagickBooleanType
3993 proceed;
3994
cristyb5d5f722009-11-04 03:03:49 +00003995#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyf4ad9df2011-07-08 16:49:03 +00003996 #pragma omp critical (MagickCore_UnsharpMaskImage)
cristy3ed852e2009-09-05 21:47:34 +00003997#endif
3998 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3999 if (proceed == MagickFalse)
4000 status=MagickFalse;
4001 }
4002 }
4003 unsharp_image->type=image->type;
4004 unsharp_view=DestroyCacheView(unsharp_view);
4005 image_view=DestroyCacheView(image_view);
4006 if (status == MagickFalse)
4007 unsharp_image=DestroyImage(unsharp_image);
4008 return(unsharp_image);
4009}