blob: 5459f73345f47284414415491550cc9badf86738 [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],
cristy0beccfa2011-09-25 20:47:53 +0000572 GetPixelChannel(evaluate_image,channel,p),op,
573 evaluate_pixel[j].channel[i]);
cristyba18a7a2011-09-25 15:43:43 +0000574 }
cristy08a3d702010-11-28 01:57:36 +0000575 image_view=DestroyCacheView(image_view);
576 next=GetNextImageInList(next);
577 }
578 qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
579 IntensityCompare);
cristyba18a7a2011-09-25 15:43:43 +0000580 for (k=0; k < (ssize_t) GetPixelChannels(evaluate_image); k++)
581 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
cristyed231572011-07-14 02:18:59 +0000582 q+=GetPixelChannels(evaluate_image);
cristy125a5a32010-05-07 13:30:52 +0000583 }
cristy08a3d702010-11-28 01:57:36 +0000584 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
585 status=MagickFalse;
586 if (images->progress_monitor != (MagickProgressMonitor) NULL)
587 {
588 MagickBooleanType
589 proceed;
cristyd18ae7c2010-03-07 17:39:52 +0000590
591#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy08a3d702010-11-28 01:57:36 +0000592 #pragma omp critical (MagickCore_EvaluateImages)
cristyd18ae7c2010-03-07 17:39:52 +0000593#endif
cristy08a3d702010-11-28 01:57:36 +0000594 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
595 evaluate_image->rows);
596 if (proceed == MagickFalse)
597 status=MagickFalse;
598 }
599 }
600 else
601#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyba18a7a2011-09-25 15:43:43 +0000602 #pragma omp parallel for schedule(dynamic) shared(progress,status)
cristy08a3d702010-11-28 01:57:36 +0000603#endif
604 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
605 {
606 CacheView
607 *image_view;
608
609 const Image
610 *next;
611
612 const int
613 id = GetOpenMPThreadId();
614
cristy08a3d702010-11-28 01:57:36 +0000615 register ssize_t
616 i,
617 x;
618
cristyba18a7a2011-09-25 15:43:43 +0000619 register WenusInfo
cristy08a3d702010-11-28 01:57:36 +0000620 *evaluate_pixel;
621
cristy4c08aed2011-07-01 19:47:50 +0000622 register Quantum
cristy08a3d702010-11-28 01:57:36 +0000623 *restrict q;
624
cristyba18a7a2011-09-25 15:43:43 +0000625 ssize_t
626 j;
627
cristy08a3d702010-11-28 01:57:36 +0000628 if (status == MagickFalse)
629 continue;
630 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,evaluate_image->columns,
631 1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000632 if (q == (Quantum *) NULL)
cristy08a3d702010-11-28 01:57:36 +0000633 {
cristyd18ae7c2010-03-07 17:39:52 +0000634 status=MagickFalse;
cristy08a3d702010-11-28 01:57:36 +0000635 continue;
636 }
cristy08a3d702010-11-28 01:57:36 +0000637 evaluate_pixel=evaluate_pixels[id];
cristyba18a7a2011-09-25 15:43:43 +0000638 for (j=0; j < (ssize_t) evaluate_image->columns; j++)
639 for (i=0; i < MaxPixelChannels; i++)
640 evaluate_pixel[j].channel[i]=0.0;
cristy08a3d702010-11-28 01:57:36 +0000641 next=images;
cristyba18a7a2011-09-25 15:43:43 +0000642 for (j=0; j < (ssize_t) number_images; j++)
cristy08a3d702010-11-28 01:57:36 +0000643 {
cristy4c08aed2011-07-01 19:47:50 +0000644 register const Quantum
cristy08a3d702010-11-28 01:57:36 +0000645 *p;
646
647 image_view=AcquireCacheView(next);
648 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000649 if (p == (const Quantum *) NULL)
cristy08a3d702010-11-28 01:57:36 +0000650 {
651 image_view=DestroyCacheView(image_view);
652 break;
653 }
cristy08a3d702010-11-28 01:57:36 +0000654 for (x=0; x < (ssize_t) next->columns; x++)
655 {
cristyba18a7a2011-09-25 15:43:43 +0000656 register ssize_t
657 i;
658
659 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
660 {
661 PixelChannel
662 channel;
663
664 PixelTrait
665 evaluate_traits,
666 traits;
667
668 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,
669 (PixelChannel) i);
670 channel=GetPixelChannelMapChannel(evaluate_image,(PixelChannel) i);
671 traits=GetPixelChannelMapTraits(next,channel);
672 if ((traits == UndefinedPixelTrait) ||
673 (evaluate_traits == UndefinedPixelTrait))
674 continue;
675 if ((traits & UpdatePixelTrait) == 0)
676 continue;
677 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(random_info[id],
cristy0beccfa2011-09-25 20:47:53 +0000678 GetPixelChannel(evaluate_image,channel,p),j == 0 ?
679 AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
cristyba18a7a2011-09-25 15:43:43 +0000680 }
cristyed231572011-07-14 02:18:59 +0000681 p+=GetPixelChannels(next);
cristy08a3d702010-11-28 01:57:36 +0000682 }
683 image_view=DestroyCacheView(image_view);
684 next=GetNextImageInList(next);
cristyd18ae7c2010-03-07 17:39:52 +0000685 }
cristy08a3d702010-11-28 01:57:36 +0000686 if (op == MeanEvaluateOperator)
687 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
688 {
cristyba18a7a2011-09-25 15:43:43 +0000689 register ssize_t
690 i;
691
692 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
693 evaluate_pixel[x].channel[i]/=(MagickRealType) number_images;
cristy08a3d702010-11-28 01:57:36 +0000694 }
695 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
696 {
cristyba18a7a2011-09-25 15:43:43 +0000697 register ssize_t
698 i;
699
700 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
701 {
702 PixelTrait
703 traits;
704
705 traits=GetPixelChannelMapTraits(evaluate_image,(PixelChannel) i);
706 if (traits == UndefinedPixelTrait)
707 continue;
708 if ((traits & UpdatePixelTrait) == 0)
709 continue;
710 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
711 }
cristyed231572011-07-14 02:18:59 +0000712 q+=GetPixelChannels(evaluate_image);
cristy08a3d702010-11-28 01:57:36 +0000713 }
714 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
715 status=MagickFalse;
716 if (images->progress_monitor != (MagickProgressMonitor) NULL)
717 {
718 MagickBooleanType
719 proceed;
720
721#if defined(MAGICKCORE_OPENMP_SUPPORT)
722 #pragma omp critical (MagickCore_EvaluateImages)
723#endif
724 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
725 evaluate_image->rows);
726 if (proceed == MagickFalse)
727 status=MagickFalse;
728 }
729 }
cristyd18ae7c2010-03-07 17:39:52 +0000730 evaluate_view=DestroyCacheView(evaluate_view);
731 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
732 random_info=DestroyRandomInfoThreadSet(random_info);
733 if (status == MagickFalse)
734 evaluate_image=DestroyImage(evaluate_image);
735 return(evaluate_image);
736}
737
cristyd42d9952011-07-08 14:21:50 +0000738MagickExport MagickBooleanType EvaluateImage(Image *image,
739 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
cristy351842f2010-03-07 15:27:38 +0000740{
cristy351842f2010-03-07 15:27:38 +0000741 CacheView
742 *image_view;
743
cristy351842f2010-03-07 15:27:38 +0000744 MagickBooleanType
745 status;
746
cristy5f959472010-05-27 22:19:46 +0000747 MagickOffsetType
748 progress;
749
cristy351842f2010-03-07 15:27:38 +0000750 RandomInfo
751 **restrict random_info;
752
cristy5f959472010-05-27 22:19:46 +0000753 ssize_t
754 y;
755
cristy351842f2010-03-07 15:27:38 +0000756 assert(image != (Image *) NULL);
757 assert(image->signature == MagickSignature);
758 if (image->debug != MagickFalse)
759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
760 assert(exception != (ExceptionInfo *) NULL);
761 assert(exception->signature == MagickSignature);
cristy574cc262011-08-05 01:23:58 +0000762 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
763 return(MagickFalse);
cristy351842f2010-03-07 15:27:38 +0000764 status=MagickTrue;
765 progress=0;
766 random_info=AcquireRandomInfoThreadSet();
767 image_view=AcquireCacheView(image);
768#if defined(MAGICKCORE_OPENMP_SUPPORT)
769 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
770#endif
cristybb503372010-05-27 20:51:26 +0000771 for (y=0; y < (ssize_t) image->rows; y++)
cristy351842f2010-03-07 15:27:38 +0000772 {
cristy5c9e6f22010-09-17 17:31:01 +0000773 const int
774 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000775
cristy4c08aed2011-07-01 19:47:50 +0000776 register Quantum
cristy351842f2010-03-07 15:27:38 +0000777 *restrict q;
778
cristy5c9e6f22010-09-17 17:31:01 +0000779 register ssize_t
780 x;
781
cristy351842f2010-03-07 15:27:38 +0000782 if (status == MagickFalse)
783 continue;
784 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000785 if (q == (Quantum *) NULL)
cristy351842f2010-03-07 15:27:38 +0000786 {
787 status=MagickFalse;
788 continue;
789 }
cristybb503372010-05-27 20:51:26 +0000790 for (x=0; x < (ssize_t) image->columns; x++)
cristy351842f2010-03-07 15:27:38 +0000791 {
cristy49dd6a02011-09-24 23:08:01 +0000792 register ssize_t
793 i;
794
795 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
796 {
797 PixelTrait
798 traits;
799
800 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
801 if (traits == UndefinedPixelTrait)
802 continue;
803 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
804 value));
805 }
cristyed231572011-07-14 02:18:59 +0000806 q+=GetPixelChannels(image);
cristy351842f2010-03-07 15:27:38 +0000807 }
808 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
809 status=MagickFalse;
810 if (image->progress_monitor != (MagickProgressMonitor) NULL)
811 {
812 MagickBooleanType
813 proceed;
814
815#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000816 #pragma omp critical (MagickCore_EvaluateImage)
cristy351842f2010-03-07 15:27:38 +0000817#endif
818 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
819 if (proceed == MagickFalse)
820 status=MagickFalse;
821 }
822 }
823 image_view=DestroyCacheView(image_view);
824 random_info=DestroyRandomInfoThreadSet(random_info);
825 return(status);
826}
827
828/*
829%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830% %
831% %
832% %
833% F u n c t i o n I m a g e %
834% %
835% %
836% %
837%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838%
839% FunctionImage() applies a value to the image with an arithmetic, relational,
840% or logical operator to an image. Use these operations to lighten or darken
841% an image, to increase or decrease contrast in an image, or to produce the
842% "negative" of an image.
843%
cristyd42d9952011-07-08 14:21:50 +0000844% The format of the FunctionImage method is:
cristy351842f2010-03-07 15:27:38 +0000845%
846% MagickBooleanType FunctionImage(Image *image,
cristybb503372010-05-27 20:51:26 +0000847% const MagickFunction function,const ssize_t number_parameters,
cristy351842f2010-03-07 15:27:38 +0000848% const double *parameters,ExceptionInfo *exception)
cristy351842f2010-03-07 15:27:38 +0000849%
850% A description of each parameter follows:
851%
852% o image: the image.
853%
cristy351842f2010-03-07 15:27:38 +0000854% o function: A channel function.
855%
856% o parameters: one or more parameters.
857%
858% o exception: return any errors or warnings in this structure.
859%
860*/
861
862static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
cristybb503372010-05-27 20:51:26 +0000863 const size_t number_parameters,const double *parameters,
cristy351842f2010-03-07 15:27:38 +0000864 ExceptionInfo *exception)
865{
866 MagickRealType
867 result;
868
cristybb503372010-05-27 20:51:26 +0000869 register ssize_t
cristy351842f2010-03-07 15:27:38 +0000870 i;
871
872 (void) exception;
873 result=0.0;
874 switch (function)
875 {
876 case PolynomialFunction:
877 {
878 /*
cristy94a75782011-09-25 02:33:56 +0000879 Polynomial: polynomial constants, highest to lowest order
880 (e.g. c0*x^3 + c1*x^2 + c2*x + c3).
881 */
cristy351842f2010-03-07 15:27:38 +0000882 result=0.0;
cristybb503372010-05-27 20:51:26 +0000883 for (i=0; i < (ssize_t) number_parameters; i++)
cristy94a75782011-09-25 02:33:56 +0000884 result=result*QuantumScale*pixel+parameters[i];
885 result*=QuantumRange;
cristy351842f2010-03-07 15:27:38 +0000886 break;
887 }
888 case SinusoidFunction:
889 {
cristy94a75782011-09-25 02:33:56 +0000890 MagickRealType
891 amplitude,
892 bias,
893 frequency,
894 phase;
895
896 /*
897 Sinusoid: frequency, phase, amplitude, bias.
898 */
899 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
900 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
901 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
902 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
903 result=(MagickRealType) (QuantumRange*(amplitude*sin((double) (2.0*
904 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
cristy351842f2010-03-07 15:27:38 +0000905 break;
906 }
907 case ArcsinFunction:
908 {
cristy94a75782011-09-25 02:33:56 +0000909 MagickRealType
910 bias,
911 center,
912 range,
913 width;
914
915 /*
916 Arcsin (peged at range limits for invalid results):
917 width, center, range, and bias.
918 */
919 width=(number_parameters >= 1) ? parameters[0] : 1.0;
920 center=(number_parameters >= 2) ? parameters[1] : 0.5;
921 range=(number_parameters >= 3) ? parameters[2] : 1.0;
922 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
923 result=2.0/width*(QuantumScale*pixel-center);
cristy351842f2010-03-07 15:27:38 +0000924 if ( result <= -1.0 )
cristy94a75782011-09-25 02:33:56 +0000925 result=bias-range/2.0;
cristy351842f2010-03-07 15:27:38 +0000926 else
cristy94a75782011-09-25 02:33:56 +0000927 if (result >= 1.0)
928 result=bias+range/2.0;
929 else
930 result=(MagickRealType) (range/MagickPI*asin((double) result)+bias);
931 result*=QuantumRange;
cristy351842f2010-03-07 15:27:38 +0000932 break;
933 }
934 case ArctanFunction:
935 {
cristy94a75782011-09-25 02:33:56 +0000936 MagickRealType
937 center,
938 bias,
939 range,
940 slope;
941
942 /*
943 Arctan: slope, center, range, and bias.
944 */
945 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
946 center=(number_parameters >= 2) ? parameters[1] : 0.5;
947 range=(number_parameters >= 3) ? parameters[2] : 1.0;
948 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
cristy7a07f9f2010-11-27 19:09:51 +0000949 result=(MagickRealType) (MagickPI*slope*(QuantumScale*pixel-center));
cristy351842f2010-03-07 15:27:38 +0000950 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
cristy94a75782011-09-25 02:33:56 +0000951 result)+bias));
cristy351842f2010-03-07 15:27:38 +0000952 break;
953 }
954 case UndefinedFunction:
955 break;
956 }
957 return(ClampToQuantum(result));
958}
959
960MagickExport MagickBooleanType FunctionImage(Image *image,
cristybb503372010-05-27 20:51:26 +0000961 const MagickFunction function,const size_t number_parameters,
cristy351842f2010-03-07 15:27:38 +0000962 const double *parameters,ExceptionInfo *exception)
963{
cristy351842f2010-03-07 15:27:38 +0000964#define FunctionImageTag "Function/Image "
965
966 CacheView
967 *image_view;
968
cristy351842f2010-03-07 15:27:38 +0000969 MagickBooleanType
970 status;
971
cristy5f959472010-05-27 22:19:46 +0000972 MagickOffsetType
973 progress;
974
975 ssize_t
976 y;
977
cristy351842f2010-03-07 15:27:38 +0000978 assert(image != (Image *) NULL);
979 assert(image->signature == MagickSignature);
980 if (image->debug != MagickFalse)
981 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
982 assert(exception != (ExceptionInfo *) NULL);
983 assert(exception->signature == MagickSignature);
cristy574cc262011-08-05 01:23:58 +0000984 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
985 return(MagickFalse);
cristy351842f2010-03-07 15:27:38 +0000986 status=MagickTrue;
987 progress=0;
988 image_view=AcquireCacheView(image);
989#if defined(MAGICKCORE_OPENMP_SUPPORT)
990 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
991#endif
cristybb503372010-05-27 20:51:26 +0000992 for (y=0; y < (ssize_t) image->rows; y++)
cristy351842f2010-03-07 15:27:38 +0000993 {
cristy4c08aed2011-07-01 19:47:50 +0000994 register Quantum
cristy351842f2010-03-07 15:27:38 +0000995 *restrict q;
996
cristy94a75782011-09-25 02:33:56 +0000997 register ssize_t
998 x;
999
cristy351842f2010-03-07 15:27:38 +00001000 if (status == MagickFalse)
1001 continue;
1002 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001003 if (q == (Quantum *) NULL)
cristy351842f2010-03-07 15:27:38 +00001004 {
1005 status=MagickFalse;
1006 continue;
1007 }
cristybb503372010-05-27 20:51:26 +00001008 for (x=0; x < (ssize_t) image->columns; x++)
cristy351842f2010-03-07 15:27:38 +00001009 {
cristy94a75782011-09-25 02:33:56 +00001010 register ssize_t
1011 i;
1012
1013 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1014 {
1015 PixelTrait
1016 traits;
1017
1018 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1019 if (traits == UndefinedPixelTrait)
1020 continue;
1021 if ((traits & UpdatePixelTrait) == 0)
1022 continue;
1023 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1024 exception);
1025 }
cristyed231572011-07-14 02:18:59 +00001026 q+=GetPixelChannels(image);
cristy351842f2010-03-07 15:27:38 +00001027 }
1028 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1029 status=MagickFalse;
1030 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1031 {
1032 MagickBooleanType
1033 proceed;
1034
1035#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +00001036 #pragma omp critical (MagickCore_FunctionImage)
cristy351842f2010-03-07 15:27:38 +00001037#endif
1038 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1039 if (proceed == MagickFalse)
1040 status=MagickFalse;
1041 }
1042 }
1043 image_view=DestroyCacheView(image_view);
1044 return(status);
1045}
1046
1047/*
1048%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1049% %
1050% %
1051% %
cristyd42d9952011-07-08 14:21:50 +00001052% G e t I m a g e E x t r e m a %
cristy3ed852e2009-09-05 21:47:34 +00001053% %
1054% %
1055% %
1056%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1057%
cristyd42d9952011-07-08 14:21:50 +00001058% GetImageExtrema() returns the extrema of one or more image channels.
cristy3ed852e2009-09-05 21:47:34 +00001059%
cristyd42d9952011-07-08 14:21:50 +00001060% The format of the GetImageExtrema method is:
cristy3ed852e2009-09-05 21:47:34 +00001061%
cristyd42d9952011-07-08 14:21:50 +00001062% MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1063% size_t *maxima,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001064%
1065% A description of each parameter follows:
1066%
1067% o image: the image.
1068%
cristy3ed852e2009-09-05 21:47:34 +00001069% o minima: the minimum value in the channel.
1070%
1071% o maxima: the maximum value in the channel.
1072%
1073% o exception: return any errors or warnings in this structure.
1074%
1075*/
cristy3ed852e2009-09-05 21:47:34 +00001076MagickExport MagickBooleanType GetImageExtrema(const Image *image,
cristybb503372010-05-27 20:51:26 +00001077 size_t *minima,size_t *maxima,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001078{
cristy3ed852e2009-09-05 21:47:34 +00001079 double
1080 max,
1081 min;
1082
1083 MagickBooleanType
1084 status;
1085
1086 assert(image != (Image *) NULL);
1087 assert(image->signature == MagickSignature);
1088 if (image->debug != MagickFalse)
1089 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyd42d9952011-07-08 14:21:50 +00001090 status=GetImageRange(image,&min,&max,exception);
cristybb503372010-05-27 20:51:26 +00001091 *minima=(size_t) ceil(min-0.5);
1092 *maxima=(size_t) floor(max+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001093 return(status);
1094}
1095
1096/*
1097%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1098% %
1099% %
1100% %
cristyd42d9952011-07-08 14:21:50 +00001101% G e t I m a g e M e a n %
cristy3ed852e2009-09-05 21:47:34 +00001102% %
1103% %
1104% %
1105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1106%
cristyd42d9952011-07-08 14:21:50 +00001107% GetImageMean() returns the mean and standard deviation of one or more
cristy3ed852e2009-09-05 21:47:34 +00001108% image channels.
1109%
cristyd42d9952011-07-08 14:21:50 +00001110% The format of the GetImageMean method is:
cristy3ed852e2009-09-05 21:47:34 +00001111%
cristyd42d9952011-07-08 14:21:50 +00001112% MagickBooleanType GetImageMean(const Image *image,double *mean,
1113% double *standard_deviation,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001114%
1115% A description of each parameter follows:
1116%
1117% o image: the image.
1118%
cristy3ed852e2009-09-05 21:47:34 +00001119% o mean: the average value in the channel.
1120%
1121% o standard_deviation: the standard deviation of the channel.
1122%
1123% o exception: return any errors or warnings in this structure.
1124%
1125*/
cristy3ed852e2009-09-05 21:47:34 +00001126MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1127 double *standard_deviation,ExceptionInfo *exception)
1128{
cristyfd9dcd42010-08-08 18:07:02 +00001129 ChannelStatistics
1130 *channel_statistics;
cristy3ed852e2009-09-05 21:47:34 +00001131
cristy94a75782011-09-25 02:33:56 +00001132 register ssize_t
1133 i;
1134
cristyfd9dcd42010-08-08 18:07:02 +00001135 size_t
cristy94a75782011-09-25 02:33:56 +00001136 area;
cristy3ed852e2009-09-05 21:47:34 +00001137
1138 assert(image != (Image *) NULL);
1139 assert(image->signature == MagickSignature);
1140 if (image->debug != MagickFalse)
1141 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyd42d9952011-07-08 14:21:50 +00001142 channel_statistics=GetImageStatistics(image,exception);
cristyfd9dcd42010-08-08 18:07:02 +00001143 if (channel_statistics == (ChannelStatistics *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001144 return(MagickFalse);
cristy94a75782011-09-25 02:33:56 +00001145 area=0;
cristy25a5f2f2011-09-24 14:09:43 +00001146 channel_statistics[MaxPixelChannels].mean=0.0;
1147 channel_statistics[MaxPixelChannels].standard_deviation=0.0;
cristy94a75782011-09-25 02:33:56 +00001148 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1149 {
1150 PixelTrait
1151 traits;
1152
1153 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1154 if (traits == UndefinedPixelTrait)
1155 continue;
1156 if ((traits & UpdatePixelTrait) == 0)
1157 continue;
1158 channel_statistics[MaxPixelChannels].mean+=channel_statistics[i].mean;
1159 channel_statistics[MaxPixelChannels].standard_deviation+=
1160 channel_statistics[i].variance-channel_statistics[i].mean*
1161 channel_statistics[i].mean;
1162 area++;
1163 }
1164 channel_statistics[MaxPixelChannels].mean/=area;
cristy25a5f2f2011-09-24 14:09:43 +00001165 channel_statistics[MaxPixelChannels].standard_deviation=
cristy94a75782011-09-25 02:33:56 +00001166 sqrt(channel_statistics[MaxPixelChannels].standard_deviation/area);
cristy25a5f2f2011-09-24 14:09:43 +00001167 *mean=channel_statistics[MaxPixelChannels].mean;
1168 *standard_deviation=channel_statistics[MaxPixelChannels].standard_deviation;
cristyfd9dcd42010-08-08 18:07:02 +00001169 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1170 channel_statistics);
1171 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +00001172}
1173
1174/*
1175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1176% %
1177% %
1178% %
cristyd42d9952011-07-08 14:21:50 +00001179% G e t I m a g e K u r t o s i s %
cristy3ed852e2009-09-05 21:47:34 +00001180% %
1181% %
1182% %
1183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1184%
cristyd42d9952011-07-08 14:21:50 +00001185% GetImageKurtosis() returns the kurtosis and skewness of one or more
cristy3ed852e2009-09-05 21:47:34 +00001186% image channels.
1187%
cristyd42d9952011-07-08 14:21:50 +00001188% The format of the GetImageKurtosis method is:
cristy3ed852e2009-09-05 21:47:34 +00001189%
cristyd42d9952011-07-08 14:21:50 +00001190% MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1191% double *skewness,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001192%
1193% A description of each parameter follows:
1194%
1195% o image: the image.
1196%
cristy3ed852e2009-09-05 21:47:34 +00001197% o kurtosis: the kurtosis of the channel.
1198%
1199% o skewness: the skewness of the channel.
1200%
1201% o exception: return any errors or warnings in this structure.
1202%
1203*/
cristy3ed852e2009-09-05 21:47:34 +00001204MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1205 double *kurtosis,double *skewness,ExceptionInfo *exception)
1206{
cristy94a75782011-09-25 02:33:56 +00001207 CacheView
1208 *image_view;
1209
cristy3ed852e2009-09-05 21:47:34 +00001210 double
1211 area,
1212 mean,
1213 standard_deviation,
1214 sum_squares,
1215 sum_cubes,
1216 sum_fourth_power;
1217
cristy94a75782011-09-25 02:33:56 +00001218 MagickBooleanType
1219 status;
1220
cristybb503372010-05-27 20:51:26 +00001221 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001222 y;
1223
1224 assert(image != (Image *) NULL);
1225 assert(image->signature == MagickSignature);
1226 if (image->debug != MagickFalse)
1227 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy94a75782011-09-25 02:33:56 +00001228 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001229 *kurtosis=0.0;
1230 *skewness=0.0;
1231 area=0.0;
1232 mean=0.0;
1233 standard_deviation=0.0;
1234 sum_squares=0.0;
1235 sum_cubes=0.0;
1236 sum_fourth_power=0.0;
cristy94a75782011-09-25 02:33:56 +00001237 image_view=AcquireCacheView(image);
1238#if defined(MAGICKCORE_OPENMP_SUPPORT)
1239 #pragma omp parallel for schedule(dynamic) shared(status) omp_throttle(1)
1240#endif
cristybb503372010-05-27 20:51:26 +00001241 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001242 {
cristy4c08aed2011-07-01 19:47:50 +00001243 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001244 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001245
cristybb503372010-05-27 20:51:26 +00001246 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001247 x;
1248
cristy94a75782011-09-25 02:33:56 +00001249 if (status == MagickFalse)
1250 continue;
1251 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001252 if (p == (const Quantum *) NULL)
cristy94a75782011-09-25 02:33:56 +00001253 {
1254 status=MagickFalse;
1255 continue;
1256 }
cristybb503372010-05-27 20:51:26 +00001257 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001258 {
cristy94a75782011-09-25 02:33:56 +00001259 register ssize_t
1260 i;
1261
1262 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1263 {
1264 PixelTrait
1265 traits;
1266
1267 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1268 if (traits == UndefinedPixelTrait)
1269 continue;
1270 if ((traits & UpdatePixelTrait) == 0)
1271 continue;
1272#if defined(MAGICKCORE_OPENMP_SUPPORT)
1273 #pragma omp critical (MagickCore_GetImageKurtosis)
1274#endif
cristy3ed852e2009-09-05 21:47:34 +00001275 {
cristy94a75782011-09-25 02:33:56 +00001276 mean+=p[i];
1277 sum_squares+=(double) p[i]*p[i];
1278 sum_cubes+=(double) p[i]*p[i]*p[i];
1279 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
cristy3ed852e2009-09-05 21:47:34 +00001280 area++;
1281 }
cristy94a75782011-09-25 02:33:56 +00001282 }
cristyed231572011-07-14 02:18:59 +00001283 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001284 }
1285 }
cristy94a75782011-09-25 02:33:56 +00001286 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001287 if (area != 0.0)
1288 {
1289 mean/=area;
1290 sum_squares/=area;
1291 sum_cubes/=area;
1292 sum_fourth_power/=area;
1293 }
1294 standard_deviation=sqrt(sum_squares-(mean*mean));
1295 if (standard_deviation != 0.0)
1296 {
1297 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1298 3.0*mean*mean*mean*mean;
1299 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1300 standard_deviation;
1301 *kurtosis-=3.0;
1302 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1303 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1304 }
cristy94a75782011-09-25 02:33:56 +00001305 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001306}
cristy46f08202010-01-10 04:04:21 +00001307
cristy3ed852e2009-09-05 21:47:34 +00001308/*
1309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1310% %
1311% %
1312% %
cristyd42d9952011-07-08 14:21:50 +00001313% G e t I m a g e R a n g e %
cristy3ed852e2009-09-05 21:47:34 +00001314% %
1315% %
1316% %
1317%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1318%
cristyd42d9952011-07-08 14:21:50 +00001319% GetImageRange() returns the range of one or more image channels.
cristy3ed852e2009-09-05 21:47:34 +00001320%
cristyd42d9952011-07-08 14:21:50 +00001321% The format of the GetImageRange method is:
cristy3ed852e2009-09-05 21:47:34 +00001322%
cristyd42d9952011-07-08 14:21:50 +00001323% MagickBooleanType GetImageRange(const Image *image,double *minima,
1324% double *maxima,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001325%
1326% A description of each parameter follows:
1327%
1328% o image: the image.
1329%
cristy3ed852e2009-09-05 21:47:34 +00001330% o minima: the minimum value in the channel.
1331%
1332% o maxima: the maximum value in the channel.
1333%
1334% o exception: return any errors or warnings in this structure.
1335%
1336*/
cristyd42d9952011-07-08 14:21:50 +00001337MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1338 double *maxima,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001339{
cristy4a9be682011-09-25 02:02:32 +00001340 CacheView
1341 *image_view;
1342
1343 MagickBooleanType
1344 status;
cristy3ed852e2009-09-05 21:47:34 +00001345
cristy9d314ff2011-03-09 01:30:28 +00001346 ssize_t
1347 y;
1348
cristy3ed852e2009-09-05 21:47:34 +00001349 assert(image != (Image *) NULL);
1350 assert(image->signature == MagickSignature);
1351 if (image->debug != MagickFalse)
1352 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy4a9be682011-09-25 02:02:32 +00001353 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001354 *maxima=(-1.0E-37);
1355 *minima=1.0E+37;
cristy4a9be682011-09-25 02:02:32 +00001356 image_view=AcquireCacheView(image);
1357#if defined(MAGICKCORE_OPENMP_SUPPORT)
1358 #pragma omp parallel for schedule(dynamic) shared(status) omp_throttle(1)
1359#endif
cristybb503372010-05-27 20:51:26 +00001360 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001361 {
cristy4c08aed2011-07-01 19:47:50 +00001362 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001363 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001364
cristybb503372010-05-27 20:51:26 +00001365 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001366 x;
1367
cristy4a9be682011-09-25 02:02:32 +00001368 if (status == MagickFalse)
1369 continue;
1370 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001371 if (p == (const Quantum *) NULL)
cristy4a9be682011-09-25 02:02:32 +00001372 {
1373 status=MagickFalse;
1374 continue;
1375 }
cristybb503372010-05-27 20:51:26 +00001376 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001377 {
cristy4a9be682011-09-25 02:02:32 +00001378 register ssize_t
1379 i;
1380
1381 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1382 {
1383 PixelTrait
1384 traits;
1385
1386 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1387 if (traits == UndefinedPixelTrait)
1388 continue;
1389 if ((traits & UpdatePixelTrait) == 0)
1390 continue;
1391#if defined(MAGICKCORE_OPENMP_SUPPORT)
1392 #pragma omp critical (MagickCore_GetImageRange)
1393#endif
cristy3ed852e2009-09-05 21:47:34 +00001394 {
cristyba18a7a2011-09-25 15:43:43 +00001395 if (p[i] < *minima)
cristy4a9be682011-09-25 02:02:32 +00001396 *minima=(double) p[i];
cristyba18a7a2011-09-25 15:43:43 +00001397 if (p[i] > *maxima)
cristy4a9be682011-09-25 02:02:32 +00001398 *maxima=(double) p[i];
cristy3ed852e2009-09-05 21:47:34 +00001399 }
cristy4a9be682011-09-25 02:02:32 +00001400 }
cristyed231572011-07-14 02:18:59 +00001401 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001402 }
1403 }
cristy4a9be682011-09-25 02:02:32 +00001404 image_view=DestroyCacheView(image_view);
1405 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001406}
1407
1408/*
1409%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410% %
1411% %
1412% %
cristyd42d9952011-07-08 14:21:50 +00001413% G e t I m a g e S t a t i s t i c s %
cristy3ed852e2009-09-05 21:47:34 +00001414% %
1415% %
1416% %
1417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418%
cristyd42d9952011-07-08 14:21:50 +00001419% GetImageStatistics() returns statistics for each channel in the
cristy3ed852e2009-09-05 21:47:34 +00001420% image. The statistics include the channel depth, its minima, maxima, mean,
1421% standard deviation, kurtosis and skewness. You can access the red channel
1422% mean, for example, like this:
1423%
cristyd42d9952011-07-08 14:21:50 +00001424% channel_statistics=GetImageStatistics(image,exception);
cristy49dd6a02011-09-24 23:08:01 +00001425% red_mean=channel_statistics[RedPixelChannel].mean;
cristy3ed852e2009-09-05 21:47:34 +00001426%
1427% Use MagickRelinquishMemory() to free the statistics buffer.
1428%
cristyd42d9952011-07-08 14:21:50 +00001429% The format of the GetImageStatistics method is:
cristy3ed852e2009-09-05 21:47:34 +00001430%
cristyd42d9952011-07-08 14:21:50 +00001431% ChannelStatistics *GetImageStatistics(const Image *image,
cristy3ed852e2009-09-05 21:47:34 +00001432% ExceptionInfo *exception)
1433%
1434% A description of each parameter follows:
1435%
1436% o image: the image.
1437%
1438% o exception: return any errors or warnings in this structure.
1439%
1440*/
cristy49dd6a02011-09-24 23:08:01 +00001441
1442static size_t GetImageChannels(const Image *image)
1443{
1444 register ssize_t
1445 i;
1446
1447 size_t
1448 channels;
1449
1450 channels=0;
1451 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1452 {
1453 PixelTrait
1454 traits;
1455
1456 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1457 if ((traits & UpdatePixelTrait) != 0)
1458 channels++;
1459 }
1460 return(channels);
1461}
1462
cristyd42d9952011-07-08 14:21:50 +00001463MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
cristy3ed852e2009-09-05 21:47:34 +00001464 ExceptionInfo *exception)
1465{
1466 ChannelStatistics
1467 *channel_statistics;
1468
1469 double
cristyfd9dcd42010-08-08 18:07:02 +00001470 area;
cristy3ed852e2009-09-05 21:47:34 +00001471
cristy3ed852e2009-09-05 21:47:34 +00001472 MagickStatusType
1473 status;
1474
1475 QuantumAny
1476 range;
1477
cristybb503372010-05-27 20:51:26 +00001478 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001479 i;
1480
1481 size_t
cristy9d314ff2011-03-09 01:30:28 +00001482 channels,
cristy49dd6a02011-09-24 23:08:01 +00001483 depth;
cristy3ed852e2009-09-05 21:47:34 +00001484
cristy9d314ff2011-03-09 01:30:28 +00001485 ssize_t
1486 y;
cristy3ed852e2009-09-05 21:47:34 +00001487
1488 assert(image != (Image *) NULL);
1489 assert(image->signature == MagickSignature);
1490 if (image->debug != MagickFalse)
1491 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy49dd6a02011-09-24 23:08:01 +00001492 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1493 MaxPixelChannels+1,sizeof(*channel_statistics));
cristy3ed852e2009-09-05 21:47:34 +00001494 if (channel_statistics == (ChannelStatistics *) NULL)
1495 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy49dd6a02011-09-24 23:08:01 +00001496 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
cristy3ed852e2009-09-05 21:47:34 +00001497 sizeof(*channel_statistics));
cristy25a5f2f2011-09-24 14:09:43 +00001498 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001499 {
1500 channel_statistics[i].depth=1;
1501 channel_statistics[i].maxima=(-1.0E-37);
1502 channel_statistics[i].minima=1.0E+37;
cristy3ed852e2009-09-05 21:47:34 +00001503 }
cristybb503372010-05-27 20:51:26 +00001504 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001505 {
cristy4c08aed2011-07-01 19:47:50 +00001506 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001507 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001508
cristybb503372010-05-27 20:51:26 +00001509 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001510 x;
1511
1512 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001513 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001514 break;
cristy49dd6a02011-09-24 23:08:01 +00001515 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001516 {
cristy49dd6a02011-09-24 23:08:01 +00001517 register ssize_t
1518 i;
1519
1520 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1521 {
1522 PixelTrait
1523 traits;
1524
1525 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1526 if (traits == UndefinedPixelTrait)
1527 continue;
1528 if (channel_statistics[i].depth != MAGICKCORE_QUANTUM_DEPTH)
1529 {
1530 depth=channel_statistics[i].depth;
1531 range=GetQuantumRange(depth);
1532 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1533 range) ? MagickTrue : MagickFalse;
1534 if (status != MagickFalse)
1535 {
1536 channel_statistics[i].depth++;
1537 continue;
1538 }
cristy3ed852e2009-09-05 21:47:34 +00001539 }
cristy49dd6a02011-09-24 23:08:01 +00001540 if ((double) p[i] < channel_statistics[i].minima)
1541 channel_statistics[i].minima=(double) p[i];
1542 if ((double) p[i] > channel_statistics[i].maxima)
1543 channel_statistics[i].maxima=(double) p[i];
1544 channel_statistics[i].sum+=p[i];
1545 channel_statistics[i].sum_squared+=(double) p[i]*p[i];
1546 channel_statistics[i].sum_cubed+=(double) p[i]*p[i]*p[i];
1547 channel_statistics[i].sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1548 }
cristyed231572011-07-14 02:18:59 +00001549 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001550 }
1551 }
1552 area=(double) image->columns*image->rows;
cristy25a5f2f2011-09-24 14:09:43 +00001553 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001554 {
cristyfd9dcd42010-08-08 18:07:02 +00001555 channel_statistics[i].sum/=area;
1556 channel_statistics[i].sum_squared/=area;
cristya8178ed2010-08-10 17:31:59 +00001557 channel_statistics[i].sum_cubed/=area;
1558 channel_statistics[i].sum_fourth_power/=area;
cristyfd9dcd42010-08-08 18:07:02 +00001559 channel_statistics[i].mean=channel_statistics[i].sum;
1560 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1561 channel_statistics[i].standard_deviation=sqrt(
1562 channel_statistics[i].variance-(channel_statistics[i].mean*
1563 channel_statistics[i].mean));
cristy3ed852e2009-09-05 21:47:34 +00001564 }
cristy25a5f2f2011-09-24 14:09:43 +00001565 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001566 {
cristy25a5f2f2011-09-24 14:09:43 +00001567 channel_statistics[MaxPixelChannels].depth=(size_t) MagickMax((double)
1568 channel_statistics[MaxPixelChannels].depth,(double)
cristy3ed852e2009-09-05 21:47:34 +00001569 channel_statistics[i].depth);
cristy25a5f2f2011-09-24 14:09:43 +00001570 channel_statistics[MaxPixelChannels].minima=MagickMin(
1571 channel_statistics[MaxPixelChannels].minima,
cristy32daba42011-05-01 02:34:39 +00001572 channel_statistics[i].minima);
cristy25a5f2f2011-09-24 14:09:43 +00001573 channel_statistics[MaxPixelChannels].maxima=MagickMax(
1574 channel_statistics[MaxPixelChannels].maxima,
cristy32daba42011-05-01 02:34:39 +00001575 channel_statistics[i].maxima);
cristy25a5f2f2011-09-24 14:09:43 +00001576 channel_statistics[MaxPixelChannels].sum+=channel_statistics[i].sum;
1577 channel_statistics[MaxPixelChannels].sum_squared+=
cristyfd9dcd42010-08-08 18:07:02 +00001578 channel_statistics[i].sum_squared;
cristy25a5f2f2011-09-24 14:09:43 +00001579 channel_statistics[MaxPixelChannels].sum_cubed+=
cristy32daba42011-05-01 02:34:39 +00001580 channel_statistics[i].sum_cubed;
cristy25a5f2f2011-09-24 14:09:43 +00001581 channel_statistics[MaxPixelChannels].sum_fourth_power+=
cristya8178ed2010-08-10 17:31:59 +00001582 channel_statistics[i].sum_fourth_power;
cristy25a5f2f2011-09-24 14:09:43 +00001583 channel_statistics[MaxPixelChannels].mean+=channel_statistics[i].mean;
1584 channel_statistics[MaxPixelChannels].variance+=
cristy32daba42011-05-01 02:34:39 +00001585 channel_statistics[i].variance-channel_statistics[i].mean*
1586 channel_statistics[i].mean;
cristy25a5f2f2011-09-24 14:09:43 +00001587 channel_statistics[MaxPixelChannels].standard_deviation+=
cristyfd9dcd42010-08-08 18:07:02 +00001588 channel_statistics[i].variance-channel_statistics[i].mean*
1589 channel_statistics[i].mean;
cristy3ed852e2009-09-05 21:47:34 +00001590 }
cristy49dd6a02011-09-24 23:08:01 +00001591 channels=GetImageChannels(image);
cristy25a5f2f2011-09-24 14:09:43 +00001592 channel_statistics[MaxPixelChannels].sum/=channels;
1593 channel_statistics[MaxPixelChannels].sum_squared/=channels;
1594 channel_statistics[MaxPixelChannels].sum_cubed/=channels;
1595 channel_statistics[MaxPixelChannels].sum_fourth_power/=channels;
1596 channel_statistics[MaxPixelChannels].mean/=channels;
1597 channel_statistics[MaxPixelChannels].variance/=channels;
1598 channel_statistics[MaxPixelChannels].standard_deviation=
1599 sqrt(channel_statistics[MaxPixelChannels].standard_deviation/channels);
1600 channel_statistics[MaxPixelChannels].kurtosis/=channels;
1601 channel_statistics[MaxPixelChannels].skewness/=channels;
1602 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001603 {
cristy3ed852e2009-09-05 21:47:34 +00001604 if (channel_statistics[i].standard_deviation == 0.0)
cristya8178ed2010-08-10 17:31:59 +00001605 continue;
1606 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-
1607 3.0*channel_statistics[i].mean*channel_statistics[i].sum_squared+
1608 2.0*channel_statistics[i].mean*channel_statistics[i].mean*
1609 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1610 channel_statistics[i].standard_deviation*
1611 channel_statistics[i].standard_deviation);
1612 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-
1613 4.0*channel_statistics[i].mean*channel_statistics[i].sum_cubed+
1614 6.0*channel_statistics[i].mean*channel_statistics[i].mean*
1615 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1616 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1617 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1618 channel_statistics[i].standard_deviation*
1619 channel_statistics[i].standard_deviation*
1620 channel_statistics[i].standard_deviation)-3.0;
cristy3ed852e2009-09-05 21:47:34 +00001621 }
1622 return(channel_statistics);
1623}