blob: 9bbaa920c0ba8055f976cba4c68e469f72bf034f [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% SSSSS TTTTT AAA TTTTT IIIII SSSSS TTTTT IIIII CCCC %
7% SS T A A T I SS T I C %
8% SSS T AAAAA T I SSS T I C %
9% SS T A A T I SS T I C %
10% SSSSS T A A T IIIII SSSSS T IIIII CCCC %
11% %
12% %
cristy3e2860c2010-01-24 01:36:30 +000013% MagickCore Image Statistical Methods %
cristy3ed852e2009-09-05 21:47:34 +000014% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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/property.h"
45#include "MagickCore/animate.h"
46#include "MagickCore/blob.h"
47#include "MagickCore/blob-private.h"
48#include "MagickCore/cache.h"
49#include "MagickCore/cache-private.h"
50#include "MagickCore/cache-view.h"
51#include "MagickCore/client.h"
52#include "MagickCore/color.h"
53#include "MagickCore/color-private.h"
54#include "MagickCore/colorspace.h"
55#include "MagickCore/colorspace-private.h"
56#include "MagickCore/composite.h"
57#include "MagickCore/composite-private.h"
58#include "MagickCore/compress.h"
59#include "MagickCore/constitute.h"
60#include "MagickCore/display.h"
61#include "MagickCore/draw.h"
62#include "MagickCore/enhance.h"
63#include "MagickCore/exception.h"
64#include "MagickCore/exception-private.h"
65#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000066#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000067#include "MagickCore/geometry.h"
68#include "MagickCore/list.h"
69#include "MagickCore/image-private.h"
70#include "MagickCore/magic.h"
71#include "MagickCore/magick.h"
72#include "MagickCore/memory_.h"
73#include "MagickCore/module.h"
74#include "MagickCore/monitor.h"
75#include "MagickCore/monitor-private.h"
76#include "MagickCore/option.h"
77#include "MagickCore/paint.h"
78#include "MagickCore/pixel-accessor.h"
79#include "MagickCore/profile.h"
80#include "MagickCore/quantize.h"
81#include "MagickCore/quantum-private.h"
82#include "MagickCore/random_.h"
83#include "MagickCore/random-private.h"
84#include "MagickCore/segment.h"
85#include "MagickCore/semaphore.h"
86#include "MagickCore/signature-private.h"
87#include "MagickCore/statistic.h"
88#include "MagickCore/string_.h"
89#include "MagickCore/thread-private.h"
90#include "MagickCore/timer.h"
91#include "MagickCore/utility.h"
92#include "MagickCore/version.h"
cristy3ed852e2009-09-05 21:47:34 +000093
94/*
95%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96% %
97% %
98% %
cristyd18ae7c2010-03-07 17:39:52 +000099% E v a l u a t e I m a g e %
cristy316d5172009-09-17 19:31:25 +0000100% %
101% %
102% %
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104%
cristyd18ae7c2010-03-07 17:39:52 +0000105% EvaluateImage() applies a value to the image with an arithmetic, relational,
106% or logical operator to an image. Use these operations to lighten or darken
107% an image, to increase or decrease contrast in an image, or to produce the
108% "negative" of an image.
cristy316d5172009-09-17 19:31:25 +0000109%
cristyd42d9952011-07-08 14:21:50 +0000110% The format of the EvaluateImage method is:
cristy316d5172009-09-17 19:31:25 +0000111%
cristyd18ae7c2010-03-07 17:39:52 +0000112% MagickBooleanType EvaluateImage(Image *image,
113% const MagickEvaluateOperator op,const double value,
114% ExceptionInfo *exception)
115% MagickBooleanType EvaluateImages(Image *images,
116% const MagickEvaluateOperator op,const double value,
117% ExceptionInfo *exception)
cristy316d5172009-09-17 19:31:25 +0000118%
119% A description of each parameter follows:
120%
cristyd18ae7c2010-03-07 17:39:52 +0000121% o image: the image.
122%
cristyd18ae7c2010-03-07 17:39:52 +0000123% o op: A channel op.
124%
125% o value: A value value.
cristy316d5172009-09-17 19:31:25 +0000126%
127% o exception: return any errors or warnings in this structure.
128%
129*/
130
cristyba18a7a2011-09-25 15:43:43 +0000131typedef struct _WenusInfo
132{
133 MagickRealType
134 channel[MaxPixelChannels];
135} WenusInfo;
136
137static WenusInfo **DestroyPixelThreadSet(WenusInfo **pixels)
cristy316d5172009-09-17 19:31:25 +0000138{
cristybb503372010-05-27 20:51:26 +0000139 register ssize_t
cristy316d5172009-09-17 19:31:25 +0000140 i;
141
cristyba18a7a2011-09-25 15:43:43 +0000142 assert(pixels != (WenusInfo **) NULL);
cristybb503372010-05-27 20:51:26 +0000143 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristyba18a7a2011-09-25 15:43:43 +0000144 if (pixels[i] != (WenusInfo *) NULL)
145 pixels[i]=(WenusInfo *) RelinquishMagickMemory(pixels[i]);
146 pixels=(WenusInfo **) RelinquishMagickMemory(pixels);
cristy316d5172009-09-17 19:31:25 +0000147 return(pixels);
148}
149
cristyba18a7a2011-09-25 15:43:43 +0000150static WenusInfo **AcquirePixelThreadSet(const Image *image,
cristy08a3d702010-11-28 01:57:36 +0000151 const size_t number_images)
cristy316d5172009-09-17 19:31:25 +0000152{
cristybb503372010-05-27 20:51:26 +0000153 register ssize_t
cristyba18a7a2011-09-25 15:43:43 +0000154 i;
cristy316d5172009-09-17 19:31:25 +0000155
cristyba18a7a2011-09-25 15:43:43 +0000156 WenusInfo
cristy316d5172009-09-17 19:31:25 +0000157 **pixels;
158
cristybb503372010-05-27 20:51:26 +0000159 size_t
cristy08a3d702010-11-28 01:57:36 +0000160 length,
cristy316d5172009-09-17 19:31:25 +0000161 number_threads;
162
163 number_threads=GetOpenMPMaximumThreads();
cristyba18a7a2011-09-25 15:43:43 +0000164 pixels=(WenusInfo **) AcquireQuantumMemory(number_threads,sizeof(*pixels));
165 if (pixels == (WenusInfo **) NULL)
166 return((WenusInfo **) NULL);
cristy316d5172009-09-17 19:31:25 +0000167 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
cristybb503372010-05-27 20:51:26 +0000168 for (i=0; i < (ssize_t) number_threads; i++)
cristy316d5172009-09-17 19:31:25 +0000169 {
cristyba18a7a2011-09-25 15:43:43 +0000170 register ssize_t
171 j;
172
cristy08a3d702010-11-28 01:57:36 +0000173 length=image->columns;
174 if (length < number_images)
175 length=number_images;
cristyba18a7a2011-09-25 15:43:43 +0000176 pixels[i]=(WenusInfo *) AcquireQuantumMemory(length,sizeof(**pixels));
177 if (pixels[i] == (WenusInfo *) NULL)
cristy316d5172009-09-17 19:31:25 +0000178 return(DestroyPixelThreadSet(pixels));
cristy08a3d702010-11-28 01:57:36 +0000179 for (j=0; j < (ssize_t) length; j++)
cristyba18a7a2011-09-25 15:43:43 +0000180 {
181 register ssize_t
182 k;
183
184 for (k=0; k < MaxPixelChannels; k++)
185 pixels[i][j].channel[k]=0.0;
186 }
cristy316d5172009-09-17 19:31:25 +0000187 }
188 return(pixels);
189}
190
cristy351842f2010-03-07 15:27:38 +0000191static inline double MagickMax(const double x,const double y)
192{
193 if (x > y)
194 return(x);
195 return(y);
196}
197
cristy08a3d702010-11-28 01:57:36 +0000198#if defined(__cplusplus) || defined(c_plusplus)
199extern "C" {
200#endif
201
202static int IntensityCompare(const void *x,const void *y)
203{
cristyba18a7a2011-09-25 15:43:43 +0000204 const WenusInfo
cristy08a3d702010-11-28 01:57:36 +0000205 *color_1,
206 *color_2;
207
cristyba18a7a2011-09-25 15:43:43 +0000208 MagickRealType
209 distance;
cristy08a3d702010-11-28 01:57:36 +0000210
cristyba18a7a2011-09-25 15:43:43 +0000211 register ssize_t
212 i;
213
214 color_1=(const WenusInfo *) x;
215 color_2=(const WenusInfo *) y;
216 distance=0.0;
217 for (i=0; i < MaxPixelChannels; i++)
218 distance+=color_1->channel[i]-(MagickRealType) color_2->channel[i];
219 return(distance < 0 ? -1 : distance > 0 ? 1 : 0);
cristy08a3d702010-11-28 01:57:36 +0000220}
221
222#if defined(__cplusplus) || defined(c_plusplus)
223}
224#endif
225
cristy351842f2010-03-07 15:27:38 +0000226static inline double MagickMin(const double x,const double y)
227{
228 if (x < y)
229 return(x);
230 return(y);
231}
232
cristyd18ae7c2010-03-07 17:39:52 +0000233static MagickRealType ApplyEvaluateOperator(RandomInfo *random_info,
234 Quantum pixel,const MagickEvaluateOperator op,const MagickRealType value)
cristy351842f2010-03-07 15:27:38 +0000235{
236 MagickRealType
237 result;
238
239 result=0.0;
240 switch (op)
241 {
242 case UndefinedEvaluateOperator:
243 break;
cristy33aaed22010-08-11 18:10:50 +0000244 case AbsEvaluateOperator:
245 {
246 result=(MagickRealType) fabs((double) (pixel+value));
247 break;
248 }
cristy351842f2010-03-07 15:27:38 +0000249 case AddEvaluateOperator:
250 {
251 result=(MagickRealType) (pixel+value);
252 break;
253 }
254 case AddModulusEvaluateOperator:
255 {
256 /*
257 This returns a 'floored modulus' of the addition which is a
cristy49dd6a02011-09-24 23:08:01 +0000258 positive result. It differs from % or fmod() that returns a
259 'truncated modulus' result, where floor() is replaced by trunc() and
260 could return a negative result (which is clipped).
cristy351842f2010-03-07 15:27:38 +0000261 */
262 result=pixel+value;
cristy364a8e22010-05-10 17:06:33 +0000263 result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
cristy351842f2010-03-07 15:27:38 +0000264 break;
265 }
266 case AndEvaluateOperator:
267 {
cristy9fe8cd72010-10-19 01:24:07 +0000268 result=(MagickRealType) ((size_t) pixel & (size_t) (value+0.5));
cristy351842f2010-03-07 15:27:38 +0000269 break;
270 }
271 case CosineEvaluateOperator:
272 {
273 result=(MagickRealType) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
274 QuantumScale*pixel*value))+0.5));
275 break;
276 }
277 case DivideEvaluateOperator:
278 {
279 result=pixel/(value == 0.0 ? 1.0 : value);
280 break;
281 }
cristy9fe8cd72010-10-19 01:24:07 +0000282 case ExponentialEvaluateOperator:
283 {
284 result=(MagickRealType) (QuantumRange*exp((double) (value*QuantumScale*
285 pixel)));
286 break;
287 }
cristy351842f2010-03-07 15:27:38 +0000288 case GaussianNoiseEvaluateOperator:
289 {
290 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
291 GaussianNoise,value);
292 break;
293 }
294 case ImpulseNoiseEvaluateOperator:
295 {
296 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
297 ImpulseNoise,value);
298 break;
299 }
300 case LaplacianNoiseEvaluateOperator:
301 {
302 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
303 LaplacianNoise,value);
304 break;
305 }
306 case LeftShiftEvaluateOperator:
307 {
cristy9fe8cd72010-10-19 01:24:07 +0000308 result=(MagickRealType) ((size_t) pixel << (size_t) (value+0.5));
cristy351842f2010-03-07 15:27:38 +0000309 break;
310 }
311 case LogEvaluateOperator:
312 {
313 result=(MagickRealType) (QuantumRange*log((double) (QuantumScale*value*
314 pixel+1.0))/log((double) (value+1.0)));
315 break;
316 }
317 case MaxEvaluateOperator:
318 {
319 result=(MagickRealType) MagickMax((double) pixel,value);
320 break;
321 }
322 case MeanEvaluateOperator:
323 {
cristy125a5a32010-05-07 13:30:52 +0000324 result=(MagickRealType) (pixel+value);
cristy351842f2010-03-07 15:27:38 +0000325 break;
326 }
cristy08a3d702010-11-28 01:57:36 +0000327 case MedianEvaluateOperator:
328 {
329 result=(MagickRealType) (pixel+value);
330 break;
331 }
cristy351842f2010-03-07 15:27:38 +0000332 case MinEvaluateOperator:
333 {
334 result=(MagickRealType) MagickMin((double) pixel,value);
335 break;
336 }
337 case MultiplicativeNoiseEvaluateOperator:
338 {
339 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
340 MultiplicativeGaussianNoise,value);
341 break;
342 }
343 case MultiplyEvaluateOperator:
344 {
345 result=(MagickRealType) (value*pixel);
346 break;
347 }
348 case OrEvaluateOperator:
349 {
cristy9fe8cd72010-10-19 01:24:07 +0000350 result=(MagickRealType) ((size_t) pixel | (size_t) (value+0.5));
cristy351842f2010-03-07 15:27:38 +0000351 break;
352 }
353 case PoissonNoiseEvaluateOperator:
354 {
355 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
356 PoissonNoise,value);
357 break;
358 }
359 case PowEvaluateOperator:
360 {
361 result=(MagickRealType) (QuantumRange*pow((double) (QuantumScale*pixel),
362 (double) value));
363 break;
364 }
365 case RightShiftEvaluateOperator:
366 {
cristy9fe8cd72010-10-19 01:24:07 +0000367 result=(MagickRealType) ((size_t) pixel >> (size_t) (value+0.5));
cristy351842f2010-03-07 15:27:38 +0000368 break;
369 }
370 case SetEvaluateOperator:
371 {
372 result=value;
373 break;
374 }
375 case SineEvaluateOperator:
376 {
377 result=(MagickRealType) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
378 QuantumScale*pixel*value))+0.5));
379 break;
380 }
381 case SubtractEvaluateOperator:
382 {
383 result=(MagickRealType) (pixel-value);
384 break;
385 }
386 case ThresholdEvaluateOperator:
387 {
388 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 :
389 QuantumRange);
390 break;
391 }
392 case ThresholdBlackEvaluateOperator:
393 {
394 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 : pixel);
395 break;
396 }
397 case ThresholdWhiteEvaluateOperator:
398 {
399 result=(MagickRealType) (((MagickRealType) pixel > value) ? QuantumRange :
400 pixel);
401 break;
402 }
403 case UniformNoiseEvaluateOperator:
404 {
405 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
406 UniformNoise,value);
407 break;
408 }
409 case XorEvaluateOperator:
410 {
cristy9fe8cd72010-10-19 01:24:07 +0000411 result=(MagickRealType) ((size_t) pixel ^ (size_t) (value+0.5));
cristy351842f2010-03-07 15:27:38 +0000412 break;
413 }
414 }
cristyd18ae7c2010-03-07 17:39:52 +0000415 return(result);
cristy351842f2010-03-07 15:27:38 +0000416}
417
cristyd18ae7c2010-03-07 17:39:52 +0000418MagickExport Image *EvaluateImages(const Image *images,
419 const MagickEvaluateOperator op,ExceptionInfo *exception)
420{
421#define EvaluateImageTag "Evaluate/Image"
422
423 CacheView
424 *evaluate_view;
425
426 const Image
427 *next;
428
429 Image
430 *evaluate_image;
431
cristyd18ae7c2010-03-07 17:39:52 +0000432 MagickBooleanType
433 status;
434
cristy5f959472010-05-27 22:19:46 +0000435 MagickOffsetType
436 progress;
437
cristyba18a7a2011-09-25 15:43:43 +0000438 WenusInfo
439 **restrict evaluate_pixels;
cristyd18ae7c2010-03-07 17:39:52 +0000440
441 RandomInfo
442 **restrict random_info;
443
cristybb503372010-05-27 20:51:26 +0000444 size_t
cristyd18ae7c2010-03-07 17:39:52 +0000445 number_images;
446
cristy5f959472010-05-27 22:19:46 +0000447 ssize_t
448 y;
449
cristyd18ae7c2010-03-07 17:39:52 +0000450 /*
451 Ensure the image are the same size.
452 */
453 assert(images != (Image *) NULL);
454 assert(images->signature == MagickSignature);
455 if (images->debug != MagickFalse)
456 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
457 assert(exception != (ExceptionInfo *) NULL);
458 assert(exception->signature == MagickSignature);
459 for (next=images; next != (Image *) NULL; next=GetNextImageInList(next))
460 if ((next->columns != images->columns) || (next->rows != images->rows))
461 {
462 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
463 "ImageWidthsOrHeightsDiffer","`%s'",images->filename);
464 return((Image *) NULL);
465 }
466 /*
467 Initialize evaluate next attributes.
468 */
469 evaluate_image=CloneImage(images,images->columns,images->rows,MagickTrue,
470 exception);
471 if (evaluate_image == (Image *) NULL)
472 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000473 if (SetImageStorageClass(evaluate_image,DirectClass,exception) == MagickFalse)
cristyd18ae7c2010-03-07 17:39:52 +0000474 {
cristyd18ae7c2010-03-07 17:39:52 +0000475 evaluate_image=DestroyImage(evaluate_image);
476 return((Image *) NULL);
477 }
cristy08a3d702010-11-28 01:57:36 +0000478 number_images=GetImageListLength(images);
479 evaluate_pixels=AcquirePixelThreadSet(images,number_images);
cristyba18a7a2011-09-25 15:43:43 +0000480 if (evaluate_pixels == (WenusInfo **) NULL)
cristyd18ae7c2010-03-07 17:39:52 +0000481 {
482 evaluate_image=DestroyImage(evaluate_image);
483 (void) ThrowMagickException(exception,GetMagickModule(),
484 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
485 return((Image *) NULL);
486 }
487 /*
488 Evaluate image pixels.
489 */
490 status=MagickTrue;
491 progress=0;
cristyd18ae7c2010-03-07 17:39:52 +0000492 random_info=AcquireRandomInfoThreadSet();
cristyd18ae7c2010-03-07 17:39:52 +0000493 evaluate_view=AcquireCacheView(evaluate_image);
cristy08a3d702010-11-28 01:57:36 +0000494 if (op == MedianEvaluateOperator)
cristyd18ae7c2010-03-07 17:39:52 +0000495#if defined(MAGICKCORE_OPENMP_SUPPORT)
496 #pragma omp parallel for schedule(dynamic) shared(progress,status)
497#endif
cristy08a3d702010-11-28 01:57:36 +0000498 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
cristyd18ae7c2010-03-07 17:39:52 +0000499 {
cristy08a3d702010-11-28 01:57:36 +0000500 CacheView
501 *image_view;
cristyd18ae7c2010-03-07 17:39:52 +0000502
cristy08a3d702010-11-28 01:57:36 +0000503 const Image
504 *next;
cristyd18ae7c2010-03-07 17:39:52 +0000505
cristy08a3d702010-11-28 01:57:36 +0000506 const int
507 id = GetOpenMPThreadId();
508
cristyba18a7a2011-09-25 15:43:43 +0000509 register WenusInfo
cristy08a3d702010-11-28 01:57:36 +0000510 *evaluate_pixel;
511
cristy4c08aed2011-07-01 19:47:50 +0000512 register Quantum
cristy08a3d702010-11-28 01:57:36 +0000513 *restrict q;
514
515 register ssize_t
516 x;
517
518 if (status == MagickFalse)
519 continue;
520 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,evaluate_image->columns,
521 1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000522 if (q == (Quantum *) NULL)
cristyd18ae7c2010-03-07 17:39:52 +0000523 {
cristy08a3d702010-11-28 01:57:36 +0000524 status=MagickFalse;
525 continue;
cristyd18ae7c2010-03-07 17:39:52 +0000526 }
cristy08a3d702010-11-28 01:57:36 +0000527 evaluate_pixel=evaluate_pixels[id];
cristybb503372010-05-27 20:51:26 +0000528 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
cristy125a5a32010-05-07 13:30:52 +0000529 {
cristy08a3d702010-11-28 01:57:36 +0000530 register ssize_t
cristyba18a7a2011-09-25 15:43:43 +0000531 j,
532 k;
cristy08a3d702010-11-28 01:57:36 +0000533
cristyba18a7a2011-09-25 15:43:43 +0000534 for (j=0; j < (ssize_t) number_images; j++)
535 for (k=0; k < MaxPixelChannels; k++)
536 evaluate_pixel[j].channel[k]=0.0;
cristy08a3d702010-11-28 01:57:36 +0000537 next=images;
cristyba18a7a2011-09-25 15:43:43 +0000538 for (j=0; j < (ssize_t) number_images; j++)
cristy08a3d702010-11-28 01:57:36 +0000539 {
cristy4c08aed2011-07-01 19:47:50 +0000540 register const Quantum
cristy08a3d702010-11-28 01:57:36 +0000541 *p;
542
cristyba18a7a2011-09-25 15:43:43 +0000543 register ssize_t
544 i;
545
cristy08a3d702010-11-28 01:57:36 +0000546 image_view=AcquireCacheView(next);
547 p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000548 if (p == (const Quantum *) NULL)
cristy08a3d702010-11-28 01:57:36 +0000549 {
550 image_view=DestroyCacheView(image_view);
551 break;
552 }
cristyba18a7a2011-09-25 15:43:43 +0000553 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
554 {
555 PixelChannel
556 channel;
557
558 PixelTrait
559 evaluate_traits,
560 traits;
561
562 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,
563 (PixelChannel) i);
564 channel=GetPixelChannelMapChannel(evaluate_image,(PixelChannel) i);
565 traits=GetPixelChannelMapTraits(next,channel);
566 if ((traits == UndefinedPixelTrait) ||
567 (evaluate_traits == UndefinedPixelTrait))
568 continue;
569 if ((evaluate_traits & UpdatePixelTrait) == 0)
570 continue;
571 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(random_info[id],
572 p[channel],op,evaluate_pixel[j].channel[i]);
573 }
cristy08a3d702010-11-28 01:57:36 +0000574 image_view=DestroyCacheView(image_view);
575 next=GetNextImageInList(next);
576 }
577 qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
578 IntensityCompare);
cristyba18a7a2011-09-25 15:43:43 +0000579 for (k=0; k < (ssize_t) GetPixelChannels(evaluate_image); k++)
580 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
cristyed231572011-07-14 02:18:59 +0000581 q+=GetPixelChannels(evaluate_image);
cristy125a5a32010-05-07 13:30:52 +0000582 }
cristy08a3d702010-11-28 01:57:36 +0000583 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
584 status=MagickFalse;
585 if (images->progress_monitor != (MagickProgressMonitor) NULL)
586 {
587 MagickBooleanType
588 proceed;
cristyd18ae7c2010-03-07 17:39:52 +0000589
590#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy08a3d702010-11-28 01:57:36 +0000591 #pragma omp critical (MagickCore_EvaluateImages)
cristyd18ae7c2010-03-07 17:39:52 +0000592#endif
cristy08a3d702010-11-28 01:57:36 +0000593 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
594 evaluate_image->rows);
595 if (proceed == MagickFalse)
596 status=MagickFalse;
597 }
598 }
599 else
600#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyba18a7a2011-09-25 15:43:43 +0000601 #pragma omp parallel for schedule(dynamic) shared(progress,status)
cristy08a3d702010-11-28 01:57:36 +0000602#endif
603 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
604 {
605 CacheView
606 *image_view;
607
608 const Image
609 *next;
610
611 const int
612 id = GetOpenMPThreadId();
613
cristy08a3d702010-11-28 01:57:36 +0000614 register ssize_t
615 i,
616 x;
617
cristyba18a7a2011-09-25 15:43:43 +0000618 register WenusInfo
cristy08a3d702010-11-28 01:57:36 +0000619 *evaluate_pixel;
620
cristy4c08aed2011-07-01 19:47:50 +0000621 register Quantum
cristy08a3d702010-11-28 01:57:36 +0000622 *restrict q;
623
cristyba18a7a2011-09-25 15:43:43 +0000624 ssize_t
625 j;
626
cristy08a3d702010-11-28 01:57:36 +0000627 if (status == MagickFalse)
628 continue;
629 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,evaluate_image->columns,
630 1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000631 if (q == (Quantum *) NULL)
cristy08a3d702010-11-28 01:57:36 +0000632 {
cristyd18ae7c2010-03-07 17:39:52 +0000633 status=MagickFalse;
cristy08a3d702010-11-28 01:57:36 +0000634 continue;
635 }
cristy08a3d702010-11-28 01:57:36 +0000636 evaluate_pixel=evaluate_pixels[id];
cristyba18a7a2011-09-25 15:43:43 +0000637 for (j=0; j < (ssize_t) evaluate_image->columns; j++)
638 for (i=0; i < MaxPixelChannels; i++)
639 evaluate_pixel[j].channel[i]=0.0;
cristy08a3d702010-11-28 01:57:36 +0000640 next=images;
cristyba18a7a2011-09-25 15:43:43 +0000641 for (j=0; j < (ssize_t) number_images; j++)
cristy08a3d702010-11-28 01:57:36 +0000642 {
cristy4c08aed2011-07-01 19:47:50 +0000643 register const Quantum
cristy08a3d702010-11-28 01:57:36 +0000644 *p;
645
646 image_view=AcquireCacheView(next);
647 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000648 if (p == (const Quantum *) NULL)
cristy08a3d702010-11-28 01:57:36 +0000649 {
650 image_view=DestroyCacheView(image_view);
651 break;
652 }
cristy08a3d702010-11-28 01:57:36 +0000653 for (x=0; x < (ssize_t) next->columns; x++)
654 {
cristyba18a7a2011-09-25 15:43:43 +0000655 register ssize_t
656 i;
657
658 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
659 {
660 PixelChannel
661 channel;
662
663 PixelTrait
664 evaluate_traits,
665 traits;
666
667 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,
668 (PixelChannel) i);
669 channel=GetPixelChannelMapChannel(evaluate_image,(PixelChannel) i);
670 traits=GetPixelChannelMapTraits(next,channel);
671 if ((traits == UndefinedPixelTrait) ||
672 (evaluate_traits == UndefinedPixelTrait))
673 continue;
674 if ((traits & UpdatePixelTrait) == 0)
675 continue;
676 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(random_info[id],
677 p[channel],j == 0 ? AddEvaluateOperator : op,
678 evaluate_pixel[x].channel[i]);
679 }
cristyed231572011-07-14 02:18:59 +0000680 p+=GetPixelChannels(next);
cristy08a3d702010-11-28 01:57:36 +0000681 }
682 image_view=DestroyCacheView(image_view);
683 next=GetNextImageInList(next);
cristyd18ae7c2010-03-07 17:39:52 +0000684 }
cristy08a3d702010-11-28 01:57:36 +0000685 if (op == MeanEvaluateOperator)
686 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
687 {
cristyba18a7a2011-09-25 15:43:43 +0000688 register ssize_t
689 i;
690
691 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
692 evaluate_pixel[x].channel[i]/=(MagickRealType) number_images;
cristy08a3d702010-11-28 01:57:36 +0000693 }
694 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
695 {
cristyba18a7a2011-09-25 15:43:43 +0000696 register ssize_t
697 i;
698
699 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
700 {
701 PixelTrait
702 traits;
703
704 traits=GetPixelChannelMapTraits(evaluate_image,(PixelChannel) i);
705 if (traits == UndefinedPixelTrait)
706 continue;
707 if ((traits & UpdatePixelTrait) == 0)
708 continue;
709 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
710 }
cristyed231572011-07-14 02:18:59 +0000711 q+=GetPixelChannels(evaluate_image);
cristy08a3d702010-11-28 01:57:36 +0000712 }
713 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
714 status=MagickFalse;
715 if (images->progress_monitor != (MagickProgressMonitor) NULL)
716 {
717 MagickBooleanType
718 proceed;
719
720#if defined(MAGICKCORE_OPENMP_SUPPORT)
721 #pragma omp critical (MagickCore_EvaluateImages)
722#endif
723 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
724 evaluate_image->rows);
725 if (proceed == MagickFalse)
726 status=MagickFalse;
727 }
728 }
cristyd18ae7c2010-03-07 17:39:52 +0000729 evaluate_view=DestroyCacheView(evaluate_view);
730 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
731 random_info=DestroyRandomInfoThreadSet(random_info);
732 if (status == MagickFalse)
733 evaluate_image=DestroyImage(evaluate_image);
734 return(evaluate_image);
735}
736
cristyd42d9952011-07-08 14:21:50 +0000737MagickExport MagickBooleanType EvaluateImage(Image *image,
738 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
cristy351842f2010-03-07 15:27:38 +0000739{
cristy351842f2010-03-07 15:27:38 +0000740 CacheView
741 *image_view;
742
cristy351842f2010-03-07 15:27:38 +0000743 MagickBooleanType
744 status;
745
cristy5f959472010-05-27 22:19:46 +0000746 MagickOffsetType
747 progress;
748
cristy351842f2010-03-07 15:27:38 +0000749 RandomInfo
750 **restrict random_info;
751
cristy5f959472010-05-27 22:19:46 +0000752 ssize_t
753 y;
754
cristy351842f2010-03-07 15:27:38 +0000755 assert(image != (Image *) NULL);
756 assert(image->signature == MagickSignature);
757 if (image->debug != MagickFalse)
758 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
759 assert(exception != (ExceptionInfo *) NULL);
760 assert(exception->signature == MagickSignature);
cristy574cc262011-08-05 01:23:58 +0000761 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
762 return(MagickFalse);
cristy351842f2010-03-07 15:27:38 +0000763 status=MagickTrue;
764 progress=0;
765 random_info=AcquireRandomInfoThreadSet();
766 image_view=AcquireCacheView(image);
767#if defined(MAGICKCORE_OPENMP_SUPPORT)
768 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
769#endif
cristybb503372010-05-27 20:51:26 +0000770 for (y=0; y < (ssize_t) image->rows; y++)
cristy351842f2010-03-07 15:27:38 +0000771 {
cristy5c9e6f22010-09-17 17:31:01 +0000772 const int
773 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000774
cristy4c08aed2011-07-01 19:47:50 +0000775 register Quantum
cristy351842f2010-03-07 15:27:38 +0000776 *restrict q;
777
cristy5c9e6f22010-09-17 17:31:01 +0000778 register ssize_t
779 x;
780
cristy351842f2010-03-07 15:27:38 +0000781 if (status == MagickFalse)
782 continue;
783 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000784 if (q == (Quantum *) NULL)
cristy351842f2010-03-07 15:27:38 +0000785 {
786 status=MagickFalse;
787 continue;
788 }
cristybb503372010-05-27 20:51:26 +0000789 for (x=0; x < (ssize_t) image->columns; x++)
cristy351842f2010-03-07 15:27:38 +0000790 {
cristy49dd6a02011-09-24 23:08:01 +0000791 register ssize_t
792 i;
793
794 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
795 {
796 PixelTrait
797 traits;
798
799 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
800 if (traits == UndefinedPixelTrait)
801 continue;
802 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
803 value));
804 }
cristyed231572011-07-14 02:18:59 +0000805 q+=GetPixelChannels(image);
cristy351842f2010-03-07 15:27:38 +0000806 }
807 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
808 status=MagickFalse;
809 if (image->progress_monitor != (MagickProgressMonitor) NULL)
810 {
811 MagickBooleanType
812 proceed;
813
814#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000815 #pragma omp critical (MagickCore_EvaluateImage)
cristy351842f2010-03-07 15:27:38 +0000816#endif
817 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
818 if (proceed == MagickFalse)
819 status=MagickFalse;
820 }
821 }
822 image_view=DestroyCacheView(image_view);
823 random_info=DestroyRandomInfoThreadSet(random_info);
824 return(status);
825}
826
827/*
828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829% %
830% %
831% %
832% F u n c t i o n I m a g e %
833% %
834% %
835% %
836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837%
838% FunctionImage() applies a value to the image with an arithmetic, relational,
839% or logical operator to an image. Use these operations to lighten or darken
840% an image, to increase or decrease contrast in an image, or to produce the
841% "negative" of an image.
842%
cristyd42d9952011-07-08 14:21:50 +0000843% The format of the FunctionImage method is:
cristy351842f2010-03-07 15:27:38 +0000844%
845% MagickBooleanType FunctionImage(Image *image,
cristybb503372010-05-27 20:51:26 +0000846% const MagickFunction function,const ssize_t number_parameters,
cristy351842f2010-03-07 15:27:38 +0000847% const double *parameters,ExceptionInfo *exception)
cristy351842f2010-03-07 15:27:38 +0000848%
849% A description of each parameter follows:
850%
851% o image: the image.
852%
cristy351842f2010-03-07 15:27:38 +0000853% o function: A channel function.
854%
855% o parameters: one or more parameters.
856%
857% o exception: return any errors or warnings in this structure.
858%
859*/
860
861static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
cristybb503372010-05-27 20:51:26 +0000862 const size_t number_parameters,const double *parameters,
cristy351842f2010-03-07 15:27:38 +0000863 ExceptionInfo *exception)
864{
865 MagickRealType
866 result;
867
cristybb503372010-05-27 20:51:26 +0000868 register ssize_t
cristy351842f2010-03-07 15:27:38 +0000869 i;
870
871 (void) exception;
872 result=0.0;
873 switch (function)
874 {
875 case PolynomialFunction:
876 {
877 /*
cristy94a75782011-09-25 02:33:56 +0000878 Polynomial: polynomial constants, highest to lowest order
879 (e.g. c0*x^3 + c1*x^2 + c2*x + c3).
880 */
cristy351842f2010-03-07 15:27:38 +0000881 result=0.0;
cristybb503372010-05-27 20:51:26 +0000882 for (i=0; i < (ssize_t) number_parameters; i++)
cristy94a75782011-09-25 02:33:56 +0000883 result=result*QuantumScale*pixel+parameters[i];
884 result*=QuantumRange;
cristy351842f2010-03-07 15:27:38 +0000885 break;
886 }
887 case SinusoidFunction:
888 {
cristy94a75782011-09-25 02:33:56 +0000889 MagickRealType
890 amplitude,
891 bias,
892 frequency,
893 phase;
894
895 /*
896 Sinusoid: frequency, phase, amplitude, bias.
897 */
898 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
899 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
900 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
901 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
902 result=(MagickRealType) (QuantumRange*(amplitude*sin((double) (2.0*
903 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
cristy351842f2010-03-07 15:27:38 +0000904 break;
905 }
906 case ArcsinFunction:
907 {
cristy94a75782011-09-25 02:33:56 +0000908 MagickRealType
909 bias,
910 center,
911 range,
912 width;
913
914 /*
915 Arcsin (peged at range limits for invalid results):
916 width, center, range, and bias.
917 */
918 width=(number_parameters >= 1) ? parameters[0] : 1.0;
919 center=(number_parameters >= 2) ? parameters[1] : 0.5;
920 range=(number_parameters >= 3) ? parameters[2] : 1.0;
921 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
922 result=2.0/width*(QuantumScale*pixel-center);
cristy351842f2010-03-07 15:27:38 +0000923 if ( result <= -1.0 )
cristy94a75782011-09-25 02:33:56 +0000924 result=bias-range/2.0;
cristy351842f2010-03-07 15:27:38 +0000925 else
cristy94a75782011-09-25 02:33:56 +0000926 if (result >= 1.0)
927 result=bias+range/2.0;
928 else
929 result=(MagickRealType) (range/MagickPI*asin((double) result)+bias);
930 result*=QuantumRange;
cristy351842f2010-03-07 15:27:38 +0000931 break;
932 }
933 case ArctanFunction:
934 {
cristy94a75782011-09-25 02:33:56 +0000935 MagickRealType
936 center,
937 bias,
938 range,
939 slope;
940
941 /*
942 Arctan: slope, center, range, and bias.
943 */
944 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
945 center=(number_parameters >= 2) ? parameters[1] : 0.5;
946 range=(number_parameters >= 3) ? parameters[2] : 1.0;
947 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
cristy7a07f9f2010-11-27 19:09:51 +0000948 result=(MagickRealType) (MagickPI*slope*(QuantumScale*pixel-center));
cristy351842f2010-03-07 15:27:38 +0000949 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
cristy94a75782011-09-25 02:33:56 +0000950 result)+bias));
cristy351842f2010-03-07 15:27:38 +0000951 break;
952 }
953 case UndefinedFunction:
954 break;
955 }
956 return(ClampToQuantum(result));
957}
958
959MagickExport MagickBooleanType FunctionImage(Image *image,
cristybb503372010-05-27 20:51:26 +0000960 const MagickFunction function,const size_t number_parameters,
cristy351842f2010-03-07 15:27:38 +0000961 const double *parameters,ExceptionInfo *exception)
962{
cristy351842f2010-03-07 15:27:38 +0000963#define FunctionImageTag "Function/Image "
964
965 CacheView
966 *image_view;
967
cristy351842f2010-03-07 15:27:38 +0000968 MagickBooleanType
969 status;
970
cristy5f959472010-05-27 22:19:46 +0000971 MagickOffsetType
972 progress;
973
974 ssize_t
975 y;
976
cristy351842f2010-03-07 15:27:38 +0000977 assert(image != (Image *) NULL);
978 assert(image->signature == MagickSignature);
979 if (image->debug != MagickFalse)
980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
981 assert(exception != (ExceptionInfo *) NULL);
982 assert(exception->signature == MagickSignature);
cristy574cc262011-08-05 01:23:58 +0000983 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
984 return(MagickFalse);
cristy351842f2010-03-07 15:27:38 +0000985 status=MagickTrue;
986 progress=0;
987 image_view=AcquireCacheView(image);
988#if defined(MAGICKCORE_OPENMP_SUPPORT)
989 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
990#endif
cristybb503372010-05-27 20:51:26 +0000991 for (y=0; y < (ssize_t) image->rows; y++)
cristy351842f2010-03-07 15:27:38 +0000992 {
cristy4c08aed2011-07-01 19:47:50 +0000993 register Quantum
cristy351842f2010-03-07 15:27:38 +0000994 *restrict q;
995
cristy94a75782011-09-25 02:33:56 +0000996 register ssize_t
997 x;
998
cristy351842f2010-03-07 15:27:38 +0000999 if (status == MagickFalse)
1000 continue;
1001 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001002 if (q == (Quantum *) NULL)
cristy351842f2010-03-07 15:27:38 +00001003 {
1004 status=MagickFalse;
1005 continue;
1006 }
cristybb503372010-05-27 20:51:26 +00001007 for (x=0; x < (ssize_t) image->columns; x++)
cristy351842f2010-03-07 15:27:38 +00001008 {
cristy94a75782011-09-25 02:33:56 +00001009 register ssize_t
1010 i;
1011
1012 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1013 {
1014 PixelTrait
1015 traits;
1016
1017 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1018 if (traits == UndefinedPixelTrait)
1019 continue;
1020 if ((traits & UpdatePixelTrait) == 0)
1021 continue;
1022 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1023 exception);
1024 }
cristyed231572011-07-14 02:18:59 +00001025 q+=GetPixelChannels(image);
cristy351842f2010-03-07 15:27:38 +00001026 }
1027 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1028 status=MagickFalse;
1029 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1030 {
1031 MagickBooleanType
1032 proceed;
1033
1034#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +00001035 #pragma omp critical (MagickCore_FunctionImage)
cristy351842f2010-03-07 15:27:38 +00001036#endif
1037 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1038 if (proceed == MagickFalse)
1039 status=MagickFalse;
1040 }
1041 }
1042 image_view=DestroyCacheView(image_view);
1043 return(status);
1044}
1045
1046/*
1047%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1048% %
1049% %
1050% %
cristyd42d9952011-07-08 14:21:50 +00001051% G e t I m a g e E x t r e m a %
cristy3ed852e2009-09-05 21:47:34 +00001052% %
1053% %
1054% %
1055%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1056%
cristyd42d9952011-07-08 14:21:50 +00001057% GetImageExtrema() returns the extrema of one or more image channels.
cristy3ed852e2009-09-05 21:47:34 +00001058%
cristyd42d9952011-07-08 14:21:50 +00001059% The format of the GetImageExtrema method is:
cristy3ed852e2009-09-05 21:47:34 +00001060%
cristyd42d9952011-07-08 14:21:50 +00001061% MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1062% size_t *maxima,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001063%
1064% A description of each parameter follows:
1065%
1066% o image: the image.
1067%
cristy3ed852e2009-09-05 21:47:34 +00001068% o minima: the minimum value in the channel.
1069%
1070% o maxima: the maximum value in the channel.
1071%
1072% o exception: return any errors or warnings in this structure.
1073%
1074*/
cristy3ed852e2009-09-05 21:47:34 +00001075MagickExport MagickBooleanType GetImageExtrema(const Image *image,
cristybb503372010-05-27 20:51:26 +00001076 size_t *minima,size_t *maxima,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001077{
cristy3ed852e2009-09-05 21:47:34 +00001078 double
1079 max,
1080 min;
1081
1082 MagickBooleanType
1083 status;
1084
1085 assert(image != (Image *) NULL);
1086 assert(image->signature == MagickSignature);
1087 if (image->debug != MagickFalse)
1088 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyd42d9952011-07-08 14:21:50 +00001089 status=GetImageRange(image,&min,&max,exception);
cristybb503372010-05-27 20:51:26 +00001090 *minima=(size_t) ceil(min-0.5);
1091 *maxima=(size_t) floor(max+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001092 return(status);
1093}
1094
1095/*
1096%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1097% %
1098% %
1099% %
cristyd42d9952011-07-08 14:21:50 +00001100% G e t I m a g e M e a n %
cristy3ed852e2009-09-05 21:47:34 +00001101% %
1102% %
1103% %
1104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1105%
cristyd42d9952011-07-08 14:21:50 +00001106% GetImageMean() returns the mean and standard deviation of one or more
cristy3ed852e2009-09-05 21:47:34 +00001107% image channels.
1108%
cristyd42d9952011-07-08 14:21:50 +00001109% The format of the GetImageMean method is:
cristy3ed852e2009-09-05 21:47:34 +00001110%
cristyd42d9952011-07-08 14:21:50 +00001111% MagickBooleanType GetImageMean(const Image *image,double *mean,
1112% double *standard_deviation,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001113%
1114% A description of each parameter follows:
1115%
1116% o image: the image.
1117%
cristy3ed852e2009-09-05 21:47:34 +00001118% o mean: the average value in the channel.
1119%
1120% o standard_deviation: the standard deviation of the channel.
1121%
1122% o exception: return any errors or warnings in this structure.
1123%
1124*/
cristy3ed852e2009-09-05 21:47:34 +00001125MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1126 double *standard_deviation,ExceptionInfo *exception)
1127{
cristyfd9dcd42010-08-08 18:07:02 +00001128 ChannelStatistics
1129 *channel_statistics;
cristy3ed852e2009-09-05 21:47:34 +00001130
cristy94a75782011-09-25 02:33:56 +00001131 register ssize_t
1132 i;
1133
cristyfd9dcd42010-08-08 18:07:02 +00001134 size_t
cristy94a75782011-09-25 02:33:56 +00001135 area;
cristy3ed852e2009-09-05 21:47:34 +00001136
1137 assert(image != (Image *) NULL);
1138 assert(image->signature == MagickSignature);
1139 if (image->debug != MagickFalse)
1140 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyd42d9952011-07-08 14:21:50 +00001141 channel_statistics=GetImageStatistics(image,exception);
cristyfd9dcd42010-08-08 18:07:02 +00001142 if (channel_statistics == (ChannelStatistics *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001143 return(MagickFalse);
cristy94a75782011-09-25 02:33:56 +00001144 area=0;
cristy25a5f2f2011-09-24 14:09:43 +00001145 channel_statistics[MaxPixelChannels].mean=0.0;
1146 channel_statistics[MaxPixelChannels].standard_deviation=0.0;
cristy94a75782011-09-25 02:33:56 +00001147 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1148 {
1149 PixelTrait
1150 traits;
1151
1152 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1153 if (traits == UndefinedPixelTrait)
1154 continue;
1155 if ((traits & UpdatePixelTrait) == 0)
1156 continue;
1157 channel_statistics[MaxPixelChannels].mean+=channel_statistics[i].mean;
1158 channel_statistics[MaxPixelChannels].standard_deviation+=
1159 channel_statistics[i].variance-channel_statistics[i].mean*
1160 channel_statistics[i].mean;
1161 area++;
1162 }
1163 channel_statistics[MaxPixelChannels].mean/=area;
cristy25a5f2f2011-09-24 14:09:43 +00001164 channel_statistics[MaxPixelChannels].standard_deviation=
cristy94a75782011-09-25 02:33:56 +00001165 sqrt(channel_statistics[MaxPixelChannels].standard_deviation/area);
cristy25a5f2f2011-09-24 14:09:43 +00001166 *mean=channel_statistics[MaxPixelChannels].mean;
1167 *standard_deviation=channel_statistics[MaxPixelChannels].standard_deviation;
cristyfd9dcd42010-08-08 18:07:02 +00001168 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1169 channel_statistics);
1170 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +00001171}
1172
1173/*
1174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1175% %
1176% %
1177% %
cristyd42d9952011-07-08 14:21:50 +00001178% G e t I m a g e K u r t o s i s %
cristy3ed852e2009-09-05 21:47:34 +00001179% %
1180% %
1181% %
1182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183%
cristyd42d9952011-07-08 14:21:50 +00001184% GetImageKurtosis() returns the kurtosis and skewness of one or more
cristy3ed852e2009-09-05 21:47:34 +00001185% image channels.
1186%
cristyd42d9952011-07-08 14:21:50 +00001187% The format of the GetImageKurtosis method is:
cristy3ed852e2009-09-05 21:47:34 +00001188%
cristyd42d9952011-07-08 14:21:50 +00001189% MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1190% double *skewness,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001191%
1192% A description of each parameter follows:
1193%
1194% o image: the image.
1195%
cristy3ed852e2009-09-05 21:47:34 +00001196% o kurtosis: the kurtosis of the channel.
1197%
1198% o skewness: the skewness of the channel.
1199%
1200% o exception: return any errors or warnings in this structure.
1201%
1202*/
cristy3ed852e2009-09-05 21:47:34 +00001203MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1204 double *kurtosis,double *skewness,ExceptionInfo *exception)
1205{
cristy94a75782011-09-25 02:33:56 +00001206 CacheView
1207 *image_view;
1208
cristy3ed852e2009-09-05 21:47:34 +00001209 double
1210 area,
1211 mean,
1212 standard_deviation,
1213 sum_squares,
1214 sum_cubes,
1215 sum_fourth_power;
1216
cristy94a75782011-09-25 02:33:56 +00001217 MagickBooleanType
1218 status;
1219
cristybb503372010-05-27 20:51:26 +00001220 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001221 y;
1222
1223 assert(image != (Image *) NULL);
1224 assert(image->signature == MagickSignature);
1225 if (image->debug != MagickFalse)
1226 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy94a75782011-09-25 02:33:56 +00001227 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001228 *kurtosis=0.0;
1229 *skewness=0.0;
1230 area=0.0;
1231 mean=0.0;
1232 standard_deviation=0.0;
1233 sum_squares=0.0;
1234 sum_cubes=0.0;
1235 sum_fourth_power=0.0;
cristy94a75782011-09-25 02:33:56 +00001236 image_view=AcquireCacheView(image);
1237#if defined(MAGICKCORE_OPENMP_SUPPORT)
1238 #pragma omp parallel for schedule(dynamic) shared(status) omp_throttle(1)
1239#endif
cristybb503372010-05-27 20:51:26 +00001240 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001241 {
cristy4c08aed2011-07-01 19:47:50 +00001242 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001243 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001244
cristybb503372010-05-27 20:51:26 +00001245 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001246 x;
1247
cristy94a75782011-09-25 02:33:56 +00001248 if (status == MagickFalse)
1249 continue;
1250 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001251 if (p == (const Quantum *) NULL)
cristy94a75782011-09-25 02:33:56 +00001252 {
1253 status=MagickFalse;
1254 continue;
1255 }
cristybb503372010-05-27 20:51:26 +00001256 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001257 {
cristy94a75782011-09-25 02:33:56 +00001258 register ssize_t
1259 i;
1260
1261 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1262 {
1263 PixelTrait
1264 traits;
1265
1266 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1267 if (traits == UndefinedPixelTrait)
1268 continue;
1269 if ((traits & UpdatePixelTrait) == 0)
1270 continue;
1271#if defined(MAGICKCORE_OPENMP_SUPPORT)
1272 #pragma omp critical (MagickCore_GetImageKurtosis)
1273#endif
cristy3ed852e2009-09-05 21:47:34 +00001274 {
cristy94a75782011-09-25 02:33:56 +00001275 mean+=p[i];
1276 sum_squares+=(double) p[i]*p[i];
1277 sum_cubes+=(double) p[i]*p[i]*p[i];
1278 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
cristy3ed852e2009-09-05 21:47:34 +00001279 area++;
1280 }
cristy94a75782011-09-25 02:33:56 +00001281 }
cristyed231572011-07-14 02:18:59 +00001282 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001283 }
1284 }
cristy94a75782011-09-25 02:33:56 +00001285 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001286 if (area != 0.0)
1287 {
1288 mean/=area;
1289 sum_squares/=area;
1290 sum_cubes/=area;
1291 sum_fourth_power/=area;
1292 }
1293 standard_deviation=sqrt(sum_squares-(mean*mean));
1294 if (standard_deviation != 0.0)
1295 {
1296 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1297 3.0*mean*mean*mean*mean;
1298 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1299 standard_deviation;
1300 *kurtosis-=3.0;
1301 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1302 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1303 }
cristy94a75782011-09-25 02:33:56 +00001304 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001305}
cristy46f08202010-01-10 04:04:21 +00001306
cristy3ed852e2009-09-05 21:47:34 +00001307/*
1308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1309% %
1310% %
1311% %
cristyd42d9952011-07-08 14:21:50 +00001312% G e t I m a g e R a n g e %
cristy3ed852e2009-09-05 21:47:34 +00001313% %
1314% %
1315% %
1316%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1317%
cristyd42d9952011-07-08 14:21:50 +00001318% GetImageRange() returns the range of one or more image channels.
cristy3ed852e2009-09-05 21:47:34 +00001319%
cristyd42d9952011-07-08 14:21:50 +00001320% The format of the GetImageRange method is:
cristy3ed852e2009-09-05 21:47:34 +00001321%
cristyd42d9952011-07-08 14:21:50 +00001322% MagickBooleanType GetImageRange(const Image *image,double *minima,
1323% double *maxima,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001324%
1325% A description of each parameter follows:
1326%
1327% o image: the image.
1328%
cristy3ed852e2009-09-05 21:47:34 +00001329% o minima: the minimum value in the channel.
1330%
1331% o maxima: the maximum value in the channel.
1332%
1333% o exception: return any errors or warnings in this structure.
1334%
1335*/
cristyd42d9952011-07-08 14:21:50 +00001336MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1337 double *maxima,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001338{
cristy4a9be682011-09-25 02:02:32 +00001339 CacheView
1340 *image_view;
1341
1342 MagickBooleanType
1343 status;
cristy3ed852e2009-09-05 21:47:34 +00001344
cristy9d314ff2011-03-09 01:30:28 +00001345 ssize_t
1346 y;
1347
cristy3ed852e2009-09-05 21:47:34 +00001348 assert(image != (Image *) NULL);
1349 assert(image->signature == MagickSignature);
1350 if (image->debug != MagickFalse)
1351 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy4a9be682011-09-25 02:02:32 +00001352 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001353 *maxima=(-1.0E-37);
1354 *minima=1.0E+37;
cristy4a9be682011-09-25 02:02:32 +00001355 image_view=AcquireCacheView(image);
1356#if defined(MAGICKCORE_OPENMP_SUPPORT)
1357 #pragma omp parallel for schedule(dynamic) shared(status) omp_throttle(1)
1358#endif
cristybb503372010-05-27 20:51:26 +00001359 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001360 {
cristy4c08aed2011-07-01 19:47:50 +00001361 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001362 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001363
cristybb503372010-05-27 20:51:26 +00001364 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001365 x;
1366
cristy4a9be682011-09-25 02:02:32 +00001367 if (status == MagickFalse)
1368 continue;
1369 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001370 if (p == (const Quantum *) NULL)
cristy4a9be682011-09-25 02:02:32 +00001371 {
1372 status=MagickFalse;
1373 continue;
1374 }
cristybb503372010-05-27 20:51:26 +00001375 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001376 {
cristy4a9be682011-09-25 02:02:32 +00001377 register ssize_t
1378 i;
1379
1380 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1381 {
1382 PixelTrait
1383 traits;
1384
1385 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1386 if (traits == UndefinedPixelTrait)
1387 continue;
1388 if ((traits & UpdatePixelTrait) == 0)
1389 continue;
1390#if defined(MAGICKCORE_OPENMP_SUPPORT)
1391 #pragma omp critical (MagickCore_GetImageRange)
1392#endif
cristy3ed852e2009-09-05 21:47:34 +00001393 {
cristyba18a7a2011-09-25 15:43:43 +00001394 if (p[i] < *minima)
cristy4a9be682011-09-25 02:02:32 +00001395 *minima=(double) p[i];
cristyba18a7a2011-09-25 15:43:43 +00001396 if (p[i] > *maxima)
cristy4a9be682011-09-25 02:02:32 +00001397 *maxima=(double) p[i];
cristy3ed852e2009-09-05 21:47:34 +00001398 }
cristy4a9be682011-09-25 02:02:32 +00001399 }
cristyed231572011-07-14 02:18:59 +00001400 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001401 }
1402 }
cristy4a9be682011-09-25 02:02:32 +00001403 image_view=DestroyCacheView(image_view);
1404 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001405}
1406
1407/*
1408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409% %
1410% %
1411% %
cristyd42d9952011-07-08 14:21:50 +00001412% G e t I m a g e S t a t i s t i c s %
cristy3ed852e2009-09-05 21:47:34 +00001413% %
1414% %
1415% %
1416%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417%
cristyd42d9952011-07-08 14:21:50 +00001418% GetImageStatistics() returns statistics for each channel in the
cristy3ed852e2009-09-05 21:47:34 +00001419% image. The statistics include the channel depth, its minima, maxima, mean,
1420% standard deviation, kurtosis and skewness. You can access the red channel
1421% mean, for example, like this:
1422%
cristyd42d9952011-07-08 14:21:50 +00001423% channel_statistics=GetImageStatistics(image,exception);
cristy49dd6a02011-09-24 23:08:01 +00001424% red_mean=channel_statistics[RedPixelChannel].mean;
cristy3ed852e2009-09-05 21:47:34 +00001425%
1426% Use MagickRelinquishMemory() to free the statistics buffer.
1427%
cristyd42d9952011-07-08 14:21:50 +00001428% The format of the GetImageStatistics method is:
cristy3ed852e2009-09-05 21:47:34 +00001429%
cristyd42d9952011-07-08 14:21:50 +00001430% ChannelStatistics *GetImageStatistics(const Image *image,
cristy3ed852e2009-09-05 21:47:34 +00001431% ExceptionInfo *exception)
1432%
1433% A description of each parameter follows:
1434%
1435% o image: the image.
1436%
1437% o exception: return any errors or warnings in this structure.
1438%
1439*/
cristy49dd6a02011-09-24 23:08:01 +00001440
1441static size_t GetImageChannels(const Image *image)
1442{
1443 register ssize_t
1444 i;
1445
1446 size_t
1447 channels;
1448
1449 channels=0;
1450 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1451 {
1452 PixelTrait
1453 traits;
1454
1455 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1456 if ((traits & UpdatePixelTrait) != 0)
1457 channels++;
1458 }
1459 return(channels);
1460}
1461
cristyd42d9952011-07-08 14:21:50 +00001462MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
cristy3ed852e2009-09-05 21:47:34 +00001463 ExceptionInfo *exception)
1464{
1465 ChannelStatistics
1466 *channel_statistics;
1467
1468 double
cristyfd9dcd42010-08-08 18:07:02 +00001469 area;
cristy3ed852e2009-09-05 21:47:34 +00001470
cristy3ed852e2009-09-05 21:47:34 +00001471 MagickStatusType
1472 status;
1473
1474 QuantumAny
1475 range;
1476
cristybb503372010-05-27 20:51:26 +00001477 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001478 i;
1479
1480 size_t
cristy9d314ff2011-03-09 01:30:28 +00001481 channels,
cristy49dd6a02011-09-24 23:08:01 +00001482 depth;
cristy3ed852e2009-09-05 21:47:34 +00001483
cristy9d314ff2011-03-09 01:30:28 +00001484 ssize_t
1485 y;
cristy3ed852e2009-09-05 21:47:34 +00001486
1487 assert(image != (Image *) NULL);
1488 assert(image->signature == MagickSignature);
1489 if (image->debug != MagickFalse)
1490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy49dd6a02011-09-24 23:08:01 +00001491 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1492 MaxPixelChannels+1,sizeof(*channel_statistics));
cristy3ed852e2009-09-05 21:47:34 +00001493 if (channel_statistics == (ChannelStatistics *) NULL)
1494 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy49dd6a02011-09-24 23:08:01 +00001495 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
cristy3ed852e2009-09-05 21:47:34 +00001496 sizeof(*channel_statistics));
cristy25a5f2f2011-09-24 14:09:43 +00001497 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001498 {
1499 channel_statistics[i].depth=1;
1500 channel_statistics[i].maxima=(-1.0E-37);
1501 channel_statistics[i].minima=1.0E+37;
cristy3ed852e2009-09-05 21:47:34 +00001502 }
cristybb503372010-05-27 20:51:26 +00001503 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001504 {
cristy4c08aed2011-07-01 19:47:50 +00001505 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001506 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001507
cristybb503372010-05-27 20:51:26 +00001508 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001509 x;
1510
1511 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001512 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001513 break;
cristy49dd6a02011-09-24 23:08:01 +00001514 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001515 {
cristy49dd6a02011-09-24 23:08:01 +00001516 register ssize_t
1517 i;
1518
1519 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1520 {
1521 PixelTrait
1522 traits;
1523
1524 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1525 if (traits == UndefinedPixelTrait)
1526 continue;
1527 if (channel_statistics[i].depth != MAGICKCORE_QUANTUM_DEPTH)
1528 {
1529 depth=channel_statistics[i].depth;
1530 range=GetQuantumRange(depth);
1531 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1532 range) ? MagickTrue : MagickFalse;
1533 if (status != MagickFalse)
1534 {
1535 channel_statistics[i].depth++;
1536 continue;
1537 }
cristy3ed852e2009-09-05 21:47:34 +00001538 }
cristy49dd6a02011-09-24 23:08:01 +00001539 if ((double) p[i] < channel_statistics[i].minima)
1540 channel_statistics[i].minima=(double) p[i];
1541 if ((double) p[i] > channel_statistics[i].maxima)
1542 channel_statistics[i].maxima=(double) p[i];
1543 channel_statistics[i].sum+=p[i];
1544 channel_statistics[i].sum_squared+=(double) p[i]*p[i];
1545 channel_statistics[i].sum_cubed+=(double) p[i]*p[i]*p[i];
1546 channel_statistics[i].sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1547 }
cristyed231572011-07-14 02:18:59 +00001548 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001549 }
1550 }
1551 area=(double) image->columns*image->rows;
cristy25a5f2f2011-09-24 14:09:43 +00001552 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001553 {
cristyfd9dcd42010-08-08 18:07:02 +00001554 channel_statistics[i].sum/=area;
1555 channel_statistics[i].sum_squared/=area;
cristya8178ed2010-08-10 17:31:59 +00001556 channel_statistics[i].sum_cubed/=area;
1557 channel_statistics[i].sum_fourth_power/=area;
cristyfd9dcd42010-08-08 18:07:02 +00001558 channel_statistics[i].mean=channel_statistics[i].sum;
1559 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1560 channel_statistics[i].standard_deviation=sqrt(
1561 channel_statistics[i].variance-(channel_statistics[i].mean*
1562 channel_statistics[i].mean));
cristy3ed852e2009-09-05 21:47:34 +00001563 }
cristy25a5f2f2011-09-24 14:09:43 +00001564 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001565 {
cristy25a5f2f2011-09-24 14:09:43 +00001566 channel_statistics[MaxPixelChannels].depth=(size_t) MagickMax((double)
1567 channel_statistics[MaxPixelChannels].depth,(double)
cristy3ed852e2009-09-05 21:47:34 +00001568 channel_statistics[i].depth);
cristy25a5f2f2011-09-24 14:09:43 +00001569 channel_statistics[MaxPixelChannels].minima=MagickMin(
1570 channel_statistics[MaxPixelChannels].minima,
cristy32daba42011-05-01 02:34:39 +00001571 channel_statistics[i].minima);
cristy25a5f2f2011-09-24 14:09:43 +00001572 channel_statistics[MaxPixelChannels].maxima=MagickMax(
1573 channel_statistics[MaxPixelChannels].maxima,
cristy32daba42011-05-01 02:34:39 +00001574 channel_statistics[i].maxima);
cristy25a5f2f2011-09-24 14:09:43 +00001575 channel_statistics[MaxPixelChannels].sum+=channel_statistics[i].sum;
1576 channel_statistics[MaxPixelChannels].sum_squared+=
cristyfd9dcd42010-08-08 18:07:02 +00001577 channel_statistics[i].sum_squared;
cristy25a5f2f2011-09-24 14:09:43 +00001578 channel_statistics[MaxPixelChannels].sum_cubed+=
cristy32daba42011-05-01 02:34:39 +00001579 channel_statistics[i].sum_cubed;
cristy25a5f2f2011-09-24 14:09:43 +00001580 channel_statistics[MaxPixelChannels].sum_fourth_power+=
cristya8178ed2010-08-10 17:31:59 +00001581 channel_statistics[i].sum_fourth_power;
cristy25a5f2f2011-09-24 14:09:43 +00001582 channel_statistics[MaxPixelChannels].mean+=channel_statistics[i].mean;
1583 channel_statistics[MaxPixelChannels].variance+=
cristy32daba42011-05-01 02:34:39 +00001584 channel_statistics[i].variance-channel_statistics[i].mean*
1585 channel_statistics[i].mean;
cristy25a5f2f2011-09-24 14:09:43 +00001586 channel_statistics[MaxPixelChannels].standard_deviation+=
cristyfd9dcd42010-08-08 18:07:02 +00001587 channel_statistics[i].variance-channel_statistics[i].mean*
1588 channel_statistics[i].mean;
cristy3ed852e2009-09-05 21:47:34 +00001589 }
cristy49dd6a02011-09-24 23:08:01 +00001590 channels=GetImageChannels(image);
cristy25a5f2f2011-09-24 14:09:43 +00001591 channel_statistics[MaxPixelChannels].sum/=channels;
1592 channel_statistics[MaxPixelChannels].sum_squared/=channels;
1593 channel_statistics[MaxPixelChannels].sum_cubed/=channels;
1594 channel_statistics[MaxPixelChannels].sum_fourth_power/=channels;
1595 channel_statistics[MaxPixelChannels].mean/=channels;
1596 channel_statistics[MaxPixelChannels].variance/=channels;
1597 channel_statistics[MaxPixelChannels].standard_deviation=
1598 sqrt(channel_statistics[MaxPixelChannels].standard_deviation/channels);
1599 channel_statistics[MaxPixelChannels].kurtosis/=channels;
1600 channel_statistics[MaxPixelChannels].skewness/=channels;
1601 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001602 {
cristy3ed852e2009-09-05 21:47:34 +00001603 if (channel_statistics[i].standard_deviation == 0.0)
cristya8178ed2010-08-10 17:31:59 +00001604 continue;
1605 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-
1606 3.0*channel_statistics[i].mean*channel_statistics[i].sum_squared+
1607 2.0*channel_statistics[i].mean*channel_statistics[i].mean*
1608 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1609 channel_statistics[i].standard_deviation*
1610 channel_statistics[i].standard_deviation);
1611 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-
1612 4.0*channel_statistics[i].mean*channel_statistics[i].sum_cubed+
1613 6.0*channel_statistics[i].mean*channel_statistics[i].mean*
1614 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1615 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1616 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1617 channel_statistics[i].standard_deviation*
1618 channel_statistics[i].standard_deviation*
1619 channel_statistics[i].standard_deviation)-3.0;
cristy3ed852e2009-09-05 21:47:34 +00001620 }
1621 return(channel_statistics);
1622}