43#include "MagickCore/studio.h"
44#include "MagickCore/accelerate-private.h"
45#include "MagickCore/animate.h"
46#include "MagickCore/artifact.h"
47#include "MagickCore/blob.h"
48#include "MagickCore/blob-private.h"
49#include "MagickCore/cache.h"
50#include "MagickCore/cache-private.h"
51#include "MagickCore/cache-view.h"
52#include "MagickCore/client.h"
53#include "MagickCore/color.h"
54#include "MagickCore/color-private.h"
55#include "MagickCore/colorspace.h"
56#include "MagickCore/colorspace-private.h"
57#include "MagickCore/composite.h"
58#include "MagickCore/composite-private.h"
59#include "MagickCore/compress.h"
60#include "MagickCore/constitute.h"
61#include "MagickCore/display.h"
62#include "MagickCore/draw.h"
63#include "MagickCore/enhance.h"
64#include "MagickCore/exception.h"
65#include "MagickCore/exception-private.h"
66#include "MagickCore/gem.h"
67#include "MagickCore/gem-private.h"
68#include "MagickCore/geometry.h"
69#include "MagickCore/list.h"
70#include "MagickCore/image-private.h"
71#include "MagickCore/magic.h"
72#include "MagickCore/magick.h"
73#include "MagickCore/memory_.h"
74#include "MagickCore/module.h"
75#include "MagickCore/monitor.h"
76#include "MagickCore/monitor-private.h"
77#include "MagickCore/option.h"
78#include "MagickCore/paint.h"
79#include "MagickCore/pixel-accessor.h"
80#include "MagickCore/profile.h"
81#include "MagickCore/property.h"
82#include "MagickCore/quantize.h"
83#include "MagickCore/quantum-private.h"
84#include "MagickCore/random_.h"
85#include "MagickCore/random-private.h"
86#include "MagickCore/resource_.h"
87#include "MagickCore/segment.h"
88#include "MagickCore/semaphore.h"
89#include "MagickCore/signature-private.h"
90#include "MagickCore/statistic.h"
91#include "MagickCore/statistic-private.h"
92#include "MagickCore/string_.h"
93#include "MagickCore/thread-private.h"
94#include "MagickCore/timer.h"
95#include "MagickCore/utility.h"
96#include "MagickCore/version.h"
138 channel[MaxPixelChannels];
151 rows=MagickMax(GetImageListLength(images),(
size_t)
152 GetMagickResourceLimit(ThreadResource));
153 for (i=0; i < (ssize_t) rows; i++)
155 pixels[i]=(
PixelChannels *) RelinquishMagickMemory(pixels[i]);
176 number_images=GetImageListLength(images);
177 rows=MagickMax(number_images,(
size_t) GetMagickResourceLimit(ThreadResource));
178 pixels=(
PixelChannels **) AcquireQuantumMemory(rows,
sizeof(*pixels));
181 (void) memset(pixels,0,rows*
sizeof(*pixels));
182 columns=MagickMax(number_images,MaxPixelChannels);
183 for (next=images; next != (
Image *) NULL; next=next->next)
184 columns=MagickMax(next->columns,columns);
185 for (i=0; i < (ssize_t) rows; i++)
190 pixels[i]=(
PixelChannels *) AcquireQuantumMemory(columns,
sizeof(**pixels));
192 return(DestroyPixelTLS(images,pixels));
193 for (j=0; j < (ssize_t) columns; j++)
198 for (k=0; k < MaxPixelChannels; k++)
199 pixels[i][j].channel[k]=0.0;
205static inline double EvaluateMax(
const double x,
const double y)
212#if defined(__cplusplus) || defined(c_plusplus)
216static int IntensityCompare(
const void *x,
const void *y)
231 for (i=0; i < MaxPixelChannels; i++)
232 distance+=color_1->channel[i]-(
double) color_2->channel[i];
233 return(distance < 0.0 ? -1 : distance > 0.0 ? 1 : 0);
236#if defined(__cplusplus) || defined(c_plusplus)
240static double ApplyEvaluateOperator(
RandomInfo *random_info,
const Quantum pixel,
241 const MagickEvaluateOperator op,
const double value)
252 case UndefinedEvaluateOperator:
254 case AbsEvaluateOperator:
256 result=(double) fabs((
double) pixel+value);
259 case AddEvaluateOperator:
261 result=(double) pixel+value;
264 case AddModulusEvaluateOperator:
272 result=(double) pixel+value;
273 result-=((double) QuantumRange+1.0)*floor(result/((
double)
277 case AndEvaluateOperator:
279 result=(double) ((ssize_t) pixel & (ssize_t) (value+0.5));
282 case CosineEvaluateOperator:
284 result=(double) QuantumRange*(0.5*cos((
double) (2.0*MagickPI*
285 QuantumScale*(
double) pixel*value))+0.5);
288 case DivideEvaluateOperator:
290 result=(double) pixel/(value == 0.0 ? 1.0 : value);
293 case ExponentialEvaluateOperator:
295 result=(double) QuantumRange*exp(value*QuantumScale*(
double) pixel);
298 case GaussianNoiseEvaluateOperator:
300 result=(double) GenerateDifferentialNoise(random_info,pixel,GaussianNoise,
304 case ImpulseNoiseEvaluateOperator:
306 result=(double) GenerateDifferentialNoise(random_info,pixel,ImpulseNoise,
310 case InverseLogEvaluateOperator:
312 result=(double) QuantumRange*pow((value+1.0),QuantumScale*(
double)
313 pixel-1.0)*PerceptibleReciprocal(value);
316 case LaplacianNoiseEvaluateOperator:
318 result=(double) GenerateDifferentialNoise(random_info,pixel,
319 LaplacianNoise,value);
322 case LeftShiftEvaluateOperator:
324 result=(double) pixel;
325 for (i=0; i < (ssize_t) value; i++)
329 case LogEvaluateOperator:
331 if ((QuantumScale*(
double) pixel) >= MagickEpsilon)
332 result=(double) QuantumRange*log(QuantumScale*value*
333 (
double) pixel+1.0)/log((
double) (value+1.0));
336 case MaxEvaluateOperator:
338 result=(double) EvaluateMax((
double) pixel,value);
341 case MeanEvaluateOperator:
343 result=(double) pixel+value;
346 case MedianEvaluateOperator:
348 result=(double) pixel+value;
351 case MinEvaluateOperator:
353 result=MagickMin((
double) pixel,value);
356 case MultiplicativeNoiseEvaluateOperator:
358 result=(double) GenerateDifferentialNoise(random_info,pixel,
359 MultiplicativeGaussianNoise,value);
362 case MultiplyEvaluateOperator:
364 result=(double) pixel*value;
367 case OrEvaluateOperator:
369 result=(double) ((ssize_t) pixel | (ssize_t) (value+0.5));
372 case PoissonNoiseEvaluateOperator:
374 result=(double) GenerateDifferentialNoise(random_info,pixel,PoissonNoise,
378 case PowEvaluateOperator:
380 if (((
double) pixel < 0) && ((value-floor(value)) > MagickEpsilon))
381 result=(double) -((
double) QuantumRange*pow(-(QuantumScale*(
double)
382 pixel),(
double) value));
384 result=(double) QuantumRange*pow(QuantumScale*(
double) pixel,
388 case RightShiftEvaluateOperator:
390 result=(double) pixel;
391 for (i=0; i < (ssize_t) value; i++)
395 case RootMeanSquareEvaluateOperator:
397 result=((double) pixel*(double) pixel+value);
400 case SetEvaluateOperator:
405 case SineEvaluateOperator:
407 result=(double) QuantumRange*(0.5*sin((
double) (2.0*MagickPI*
408 QuantumScale*(
double) pixel*value))+0.5);
411 case SubtractEvaluateOperator:
413 result=(double) pixel-value;
416 case SumEvaluateOperator:
418 result=(double) pixel+value;
421 case ThresholdEvaluateOperator:
423 result=(double) (((
double) pixel <= value) ? 0 : QuantumRange);
426 case ThresholdBlackEvaluateOperator:
428 result=(double) (((
double) pixel <= value) ? 0 : pixel);
431 case ThresholdWhiteEvaluateOperator:
433 result=(double) (((
double) pixel > value) ? QuantumRange : pixel);
436 case UniformNoiseEvaluateOperator:
438 result=(double) GenerateDifferentialNoise(random_info,pixel,UniformNoise,
442 case XorEvaluateOperator:
444 result=(double) ((ssize_t) pixel ^ (ssize_t) (value+0.5));
462 columns=images->columns;
464 for (p=images; p != (
Image *) NULL; p=p->next)
466 if (p->number_channels > q->number_channels)
468 if (p->columns > columns)
473 return(CloneImage(q,columns,rows,MagickTrue,exception));
476MagickExport
Image *EvaluateImages(
const Image *images,
479#define EvaluateImageTag "Evaluate/Image"
498 **magick_restrict evaluate_pixels;
501 **magick_restrict random_info;
510#if defined(MAGICKCORE_OPENMP_SUPPORT)
515 assert(images != (
Image *) NULL);
516 assert(images->signature == MagickCoreSignature);
518 assert(exception->signature == MagickCoreSignature);
519 if (IsEventLogging() != MagickFalse)
520 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",images->filename);
521 image=AcquireImageCanvas(images,exception);
522 if (image == (
Image *) NULL)
523 return((
Image *) NULL);
524 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
526 image=DestroyImage(image);
527 return((
Image *) NULL);
529 number_images=GetImageListLength(images);
530 evaluate_pixels=AcquirePixelTLS(images);
533 image=DestroyImage(image);
534 (void) ThrowMagickException(exception,GetMagickModule(),
535 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
536 return((
Image *) NULL);
538 image_view=(
CacheView **) AcquireQuantumMemory(number_images,
539 sizeof(*image_view));
542 image=DestroyImage(image);
543 evaluate_pixels=DestroyPixelTLS(images,evaluate_pixels);
544 (void) ThrowMagickException(exception,GetMagickModule(),
545 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
549 for (n=0; n < (ssize_t) number_images; n++)
551 image_view[n]=AcquireVirtualCacheView(view,exception);
552 view=GetNextImageInList(view);
559 random_info=AcquireRandomInfoTLS();
560 evaluate_view=AcquireAuthenticCacheView(image,exception);
561 if (op == MedianEvaluateOperator)
563#if defined(MAGICKCORE_OPENMP_SUPPORT)
564 key=GetRandomSecretKey(random_info[0]);
565 #pragma omp parallel for schedule(static) shared(progress,status) \
566 magick_number_threads(image,images,image->rows,key == ~0UL)
568 for (y=0; y < (ssize_t) image->rows; y++)
571 id = GetOpenMPThreadId();
588 if (status == MagickFalse)
590 p=(
const Quantum **) AcquireQuantumMemory(number_images,
sizeof(*p));
591 if (p == (
const Quantum **) NULL)
594 (void) ThrowMagickException(exception,GetMagickModule(),
595 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
599 for (j=0; j < (ssize_t) number_images; j++)
601 p[j]=GetCacheViewVirtualPixels(image_view[j],0,y,image->columns,1,
603 if (p[j] == (
const Quantum *) NULL)
606 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
608 if ((j < (ssize_t) number_images) || (q == (Quantum *) NULL))
613 evaluate_pixel=evaluate_pixels[id];
614 for (x=0; x < (ssize_t) image->columns; x++)
623 for (j=0; j < (ssize_t) number_images; j++)
625 for (i=0; i < MaxPixelChannels; i++)
626 evaluate_pixel[j].channel[i]=0.0;
627 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
629 PixelChannel channel = GetPixelChannelChannel(image,i);
630 PixelTrait traits = GetPixelChannelTraits(next,channel);
631 PixelTrait evaluate_traits = GetPixelChannelTraits(image,channel);
632 if ((traits == UndefinedPixelTrait) ||
633 (evaluate_traits == UndefinedPixelTrait) ||
634 ((traits & UpdatePixelTrait) == 0))
636 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
637 random_info[
id],GetPixelChannel(next,channel,p[j]),op,
638 evaluate_pixel[j].channel[i]);
640 p[j]+=(ptrdiff_t) GetPixelChannels(next);
641 next=GetNextImageInList(next);
643 qsort((
void *) evaluate_pixel,number_images,
sizeof(*evaluate_pixel),
645 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
647 PixelChannel channel = GetPixelChannelChannel(image,i);
648 PixelTrait traits = GetPixelChannelTraits(image,channel);
649 if ((traits == UndefinedPixelTrait) ||
650 ((traits & UpdatePixelTrait) == 0))
652 q[i]=ClampToQuantum(evaluate_pixel[number_images/2].channel[i]);
654 q+=(ptrdiff_t) GetPixelChannels(image);
656 p=(
const Quantum **) RelinquishMagickMemory((
void *) p);
657 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
659 if (images->progress_monitor != (MagickProgressMonitor) NULL)
664#if defined(MAGICKCORE_OPENMP_SUPPORT)
668 proceed=SetImageProgress(images,EvaluateImageTag,progress,
670 if (proceed == MagickFalse)
677#if defined(MAGICKCORE_OPENMP_SUPPORT)
678 key=GetRandomSecretKey(random_info[0]);
679 #pragma omp parallel for schedule(static) shared(progress,status) \
680 magick_number_threads(image,images,image->rows,key == ~0UL)
682 for (y=0; y < (ssize_t) image->rows; y++)
688 id = GetOpenMPThreadId();
706 if (status == MagickFalse)
708 p=(
const Quantum **) AcquireQuantumMemory(number_images,
sizeof(*p));
709 if (p == (
const Quantum **) NULL)
712 (void) ThrowMagickException(exception,GetMagickModule(),
713 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
717 for (j=0; j < (ssize_t) number_images; j++)
719 p[j]=GetCacheViewVirtualPixels(image_view[j],0,y,image->columns,1,
721 if (p[j] == (
const Quantum *) NULL)
724 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
726 if ((j < (ssize_t) number_images) || (q == (Quantum *) NULL))
731 evaluate_pixel=evaluate_pixels[id];
732 for (j=0; j < (ssize_t) image->columns; j++)
733 for (i=0; i < MaxPixelChannels; i++)
734 evaluate_pixel[j].channel[i]=0.0;
736 for (j=0; j < (ssize_t) number_images; j++)
738 for (x=0; x < (ssize_t) image->columns; x++)
740 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
742 PixelChannel channel = GetPixelChannelChannel(image,i);
743 PixelTrait traits = GetPixelChannelTraits(next,channel);
744 PixelTrait evaluate_traits = GetPixelChannelTraits(image,channel);
745 if ((traits == UndefinedPixelTrait) ||
746 (evaluate_traits == UndefinedPixelTrait))
748 if ((traits & UpdatePixelTrait) == 0)
750 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
751 random_info[
id],GetPixelChannel(next,channel,p[j]),j == 0 ?
752 AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
754 p[j]+=(ptrdiff_t) GetPixelChannels(next);
756 next=GetNextImageInList(next);
758 for (x=0; x < (ssize_t) image->columns; x++)
762 case MeanEvaluateOperator:
764 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
765 evaluate_pixel[x].channel[i]/=(
double) number_images;
768 case MultiplyEvaluateOperator:
770 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
772 for (j=0; j < (ssize_t) (number_images-1); j++)
773 evaluate_pixel[x].channel[i]*=QuantumScale;
777 case RootMeanSquareEvaluateOperator:
779 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
780 evaluate_pixel[x].channel[i]=sqrt(evaluate_pixel[x].channel[i]/
788 for (x=0; x < (ssize_t) image->columns; x++)
790 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
792 PixelChannel channel = GetPixelChannelChannel(image,i);
793 PixelTrait traits = GetPixelChannelTraits(image,channel);
794 if ((traits == UndefinedPixelTrait) ||
795 ((traits & UpdatePixelTrait) == 0))
797 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
799 q+=(ptrdiff_t) GetPixelChannels(image);
801 p=(
const Quantum **) RelinquishMagickMemory((
void *) p);
802 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
804 if (images->progress_monitor != (MagickProgressMonitor) NULL)
809#if defined(MAGICKCORE_OPENMP_SUPPORT)
813 proceed=SetImageProgress(images,EvaluateImageTag,progress,
815 if (proceed == MagickFalse)
820 for (n=0; n < (ssize_t) number_images; n++)
821 image_view[n]=DestroyCacheView(image_view[n]);
822 image_view=(
CacheView **) RelinquishMagickMemory(image_view);
823 evaluate_view=DestroyCacheView(evaluate_view);
824 evaluate_pixels=DestroyPixelTLS(images,evaluate_pixels);
825 random_info=DestroyRandomInfoTLS(random_info);
826 if (status == MagickFalse)
827 image=DestroyImage(image);
831MagickExport MagickBooleanType EvaluateImage(
Image *image,
832 const MagickEvaluateOperator op,
const double value,
ExceptionInfo *exception)
848 **magick_restrict random_info;
853#if defined(MAGICKCORE_OPENMP_SUPPORT)
858 assert(image != (
Image *) NULL);
859 assert(image->signature == MagickCoreSignature);
861 assert(exception->signature == MagickCoreSignature);
862 if (IsEventLogging() != MagickFalse)
863 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
864 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
869 artifact=GetImageArtifact(image,
"evaluate:clamp");
870 if (artifact != (
const char *) NULL)
871 clamp=IsStringTrue(artifact);
872 random_info=AcquireRandomInfoTLS();
873 image_view=AcquireAuthenticCacheView(image,exception);
874#if defined(MAGICKCORE_OPENMP_SUPPORT)
875 key=GetRandomSecretKey(random_info[0]);
876 #pragma omp parallel for schedule(static) shared(progress,status) \
877 magick_number_threads(image,image,image->rows,key == ~0UL)
879 for (y=0; y < (ssize_t) image->rows; y++)
882 id = GetOpenMPThreadId();
890 if (status == MagickFalse)
892 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
893 if (q == (Quantum *) NULL)
898 for (x=0; x < (ssize_t) image->columns; x++)
906 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
908 PixelChannel channel = GetPixelChannelChannel(image,i);
909 PixelTrait traits = GetPixelChannelTraits(image,channel);
910 if (traits == UndefinedPixelTrait)
912 if ((traits & CopyPixelTrait) != 0)
914 if ((traits & UpdatePixelTrait) == 0)
916 result=ApplyEvaluateOperator(random_info[
id],q[i],op,value);
917 if (op == MeanEvaluateOperator)
919 q[i]=clamp != MagickFalse ? ClampPixel(result) : ClampToQuantum(result);
921 q+=(ptrdiff_t) GetPixelChannels(image);
923 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
925 if (image->progress_monitor != (MagickProgressMonitor) NULL)
930#if defined(MAGICKCORE_OPENMP_SUPPORT)
934 proceed=SetImageProgress(image,EvaluateImageTag,progress,image->rows);
935 if (proceed == MagickFalse)
939 image_view=DestroyCacheView(image_view);
940 random_info=DestroyRandomInfoTLS(random_info);
978static Quantum ApplyFunction(Quantum pixel,
const MagickFunction function,
979 const size_t number_parameters,
const double *parameters,
992 case PolynomialFunction:
999 for (i=0; i < (ssize_t) number_parameters; i++)
1000 result=result*QuantumScale*(
double) pixel+parameters[i];
1001 result*=(double) QuantumRange;
1004 case SinusoidFunction:
1015 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
1016 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
1017 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
1018 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
1019 result=(double) QuantumRange*(amplitude*sin((
double) (2.0*
1020 MagickPI*(frequency*QuantumScale*(
double) pixel+phase/360.0)))+bias);
1023 case ArcsinFunction:
1035 width=(number_parameters >= 1) ? parameters[0] : 1.0;
1036 center=(number_parameters >= 2) ? parameters[1] : 0.5;
1037 range=(number_parameters >= 3) ? parameters[2] : 1.0;
1038 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
1039 result=2.0*PerceptibleReciprocal(width)*(QuantumScale*(double) pixel-
1042 result=bias-range/2.0;
1045 result=bias+range/2.0;
1047 result=(double) (range/MagickPI*asin((
double) result)+bias);
1048 result*=(double) QuantumRange;
1051 case ArctanFunction:
1062 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
1063 center=(number_parameters >= 2) ? parameters[1] : 0.5;
1064 range=(number_parameters >= 3) ? parameters[2] : 1.0;
1065 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
1066 result=MagickPI*slope*(QuantumScale*(double) pixel-center);
1067 result=(double) QuantumRange*(range/MagickPI*atan((
double) result)+bias);
1070 case UndefinedFunction:
1073 return(ClampToQuantum(result));
1076MagickExport MagickBooleanType FunctionImage(
Image *image,
1077 const MagickFunction function,
const size_t number_parameters,
1080#define FunctionImageTag "Function/Image "
1094 assert(image != (
Image *) NULL);
1095 assert(image->signature == MagickCoreSignature);
1097 assert(exception->signature == MagickCoreSignature);
1098 if (IsEventLogging() != MagickFalse)
1099 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1100#if defined(MAGICKCORE_OPENCL_SUPPORT)
1101 if (AccelerateFunctionImage(image,function,number_parameters,parameters,
1102 exception) != MagickFalse)
1105 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1106 return(MagickFalse);
1109 image_view=AcquireAuthenticCacheView(image,exception);
1110#if defined(MAGICKCORE_OPENMP_SUPPORT)
1111 #pragma omp parallel for schedule(static) shared(progress,status) \
1112 magick_number_threads(image,image,image->rows,1)
1114 for (y=0; y < (ssize_t) image->rows; y++)
1122 if (status == MagickFalse)
1124 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1125 if (q == (Quantum *) NULL)
1130 for (x=0; x < (ssize_t) image->columns; x++)
1135 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1137 PixelChannel channel = GetPixelChannelChannel(image,i);
1138 PixelTrait traits = GetPixelChannelTraits(image,channel);
1139 if (traits == UndefinedPixelTrait)
1141 if ((traits & UpdatePixelTrait) == 0)
1143 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1146 q+=(ptrdiff_t) GetPixelChannels(image);
1148 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1150 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1155#if defined(MAGICKCORE_OPENMP_SUPPORT)
1159 proceed=SetImageProgress(image,FunctionImageTag,progress,image->rows);
1160 if (proceed == MagickFalse)
1164 image_view=DestroyCacheView(image_view);
1195MagickExport MagickBooleanType GetImageEntropy(
const Image *image,
1199 *channel_statistics;
1201 assert(image != (
Image *) NULL);
1202 assert(image->signature == MagickCoreSignature);
1203 if (IsEventLogging() != MagickFalse)
1204 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1205 channel_statistics=GetImageStatistics(image,exception);
1209 return(MagickFalse);
1211 *entropy=channel_statistics[CompositePixelChannel].entropy;
1213 channel_statistics);
1246MagickExport MagickBooleanType GetImageExtrema(
const Image *image,
1256 assert(image != (
Image *) NULL);
1257 assert(image->signature == MagickCoreSignature);
1258 if (IsEventLogging() != MagickFalse)
1259 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1260 status=GetImageRange(image,&min,&max,exception);
1261 *minima=(size_t) ceil(min-0.5);
1262 *maxima=(size_t) floor(max+0.5);
1296MagickExport MagickBooleanType GetImageKurtosis(
const Image *image,
1300 *channel_statistics;
1302 assert(image != (
Image *) NULL);
1303 assert(image->signature == MagickCoreSignature);
1304 if (IsEventLogging() != MagickFalse)
1305 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1306 channel_statistics=GetImageStatistics(image,exception);
1311 return(MagickFalse);
1313 *kurtosis=channel_statistics[CompositePixelChannel].kurtosis;
1314 *skewness=channel_statistics[CompositePixelChannel].skewness;
1316 channel_statistics);
1350MagickExport MagickBooleanType GetImageMean(
const Image *image,
double *mean,
1354 *channel_statistics;
1356 assert(image != (
Image *) NULL);
1357 assert(image->signature == MagickCoreSignature);
1358 if (IsEventLogging() != MagickFalse)
1359 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1360 channel_statistics=GetImageStatistics(image,exception);
1364 *standard_deviation=NAN;
1365 return(MagickFalse);
1367 *mean=channel_statistics[CompositePixelChannel].mean;
1368 *standard_deviation=
1369 channel_statistics[CompositePixelChannel].standard_deviation;
1371 channel_statistics);
1402MagickExport MagickBooleanType GetImageMedian(
const Image *image,
double *median,
1406 *channel_statistics;
1408 assert(image != (
Image *) NULL);
1409 assert(image->signature == MagickCoreSignature);
1410 if (IsEventLogging() != MagickFalse)
1411 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1412 channel_statistics=GetImageStatistics(image,exception);
1416 return(MagickFalse);
1418 *median=channel_statistics[CompositePixelChannel].median;
1420 channel_statistics);
1453#define MaxNumberImageMoments 8
1463 M00[2*MaxPixelChannels+1],
1464 M01[2*MaxPixelChannels+1],
1465 M02[2*MaxPixelChannels+1],
1466 M03[2*MaxPixelChannels+1],
1467 M10[2*MaxPixelChannels+1],
1468 M11[2*MaxPixelChannels+1],
1469 M12[2*MaxPixelChannels+1],
1470 M20[2*MaxPixelChannels+1],
1471 M21[2*MaxPixelChannels+1],
1472 M22[2*MaxPixelChannels+1],
1473 M30[2*MaxPixelChannels+1];
1476 centroid[2*MaxPixelChannels+1];
1482 assert(image != (
Image *) NULL);
1483 assert(image->signature == MagickCoreSignature);
1484 if (IsEventLogging() != MagickFalse)
1485 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1486 channel_moments=(
ChannelMoments *) AcquireQuantumMemory(MaxPixelChannels+1,
1487 sizeof(*channel_moments));
1489 return(channel_moments);
1490 (void) memset(channel_moments,0,(MaxPixelChannels+1)*
1491 sizeof(*channel_moments));
1492 (void) memset(centroid,0,
sizeof(centroid));
1493 (void) memset(M00,0,
sizeof(M00));
1494 (void) memset(M01,0,
sizeof(M01));
1495 (void) memset(M02,0,
sizeof(M02));
1496 (void) memset(M03,0,
sizeof(M03));
1497 (void) memset(M10,0,
sizeof(M10));
1498 (void) memset(M11,0,
sizeof(M11));
1499 (void) memset(M12,0,
sizeof(M12));
1500 (void) memset(M20,0,
sizeof(M20));
1501 (void) memset(M21,0,
sizeof(M21));
1502 (void) memset(M22,0,
sizeof(M22));
1503 (void) memset(M30,0,
sizeof(M30));
1504 image_view=AcquireVirtualCacheView(image,exception);
1505 for (y=0; y < (ssize_t) image->rows; y++)
1516 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1517 if (p == (
const Quantum *) NULL)
1519 for (x=0; x < (ssize_t) image->columns; x++)
1524 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1526 PixelChannel channel = GetPixelChannelChannel(image,i);
1527 PixelTrait traits = GetPixelChannelTraits(image,channel);
1528 if (traits == UndefinedPixelTrait)
1530 if ((traits & UpdatePixelTrait) == 0)
1532 M00[channel]+=QuantumScale*(double) p[i];
1533 M00[MaxPixelChannels]+=QuantumScale*(double) p[i];
1534 M10[channel]+=x*QuantumScale*(double) p[i];
1535 M10[MaxPixelChannels]+=x*QuantumScale*(double) p[i];
1536 M01[channel]+=y*QuantumScale*(double) p[i];
1537 M01[MaxPixelChannels]+=y*QuantumScale*(double) p[i];
1539 p+=(ptrdiff_t) GetPixelChannels(image);
1542 for (c=0; c <= MaxPixelChannels; c++)
1547 centroid[c].x=M10[c]*PerceptibleReciprocal(M00[c]);
1548 centroid[c].y=M01[c]*PerceptibleReciprocal(M00[c]);
1550 for (y=0; y < (ssize_t) image->rows; y++)
1561 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1562 if (p == (
const Quantum *) NULL)
1564 for (x=0; x < (ssize_t) image->columns; x++)
1569 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1571 PixelChannel channel = GetPixelChannelChannel(image,i);
1572 PixelTrait traits = GetPixelChannelTraits(image,channel);
1573 if (traits == UndefinedPixelTrait)
1575 if ((traits & UpdatePixelTrait) == 0)
1577 M11[channel]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1578 QuantumScale*(double) p[i];
1579 M11[MaxPixelChannels]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1580 QuantumScale*(double) p[i];
1581 M20[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1582 QuantumScale*(double) p[i];
1583 M20[MaxPixelChannels]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1584 QuantumScale*(double) p[i];
1585 M02[channel]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1586 QuantumScale*(double) p[i];
1587 M02[MaxPixelChannels]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1588 QuantumScale*(double) p[i];
1589 M21[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1590 (y-centroid[channel].y)*QuantumScale*(
double) p[i];
1591 M21[MaxPixelChannels]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1592 (y-centroid[channel].y)*QuantumScale*(
double) p[i];
1593 M12[channel]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1594 (y-centroid[channel].y)*QuantumScale*(
double) p[i];
1595 M12[MaxPixelChannels]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1596 (y-centroid[channel].y)*QuantumScale*(
double) p[i];
1597 M22[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1598 (y-centroid[channel].y)*(y-centroid[channel].y)*QuantumScale*(double)
1600 M22[MaxPixelChannels]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1601 (y-centroid[channel].y)*(y-centroid[channel].y)*QuantumScale*(double)
1603 M30[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1604 (x-centroid[channel].x)*QuantumScale*(
double) p[i];
1605 M30[MaxPixelChannels]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1606 (x-centroid[channel].x)*QuantumScale*(
double) p[i];
1607 M03[channel]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1608 (y-centroid[channel].y)*QuantumScale*(
double) p[i];
1609 M03[MaxPixelChannels]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1610 (y-centroid[channel].y)*QuantumScale*(
double) p[i];
1612 p+=(ptrdiff_t) GetPixelChannels(image);
1615 channels=(double) GetImageChannels(image);
1616 M00[MaxPixelChannels]/=channels;
1617 M01[MaxPixelChannels]/=channels;
1618 M02[MaxPixelChannels]/=channels;
1619 M03[MaxPixelChannels]/=channels;
1620 M10[MaxPixelChannels]/=channels;
1621 M11[MaxPixelChannels]/=channels;
1622 M12[MaxPixelChannels]/=channels;
1623 M20[MaxPixelChannels]/=channels;
1624 M21[MaxPixelChannels]/=channels;
1625 M22[MaxPixelChannels]/=channels;
1626 M30[MaxPixelChannels]/=channels;
1627 for (c=0; c <= MaxPixelChannels; c++)
1632 channel_moments[c].centroid=centroid[c];
1633 channel_moments[c].ellipse_axis.x=sqrt((2.0*PerceptibleReciprocal(M00[c]))*
1634 ((M20[c]+M02[c])+sqrt(4.0*M11[c]*M11[c]+(M20[c]-M02[c])*(M20[c]-M02[c]))));
1635 channel_moments[c].ellipse_axis.y=sqrt((2.0*PerceptibleReciprocal(M00[c]))*
1636 ((M20[c]+M02[c])-sqrt(4.0*M11[c]*M11[c]+(M20[c]-M02[c])*(M20[c]-M02[c]))));
1637 channel_moments[c].ellipse_angle=RadiansToDegrees(1.0/2.0*atan(2.0*
1638 M11[c]*PerceptibleReciprocal(M20[c]-M02[c])));
1639 if (fabs(M11[c]) < 0.0)
1641 if ((fabs(M20[c]-M02[c]) >= 0.0) &&
1642 ((M20[c]-M02[c]) < 0.0))
1643 channel_moments[c].ellipse_angle+=90.0;
1648 if (fabs(M20[c]-M02[c]) >= 0.0)
1650 if ((M20[c]-M02[c]) < 0.0)
1651 channel_moments[c].ellipse_angle+=90.0;
1653 channel_moments[c].ellipse_angle+=180.0;
1657 if ((fabs(M20[c]-M02[c]) >= 0.0) && ((M20[c]-M02[c]) < 0.0))
1658 channel_moments[c].ellipse_angle+=90.0;
1659 channel_moments[c].ellipse_eccentricity=sqrt(1.0-(
1660 channel_moments[c].ellipse_axis.y*
1661 channel_moments[c].ellipse_axis.y*PerceptibleReciprocal(
1662 channel_moments[c].ellipse_axis.x*
1663 channel_moments[c].ellipse_axis.x)));
1664 channel_moments[c].ellipse_intensity=M00[c]*
1665 PerceptibleReciprocal(MagickPI*channel_moments[c].ellipse_axis.x*
1666 channel_moments[c].ellipse_axis.y+MagickEpsilon);
1668 for (c=0; c <= MaxPixelChannels; c++)
1675 M11[c]*=PerceptibleReciprocal(pow(M00[c],1.0+(1.0+1.0)/2.0));
1676 M20[c]*=PerceptibleReciprocal(pow(M00[c],1.0+(2.0+0.0)/2.0));
1677 M02[c]*=PerceptibleReciprocal(pow(M00[c],1.0+(0.0+2.0)/2.0));
1678 M21[c]*=PerceptibleReciprocal(pow(M00[c],1.0+(2.0+1.0)/2.0));
1679 M12[c]*=PerceptibleReciprocal(pow(M00[c],1.0+(1.0+2.0)/2.0));
1680 M22[c]*=PerceptibleReciprocal(pow(M00[c],1.0+(2.0+2.0)/2.0));
1681 M30[c]*=PerceptibleReciprocal(pow(M00[c],1.0+(3.0+0.0)/2.0));
1682 M03[c]*=PerceptibleReciprocal(pow(M00[c],1.0+(0.0+3.0)/2.0));
1685 image_view=DestroyCacheView(image_view);
1686 for (c=0; c <= MaxPixelChannels; c++)
1691 channel_moments[c].invariant[0]=M20[c]+M02[c];
1692 channel_moments[c].invariant[1]=(M20[c]-M02[c])*(M20[c]-M02[c])+4.0*M11[c]*
1694 channel_moments[c].invariant[2]=(M30[c]-3.0*M12[c])*(M30[c]-3.0*M12[c])+
1695 (3.0*M21[c]-M03[c])*(3.0*M21[c]-M03[c]);
1696 channel_moments[c].invariant[3]=(M30[c]+M12[c])*(M30[c]+M12[c])+
1697 (M21[c]+M03[c])*(M21[c]+M03[c]);
1698 channel_moments[c].invariant[4]=(M30[c]-3.0*M12[c])*(M30[c]+M12[c])*
1699 ((M30[c]+M12[c])*(M30[c]+M12[c])-3.0*(M21[c]+M03[c])*(M21[c]+M03[c]))+
1700 (3.0*M21[c]-M03[c])*(M21[c]+M03[c])*(3.0*(M30[c]+M12[c])*(M30[c]+M12[c])-
1701 (M21[c]+M03[c])*(M21[c]+M03[c]));
1702 channel_moments[c].invariant[5]=(M20[c]-M02[c])*((M30[c]+M12[c])*
1703 (M30[c]+M12[c])-(M21[c]+M03[c])*(M21[c]+M03[c]))+4.0*M11[c]*
1704 (M30[c]+M12[c])*(M21[c]+M03[c]);
1705 channel_moments[c].invariant[6]=(3.0*M21[c]-M03[c])*(M30[c]+M12[c])*
1706 ((M30[c]+M12[c])*(M30[c]+M12[c])-3.0*(M21[c]+M03[c])*(M21[c]+M03[c]))-
1707 (M30[c]-3*M12[c])*(M21[c]+M03[c])*(3.0*(M30[c]+M12[c])*(M30[c]+M12[c])-
1708 (M21[c]+M03[c])*(M21[c]+M03[c]));
1709 channel_moments[c].invariant[7]=M11[c]*((M30[c]+M12[c])*(M30[c]+M12[c])-
1710 (M03[c]+M21[c])*(M03[c]+M21[c]))-(M20[c]-M02[c])*(M30[c]+M12[c])*
1713 if (y < (ssize_t) image->rows)
1714 channel_moments=(
ChannelMoments *) RelinquishMagickMemory(channel_moments);
1715 return(channel_moments);
1765 MaxPixelChannels+1UL,
sizeof(*perceptual_hash));
1768 artifact=GetImageArtifact(image,
"phash:colorspaces");
1769 if (artifact != NULL)
1770 colorspaces=AcquireString(artifact);
1772 colorspaces=AcquireString(
"xyY,HSB");
1773 perceptual_hash[0].number_colorspaces=0;
1774 perceptual_hash[0].number_channels=0;
1776 for (i=0; (p=StringToken(
",",&q)) != (
char *) NULL; i++)
1791 if (i >= MaximumNumberOfPerceptualColorspaces)
1793 colorspace=ParseCommandOption(MagickColorspaceOptions,MagickFalse,p);
1796 perceptual_hash[0].colorspace[i]=(ColorspaceType) colorspace;
1797 hash_image=BlurImage(image,0.0,1.0,exception);
1798 if (hash_image == (
Image *) NULL)
1800 hash_image->depth=8;
1801 status=TransformImageColorspace(hash_image,(ColorspaceType) colorspace,
1803 if (status == MagickFalse)
1805 moments=GetImageMoments(hash_image,exception);
1806 perceptual_hash[0].number_colorspaces++;
1807 perceptual_hash[0].number_channels+=GetImageChannels(hash_image);
1808 hash_image=DestroyImage(hash_image);
1811 for (channel=0; channel <= MaxPixelChannels; channel++)
1812 for (j=0; j < MaximumNumberOfImageMoments; j++)
1813 perceptual_hash[channel].phash[i][j]=
1814 (-MagickLog10(moments[channel].invariant[j]));
1817 colorspaces=DestroyString(colorspaces);
1818 return(perceptual_hash);
1850MagickExport MagickBooleanType GetImageRange(
const Image *image,
double *minima,
1863 assert(image != (
Image *) NULL);
1864 assert(image->signature == MagickCoreSignature);
1865 if (IsEventLogging() != MagickFalse)
1866 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1868 initialize=MagickTrue;
1871 image_view=AcquireVirtualCacheView(image,exception);
1872#if defined(MAGICKCORE_OPENMP_SUPPORT)
1873 #pragma omp parallel for schedule(static) shared(status,initialize) \
1874 magick_number_threads(image,image,image->rows,1)
1876 for (y=0; y < (ssize_t) image->rows; y++)
1891 if (status == MagickFalse)
1893 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1894 if (p == (
const Quantum *) NULL)
1899 row_initialize=MagickTrue;
1900 for (x=0; x < (ssize_t) image->columns; x++)
1905 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1907 PixelChannel channel = GetPixelChannelChannel(image,i);
1908 PixelTrait traits = GetPixelChannelTraits(image,channel);
1909 if (traits == UndefinedPixelTrait)
1911 if ((traits & UpdatePixelTrait) == 0)
1913 if (row_initialize != MagickFalse)
1915 row_minima=(double) p[i];
1916 row_maxima=(double) p[i];
1917 row_initialize=MagickFalse;
1921 if ((
double) p[i] < row_minima)
1922 row_minima=(double) p[i];
1923 if ((
double) p[i] > row_maxima)
1924 row_maxima=(double) p[i];
1927 p+=(ptrdiff_t) GetPixelChannels(image);
1929#if defined(MAGICKCORE_OPENMP_SUPPORT)
1930#pragma omp critical (MagickCore_GetImageRange)
1933 if (initialize != MagickFalse)
1937 initialize=MagickFalse;
1941 if (row_minima < *minima)
1943 if (row_maxima > *maxima)
1948 image_view=DestroyCacheView(image_view);
1986static ssize_t GetMedianPixel(Quantum *pixels,
const size_t n)
1988#define SwapPixels(alpha,beta) \
1990 Quantum gamma=(alpha); \
1991 (alpha)=(beta);(beta)=gamma; \
1996 high = (ssize_t) n-1,
1997 median = (low+high)/2;
2008 if (high == (low+1))
2010 if (pixels[low] > pixels[high])
2011 SwapPixels(pixels[low],pixels[high]);
2014 if (pixels[mid] > pixels[high])
2015 SwapPixels(pixels[mid],pixels[high]);
2016 if (pixels[low] > pixels[high])
2017 SwapPixels(pixels[low], pixels[high]);
2018 if (pixels[mid] > pixels[low])
2019 SwapPixels(pixels[mid],pixels[low]);
2020 SwapPixels(pixels[mid],pixels[low+1]);
2023 do l++;
while (pixels[low] > pixels[l]);
2024 do h--;
while (pixels[h] > pixels[low]);
2027 SwapPixels(pixels[l],pixels[h]);
2029 SwapPixels(pixels[low],pixels[h]);
2037static inline long double PerceptibleReciprocalLD(
const long double x)
2045 sign=x < 0.0 ? -1.0 : 1.0;
2046 if ((sign*x) >= MagickEpsilon)
2048 return(sign/MagickEpsilon);
2055 *channel_statistics;
2083 assert(image != (
Image *) NULL);
2084 assert(image->signature == MagickCoreSignature);
2085 if (IsEventLogging() != MagickFalse)
2086 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2087 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,
2088 MagickMax(GetPixelChannels(image),1)*
sizeof(*histogram));
2090 MaxPixelChannels+1,
sizeof(*channel_statistics));
2092 (histogram == (
double *) NULL))
2094 if (histogram != (
double *) NULL)
2095 histogram=(
double *) RelinquishMagickMemory(histogram);
2098 channel_statistics);
2099 (void) ThrowMagickException(exception,GetMagickModule(),
2100 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2101 return(channel_statistics);
2103 (void) memset(channel_statistics,0,(MaxPixelChannels+1)*
2104 sizeof(*channel_statistics));
2105 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
2110 cs->maxima=(-MagickMaximumValue);
2111 cs->minima=MagickMaximumValue;
2115 cs->standard_deviation=0.0;
2121 (void) memset(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
2122 sizeof(*histogram));
2123 for (y=0; y < (ssize_t) image->rows; y++)
2134 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2135 if (p == (
const Quantum *) NULL)
2137 for (x=0; x < (ssize_t) image->columns; x++)
2139 if (GetPixelReadMask(image,p) <= (QuantumRange/2))
2141 p+=(ptrdiff_t) GetPixelChannels(image);
2144 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2149 PixelChannel channel = GetPixelChannelChannel(image,i);
2150 PixelTrait traits = GetPixelChannelTraits(image,channel);
2151 if (traits == UndefinedPixelTrait)
2153 cs=channel_statistics+channel;
2154 if (cs->depth != MAGICKCORE_QUANTUM_DEPTH)
2157 range=GetQuantumRange(depth);
2158 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),range) ?
2159 MagickTrue : MagickFalse;
2160 if (status != MagickFalse)
2163 if (cs->depth > channel_statistics[CompositePixelChannel].depth)
2164 channel_statistics[CompositePixelChannel].depth=cs->depth;
2170 if ((
double) p[i] < cs->minima)
2171 cs->minima=(double) p[i];
2172 if ((
double) p[i] > cs->maxima)
2173 cs->maxima=(double) p[i];
2174 histogram[(ssize_t) GetPixelChannels(image)*ScaleQuantumToMap(
2175 ClampToQuantum((
double) p[i]))+i]++;
2176 cs->sumLD+=(
long double) p[i];
2182 cs->sum_squared+=(double) p[i]*(
double) p[i];
2183 cs->sum_cubed+=(double) p[i]*(
double) p[i]*(double) p[i];
2184 cs->sum_fourth_power+=(double) p[i]*(
double) p[i]*(double) p[i]*
2199 delta=(double) p[i]-cs->M1;
2201 delta_n2=delta_n*delta_n;
2202 term1=delta*delta_n*n1;
2203 cs->M4+=term1*delta_n2*(n*n-3.0*n+3.0)+6.0*delta_n2*cs->M2-4.0*
2205 cs->M3+=term1*delta_n*(n-2.0)-3.0*delta_n*cs->M2;
2210 p+=(ptrdiff_t) GetPixelChannels(image);
2213 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2218 PixelChannel channel = GetPixelChannelChannel(image,i);
2219 PixelTrait traits = GetPixelChannelTraits(image,channel);
2220 double AdjArea = 1.0;
2221 if (traits == UndefinedPixelTrait)
2223 cs=channel_statistics+channel;
2227 cs->mean=cs->sumLD/(
long double) cs->area;
2229 AdjArea=cs->area/(cs->area-1.0);
2231 cs->sum=(double) cs->sum;
2234 cs->standard_deviation=0.0;
2242 cs->standard_deviation=sqrtl(cs->M2/((
long double) cs->area-1.0));
2244 cs->standard_deviation=sqrtl(cs->M2/((
long double) cs->area));
2245 cs->variance=cs->standard_deviation*cs->standard_deviation;
2246 cs->skewness=sqrtl(cs->area)*cs->M3/powl(cs->M2*AdjArea,1.5);
2247 cs->kurtosis=cs->area*cs->M4/(cs->M2*cs->M2*AdjArea*AdjArea)-3.0;
2251 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2259 PixelChannel channel = GetPixelChannelChannel(image,i);
2264 cs->sum_squared/=cs->area;
2265 cs->sum_cubed/=cs->area;
2266 cs->sum_fourth_power/=cs->area;
2272 for (j=0; j <= (ssize_t) MaxMap; j++)
2273 if (histogram[(ssize_t) GetPixelChannels(image)*j+i] > 0.0)
2275 area=PerceptibleReciprocalLD(channel_statistics[channel].area);
2276 for (j=0; j <= (ssize_t) MaxMap; j++)
2281 count=area*histogram[(ssize_t) GetPixelChannels(image)*j+i];
2282 channel_statistics[channel].entropy+=((
long double) -count*
2283 MagickLog10(count)*PerceptibleReciprocalLD((
long double)
2284 MagickLog10(number_bins)));
2285 channel_statistics[CompositePixelChannel].entropy+=((
long double) -count*
2286 MagickLog10(count)*PerceptibleReciprocalLD((
long double)
2287 MagickLog10(number_bins))/GetPixelChannels(image));
2290 histogram=(
double *) RelinquishMagickMemory(histogram);
2291 median_info=AcquireVirtualMemory(image->columns,image->rows*
sizeof(*median));
2293 (void) ThrowMagickException(exception,GetMagickModule(),
2294 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2297 median=(Quantum *) GetVirtualMemoryBlob(median_info);
2298 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2306 PixelChannel channel = GetPixelChannelChannel(image,i);
2307 PixelTrait traits = GetPixelChannelTraits(image,channel);
2308 if (traits == UndefinedPixelTrait)
2310 if ((traits & UpdatePixelTrait) == 0)
2312 for (y=0; y < (ssize_t) image->rows; y++)
2320 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2321 if (p == (
const Quantum *) NULL)
2323 for (x=0; x < (ssize_t) image->columns; x++)
2325 if (GetPixelReadMask(image,p) <= (QuantumRange/2))
2327 p+=(ptrdiff_t) GetPixelChannels(image);
2331 p+=(ptrdiff_t) GetPixelChannels(image);
2334 channel_statistics[channel].median=(double) median[
2335 GetMedianPixel(median,n)];
2337 median_info=RelinquishVirtualMemory(median_info);
2342 csComp->sum_squared=0.0;
2343 csComp->sum_cubed=0.0;
2344 csComp->sum_fourth_power=0.0;
2345 csComp->maxima=(-MagickMaximumValue);
2346 csComp->minima=MagickMaximumValue;
2350 csComp->variance=0.0;
2351 csComp->standard_deviation=0.0;
2352 csComp->entropy=0.0;
2353 csComp->skewness=0.0;
2354 csComp->kurtosis=0.0;
2355 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2360 PixelChannel channel = GetPixelChannelChannel(image,i);
2361 PixelTrait traits = GetPixelChannelTraits(image,channel);
2362 if (traits == UndefinedPixelTrait)
2364 if ((traits & UpdatePixelTrait) == 0)
2366 cs=channel_statistics+channel;
2367 if (csComp->maxima < cs->maxima)
2368 csComp->maxima=cs->maxima;
2369 if (csComp->minima > cs->minima)
2370 csComp->minima=cs->minima;
2371 csComp->sum+=cs->sum;
2372 csComp->sum_squared+=cs->sum_squared;
2373 csComp->sum_cubed+=cs->sum_cubed;
2374 csComp->sum_fourth_power+=cs->sum_fourth_power;
2375 csComp->median+=cs->median;
2376 csComp->area+=cs->area;
2377 csComp->mean+=cs->mean;
2378 csComp->variance+=cs->variance;
2379 csComp->standard_deviation+=cs->standard_deviation;
2380 csComp->skewness+=cs->skewness;
2381 csComp->kurtosis+=cs->kurtosis;
2382 csComp->entropy+=cs->entropy;
2384 channels=(double) GetImageChannels(image);
2385 csComp->sum/=channels;
2386 csComp->sum_squared/=channels;
2387 csComp->sum_cubed/=channels;
2388 csComp->sum_fourth_power/=channels;
2389 csComp->median/=channels;
2390 csComp->area/=channels;
2391 csComp->mean/=channels;
2392 csComp->variance/=channels;
2393 csComp->standard_deviation/=channels;
2394 csComp->skewness/=channels;
2395 csComp->kurtosis/=channels;
2396 csComp->entropy/=channels;
2398 if (y < (ssize_t) image->rows)
2400 channel_statistics);
2401 return(channel_statistics);
2437MagickExport
Image *PolynomialImage(
const Image *images,
2438 const size_t number_terms,
const double *terms,
ExceptionInfo *exception)
2440#define PolynomialImageTag "Polynomial/Image"
2455 **magick_restrict polynomial_pixels;
2463 assert(images != (
Image *) NULL);
2464 assert(images->signature == MagickCoreSignature);
2465 if (IsEventLogging() != MagickFalse)
2466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",images->filename);
2468 assert(exception->signature == MagickCoreSignature);
2469 image=AcquireImageCanvas(images,exception);
2470 if (image == (
Image *) NULL)
2471 return((
Image *) NULL);
2472 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2474 image=DestroyImage(image);
2475 return((
Image *) NULL);
2477 number_images=GetImageListLength(images);
2478 polynomial_pixels=AcquirePixelTLS(images);
2481 image=DestroyImage(image);
2482 (void) ThrowMagickException(exception,GetMagickModule(),
2483 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2484 return((
Image *) NULL);
2491 polynomial_view=AcquireAuthenticCacheView(image,exception);
2492#if defined(MAGICKCORE_OPENMP_SUPPORT)
2493 #pragma omp parallel for schedule(static) shared(progress,status) \
2494 magick_number_threads(image,image,image->rows,1)
2496 for (y=0; y < (ssize_t) image->rows; y++)
2505 id = GetOpenMPThreadId();
2518 if (status == MagickFalse)
2520 q=QueueCacheViewAuthenticPixels(polynomial_view,0,y,image->columns,1,
2522 if (q == (Quantum *) NULL)
2527 polynomial_pixel=polynomial_pixels[id];
2528 for (j=0; j < (ssize_t) image->columns; j++)
2529 for (i=0; i < MaxPixelChannels; i++)
2530 polynomial_pixel[j].channel[i]=0.0;
2532 for (j=0; j < (ssize_t) number_images; j++)
2537 if (j >= (ssize_t) number_terms)
2539 image_view=AcquireVirtualCacheView(next,exception);
2540 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2541 if (p == (
const Quantum *) NULL)
2543 image_view=DestroyCacheView(image_view);
2546 for (x=0; x < (ssize_t) image->columns; x++)
2548 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
2554 PixelChannel channel = GetPixelChannelChannel(image,i);
2555 PixelTrait traits = GetPixelChannelTraits(next,channel);
2556 PixelTrait polynomial_traits=GetPixelChannelTraits(image,channel);
2557 if ((traits == UndefinedPixelTrait) ||
2558 (polynomial_traits == UndefinedPixelTrait))
2560 if ((traits & UpdatePixelTrait) == 0)
2562 coefficient=(MagickRealType) terms[2*j];
2563 degree=(MagickRealType) terms[(j << 1)+1];
2564 polynomial_pixel[x].channel[i]+=coefficient*
2565 pow(QuantumScale*(
double) GetPixelChannel(image,channel,p),degree);
2567 p+=(ptrdiff_t) GetPixelChannels(next);
2569 image_view=DestroyCacheView(image_view);
2570 next=GetNextImageInList(next);
2572 for (x=0; x < (ssize_t) image->columns; x++)
2574 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2576 PixelChannel channel = GetPixelChannelChannel(image,i);
2577 PixelTrait traits = GetPixelChannelTraits(image,channel);
2578 if (traits == UndefinedPixelTrait)
2580 if ((traits & UpdatePixelTrait) == 0)
2582 q[i]=ClampToQuantum((
double) QuantumRange*
2583 polynomial_pixel[x].channel[i]);
2585 q+=(ptrdiff_t) GetPixelChannels(image);
2587 if (SyncCacheViewAuthenticPixels(polynomial_view,exception) == MagickFalse)
2589 if (images->progress_monitor != (MagickProgressMonitor) NULL)
2594#if defined(MAGICKCORE_OPENMP_SUPPORT)
2598 proceed=SetImageProgress(images,PolynomialImageTag,progress,
2600 if (proceed == MagickFalse)
2604 polynomial_view=DestroyCacheView(polynomial_view);
2605 polynomial_pixels=DestroyPixelTLS(images,polynomial_pixels);
2606 if (status == MagickFalse)
2607 image=DestroyImage(image);
2678 if (pixel_list->skip_list.nodes != (
SkipNode *) NULL)
2679 pixel_list->skip_list.nodes=(
SkipNode *) RelinquishAlignedMemory(
2680 pixel_list->skip_list.nodes);
2681 pixel_list=(
PixelList *) RelinquishMagickMemory(pixel_list);
2690 assert(pixel_list != (
PixelList **) NULL);
2691 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2692 if (pixel_list[i] != (
PixelList *) NULL)
2693 pixel_list[i]=DestroyPixelList(pixel_list[i]);
2694 pixel_list=(
PixelList **) RelinquishMagickMemory(pixel_list);
2698static PixelList *AcquirePixelList(
const size_t width,
const size_t height)
2703 pixel_list=(
PixelList *) AcquireMagickMemory(
sizeof(*pixel_list));
2706 (void) memset((
void *) pixel_list,0,
sizeof(*pixel_list));
2707 pixel_list->length=width*height;
2708 pixel_list->skip_list.nodes=(
SkipNode *) AcquireAlignedMemory(65537UL,
2709 sizeof(*pixel_list->skip_list.nodes));
2710 if (pixel_list->skip_list.nodes == (
SkipNode *) NULL)
2711 return(DestroyPixelList(pixel_list));
2712 (void) memset(pixel_list->skip_list.nodes,0,65537UL*
2713 sizeof(*pixel_list->skip_list.nodes));
2714 pixel_list->signature=MagickCoreSignature;
2718static PixelList **AcquirePixelListTLS(
const size_t width,
2719 const size_t height)
2730 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2731 pixel_list=(
PixelList **) AcquireQuantumMemory(number_threads,
2732 sizeof(*pixel_list));
2735 (void) memset(pixel_list,0,number_threads*
sizeof(*pixel_list));
2736 for (i=0; i < (ssize_t) number_threads; i++)
2738 pixel_list[i]=AcquirePixelList(width,height);
2739 if (pixel_list[i] == (
PixelList *) NULL)
2740 return(DestroyPixelListTLS(pixel_list));
2745static void AddNodePixelList(
PixelList *pixel_list,
const size_t color)
2760 p=(&pixel_list->skip_list);
2761 p->nodes[color].signature=pixel_list->signature;
2762 p->nodes[color].count=1;
2767 (void) memset(update,0,
sizeof(update));
2768 for (level=p->level; level >= 0; level--)
2770 while (p->nodes[search].next[level] < color)
2771 search=p->nodes[search].next[level];
2772 update[level]=search;
2777 for (level=0; ; level++)
2779 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
2780 if ((pixel_list->seed & 0x300) != 0x300)
2785 if (level > (p->level+2))
2790 while (level > p->level)
2793 update[p->level]=65536UL;
2800 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
2801 p->nodes[update[level]].next[level]=color;
2802 }
while (level-- > 0);
2805static inline void GetMedianPixelList(
PixelList *pixel_list,Quantum *pixel)
2819 p=(&pixel_list->skip_list);
2824 color=p->nodes[color].next[0];
2825 count+=(ssize_t) p->nodes[color].count;
2826 }
while (count <= (ssize_t) (pixel_list->length >> 1));
2827 *pixel=ScaleShortToQuantum((
unsigned short) color);
2830static inline void GetModePixelList(
PixelList *pixel_list,Quantum *pixel)
2846 p=(&pixel_list->skip_list);
2849 max_count=p->nodes[mode].count;
2853 color=p->nodes[color].next[0];
2854 if (p->nodes[color].count > max_count)
2857 max_count=p->nodes[mode].count;
2859 count+=(ssize_t) p->nodes[color].count;
2860 }
while (count < (ssize_t) pixel_list->length);
2861 *pixel=ScaleShortToQuantum((
unsigned short) mode);
2864static inline void GetNonpeakPixelList(
PixelList *pixel_list,Quantum *pixel)
2880 p=(&pixel_list->skip_list);
2882 next=p->nodes[color].next[0];
2888 next=p->nodes[color].next[0];
2889 count+=(ssize_t) p->nodes[color].count;
2890 }
while (count <= (ssize_t) (pixel_list->length >> 1));
2891 if ((previous == 65536UL) && (next != 65536UL))
2894 if ((previous != 65536UL) && (next == 65536UL))
2896 *pixel=ScaleShortToQuantum((
unsigned short) color);
2899static inline void InsertPixelList(
const Quantum pixel,
PixelList *pixel_list)
2907 index=ScaleQuantumToShort(pixel);
2908 signature=pixel_list->skip_list.nodes[index].signature;
2909 if (signature == pixel_list->signature)
2911 pixel_list->skip_list.nodes[index].count++;
2914 AddNodePixelList(pixel_list,index);
2917static void ResetPixelList(
PixelList *pixel_list)
2931 p=(&pixel_list->skip_list);
2932 root=p->nodes+65536UL;
2934 for (level=0; level < 9; level++)
2935 root->next[level]=65536UL;
2936 pixel_list->seed=pixel_list->signature++;
2939MagickExport
Image *StatisticImage(
const Image *image,
const StatisticType type,
2940 const size_t width,
const size_t height,
ExceptionInfo *exception)
2942#define StatisticImageTag "Statistic/Image"
2958 **magick_restrict pixel_list;
2967 assert(image != (
Image *) NULL);
2968 assert(image->signature == MagickCoreSignature);
2969 if (IsEventLogging() != MagickFalse)
2970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2972 assert(exception->signature == MagickCoreSignature);
2973 statistic_image=CloneImage(image,0,0,MagickTrue,
2975 if (statistic_image == (
Image *) NULL)
2976 return((
Image *) NULL);
2977 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2978 if (status == MagickFalse)
2980 statistic_image=DestroyImage(statistic_image);
2981 return((
Image *) NULL);
2983 pixel_list=AcquirePixelListTLS(MagickMax(width,1),MagickMax(height,1));
2986 statistic_image=DestroyImage(statistic_image);
2987 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
2992 center=(ssize_t) GetPixelChannels(image)*((ssize_t) image->columns+
2993 MagickMax((ssize_t) width,1L))*(MagickMax((ssize_t) height,1)/2L)+(ssize_t)
2994 GetPixelChannels(image)*(MagickMax((ssize_t) width,1L)/2L);
2997 image_view=AcquireVirtualCacheView(image,exception);
2998 statistic_view=AcquireAuthenticCacheView(statistic_image,exception);
2999#if defined(MAGICKCORE_OPENMP_SUPPORT)
3000 #pragma omp parallel for schedule(static) shared(progress,status) \
3001 magick_number_threads(image,statistic_image,statistic_image->rows,1)
3003 for (y=0; y < (ssize_t) statistic_image->rows; y++)
3006 id = GetOpenMPThreadId();
3017 if (status == MagickFalse)
3019 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
3020 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
3021 MagickMax(height,1),exception);
3022 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
3023 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3028 for (x=0; x < (ssize_t) statistic_image->columns; x++)
3033 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3046 *magick_restrict pixels;
3054 PixelChannel channel = GetPixelChannelChannel(image,i);
3055 PixelTrait traits = GetPixelChannelTraits(image,channel);
3056 PixelTrait statistic_traits=GetPixelChannelTraits(statistic_image,
3058 if ((traits == UndefinedPixelTrait) ||
3059 (statistic_traits == UndefinedPixelTrait))
3061 if (((statistic_traits & CopyPixelTrait) != 0) ||
3062 (GetPixelWriteMask(image,p) <= (QuantumRange/2)))
3064 SetPixelChannel(statistic_image,channel,p[center+i],q);
3067 if ((statistic_traits & UpdatePixelTrait) == 0)
3075 ResetPixelList(pixel_list[
id]);
3076 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
3078 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
3080 if ((type == MedianStatistic) || (type == ModeStatistic) ||
3081 (type == NonpeakStatistic))
3083 InsertPixelList(pixels[i],pixel_list[
id]);
3084 pixels+=(ptrdiff_t) GetPixelChannels(image);
3088 if ((
double) pixels[i] < minimum)
3089 minimum=(double) pixels[i];
3090 if ((
double) pixels[i] > maximum)
3091 maximum=(double) pixels[i];
3092 sum+=(double) pixels[i];
3093 sum_squared+=(double) pixels[i]*(
double) pixels[i];
3094 pixels+=(ptrdiff_t) GetPixelChannels(image);
3096 pixels+=(ptrdiff_t) GetPixelChannels(image)*image->columns;
3100 case ContrastStatistic:
3102 pixel=ClampToQuantum(MagickAbsoluteValue((maximum-minimum)*
3103 PerceptibleReciprocal(maximum+minimum)));
3106 case GradientStatistic:
3108 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
3111 case MaximumStatistic:
3113 pixel=ClampToQuantum(maximum);
3119 pixel=ClampToQuantum(sum/area);
3122 case MedianStatistic:
3124 GetMedianPixelList(pixel_list[
id],&pixel);
3127 case MinimumStatistic:
3129 pixel=ClampToQuantum(minimum);
3134 GetModePixelList(pixel_list[
id],&pixel);
3137 case NonpeakStatistic:
3139 GetNonpeakPixelList(pixel_list[
id],&pixel);
3142 case RootMeanSquareStatistic:
3144 pixel=ClampToQuantum(sqrt(sum_squared/area));
3147 case StandardDeviationStatistic:
3149 pixel=ClampToQuantum(sqrt(sum_squared/area-(sum/area*sum/area)));
3153 SetPixelChannel(statistic_image,channel,pixel,q);
3155 p+=(ptrdiff_t) GetPixelChannels(image);
3156 q+=(ptrdiff_t) GetPixelChannels(statistic_image);
3158 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
3160 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3165#if defined(MAGICKCORE_OPENMP_SUPPORT)
3169 proceed=SetImageProgress(image,StatisticImageTag,progress,image->rows);
3170 if (proceed == MagickFalse)
3174 statistic_view=DestroyCacheView(statistic_view);
3175 image_view=DestroyCacheView(image_view);
3176 pixel_list=DestroyPixelListTLS(pixel_list);
3177 if (status == MagickFalse)
3178 statistic_image=DestroyImage(statistic_image);
3179 return(statistic_image);