43#include "MagickCore/studio.h"
44#include "MagickCore/artifact.h"
45#include "MagickCore/attribute.h"
46#include "MagickCore/cache-view.h"
47#include "MagickCore/channel.h"
48#include "MagickCore/client.h"
49#include "MagickCore/color.h"
50#include "MagickCore/color-private.h"
51#include "MagickCore/colorspace.h"
52#include "MagickCore/colorspace-private.h"
53#include "MagickCore/compare.h"
54#include "MagickCore/composite-private.h"
55#include "MagickCore/constitute.h"
56#include "MagickCore/distort.h"
57#include "MagickCore/exception-private.h"
58#include "MagickCore/enhance.h"
59#include "MagickCore/fourier.h"
60#include "MagickCore/geometry.h"
61#include "MagickCore/image-private.h"
62#include "MagickCore/list.h"
63#include "MagickCore/log.h"
64#include "MagickCore/memory_.h"
65#include "MagickCore/monitor.h"
66#include "MagickCore/monitor-private.h"
67#include "MagickCore/option.h"
68#include "MagickCore/pixel-accessor.h"
69#include "MagickCore/property.h"
70#include "MagickCore/registry.h"
71#include "MagickCore/resource_.h"
72#include "MagickCore/string_.h"
73#include "MagickCore/statistic.h"
74#include "MagickCore/statistic-private.h"
75#include "MagickCore/string-private.h"
76#include "MagickCore/thread-private.h"
77#include "MagickCore/threshold.h"
78#include "MagickCore/transform.h"
79#include "MagickCore/utility.h"
80#include "MagickCore/version.h"
114MagickExport
Image *CompareImages(
Image *image,
const Image *reconstruct_image,
115 const MetricType metric,
double *distortion,
ExceptionInfo *exception)
151 assert(image != (
Image *) NULL);
152 assert(image->signature == MagickCoreSignature);
153 assert(reconstruct_image != (
const Image *) NULL);
154 assert(reconstruct_image->signature == MagickCoreSignature);
155 assert(distortion != (
double *) NULL);
156 if (IsEventLogging() != MagickFalse)
157 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
159 status=GetImageDistortion(image,reconstruct_image,metric,distortion,
161 if (status == MagickFalse)
162 return((
Image *) NULL);
163 columns=MagickMax(image->columns,reconstruct_image->columns);
164 rows=MagickMax(image->rows,reconstruct_image->rows);
165 SetGeometry(image,&geometry);
166 geometry.width=columns;
167 geometry.height=rows;
168 clone_image=CloneImage(image,0,0,MagickTrue,exception);
169 if (clone_image == (
Image *) NULL)
170 return((
Image *) NULL);
171 (void) SetImageMask(clone_image,ReadPixelMask,(
Image *) NULL,exception);
172 difference_image=ExtentImage(clone_image,&geometry,exception);
173 clone_image=DestroyImage(clone_image);
174 if (difference_image == (
Image *) NULL)
175 return((
Image *) NULL);
176 (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel,exception);
177 highlight_image=CloneImage(image,columns,rows,MagickTrue,exception);
178 if (highlight_image == (
Image *) NULL)
180 difference_image=DestroyImage(difference_image);
181 return((
Image *) NULL);
183 status=SetImageStorageClass(highlight_image,DirectClass,exception);
184 if (status == MagickFalse)
186 difference_image=DestroyImage(difference_image);
187 highlight_image=DestroyImage(highlight_image);
188 return((
Image *) NULL);
190 (void) SetImageMask(highlight_image,ReadPixelMask,(
Image *) NULL,exception);
191 (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel,exception);
192 (void) QueryColorCompliance(
"#f1001ecc",AllCompliance,&highlight,exception);
193 artifact=GetImageArtifact(image,
"compare:highlight-color");
194 if (artifact != (
const char *) NULL)
195 (void) QueryColorCompliance(artifact,AllCompliance,&highlight,exception);
196 (void) QueryColorCompliance(
"#ffffffcc",AllCompliance,&lowlight,exception);
197 artifact=GetImageArtifact(image,
"compare:lowlight-color");
198 if (artifact != (
const char *) NULL)
199 (void) QueryColorCompliance(artifact,AllCompliance,&lowlight,exception);
200 (void) QueryColorCompliance(
"#888888cc",AllCompliance,&masklight,exception);
201 artifact=GetImageArtifact(image,
"compare:masklight-color");
202 if (artifact != (
const char *) NULL)
203 (void) QueryColorCompliance(artifact,AllCompliance,&masklight,exception);
208 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
209 image_view=AcquireVirtualCacheView(image,exception);
210 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
211 highlight_view=AcquireAuthenticCacheView(highlight_image,exception);
212#if defined(MAGICKCORE_OPENMP_SUPPORT)
213 #pragma omp parallel for schedule(static) shared(status) \
214 magick_number_threads(image,highlight_image,rows,1)
216 for (y=0; y < (ssize_t) rows; y++)
231 if (status == MagickFalse)
233 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
234 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
235 r=QueueCacheViewAuthenticPixels(highlight_view,0,y,columns,1,exception);
236 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL) ||
237 (r == (Quantum *) NULL))
242 for (x=0; x < (ssize_t) columns; x++)
254 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
255 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
257 SetPixelViaPixelInfo(highlight_image,&masklight,r);
258 p+=(ptrdiff_t) GetPixelChannels(image);
259 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
260 r+=(ptrdiff_t) GetPixelChannels(highlight_image);
263 difference=MagickFalse;
264 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
265 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
266 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
272 PixelChannel channel = GetPixelChannelChannel(image,i);
273 PixelTrait traits = GetPixelChannelTraits(image,channel);
274 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
276 if ((traits == UndefinedPixelTrait) ||
277 (reconstruct_traits == UndefinedPixelTrait) ||
278 ((reconstruct_traits & UpdatePixelTrait) == 0))
280 if (channel == AlphaPixelChannel)
281 pixel=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
284 pixel=Sa*(double) p[i]-Da*(
double)
285 GetPixelChannel(reconstruct_image,channel,q);
286 distance=pixel*pixel;
287 if (distance >= fuzz)
289 difference=MagickTrue;
293 if (difference == MagickFalse)
294 SetPixelViaPixelInfo(highlight_image,&lowlight,r);
296 SetPixelViaPixelInfo(highlight_image,&highlight,r);
297 p+=(ptrdiff_t) GetPixelChannels(image);
298 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
299 r+=(ptrdiff_t) GetPixelChannels(highlight_image);
301 sync=SyncCacheViewAuthenticPixels(highlight_view,exception);
302 if (sync == MagickFalse)
305 highlight_view=DestroyCacheView(highlight_view);
306 reconstruct_view=DestroyCacheView(reconstruct_view);
307 image_view=DestroyCacheView(image_view);
308 (void) CompositeImage(difference_image,highlight_image,image->compose,
309 MagickTrue,0,0,exception);
310 highlight_image=DestroyImage(highlight_image);
311 if (status == MagickFalse)
312 difference_image=DestroyImage(difference_image);
313 return(difference_image);
350static MagickBooleanType GetAbsoluteDistortion(
const Image *image,
374 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
375 rows=MagickMax(image->rows,reconstruct_image->rows);
376 columns=MagickMax(image->columns,reconstruct_image->columns);
377 image_view=AcquireVirtualCacheView(image,exception);
378 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
379#if defined(MAGICKCORE_OPENMP_SUPPORT)
380 #pragma omp parallel for schedule(static) shared(status) \
381 magick_number_threads(image,image,rows,1)
383 for (y=0; y < (ssize_t) rows; y++)
386 channel_distortion[MaxPixelChannels+1];
396 if (status == MagickFalse)
398 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
399 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
400 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
405 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
406 for (x=0; x < (ssize_t) columns; x++)
418 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
419 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
421 p+=(ptrdiff_t) GetPixelChannels(image);
422 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
425 difference=MagickFalse;
426 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
427 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
428 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
434 PixelChannel channel = GetPixelChannelChannel(image,i);
435 PixelTrait traits = GetPixelChannelTraits(image,channel);
436 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
438 if ((traits == UndefinedPixelTrait) ||
439 (reconstruct_traits == UndefinedPixelTrait) ||
440 ((reconstruct_traits & UpdatePixelTrait) == 0))
442 if (channel == AlphaPixelChannel)
443 pixel=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
446 pixel=Sa*(double) p[i]-Da*(
double) GetPixelChannel(reconstruct_image,
448 distance=pixel*pixel;
449 if (distance >= fuzz)
451 channel_distortion[i]++;
452 difference=MagickTrue;
455 if (difference != MagickFalse)
456 channel_distortion[CompositePixelChannel]++;
457 p+=(ptrdiff_t) GetPixelChannels(image);
458 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
460#if defined(MAGICKCORE_OPENMP_SUPPORT)
461 #pragma omp critical (MagickCore_GetAbsoluteDistortion)
463 for (j=0; j <= MaxPixelChannels; j++)
464 distortion[j]+=channel_distortion[j];
466 reconstruct_view=DestroyCacheView(reconstruct_view);
467 image_view=DestroyCacheView(image_view);
471static MagickBooleanType GetFuzzDistortion(
const Image *image,
495 rows=MagickMax(image->rows,reconstruct_image->rows);
496 columns=MagickMax(image->columns,reconstruct_image->columns);
498 image_view=AcquireVirtualCacheView(image,exception);
499 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
500#if defined(MAGICKCORE_OPENMP_SUPPORT)
501 #pragma omp parallel for schedule(static) shared(status) \
502 magick_number_threads(image,image,rows,1)
504 for (y=0; y < (ssize_t) rows; y++)
507 channel_distortion[MaxPixelChannels+1];
519 if (status == MagickFalse)
521 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
522 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
523 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
528 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
529 for (x=0; x < (ssize_t) columns; x++)
538 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
539 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
541 p+=(ptrdiff_t) GetPixelChannels(image);
542 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
545 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
546 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
547 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
552 PixelChannel channel = GetPixelChannelChannel(image,i);
553 PixelTrait traits = GetPixelChannelTraits(image,channel);
554 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
556 if ((traits == UndefinedPixelTrait) ||
557 (reconstruct_traits == UndefinedPixelTrait) ||
558 ((reconstruct_traits & UpdatePixelTrait) == 0))
560 if (channel == AlphaPixelChannel)
561 distance=QuantumScale*((double) p[i]-(double) GetPixelChannel(
562 reconstruct_image,channel,q));
564 distance=QuantumScale*(Sa*(double) p[i]-Da*(double) GetPixelChannel(
565 reconstruct_image,channel,q));
566 channel_distortion[i]+=distance*distance;
567 channel_distortion[CompositePixelChannel]+=distance*distance;
570 p+=(ptrdiff_t) GetPixelChannels(image);
571 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
573#if defined(MAGICKCORE_OPENMP_SUPPORT)
574 #pragma omp critical (MagickCore_GetFuzzDistortion)
578 for (j=0; j <= MaxPixelChannels; j++)
579 distortion[j]+=channel_distortion[j];
582 reconstruct_view=DestroyCacheView(reconstruct_view);
583 image_view=DestroyCacheView(image_view);
584 area=PerceptibleReciprocal(area);
585 for (j=0; j <= MaxPixelChannels; j++)
587 distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
588 distortion[CompositePixelChannel]=sqrt(distortion[CompositePixelChannel]);
592static MagickBooleanType GetMeanAbsoluteDistortion(
const Image *image,
616 rows=MagickMax(image->rows,reconstruct_image->rows);
617 columns=MagickMax(image->columns,reconstruct_image->columns);
619 image_view=AcquireVirtualCacheView(image,exception);
620 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
621#if defined(MAGICKCORE_OPENMP_SUPPORT)
622 #pragma omp parallel for schedule(static) shared(status) \
623 magick_number_threads(image,image,rows,1)
625 for (y=0; y < (ssize_t) rows; y++)
628 channel_distortion[MaxPixelChannels+1];
640 if (status == MagickFalse)
642 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
643 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
644 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
649 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
650 for (x=0; x < (ssize_t) columns; x++)
659 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
660 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
662 p+=(ptrdiff_t) GetPixelChannels(image);
663 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
666 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
667 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
668 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
673 PixelChannel channel = GetPixelChannelChannel(image,i);
674 PixelTrait traits = GetPixelChannelTraits(image,channel);
675 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
677 if ((traits == UndefinedPixelTrait) ||
678 (reconstruct_traits == UndefinedPixelTrait) ||
679 ((reconstruct_traits & UpdatePixelTrait) == 0))
681 if (channel == AlphaPixelChannel)
682 distance=QuantumScale*fabs((
double) p[i]-(
double)
683 GetPixelChannel(reconstruct_image,channel,q));
685 distance=QuantumScale*fabs(Sa*(
double) p[i]-Da*(
double)
686 GetPixelChannel(reconstruct_image,channel,q));
687 channel_distortion[i]+=distance;
688 channel_distortion[CompositePixelChannel]+=distance;
691 p+=(ptrdiff_t) GetPixelChannels(image);
692 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
694#if defined(MAGICKCORE_OPENMP_SUPPORT)
695 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
699 for (j=0; j <= MaxPixelChannels; j++)
700 distortion[j]+=channel_distortion[j];
703 reconstruct_view=DestroyCacheView(reconstruct_view);
704 image_view=DestroyCacheView(image_view);
705 area=PerceptibleReciprocal(area);
706 for (j=0; j <= MaxPixelChannels; j++)
708 distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
712static MagickBooleanType GetMeanErrorPerPixel(
Image *image,
738 rows=MagickMax(image->rows,reconstruct_image->rows);
739 columns=MagickMax(image->columns,reconstruct_image->columns);
740 image_view=AcquireVirtualCacheView(image,exception);
741 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
742 for (y=0; y < (ssize_t) rows; y++)
751 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
752 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
753 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
758 for (x=0; x < (ssize_t) columns; x++)
767 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
768 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
770 p+=(ptrdiff_t) GetPixelChannels(image);
771 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
774 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
775 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
776 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
781 PixelChannel channel = GetPixelChannelChannel(image,i);
782 PixelTrait traits = GetPixelChannelTraits(image,channel);
783 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
785 if ((traits == UndefinedPixelTrait) ||
786 (reconstruct_traits == UndefinedPixelTrait) ||
787 ((reconstruct_traits & UpdatePixelTrait) == 0))
789 if (channel == AlphaPixelChannel)
790 distance=fabs((
double) p[i]-(
double)
791 GetPixelChannel(reconstruct_image,channel,q));
793 distance=fabs(Sa*(
double) p[i]-Da*(
double)
794 GetPixelChannel(reconstruct_image,channel,q));
795 distortion[i]+=distance;
796 distortion[CompositePixelChannel]+=distance;
797 mean_error+=distance*distance;
798 if (distance > maximum_error)
799 maximum_error=distance;
802 p+=(ptrdiff_t) GetPixelChannels(image);
803 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
806 reconstruct_view=DestroyCacheView(reconstruct_view);
807 image_view=DestroyCacheView(image_view);
808 area=PerceptibleReciprocal(area);
809 image->error.mean_error_per_pixel=area*distortion[CompositePixelChannel];
810 image->error.normalized_mean_error=area*QuantumScale*QuantumScale*mean_error;
811 image->error.normalized_maximum_error=QuantumScale*maximum_error;
815static MagickBooleanType GetMeanSquaredDistortion(
const Image *image,
837 rows=MagickMax(image->rows,reconstruct_image->rows);
838 columns=MagickMax(image->columns,reconstruct_image->columns);
840 image_view=AcquireVirtualCacheView(image,exception);
841 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
842#if defined(MAGICKCORE_OPENMP_SUPPORT)
843 #pragma omp parallel for schedule(static) shared(status) \
844 magick_number_threads(image,image,rows,1)
846 for (y=0; y < (ssize_t) rows; y++)
853 channel_distortion[MaxPixelChannels+1];
861 if (status == MagickFalse)
863 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
864 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
865 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
870 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
871 for (x=0; x < (ssize_t) columns; x++)
880 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
881 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
883 p+=(ptrdiff_t) GetPixelChannels(image);
884 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
887 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
888 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
889 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
894 PixelChannel channel = GetPixelChannelChannel(image,i);
895 PixelTrait traits = GetPixelChannelTraits(image,channel);
896 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
898 if ((traits == UndefinedPixelTrait) ||
899 (reconstruct_traits == UndefinedPixelTrait) ||
900 ((reconstruct_traits & UpdatePixelTrait) == 0))
902 if (channel == AlphaPixelChannel)
903 distance=QuantumScale*((double) p[i]-(double) GetPixelChannel(
904 reconstruct_image,channel,q));
906 distance=QuantumScale*(Sa*(double) p[i]-Da*(double) GetPixelChannel(
907 reconstruct_image,channel,q));
908 channel_distortion[i]+=distance*distance;
909 channel_distortion[CompositePixelChannel]+=distance*distance;
912 p+=(ptrdiff_t) GetPixelChannels(image);
913 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
915#if defined(MAGICKCORE_OPENMP_SUPPORT)
916 #pragma omp critical (MagickCore_GetMeanSquaredError)
920 for (j=0; j <= MaxPixelChannels; j++)
921 distortion[j]+=channel_distortion[j];
924 reconstruct_view=DestroyCacheView(reconstruct_view);
925 image_view=DestroyCacheView(image_view);
926 area=PerceptibleReciprocal(area);
927 for (j=0; j <= MaxPixelChannels; j++)
929 distortion[CompositePixelChannel]/=GetImageChannels(image);
933static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
934 const Image *image,
const Image *reconstruct_image,
double *distortion,
937#define SimilarityImageTag "Similarity/Image"
945 *reconstruct_statistics;
948 alpha_variance[MaxPixelChannels+1],
949 beta_variance[MaxPixelChannels+1];
968 image_statistics=GetImageStatistics(image,exception);
969 reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
978 reconstruct_statistics);
981 (void) memset(distortion,0,(MaxPixelChannels+1)*
sizeof(*distortion));
982 (void) memset(alpha_variance,0,(MaxPixelChannels+1)*
sizeof(*alpha_variance));
983 (void) memset(beta_variance,0,(MaxPixelChannels+1)*
sizeof(*beta_variance));
986 columns=MagickMax(image->columns,reconstruct_image->columns);
987 rows=MagickMax(image->rows,reconstruct_image->rows);
988 image_view=AcquireVirtualCacheView(image,exception);
989 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
990 for (y=0; y < (ssize_t) rows; y++)
999 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1000 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1001 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1006 for (x=0; x < (ssize_t) columns; x++)
1012 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1013 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1015 p+=(ptrdiff_t) GetPixelChannels(image);
1016 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1019 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1020 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1021 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1027 PixelChannel channel = GetPixelChannelChannel(image,i);
1028 PixelTrait traits = GetPixelChannelTraits(image,channel);
1029 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1031 if ((traits == UndefinedPixelTrait) ||
1032 (reconstruct_traits == UndefinedPixelTrait) ||
1033 ((reconstruct_traits & UpdatePixelTrait) == 0))
1035 if (channel == AlphaPixelChannel)
1037 alpha=QuantumScale*((double) p[i]-image_statistics[channel].mean);
1038 beta=QuantumScale*((double) GetPixelChannel(reconstruct_image,
1039 channel,q)-reconstruct_statistics[channel].mean);
1043 alpha=QuantumScale*(Sa*(double) p[i]-
1044 image_statistics[channel].mean);
1045 beta=QuantumScale*(Da*(double) GetPixelChannel(reconstruct_image,
1046 channel,q)-reconstruct_statistics[channel].mean);
1048 distortion[i]+=alpha*beta;
1049 alpha_variance[i]+=alpha*alpha;
1050 beta_variance[i]+=beta*beta;
1052 p+=(ptrdiff_t) GetPixelChannels(image);
1053 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1055 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1060#if defined(MAGICKCORE_OPENMP_SUPPORT)
1064 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
1065 if (proceed == MagickFalse)
1072 reconstruct_view=DestroyCacheView(reconstruct_view);
1073 image_view=DestroyCacheView(image_view);
1077 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1079 distortion[i]/=sqrt(alpha_variance[i]*beta_variance[i]);
1080 if (fabs(distortion[i]) > MagickEpsilon)
1081 distortion[CompositePixelChannel]+=distortion[i];
1083 distortion[CompositePixelChannel]=distortion[CompositePixelChannel]/
1084 GetImageChannels(image);
1089 reconstruct_statistics);
1095static MagickBooleanType GetPeakAbsoluteDistortion(
const Image *image,
1113 rows=MagickMax(image->rows,reconstruct_image->rows);
1114 columns=MagickMax(image->columns,reconstruct_image->columns);
1115 image_view=AcquireVirtualCacheView(image,exception);
1116 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1117#if defined(MAGICKCORE_OPENMP_SUPPORT)
1118 #pragma omp parallel for schedule(static) shared(status) \
1119 magick_number_threads(image,image,rows,1)
1121 for (y=0; y < (ssize_t) rows; y++)
1124 channel_distortion[MaxPixelChannels+1];
1134 if (status == MagickFalse)
1136 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1137 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1138 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1143 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
1144 for (x=0; x < (ssize_t) columns; x++)
1153 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1154 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1156 p+=(ptrdiff_t) GetPixelChannels(image);
1157 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1160 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1161 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1162 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1167 PixelChannel channel = GetPixelChannelChannel(image,i);
1168 PixelTrait traits = GetPixelChannelTraits(image,channel);
1169 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1171 if ((traits == UndefinedPixelTrait) ||
1172 (reconstruct_traits == UndefinedPixelTrait) ||
1173 ((reconstruct_traits & UpdatePixelTrait) == 0))
1175 if (channel == AlphaPixelChannel)
1176 distance=QuantumScale*fabs((
double) p[i]-(
double)
1177 GetPixelChannel(reconstruct_image,channel,q));
1179 distance=QuantumScale*fabs(Sa*(
double) p[i]-Da*(
double)
1180 GetPixelChannel(reconstruct_image,channel,q));
1181 if (distance > channel_distortion[i])
1182 channel_distortion[i]=distance;
1183 if (distance > channel_distortion[CompositePixelChannel])
1184 channel_distortion[CompositePixelChannel]=distance;
1186 p+=(ptrdiff_t) GetPixelChannels(image);
1187 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1189#if defined(MAGICKCORE_OPENMP_SUPPORT)
1190 #pragma omp critical (MagickCore_GetPeakAbsoluteError)
1192 for (j=0; j <= MaxPixelChannels; j++)
1193 if (channel_distortion[j] > distortion[j])
1194 distortion[j]=channel_distortion[j];
1196 reconstruct_view=DestroyCacheView(reconstruct_view);
1197 image_view=DestroyCacheView(image_view);
1201static MagickBooleanType GetPeakSignalToNoiseRatio(
const Image *image,
1210 status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception);
1211 for (i=0; i < MaxPixelChannels; i++)
1212 if (fabs(distortion[i]) >= MagickEpsilon)
1213 distortion[i]=(-10.0*MagickLog10(distortion[i]))/48.1647;
1214 distortion[CompositePixelChannel]=0.0;
1215 for (i=0; i < MaxPixelChannels; i++)
1216 if (fabs(distortion[i]) >= MagickEpsilon)
1217 distortion[CompositePixelChannel]+=distortion[i];
1218 distortion[CompositePixelChannel]/=GetImageChannels(image);
1222static MagickBooleanType GetPerceptualHashDistortion(
const Image *image,
1238 channel_phash=GetImagePerceptualHash(image,exception);
1240 return(MagickFalse);
1241 reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
1246 return(MagickFalse);
1248#if defined(MAGICKCORE_OPENMP_SUPPORT)
1249 #pragma omp parallel for schedule(static)
1251 for (channel=0; channel < MaxPixelChannels; channel++)
1260 for (i=0; i < MaximumNumberOfImageMoments; i++)
1269 for (j=0; j < (ssize_t) channel_phash[0].number_colorspaces; j++)
1271 alpha=channel_phash[channel].phash[j][i];
1272 beta=reconstruct_phash[channel].phash[j][i];
1273 difference+=(beta-alpha)*(beta-alpha);
1276 distortion[channel]+=difference;
1277#if defined(MAGICKCORE_OPENMP_SUPPORT)
1278 #pragma omp critical (MagickCore_GetPerceptualHashDistortion)
1280 distortion[CompositePixelChannel]+=difference;
1282 artifact=GetImageArtifact(image,
"phash:normalize");
1283 if ((artifact != (
const char *) NULL) &&
1284 (IsStringTrue(artifact) != MagickFalse))
1289 for (j=0; j <= MaxPixelChannels; j++)
1290 distortion[j]=sqrt(distortion[j]/channel_phash[0].number_channels);
1301static MagickBooleanType GetRootMeanSquaredDistortion(
const Image *image,
1310 status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception);
1311 for (i=0; i <= MaxPixelChannels; i++)
1312 distortion[i]=sqrt(distortion[i]);
1316static MagickBooleanType GetStructuralSimilarityDistortion(
const Image *image,
1319#define SSIMRadius 5.0
1320#define SSIMSigma 1.5
1321#define SSIMBlocksize 8
1331 geometry[MagickPathExtent];
1362 artifact=GetImageArtifact(image,
"compare:ssim-radius");
1363 if (artifact != (
const char *) NULL)
1364 radius=StringToDouble(artifact,(
char **) NULL);
1366 artifact=GetImageArtifact(image,
"compare:ssim-sigma");
1367 if (artifact != (
const char *) NULL)
1368 sigma=StringToDouble(artifact,(
char **) NULL);
1369 (void) FormatLocaleString(geometry,MagickPathExtent,
"gaussian:%.20gx%.20g",
1371 kernel_info=AcquireKernelInfo(geometry,exception);
1373 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1375 c1=pow(SSIMK1*SSIML,2.0);
1376 artifact=GetImageArtifact(image,
"compare:ssim-k1");
1377 if (artifact != (
const char *) NULL)
1378 c1=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
1379 c2=pow(SSIMK2*SSIML,2.0);
1380 artifact=GetImageArtifact(image,
"compare:ssim-k2");
1381 if (artifact != (
const char *) NULL)
1382 c2=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
1385 rows=MagickMax(image->rows,reconstruct_image->rows);
1386 columns=MagickMax(image->columns,reconstruct_image->columns);
1387 image_view=AcquireVirtualCacheView(image,exception);
1388 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1389#if defined(MAGICKCORE_OPENMP_SUPPORT)
1390 #pragma omp parallel for schedule(static,1) shared(area,distortion,status) \
1391 magick_number_threads(image,reconstruct_image,rows,1)
1393 for (y=0; y < (ssize_t) rows; y++)
1400 channel_distortion[MaxPixelChannels+1];
1409 if (status == MagickFalse)
1411 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1412 ((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
1413 kernel_info->height,exception);
1414 q=GetCacheViewVirtualPixels(reconstruct_view,-((ssize_t) kernel_info->width/
1415 2L),y-((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
1416 kernel_info->height,exception);
1417 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1422 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
1423 for (x=0; x < (ssize_t) columns; x++)
1426 *magick_restrict reconstruct,
1427 *magick_restrict test;
1430 x_pixel_mu[MaxPixelChannels+1],
1431 x_pixel_sigma_squared[MaxPixelChannels+1],
1432 xy_sigma[MaxPixelChannels+1],
1433 y_pixel_mu[MaxPixelChannels+1],
1434 y_pixel_sigma_squared[MaxPixelChannels+1];
1442 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1443 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1445 p+=(ptrdiff_t) GetPixelChannels(image);
1446 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1449 (void) memset(x_pixel_mu,0,
sizeof(x_pixel_mu));
1450 (void) memset(x_pixel_sigma_squared,0,
sizeof(x_pixel_sigma_squared));
1451 (void) memset(xy_sigma,0,
sizeof(xy_sigma));
1452 (void) memset(y_pixel_mu,0,
sizeof(y_pixel_mu));
1453 (void) memset(y_pixel_sigma_squared,0,
sizeof(y_pixel_sigma_squared));
1454 k=kernel_info->values;
1457 for (v=0; v < (ssize_t) kernel_info->height; v++)
1462 for (u=0; u < (ssize_t) kernel_info->width; u++)
1464 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1470 PixelChannel channel = GetPixelChannelChannel(image,i);
1471 PixelTrait traits = GetPixelChannelTraits(image,channel);
1472 PixelTrait reconstruct_traits = GetPixelChannelTraits(
1473 reconstruct_image,channel);
1474 if ((traits == UndefinedPixelTrait) ||
1475 (reconstruct_traits == UndefinedPixelTrait) ||
1476 ((reconstruct_traits & UpdatePixelTrait) == 0))
1478 x_pixel=QuantumScale*(double) reconstruct[i];
1479 x_pixel_mu[i]+=(*k)*x_pixel;
1480 x_pixel_sigma_squared[i]+=(*k)*x_pixel*x_pixel;
1481 y_pixel=QuantumScale*(double)
1482 GetPixelChannel(reconstruct_image,channel,test);
1483 y_pixel_mu[i]+=(*k)*y_pixel;
1484 y_pixel_sigma_squared[i]+=(*k)*y_pixel*y_pixel;
1485 xy_sigma[i]+=(*k)*x_pixel*y_pixel;
1488 reconstruct+=(ptrdiff_t) GetPixelChannels(image);
1489 test+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1491 reconstruct+=(ptrdiff_t) GetPixelChannels(image)*columns;
1492 test+=(ptrdiff_t) GetPixelChannels(reconstruct_image)*columns;
1494 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1499 x_pixel_sigmas_squared,
1503 y_pixel_sigmas_squared;
1505 PixelChannel channel = GetPixelChannelChannel(image,i);
1506 PixelTrait traits = GetPixelChannelTraits(image,channel);
1507 PixelTrait reconstruct_traits = GetPixelChannelTraits(
1508 reconstruct_image,channel);
1509 if ((traits == UndefinedPixelTrait) ||
1510 (reconstruct_traits == UndefinedPixelTrait) ||
1511 ((reconstruct_traits & UpdatePixelTrait) == 0))
1513 x_pixel_mu_squared=x_pixel_mu[i]*x_pixel_mu[i];
1514 y_pixel_mu_squared=y_pixel_mu[i]*y_pixel_mu[i];
1515 xy_mu=x_pixel_mu[i]*y_pixel_mu[i];
1516 xy_sigmas=xy_sigma[i]-xy_mu;
1517 x_pixel_sigmas_squared=x_pixel_sigma_squared[i]-x_pixel_mu_squared;
1518 y_pixel_sigmas_squared=y_pixel_sigma_squared[i]-y_pixel_mu_squared;
1519 ssim=((2.0*xy_mu+c1)*(2.0*xy_sigmas+c2))/
1520 ((x_pixel_mu_squared+y_pixel_mu_squared+c1)*
1521 (x_pixel_sigmas_squared+y_pixel_sigmas_squared+c2));
1522 channel_distortion[i]+=ssim;
1523 channel_distortion[CompositePixelChannel]+=ssim;
1525 p+=(ptrdiff_t) GetPixelChannels(image);
1526 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1529#if defined(MAGICKCORE_OPENMP_SUPPORT)
1530 #pragma omp critical (MagickCore_GetStructuralSimilarityDistortion)
1534 for (i=0; i <= MaxPixelChannels; i++)
1535 distortion[i]+=channel_distortion[i];
1538 image_view=DestroyCacheView(image_view);
1539 reconstruct_view=DestroyCacheView(reconstruct_view);
1540 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1542 PixelChannel channel = GetPixelChannelChannel(image,j);
1543 PixelTrait traits = GetPixelChannelTraits(image,channel);
1544 if ((traits == UndefinedPixelTrait) || ((traits & UpdatePixelTrait) == 0))
1546 distortion[j]*=PerceptibleReciprocal(area);
1548 distortion[CompositePixelChannel]*=PerceptibleReciprocal(area);
1549 distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
1550 kernel_info=DestroyKernelInfo(kernel_info);
1554static MagickBooleanType GetStructuralDisimilarityDistortion(
const Image *image,
1563 status=GetStructuralSimilarityDistortion(image,reconstruct_image,
1564 distortion,exception);
1565 for (i=0; i <= MaxPixelChannels; i++)
1566 distortion[i]=(1.0-distortion[i])/2.0;
1570MagickExport MagickBooleanType GetImageDistortion(
Image *image,
1571 const Image *reconstruct_image,
const MetricType metric,
double *distortion,
1575 *channel_distortion;
1583 assert(image != (
Image *) NULL);
1584 assert(image->signature == MagickCoreSignature);
1585 assert(reconstruct_image != (
const Image *) NULL);
1586 assert(reconstruct_image->signature == MagickCoreSignature);
1587 assert(distortion != (
double *) NULL);
1588 if (IsEventLogging() != MagickFalse)
1589 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1594 length=MaxPixelChannels+1UL;
1595 channel_distortion=(
double *) AcquireQuantumMemory(length,
1596 sizeof(*channel_distortion));
1597 if (channel_distortion == (
double *) NULL)
1598 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1599 (void) memset(channel_distortion,0,length*
1600 sizeof(*channel_distortion));
1603 case AbsoluteErrorMetric:
1605 status=GetAbsoluteDistortion(image,reconstruct_image,channel_distortion,
1609 case FuzzErrorMetric:
1611 status=GetFuzzDistortion(image,reconstruct_image,channel_distortion,
1615 case MeanAbsoluteErrorMetric:
1617 status=GetMeanAbsoluteDistortion(image,reconstruct_image,
1618 channel_distortion,exception);
1621 case MeanErrorPerPixelErrorMetric:
1623 status=GetMeanErrorPerPixel(image,reconstruct_image,channel_distortion,
1627 case MeanSquaredErrorMetric:
1629 status=GetMeanSquaredDistortion(image,reconstruct_image,
1630 channel_distortion,exception);
1633 case NormalizedCrossCorrelationErrorMetric:
1636 status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image,
1637 channel_distortion,exception);
1640 case PeakAbsoluteErrorMetric:
1642 status=GetPeakAbsoluteDistortion(image,reconstruct_image,
1643 channel_distortion,exception);
1646 case PeakSignalToNoiseRatioErrorMetric:
1648 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,
1649 channel_distortion,exception);
1652 case PerceptualHashErrorMetric:
1654 status=GetPerceptualHashDistortion(image,reconstruct_image,
1655 channel_distortion,exception);
1658 case RootMeanSquaredErrorMetric:
1660 status=GetRootMeanSquaredDistortion(image,reconstruct_image,
1661 channel_distortion,exception);
1664 case StructuralSimilarityErrorMetric:
1666 status=GetStructuralSimilarityDistortion(image,reconstruct_image,
1667 channel_distortion,exception);
1670 case StructuralDissimilarityErrorMetric:
1672 status=GetStructuralDisimilarityDistortion(image,reconstruct_image,
1673 channel_distortion,exception);
1677 *distortion=channel_distortion[CompositePixelChannel];
1678 channel_distortion=(
double *) RelinquishMagickMemory(channel_distortion);
1679 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
1716MagickExport
double *GetImageDistortions(
Image *image,
1717 const Image *reconstruct_image,
const MetricType metric,
1721 *channel_distortion;
1729 assert(image != (
Image *) NULL);
1730 assert(image->signature == MagickCoreSignature);
1731 assert(reconstruct_image != (
const Image *) NULL);
1732 assert(reconstruct_image->signature == MagickCoreSignature);
1733 if (IsEventLogging() != MagickFalse)
1734 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1738 length=MaxPixelChannels+1UL;
1739 channel_distortion=(
double *) AcquireQuantumMemory(length,
1740 sizeof(*channel_distortion));
1741 if (channel_distortion == (
double *) NULL)
1742 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1743 (void) memset(channel_distortion,0,length*
1744 sizeof(*channel_distortion));
1748 case AbsoluteErrorMetric:
1750 status=GetAbsoluteDistortion(image,reconstruct_image,channel_distortion,
1754 case FuzzErrorMetric:
1756 status=GetFuzzDistortion(image,reconstruct_image,channel_distortion,
1760 case MeanAbsoluteErrorMetric:
1762 status=GetMeanAbsoluteDistortion(image,reconstruct_image,
1763 channel_distortion,exception);
1766 case MeanErrorPerPixelErrorMetric:
1768 status=GetMeanErrorPerPixel(image,reconstruct_image,channel_distortion,
1772 case MeanSquaredErrorMetric:
1774 status=GetMeanSquaredDistortion(image,reconstruct_image,
1775 channel_distortion,exception);
1778 case NormalizedCrossCorrelationErrorMetric:
1781 status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image,
1782 channel_distortion,exception);
1785 case PeakAbsoluteErrorMetric:
1787 status=GetPeakAbsoluteDistortion(image,reconstruct_image,
1788 channel_distortion,exception);
1791 case PeakSignalToNoiseRatioErrorMetric:
1793 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,
1794 channel_distortion,exception);
1797 case PerceptualHashErrorMetric:
1799 status=GetPerceptualHashDistortion(image,reconstruct_image,
1800 channel_distortion,exception);
1803 case RootMeanSquaredErrorMetric:
1805 status=GetRootMeanSquaredDistortion(image,reconstruct_image,
1806 channel_distortion,exception);
1809 case StructuralSimilarityErrorMetric:
1811 status=GetStructuralSimilarityDistortion(image,reconstruct_image,
1812 channel_distortion,exception);
1815 case StructuralDissimilarityErrorMetric:
1817 status=GetStructuralDisimilarityDistortion(image,reconstruct_image,
1818 channel_distortion,exception);
1822 if (status == MagickFalse)
1824 channel_distortion=(
double *) RelinquishMagickMemory(channel_distortion);
1825 return((
double *) NULL);
1827 return(channel_distortion);
1858MagickExport MagickBooleanType IsImagesEqual(
const Image *image,
1872 assert(image != (
Image *) NULL);
1873 assert(image->signature == MagickCoreSignature);
1874 assert(reconstruct_image != (
const Image *) NULL);
1875 assert(reconstruct_image->signature == MagickCoreSignature);
1876 rows=MagickMax(image->rows,reconstruct_image->rows);
1877 columns=MagickMax(image->columns,reconstruct_image->columns);
1878 image_view=AcquireVirtualCacheView(image,exception);
1879 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1880 for (y=0; y < (ssize_t) rows; y++)
1889 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1890 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1891 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1893 for (x=0; x < (ssize_t) columns; x++)
1898 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1903 PixelChannel channel = GetPixelChannelChannel(image,i);
1904 PixelTrait traits = GetPixelChannelTraits(image,channel);
1905 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1907 if ((traits == UndefinedPixelTrait) ||
1908 (reconstruct_traits == UndefinedPixelTrait) ||
1909 ((reconstruct_traits & UpdatePixelTrait) == 0))
1911 distance=fabs((
double) p[i]-(
double) GetPixelChannel(reconstruct_image,
1913 if (distance >= MagickEpsilon)
1916 if (i < (ssize_t) GetPixelChannels(image))
1918 p+=(ptrdiff_t) GetPixelChannels(image);
1919 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1921 if (x < (ssize_t) columns)
1924 reconstruct_view=DestroyCacheView(reconstruct_view);
1925 image_view=DestroyCacheView(image_view);
1926 return(y < (ssize_t) rows ? MagickFalse : MagickTrue);
1978MagickExport MagickBooleanType SetImageColorMetric(
Image *image,
1989 mean_error_per_pixel;
2001 assert(image != (
Image *) NULL);
2002 assert(image->signature == MagickCoreSignature);
2003 assert(reconstruct_image != (
const Image *) NULL);
2004 assert(reconstruct_image->signature == MagickCoreSignature);
2007 mean_error_per_pixel=0.0;
2009 rows=MagickMax(image->rows,reconstruct_image->rows);
2010 columns=MagickMax(image->columns,reconstruct_image->columns);
2011 image_view=AcquireVirtualCacheView(image,exception);
2012 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2013 for (y=0; y < (ssize_t) rows; y++)
2022 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
2023 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
2024 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2026 for (x=0; x < (ssize_t) columns; x++)
2031 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2036 PixelChannel channel = GetPixelChannelChannel(image,i);
2037 PixelTrait traits = GetPixelChannelTraits(image,channel);
2038 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2040 if ((traits == UndefinedPixelTrait) ||
2041 (reconstruct_traits == UndefinedPixelTrait) ||
2042 ((reconstruct_traits & UpdatePixelTrait) == 0))
2044 distance=fabs((
double) p[i]-(
double) GetPixelChannel(reconstruct_image,
2046 if (distance >= MagickEpsilon)
2048 mean_error_per_pixel+=distance;
2049 mean_error+=distance*distance;
2050 if (distance > maximum_error)
2051 maximum_error=distance;
2055 p+=(ptrdiff_t) GetPixelChannels(image);
2056 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2059 reconstruct_view=DestroyCacheView(reconstruct_view);
2060 image_view=DestroyCacheView(image_view);
2061 image->error.mean_error_per_pixel=(double) (mean_error_per_pixel/area);
2062 image->error.normalized_mean_error=(double) (QuantumScale*QuantumScale*
2064 image->error.normalized_maximum_error=(double) (QuantumScale*maximum_error);
2065 status=image->error.mean_error_per_pixel == 0.0 ? MagickTrue : MagickFalse;
2112#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
2113static Image *SIMCrossCorrelationImage(
const Image *alpha_image,
2117 *alpha_fft = (
Image *) NULL,
2118 *beta_fft = (
Image *) NULL,
2119 *complex_conjugate = (
Image *) NULL,
2120 *complex_multiplication = (
Image *) NULL,
2121 *cross_correlation = (
Image *) NULL,
2122 *temp_image = (
Image *) NULL;
2127 temp_image=CloneImage(beta_image,0,0,MagickTrue,exception);
2128 if (temp_image == (
Image *) NULL)
2129 return((
Image *) NULL);
2130 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2131 beta_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2132 temp_image=DestroyImageList(temp_image);
2133 if (beta_fft == (
Image *) NULL)
2134 return((
Image *) NULL);
2138 complex_conjugate=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
2139 beta_fft=DestroyImageList(beta_fft);
2140 if (complex_conjugate == (
Image *) NULL)
2141 return((
Image *) NULL);
2145 temp_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
2146 if (temp_image == (
Image *) NULL)
2148 complex_conjugate=DestroyImageList(complex_conjugate);
2149 return((
Image *) NULL);
2151 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2152 alpha_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2153 temp_image=DestroyImageList(temp_image);
2154 if (alpha_fft == (
Image *) NULL)
2156 complex_conjugate=DestroyImageList(complex_conjugate);
2157 return((
Image *) NULL);
2162 DisableCompositeClampUnlessSpecified(complex_conjugate);
2163 DisableCompositeClampUnlessSpecified(complex_conjugate->next);
2164 complex_conjugate->next->next=alpha_fft;
2165 complex_multiplication=ComplexImages(complex_conjugate,
2166 MultiplyComplexOperator,exception);
2167 complex_conjugate=DestroyImageList(complex_conjugate);
2168 if (complex_multiplication == (
Image *) NULL)
2169 return((
Image *) NULL);
2173 cross_correlation=InverseFourierTransformImage(complex_multiplication,
2174 complex_multiplication->next,MagickFalse,exception);
2175 complex_multiplication=DestroyImageList(complex_multiplication);
2176 return(cross_correlation);
2179static Image *SIMDerivativeImage(
const Image *image,
const char *kernel,
2188 kernel_info=AcquireKernelInfo(kernel,exception);
2190 return((
Image *) NULL);
2191 derivative_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
2193 kernel_info=DestroyKernelInfo(kernel_info);
2194 return(derivative_image);
2197static Image *SIMDivideImage(
const Image *numerator_image,
2216 divide_image=CloneImage(numerator_image,0,0,MagickTrue,exception);
2217 if (divide_image == (
Image *) NULL)
2218 return(divide_image);
2220 numerator_view=AcquireAuthenticCacheView(divide_image,exception);
2221 denominator_view=AcquireVirtualCacheView(denominator_image,exception);
2222#if defined(MAGICKCORE_OPENMP_SUPPORT)
2223 #pragma omp parallel for schedule(static) shared(status) \
2224 magick_number_threads(denominator_image,divide_image,divide_image->rows,1)
2226 for (y=0; y < (ssize_t) divide_image->rows; y++)
2237 if (status == MagickFalse)
2239 p=GetCacheViewVirtualPixels(denominator_view,0,y,
2240 denominator_image->columns,1,exception);
2241 q=GetCacheViewAuthenticPixels(numerator_view,0,y,divide_image->columns,1,
2243 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2248 for (x=0; x < (ssize_t) divide_image->columns; x++)
2253 for (i=0; i < (ssize_t) GetPixelChannels(divide_image); i++)
2255 PixelChannel channel = GetPixelChannelChannel(divide_image,i);
2256 PixelTrait traits = GetPixelChannelTraits(divide_image,channel);
2257 if ((traits & UpdatePixelTrait) == 0)
2259 if (fabs(p[i]) >= MagickEpsilon)
2260 q[i]=(Quantum) ((
double) q[i]*PerceptibleReciprocal(QuantumScale*
2263 p+=(ptrdiff_t) GetPixelChannels(denominator_image);
2264 q+=(ptrdiff_t) GetPixelChannels(divide_image);
2266 if (SyncCacheViewAuthenticPixels(numerator_view,exception) == MagickFalse)
2269 denominator_view=DestroyCacheView(denominator_view);
2270 numerator_view=DestroyCacheView(numerator_view);
2271 if (status == MagickFalse)
2272 divide_image=DestroyImage(divide_image);
2273 return(divide_image);
2276static Image *SIMDivideByMagnitude(
Image *image,
Image *magnitude_image,
2286 divide_image=SIMDivideImage(image,magnitude_image,exception);
2287 if (divide_image == (
Image *) NULL)
2288 return((
Image *) NULL);
2289 GetPixelInfoRGBA(0,0,0,0,÷_image->background_color);
2290 SetGeometry(source_image,&geometry);
2291 geometry.width=MagickMax(source_image->columns,divide_image->columns);
2292 geometry.height=MagickMax(source_image->rows,divide_image->rows);
2293 result_image=ExtentImage(divide_image,&geometry,exception);
2294 divide_image=DestroyImage(divide_image);
2295 return(result_image);
2313 image_view=AcquireAuthenticCacheView(image,exception);
2314#if defined(MAGICKCORE_OPENMP_SUPPORT)
2315 #pragma omp parallel for schedule(static) shared(status) \
2316 magick_number_threads(image,image,image->rows,1)
2318 for (y=0; y < (ssize_t) image->rows; y++)
2326 if (status == MagickFalse)
2328 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2329 if (q == (Quantum *) NULL)
2334 for (x=0; x < (ssize_t) image->columns; x++)
2339 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2341 PixelChannel channel = GetPixelChannelChannel(image,i);
2342 PixelTrait traits = GetPixelChannelTraits(image,channel);
2343 if ((traits & UpdatePixelTrait) == 0)
2345 q[i]=(Quantum) (QuantumRange*10.0*MagickLog10((
double) q[i]));
2347 q+=(ptrdiff_t) GetPixelChannels(image);
2349 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2352 image_view=DestroyCacheView(image_view);
2373 square_image=CloneImage(image,0,0,MagickTrue,exception);
2374 if (square_image == (
Image *) NULL)
2375 return(square_image);
2377 image_view=AcquireAuthenticCacheView(square_image,exception);
2378#if defined(MAGICKCORE_OPENMP_SUPPORT)
2379 #pragma omp parallel for schedule(static) shared(status) \
2380 magick_number_threads(square_image,square_image,square_image->rows,1)
2382 for (y=0; y < (ssize_t) square_image->rows; y++)
2390 if (status == MagickFalse)
2392 q=GetCacheViewAuthenticPixels(image_view,0,y,square_image->columns,1,
2394 if (q == (Quantum *) NULL)
2399 for (x=0; x < (ssize_t) square_image->columns; x++)
2404 for (i=0; i < (ssize_t) GetPixelChannels(square_image); i++)
2406 PixelChannel channel = GetPixelChannelChannel(square_image,i);
2407 PixelTrait traits = GetPixelChannelTraits(square_image,channel);
2408 if ((traits & UpdatePixelTrait) == 0)
2410 q[i]=(Quantum) ((
double) q[i]*QuantumScale*(
double) q[i]);
2412 q+=(ptrdiff_t) GetPixelChannels(square_image);
2414 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2417 image_view=DestroyCacheView(image_view);
2418 if (status == MagickFalse)
2419 square_image=DestroyImage(square_image);
2420 return(square_image);
2434 (void) SetImageArtifact(alpha_image,
"compose:clamp",
"False");
2435 xsq_image=SIMSquareImage(alpha_image,exception);
2436 if (xsq_image == (
Image *) NULL)
2437 return((
Image *) NULL);
2438 (void) SetImageArtifact(beta_image,
"compose:clamp",
"False");
2439 ysq_image=SIMSquareImage(beta_image,exception);
2440 if (ysq_image == (
Image *) NULL)
2442 xsq_image=DestroyImage(xsq_image);
2443 return((
Image *) NULL);
2445 status=CompositeImage(xsq_image,ysq_image,PlusCompositeOp,MagickTrue,0,0,
2447 magnitude_image=xsq_image;
2448 ysq_image=DestroyImage(ysq_image);
2449 if (status == MagickFalse)
2451 magnitude_image=DestroyImage(magnitude_image);
2452 return((
Image *) NULL);
2454 status=EvaluateImage(magnitude_image,PowEvaluateOperator,0.5,exception);
2455 if (status == MagickFalse)
2457 magnitude_image=DestroyImage(magnitude_image);
2458 return (
Image *) NULL;
2460 return(magnitude_image);
2463static MagickBooleanType SIMMaximaImage(
const Image *image,
double *maxima,
2479 *maxima=MagickMinimumValue;
2482 image_view=AcquireVirtualCacheView(image,exception);
2483#if defined(MAGICKCORE_OPENMP_SUPPORT)
2484 #pragma omp parallel for schedule(static) shared(status) \
2485 magick_number_threads(image,image,image->rows,1)
2487 for (y=0; y < (ssize_t) image->rows; y++)
2499 if (status == MagickFalse)
2501 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2502 if (p == (
const Quantum *) NULL)
2507 row_maxima=(double) p[0];
2509 for (x=0; x < (ssize_t) image->columns; x++)
2514 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2516 PixelChannel channel = GetPixelChannelChannel(image,i);
2517 PixelTrait traits = GetPixelChannelTraits(image,channel);
2518 if ((traits & UpdatePixelTrait) == 0)
2520 if ((
double) p[i] > row_maxima)
2522 row_maxima=(double) p[i];
2526 p+=(ptrdiff_t) GetPixelChannels(image);
2528#if defined(MAGICKCORE_OPENMP_SUPPORT)
2529 #pragma omp critical (MagickCore_SIMMaximaImage)
2531 if (row_maxima > *maxima)
2537 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2540 image_view=DestroyCacheView(image_view);
2544static MagickBooleanType SIMMinimaImage(
const Image *image,
double *minima,
2560 *minima=MagickMaximumValue;
2563 image_view=AcquireVirtualCacheView(image,exception);
2564#if defined(MAGICKCORE_OPENMP_SUPPORT)
2565 #pragma omp parallel for schedule(static) shared(status) \
2566 magick_number_threads(image,image,image->rows,1)
2568 for (y=0; y < (ssize_t) image->rows; y++)
2580 if (status == MagickFalse)
2582 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2583 if (p == (
const Quantum *) NULL)
2588 row_minima=(double) p[0];
2590 for (x=0; x < (ssize_t) image->columns; x++)
2595 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2597 PixelChannel channel = GetPixelChannelChannel(image,i);
2598 PixelTrait traits = GetPixelChannelTraits(image,channel);
2599 if ((traits & UpdatePixelTrait) == 0)
2601 if ((
double) p[i] < row_minima)
2603 row_minima=(double) p[i];
2607 p+=(ptrdiff_t) GetPixelChannels(image);
2609#if defined(MAGICKCORE_OPENMP_SUPPORT)
2610 #pragma omp critical (MagickCore_SIMMinimaImage)
2612 if (row_minima < *minima)
2618 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2621 image_view=DestroyCacheView(image_view);
2625static MagickBooleanType SIMMultiplyImage(
Image *image,
const double factor,
2641 image_view=AcquireAuthenticCacheView(image,exception);
2642#if defined(MAGICKCORE_OPENMP_SUPPORT)
2643 #pragma omp parallel for schedule(static) shared(status) \
2644 magick_number_threads(image,image,image->rows,1)
2646 for (y=0; y < (ssize_t) image->rows; y++)
2654 if (status == MagickFalse)
2656 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2657 if (q == (Quantum *) NULL)
2662 for (x=0; x < (ssize_t) image->columns; x++)
2667 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2669 PixelChannel channel = GetPixelChannelChannel(image,i);
2670 PixelTrait traits = GetPixelChannelTraits(image,channel);
2671 if ((traits & UpdatePixelTrait) == 0)
2674 q[i]=(Quantum) ((
double) q[i]*QuantumScale*
2675 channel_statistics[channel].standard_deviation);
2676 q[i]=(Quantum) ((
double) q[i]*factor);
2678 q+=(ptrdiff_t) GetPixelChannels(image);
2680 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2683 image_view=DestroyCacheView(image_view);
2687static Image *SIMPhaseCorrelationImage(
const Image *alpha_image,
2691 *alpha_fft = (
Image *) NULL,
2692 *beta_fft = (
Image *) NULL,
2693 *complex_multiplication = (
Image *) NULL,
2694 *cross_correlation = (
Image *) NULL;
2699 beta_fft=CloneImage(beta_image,0,0,MagickTrue,exception);
2700 if (beta_fft == NULL)
2701 return((
Image *) NULL);
2702 (void) SetImageArtifact(beta_fft,
"fourier:normalize",
"inverse");
2703 beta_fft=ForwardFourierTransformImage(beta_fft,MagickFalse,exception);
2704 if (beta_fft == NULL)
2705 return((
Image *) NULL);
2709 alpha_fft=CloneImage(alpha_image,0,0,MagickTrue,exception);
2710 if (alpha_fft == (
Image *) NULL)
2712 beta_fft=DestroyImageList(beta_fft);
2713 return((
Image *) NULL);
2715 (void) SetImageArtifact(alpha_fft,
"fourier:normalize",
"inverse");
2716 alpha_fft=ForwardFourierTransformImage(alpha_fft,MagickFalse,exception);
2717 if (alpha_fft == (
Image *) NULL)
2719 beta_fft=DestroyImageList(beta_fft);
2720 return((
Image *) NULL);
2725 beta_fft=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
2726 if (beta_fft == (
Image *) NULL)
2728 alpha_fft=DestroyImageList(alpha_fft);
2729 return((
Image *) NULL);
2734 beta_fft->next->next=alpha_fft;
2735 DisableCompositeClampUnlessSpecified(beta_fft);
2736 DisableCompositeClampUnlessSpecified(beta_fft->next);
2737 complex_multiplication=ComplexImages(beta_fft,MultiplyComplexOperator,
2739 beta_fft=DestroyImageList(beta_fft);
2740 if (complex_multiplication == (
Image *) NULL)
2741 return((
Image *) NULL);
2745 CompositeLayers(complex_multiplication,DivideSrcCompositeOp,(
Image *)
2746 magnitude_image,0,0,exception);
2750 (void) SetImageArtifact(complex_multiplication,
"fourier:normalize",
"inverse");
2751 cross_correlation=InverseFourierTransformImage(complex_multiplication,
2752 complex_multiplication->next,MagickFalse,exception);
2753 complex_multiplication=DestroyImageList(complex_multiplication);
2754 return(cross_correlation);
2757static MagickBooleanType SIMSetImageMean(
const Image *image,
2773 image_view=AcquireAuthenticCacheView(image,exception);
2774#if defined(MAGICKCORE_OPENMP_SUPPORT)
2775 #pragma omp parallel for schedule(static) shared(status) \
2776 magick_number_threads(image,image,image->rows,1)
2778 for (y=0; y < (ssize_t) image->rows; y++)
2786 if (status == MagickFalse)
2788 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2789 if (q == (Quantum *) NULL)
2794 for (x=0; x < (ssize_t) image->columns; x++)
2799 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2801 PixelChannel channel = GetPixelChannelChannel(image,i);
2802 PixelTrait traits = GetPixelChannelTraits(image,channel);
2803 if ((traits & UpdatePixelTrait) == 0)
2805 q[i]=(Quantum) channel_statistics[channel].mean;
2807 q+=(ptrdiff_t) GetPixelChannels(image);
2809 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2812 image_view=DestroyCacheView(image_view);
2816static Image *SIMSubtractImageMean(
const Image *alpha_image,
2836 subtract_image=CloneImage(beta_image,alpha_image->columns,alpha_image->rows,
2837 MagickTrue,exception);
2838 if (subtract_image == (
Image *) NULL)
2839 return(subtract_image);
2841 image_view=AcquireAuthenticCacheView(subtract_image,exception);
2842 beta_view=AcquireVirtualCacheView(beta_image,exception);
2843#if defined(MAGICKCORE_OPENMP_SUPPORT)
2844 #pragma omp parallel for schedule(static) shared(status) \
2845 magick_number_threads(beta_image,subtract_image,subtract_image->rows,1)
2847 for (y=0; y < (ssize_t) subtract_image->rows; y++)
2858 if (status == MagickFalse)
2860 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,exception);
2861 q=GetCacheViewAuthenticPixels(image_view,0,y,subtract_image->columns,1,
2863 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2868 for (x=0; x < (ssize_t) subtract_image->columns; x++)
2873 for (i=0; i < (ssize_t) GetPixelChannels(subtract_image); i++)
2875 PixelChannel channel = GetPixelChannelChannel(subtract_image,i);
2876 PixelTrait traits = GetPixelChannelTraits(subtract_image,channel);
2877 if ((traits & UpdatePixelTrait) == 0)
2879 if ((x >= (ssize_t) beta_image->columns) ||
2880 (y >= (ssize_t) beta_image->rows))
2883 q[i]=(Quantum) ((
double) p[i]-channel_statistics[channel].mean);
2885 p+=(ptrdiff_t) GetPixelChannels(beta_image);
2886 q+=(ptrdiff_t) GetPixelChannels(subtract_image);
2888 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2891 beta_view=DestroyCacheView(beta_view);
2892 image_view=DestroyCacheView(image_view);
2893 if (status == MagickFalse)
2894 subtract_image=DestroyImage(subtract_image);
2895 return(subtract_image);
2898static Image *SIMUnityImage(
const Image *alpha_image,
const Image *beta_image,
2916 unity_image=CloneImage(alpha_image,alpha_image->columns,alpha_image->rows,
2917 MagickTrue,exception);
2918 if (unity_image == (
Image *) NULL)
2919 return(unity_image);
2920 if (SetImageStorageClass(unity_image,DirectClass,exception) == MagickFalse)
2921 return(DestroyImage(unity_image));
2923 image_view=AcquireAuthenticCacheView(unity_image,exception);
2924#if defined(MAGICKCORE_OPENMP_SUPPORT)
2925 #pragma omp parallel for schedule(static) shared(status) \
2926 magick_number_threads(unity_image,unity_image,unity_image->rows,1)
2928 for (y=0; y < (ssize_t) unity_image->rows; y++)
2936 if (status == MagickFalse)
2938 q=GetCacheViewAuthenticPixels(image_view,0,y,unity_image->columns,1,
2940 if (q == (Quantum *) NULL)
2945 for (x=0; x < (ssize_t) unity_image->columns; x++)
2950 for (i=0; i < (ssize_t) GetPixelChannels(unity_image); i++)
2952 PixelChannel channel = GetPixelChannelChannel(unity_image,i);
2953 PixelTrait traits = GetPixelChannelTraits(unity_image,channel);
2954 if ((traits & UpdatePixelTrait) == 0)
2956 if ((x >= (ssize_t) beta_image->columns) ||
2957 (y >= (ssize_t) beta_image->rows))
2962 q+=(ptrdiff_t) GetPixelChannels(unity_image);
2964 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2967 image_view=DestroyCacheView(image_view);
2968 if (status == MagickFalse)
2969 unity_image=DestroyImage(unity_image);
2970 return(unity_image);
2973static Image *SIMVarianceImage(
Image *alpha_image,
const Image *beta_image,
2992 variance_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
2993 if (variance_image == (
Image *) NULL)
2994 return(variance_image);
2996 image_view=AcquireAuthenticCacheView(variance_image,exception);
2997 beta_view=AcquireVirtualCacheView(beta_image,exception);
2998#if defined(MAGICKCORE_OPENMP_SUPPORT)
2999 #pragma omp parallel for schedule(static) shared(status) \
3000 magick_number_threads(beta_image,variance_image,variance_image->rows,1)
3002 for (y=0; y < (ssize_t) variance_image->rows; y++)
3013 if (status == MagickFalse)
3015 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,
3017 q=GetCacheViewAuthenticPixels(image_view,0,y,variance_image->columns,1,
3019 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3024 for (x=0; x < (ssize_t) variance_image->columns; x++)
3029 for (i=0; i < (ssize_t) GetPixelChannels(variance_image); i++)
3031 PixelChannel channel = GetPixelChannelChannel(variance_image,i);
3032 PixelTrait traits = GetPixelChannelTraits(variance_image,channel);
3033 if ((traits & UpdatePixelTrait) == 0)
3035 q[i]=(Quantum) ((
double) ClampToQuantum((
double) QuantumRange*
3036 sqrt(fabs(QuantumScale*((
double) q[i]-(
double) p[i]))))/
3037 sqrt((
double) QuantumRange));
3039 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3040 q+=(ptrdiff_t) GetPixelChannels(variance_image);
3042 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3045 beta_view=DestroyCacheView(beta_view);
3046 image_view=DestroyCacheView(image_view);
3047 if (status == MagickFalse)
3048 variance_image=DestroyImage(variance_image);
3049 return(variance_image);
3052static Image *DPCSimilarityImage(
const Image *image,
const Image *reconstruct,
3055#define ThrowDPCSimilarityException() \
3057 if (dot_product_image != (Image *) NULL) \
3058 dot_product_image=DestroyImage(dot_product_image); \
3059 if (magnitude_image != (Image *) NULL) \
3060 magnitude_image=DestroyImage(magnitude_image); \
3061 if (reconstruct_image != (Image *) NULL) \
3062 reconstruct_image=DestroyImage(reconstruct_image); \
3063 if (rx_image != (Image *) NULL) \
3064 rx_image=DestroyImage(rx_image); \
3065 if (ry_image != (Image *) NULL) \
3066 ry_image=DestroyImage(ry_image); \
3067 if (test_image != (Image *) NULL) \
3068 test_image=DestroyImage(test_image); \
3069 if (threshold_image != (Image *) NULL) \
3070 threshold_image=DestroyImage(threshold_image); \
3071 if (trx_image != (Image *) NULL) \
3072 trx_image=DestroyImage(trx_image); \
3073 if (try_image != (Image *) NULL) \
3074 try_image=DestroyImage(try_image); \
3075 if (tx_image != (Image *) NULL) \
3076 tx_image=DestroyImage(tx_image); \
3077 if (ty_image != (Image *) NULL) \
3078 ty_image=DestroyImage(ty_image); \
3079 return((Image *) NULL); \
3086 standard_deviation = 0.0;
3089 *dot_product_image = (
Image *) NULL,
3090 *magnitude_image = (
Image *) NULL,
3091 *reconstruct_image = (
Image *) NULL,
3092 *rx_image = (
Image *) NULL,
3093 *ry_image = (
Image *) NULL,
3094 *trx_image = (
Image *) NULL,
3095 *temp_image = (
Image *) NULL,
3096 *test_image = (
Image *) NULL,
3097 *threshold_image = (
Image *) NULL,
3098 *try_image = (
Image *) NULL,
3099 *tx_image = (
Image *) NULL,
3100 *ty_image = (
Image *) NULL;
3111 test_image=CloneImage(image,0,0,MagickTrue,exception);
3112 if (test_image == (
Image *) NULL)
3113 return((
Image *) NULL);
3114 GetPixelInfoRGBA(0,0,0,0,&test_image->background_color);
3115 (void) ResetImagePage(test_image,
"0x0+0+0");
3116 status=SetImageExtent(test_image,2*(
size_t) ceil((
double) image->columns/2.0),
3117 2*(
size_t) ceil((
double) image->rows/2.0),exception);
3118 if (status == MagickFalse)
3119 ThrowDPCSimilarityException();
3120 (void) SetImageAlphaChannel(test_image,OffAlphaChannel,exception);
3124 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3125 if (reconstruct_image == (
Image *) NULL)
3126 ThrowDPCSimilarityException();
3127 GetPixelInfoRGBA(0,0,0,0,&reconstruct_image->background_color);
3128 (void) ResetImagePage(reconstruct_image,
"0x0+0+0");
3129 (void) SetImageAlphaChannel(reconstruct_image,OffAlphaChannel,exception);
3133 (void) SetImageVirtualPixelMethod(reconstruct_image,EdgeVirtualPixelMethod,
3135 rx_image=SIMDerivativeImage(reconstruct_image,
"Sobel",exception);
3136 if (rx_image == (
Image *) NULL)
3137 ThrowDPCSimilarityException();
3138 ry_image=SIMDerivativeImage(reconstruct_image,
"Sobel:90",exception);
3139 reconstruct_image=DestroyImage(reconstruct_image);
3140 if (ry_image == (
Image *) NULL)
3141 ThrowDPCSimilarityException();
3145 magnitude_image=SIMMagnitudeImage(rx_image,ry_image,exception);
3146 if (magnitude_image == (
Image *) NULL)
3147 ThrowDPCSimilarityException();
3151 threshold_image=CloneImage(magnitude_image,0,0,MagickTrue,exception);
3152 if (threshold_image == (
Image *) NULL)
3153 ThrowDPCSimilarityException();
3154 status=BilevelImage(threshold_image,0.0,exception);
3155 if (status == MagickFalse)
3156 ThrowDPCSimilarityException();
3157 status=GetImageMean(threshold_image,&mean,&standard_deviation,exception);
3158 threshold_image=DestroyImage(threshold_image);
3159 if (status == MagickFalse)
3160 ThrowDPCSimilarityException();
3161 edge_factor=1.0/(QuantumScale*mean)/reconstruct->columns/reconstruct->rows;
3165 temp_image=SIMDivideByMagnitude(rx_image,magnitude_image,image,exception);
3166 rx_image=DestroyImage(rx_image);
3167 if (temp_image == (
Image *) NULL)
3168 ThrowDPCSimilarityException();
3169 rx_image=temp_image;
3170 try_image=SIMDivideByMagnitude(ry_image,magnitude_image,image,exception);
3171 magnitude_image=DestroyImage(magnitude_image);
3172 ry_image=DestroyImage(ry_image);
3173 if (try_image == (
Image *) NULL)
3174 ThrowDPCSimilarityException();
3179 (void) SetImageVirtualPixelMethod(test_image,EdgeVirtualPixelMethod,
3181 tx_image=SIMDerivativeImage(test_image,
"Sobel",exception);
3182 if (tx_image == (
Image *) NULL)
3183 ThrowDPCSimilarityException();
3184 ty_image=SIMDerivativeImage(test_image,
"Sobel:90",exception);
3185 test_image=DestroyImage(test_image);
3186 if (ty_image == (
Image *) NULL)
3187 ThrowDPCSimilarityException();
3191 magnitude_image=SIMMagnitudeImage(tx_image,ty_image,exception);
3192 if (magnitude_image == (
Image *) NULL)
3193 ThrowDPCSimilarityException();
3197 temp_image=SIMDivideByMagnitude(tx_image,magnitude_image,image,exception);
3198 tx_image=DestroyImage(tx_image);
3199 if (temp_image == (
Image *) NULL)
3200 ThrowDPCSimilarityException();
3201 tx_image=temp_image;
3202 try_image=SIMDivideByMagnitude(ty_image,magnitude_image,image,exception);
3203 ty_image=DestroyImage(ty_image);
3204 magnitude_image=DestroyImage(magnitude_image);
3205 if (try_image == (
Image *) NULL)
3206 ThrowDPCSimilarityException();
3211 trx_image=SIMCrossCorrelationImage(tx_image,rx_image,exception);
3212 rx_image=DestroyImage(rx_image);
3213 tx_image=DestroyImage(tx_image);
3214 if (trx_image == (
Image *) NULL)
3215 ThrowDPCSimilarityException();
3216 try_image=SIMCrossCorrelationImage(ty_image,ry_image,exception);
3217 ry_image=DestroyImage(ry_image);
3218 ty_image=DestroyImage(ty_image);
3219 if (try_image == (
Image *) NULL)
3220 ThrowDPCSimilarityException();
3224 (void) SetImageArtifact(try_image,
"compose:clamp",
"False");
3225 status=CompositeImage(trx_image,try_image,PlusCompositeOp,MagickTrue,0,0,
3227 try_image=DestroyImage(try_image);
3228 if (status == MagickFalse)
3229 ThrowDPCSimilarityException();
3230 status=SIMMultiplyImage(trx_image,edge_factor,
3232 if (status == MagickFalse)
3233 ThrowDPCSimilarityException();
3237 SetGeometry(image,&geometry);
3238 geometry.width=image->columns;
3239 geometry.height=image->rows;
3240 (void) ResetImagePage(trx_image,
"0x0+0+0");
3241 dot_product_image=CropImage(trx_image,&geometry,exception);
3242 trx_image=DestroyImage(trx_image);
3243 if (dot_product_image == (
Image *) NULL)
3244 ThrowDPCSimilarityException();
3245 (void) ResetImagePage(dot_product_image,
"0x0+0+0");
3249 status=GrayscaleImage(dot_product_image,AveragePixelIntensityMethod,
3251 if (status == MagickFalse)
3252 ThrowDPCSimilarityException();
3253 dot_product_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3254 status=SIMMaximaImage(dot_product_image,&maxima,offset,exception);
3255 if (status == MagickFalse)
3256 ThrowDPCSimilarityException();
3257 *similarity_metric=1.0-QuantumScale*maxima;
3258 return(dot_product_image);
3261static Image *MSESimilarityImage(
const Image *image,
const Image *reconstruct,
3264#define ThrowMSESimilarityException() \
3266 if (alpha_image != (Image *) NULL) \
3267 alpha_image=DestroyImage(alpha_image); \
3268 if (beta_image != (Image *) NULL) \
3269 beta_image=DestroyImage(beta_image); \
3270 if (channel_statistics != (ChannelStatistics *) NULL) \
3271 channel_statistics=(ChannelStatistics *) \
3272 RelinquishMagickMemory(channel_statistics); \
3273 if (mean_image != (Image *) NULL) \
3274 mean_image=DestroyImage(mean_image); \
3275 if (mse_image != (Image *) NULL) \
3276 mse_image=DestroyImage(mse_image); \
3277 if (reconstruct_image != (Image *) NULL) \
3278 reconstruct_image=DestroyImage(reconstruct_image); \
3279 if (sum_image != (Image *) NULL) \
3280 sum_image=DestroyImage(sum_image); \
3281 if (alpha_image != (Image *) NULL) \
3282 alpha_image=DestroyImage(alpha_image); \
3283 return((Image *) NULL); \
3293 *alpha_image = (
Image *) NULL,
3294 *beta_image = (
Image *) NULL,
3295 *mean_image = (
Image *) NULL,
3296 *mse_image = (
Image *) NULL,
3297 *reconstruct_image = (
Image *) NULL,
3298 *sum_image = (
Image *) NULL,
3299 *test_image = (
Image *) NULL;
3310 test_image=SIMSquareImage(image,exception);
3311 if (test_image == (
Image *) NULL)
3312 ThrowMSESimilarityException();
3313 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3314 if (reconstruct_image == (
Image *) NULL)
3315 ThrowMSESimilarityException();
3319 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3320 test_image=DestroyImage(test_image);
3321 if (alpha_image == (
Image *) NULL)
3322 ThrowMSESimilarityException();
3323 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3325 if (status == MagickFalse)
3326 ThrowMSESimilarityException();
3330 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3331 MagickTrue,0,0,exception);
3332 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3333 reconstruct_image=DestroyImage(reconstruct_image);
3334 if (beta_image == (
Image *) NULL)
3335 ThrowMSESimilarityException();
3336 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3338 if (status == MagickFalse)
3339 ThrowMSESimilarityException();
3343 sum_image=SIMSquareImage(reconstruct,exception);
3344 if (sum_image == (
Image *) NULL)
3345 ThrowMSESimilarityException();
3346 channel_statistics=GetImageStatistics(sum_image,exception);
3348 ThrowMSESimilarityException();
3349 status=SetImageExtent(sum_image,image->columns,image->rows,exception);
3350 if (status == MagickFalse)
3351 ThrowMSESimilarityException();
3352 status=SetImageStorageClass(sum_image,DirectClass,exception);
3353 if (status == MagickFalse)
3354 ThrowMSESimilarityException();
3355 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3357 RelinquishMagickMemory(channel_statistics);
3358 if (status == MagickFalse)
3359 ThrowMSESimilarityException();
3363 AppendImageToList(&sum_image,alpha_image);
3364 AppendImageToList(&sum_image,beta_image);
3365 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3366 if (mean_image == (
Image *) NULL)
3367 ThrowMSESimilarityException();
3368 sum_image=DestroyImage(sum_image);
3369 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
3370 if (status == MagickFalse)
3371 ThrowMSESimilarityException();
3372 mean_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3376 SetGeometry(image,&geometry);
3377 geometry.width=image->columns;
3378 geometry.height=image->rows;
3379 (void) ResetImagePage(mean_image,
"0x0+0+0");
3380 mse_image=CropImage(mean_image,&geometry,exception);
3381 mean_image=DestroyImage(mean_image);
3382 if (mse_image == (
Image *) NULL)
3383 ThrowMSESimilarityException();
3387 (void) ResetImagePage(mse_image,
"0x0+0+0");
3388 (void) ClampImage(mse_image,exception);
3389 status=SIMMinimaImage(mse_image,&minima,offset,exception);
3390 if (status == MagickFalse)
3391 ThrowMSESimilarityException();
3392 status=NegateImage(mse_image,MagickFalse,exception);
3393 if (status == MagickFalse)
3394 ThrowMSESimilarityException();
3395 *similarity_metric=QuantumScale*minima;
3396 alpha_image=DestroyImage(alpha_image);
3397 beta_image=DestroyImage(beta_image);
3401static Image *NCCSimilarityImage(
const Image *image,
const Image *reconstruct,
3404#define ThrowNCCSimilarityException() \
3406 if (alpha_image != (Image *) NULL) \
3407 alpha_image=DestroyImage(alpha_image); \
3408 if (beta_image != (Image *) NULL) \
3409 beta_image=DestroyImage(beta_image); \
3410 if (channel_statistics != (ChannelStatistics *) NULL) \
3411 channel_statistics=(ChannelStatistics *) \
3412 RelinquishMagickMemory(channel_statistics); \
3413 if (correlation_image != (Image *) NULL) \
3414 correlation_image=DestroyImage(correlation_image); \
3415 if (divide_image != (Image *) NULL) \
3416 divide_image=DestroyImage(divide_image); \
3417 if (ncc_image != (Image *) NULL) \
3418 ncc_image=DestroyImage(ncc_image); \
3419 if (normalize_image != (Image *) NULL) \
3420 normalize_image=DestroyImage(normalize_image); \
3421 if (reconstruct_image != (Image *) NULL) \
3422 reconstruct_image=DestroyImage(reconstruct_image); \
3423 if (test_image != (Image *) NULL) \
3424 test_image=DestroyImage(test_image); \
3425 if (variance_image != (Image *) NULL) \
3426 variance_image=DestroyImage(variance_image); \
3427 return((Image *) NULL); \
3437 *alpha_image = (
Image *) NULL,
3438 *beta_image = (
Image *) NULL,
3439 *correlation_image = (
Image *) NULL,
3440 *divide_image = (
Image *) NULL,
3441 *ncc_image = (
Image *) NULL,
3442 *normalize_image = (
Image *) NULL,
3443 *reconstruct_image = (
Image *) NULL,
3444 *test_image = (
Image *) NULL,
3445 *variance_image = (
Image *) NULL;
3456 test_image=SIMSquareImage(image,exception);
3457 if (test_image == (
Image *) NULL)
3458 ThrowNCCSimilarityException();
3459 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3460 if (reconstruct_image == (
Image *) NULL)
3461 ThrowNCCSimilarityException();
3465 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3466 test_image=DestroyImage(test_image);
3467 if (alpha_image == (
Image *) NULL)
3468 ThrowNCCSimilarityException();
3469 status=SIMMultiplyImage(alpha_image,(
double) QuantumRange*
3472 if (status == MagickFalse)
3473 ThrowNCCSimilarityException();
3477 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3478 reconstruct_image=DestroyImage(reconstruct_image);
3479 if (beta_image == (
Image *) NULL)
3480 ThrowNCCSimilarityException();
3481 test_image=SIMSquareImage(beta_image,exception);
3482 beta_image=DestroyImage(beta_image);
3483 if (test_image == (
Image *) NULL)
3484 ThrowNCCSimilarityException();
3485 status=SIMMultiplyImage(test_image,(
double) QuantumRange,
3487 if (status == MagickFalse)
3488 ThrowNCCSimilarityException();
3492 variance_image=SIMVarianceImage(alpha_image,test_image,exception);
3493 test_image=DestroyImage(test_image);
3494 alpha_image=DestroyImage(alpha_image);
3495 if (variance_image == (
Image *) NULL)
3496 ThrowNCCSimilarityException();
3500 channel_statistics=GetImageStatistics(reconstruct,exception);
3502 ThrowNCCSimilarityException();
3503 status=SIMMultiplyImage(variance_image,1.0,channel_statistics,exception);
3504 if (status == MagickFalse)
3505 ThrowNCCSimilarityException();
3506 normalize_image=SIMSubtractImageMean(image,reconstruct,channel_statistics,
3509 RelinquishMagickMemory(channel_statistics);
3510 if (normalize_image == (
Image *) NULL)
3511 ThrowNCCSimilarityException();
3512 correlation_image=SIMCrossCorrelationImage(image,normalize_image,exception);
3513 normalize_image=DestroyImage(normalize_image);
3514 if (correlation_image == (
Image *) NULL)
3515 ThrowNCCSimilarityException();
3519 divide_image=SIMDivideImage(correlation_image,variance_image,exception);
3520 correlation_image=DestroyImage(correlation_image);
3521 variance_image=DestroyImage(variance_image);
3522 if (divide_image == (
Image *) NULL)
3523 ThrowNCCSimilarityException();
3527 SetGeometry(image,&geometry);
3528 geometry.width=image->columns;
3529 geometry.height=image->rows;
3530 (void) ResetImagePage(divide_image,
"0x0+0+0");
3531 ncc_image=CropImage(divide_image,&geometry,exception);
3532 divide_image=DestroyImage(divide_image);
3533 if (ncc_image == (
Image *) NULL)
3534 ThrowNCCSimilarityException();
3538 (void) ResetImagePage(ncc_image,
"0x0+0+0");
3539 status=GrayscaleImage(ncc_image,AveragePixelIntensityMethod,exception);
3540 if (status == MagickFalse)
3541 ThrowNCCSimilarityException();
3542 ncc_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3543 status=SIMMaximaImage(ncc_image,&maxima,offset,exception);
3544 if (status == MagickFalse)
3545 ThrowNCCSimilarityException();
3546 *similarity_metric=1.0-QuantumScale*maxima;
3550static Image *PhaseSimilarityImage(
const Image *image,
const Image *reconstruct,
3553#define ThrowPhaseSimilarityException() \
3555 if (correlation_image != (Image *) NULL) \
3556 correlation_image=DestroyImage(correlation_image); \
3557 if (fft_images != (Image *) NULL) \
3558 fft_images=DestroyImageList(fft_images); \
3559 if (gamma_image != (Image *) NULL) \
3560 gamma_image=DestroyImage(gamma_image); \
3561 if (magnitude_image != (Image *) NULL) \
3562 magnitude_image=DestroyImage(magnitude_image); \
3563 if (phase_image != (Image *) NULL) \
3564 phase_image=DestroyImage(phase_image); \
3565 if (reconstruct_image != (Image *) NULL) \
3566 reconstruct_image=DestroyImage(reconstruct_image); \
3567 if (reconstruct_magnitude != (Image *) NULL) \
3568 reconstruct_magnitude=DestroyImage(reconstruct_magnitude); \
3569 if (test_image != (Image *) NULL) \
3570 test_image=DestroyImage(test_image); \
3571 if (test_magnitude != (Image *) NULL) \
3572 test_magnitude=DestroyImage(test_magnitude); \
3573 return((Image *) NULL); \
3580 *correlation_image = (
Image *) NULL,
3581 *fft_images = (
Image *) NULL,
3582 *gamma_image = (
Image *) NULL,
3583 *magnitude_image = (
Image *) NULL,
3584 *phase_image = (
Image *) NULL,
3585 *reconstruct_image = (
Image *) NULL,
3586 *reconstruct_magnitude = (
Image *) NULL,
3587 *test_image = (
Image *) NULL,
3588 *test_magnitude = (
Image *) NULL;
3599 test_image=CloneImage(image,0,0,MagickTrue,exception);
3600 if (test_image == (
Image *) NULL)
3601 ThrowPhaseSimilarityException();
3602 GetPixelInfoRGBA(0,0,0,0,&test_image->background_color);
3603 (void) ResetImagePage(test_image,
"0x0+0+0");
3604 status=SetImageExtent(test_image,2*(
size_t) ceil((
double) image->columns/2.0),
3605 2*(
size_t) ceil((
double) image->rows/2.0),exception);
3606 if (status == MagickFalse)
3607 ThrowPhaseSimilarityException();
3608 (void) SetImageAlphaChannel(test_image,OffAlphaChannel,exception);
3612 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3613 if (reconstruct_image == (
Image *) NULL)
3614 ThrowPhaseSimilarityException();
3615 GetPixelInfoRGBA(0,0,0,0,&reconstruct_image->background_color);
3616 (void) ResetImagePage(reconstruct_image,
"0x0+0+0");
3617 status=SetImageExtent(reconstruct_image,2*(
size_t) ceil((
double)
3618 image->columns/2.0),2*(
size_t) ceil((
double) image->rows/2.0),exception);
3619 if (status == MagickFalse)
3620 ThrowPhaseSimilarityException();
3621 (void) SetImageAlphaChannel(reconstruct_image,OffAlphaChannel,exception);
3622 (void) SetImageArtifact(test_image,
"fourier:normalize",
"inverse");
3623 fft_images=ForwardFourierTransformImage(test_image,MagickTrue,exception);
3624 if (fft_images == (
Image *) NULL)
3625 ThrowPhaseSimilarityException();
3626 test_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
3627 fft_images=DestroyImageList(fft_images);
3628 if (test_magnitude == (
Image *) NULL)
3629 ThrowPhaseSimilarityException();
3630 (void) SetImageArtifact(reconstruct_image,
"fourier:normalize",
"inverse");
3631 fft_images=ForwardFourierTransformImage(reconstruct_image,MagickTrue,
3633 if (fft_images == (
Image *) NULL)
3634 ThrowPhaseSimilarityException();
3635 reconstruct_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
3636 fft_images=DestroyImageList(fft_images);
3637 if (reconstruct_magnitude == (
Image *) NULL)
3638 ThrowPhaseSimilarityException();
3639 magnitude_image=CloneImage(reconstruct_magnitude,0,0,MagickTrue,exception);
3640 if (magnitude_image == (
Image *) NULL)
3641 ThrowPhaseSimilarityException();
3642 DisableCompositeClampUnlessSpecified(magnitude_image);
3643 (void) CompositeImage(magnitude_image,test_magnitude,MultiplyCompositeOp,
3644 MagickTrue,0,0,exception);
3648 correlation_image=SIMPhaseCorrelationImage(test_image,reconstruct_image,
3649 magnitude_image,exception);
3650 test_image=DestroyImage(test_image);
3651 reconstruct_image=DestroyImage(reconstruct_image);
3652 test_magnitude=DestroyImage(test_magnitude);
3653 reconstruct_magnitude=DestroyImage(reconstruct_magnitude);
3654 if (correlation_image == (
Image *) NULL)
3655 ThrowPhaseSimilarityException();
3659 gamma_image=CloneImage(correlation_image,0,0,MagickTrue,exception);
3660 correlation_image=DestroyImage(correlation_image);
3661 if (gamma_image == (
Image *) NULL)
3662 ThrowPhaseSimilarityException();
3666 SetGeometry(image,&geometry);
3667 geometry.width=image->columns;
3668 geometry.height=image->rows;
3669 (void) ResetImagePage(gamma_image,
"0x0+0+0");
3670 phase_image=CropImage(gamma_image,&geometry,exception);
3671 gamma_image=DestroyImage(gamma_image);
3672 if (phase_image == (
Image *) NULL)
3673 ThrowPhaseSimilarityException();
3674 (void) ResetImagePage(phase_image,
"0x0+0+0");
3678 status=GrayscaleImage(phase_image,AveragePixelIntensityMethod,exception);
3679 if (status == MagickFalse)
3680 ThrowPhaseSimilarityException();
3681 phase_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3682 status=SIMMaximaImage(phase_image,&maxima,offset,exception);
3683 if (status == MagickFalse)
3684 ThrowPhaseSimilarityException();
3685 *similarity_metric=QuantumScale*maxima;
3686 magnitude_image=DestroyImage(magnitude_image);
3687 return(phase_image);
3690static Image *PSNRSimilarityImage(
const Image *image,
const Image *reconstruct,
3693#define ThrowPSNRSimilarityException() \
3695 if (alpha_image != (Image *) NULL) \
3696 alpha_image=DestroyImage(alpha_image); \
3697 if (beta_image != (Image *) NULL) \
3698 beta_image=DestroyImage(beta_image); \
3699 if (channel_statistics != (ChannelStatistics *) NULL) \
3700 channel_statistics=(ChannelStatistics *) \
3701 RelinquishMagickMemory(channel_statistics); \
3702 if (mean_image != (Image *) NULL) \
3703 mean_image=DestroyImage(mean_image); \
3704 if (psnr_image != (Image *) NULL) \
3705 psnr_image=DestroyImage(psnr_image); \
3706 if (reconstruct_image != (Image *) NULL) \
3707 reconstruct_image=DestroyImage(reconstruct_image); \
3708 if (sum_image != (Image *) NULL) \
3709 sum_image=DestroyImage(sum_image); \
3710 if (test_image != (Image *) NULL) \
3711 test_image=DestroyImage(test_image); \
3712 return((Image *) NULL); \
3722 *alpha_image = (
Image *) NULL,
3723 *beta_image = (
Image *) NULL,
3724 *mean_image = (
Image *) NULL,
3725 *psnr_image = (
Image *) NULL,
3726 *reconstruct_image = (
Image *) NULL,
3727 *sum_image = (
Image *) NULL,
3728 *test_image = (
Image *) NULL;
3739 test_image=SIMSquareImage(image,exception);
3740 if (test_image == (
Image *) NULL)
3741 ThrowPSNRSimilarityException();
3742 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3743 if (reconstruct_image == (
Image *) NULL)
3744 ThrowPSNRSimilarityException();
3748 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3749 test_image=DestroyImage(test_image);
3750 if (alpha_image == (
Image *) NULL)
3751 ThrowPSNRSimilarityException();
3752 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3754 if (status == MagickFalse)
3755 ThrowPSNRSimilarityException();
3759 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3760 MagickTrue,0,0,exception);
3761 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3762 reconstruct_image=DestroyImage(reconstruct_image);
3763 if (beta_image == (
Image *) NULL)
3764 ThrowPSNRSimilarityException();
3765 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3767 if (status == MagickFalse)
3768 ThrowPSNRSimilarityException();
3772 sum_image=SIMSquareImage(reconstruct,exception);
3773 if (sum_image == (
Image *) NULL)
3774 ThrowPSNRSimilarityException();
3775 channel_statistics=GetImageStatistics(sum_image,exception);
3777 ThrowPSNRSimilarityException();
3778 status=SetImageExtent(sum_image,image->columns,image->rows,exception);
3779 if (status == MagickFalse)
3780 ThrowPSNRSimilarityException();
3781 status=SetImageStorageClass(sum_image,DirectClass,exception);
3782 if (status == MagickFalse)
3783 ThrowPSNRSimilarityException();
3784 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3786 RelinquishMagickMemory(channel_statistics);
3787 if (status == MagickFalse)
3788 ThrowPSNRSimilarityException();
3792 AppendImageToList(&sum_image,alpha_image);
3793 AppendImageToList(&sum_image,beta_image);
3794 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3795 if (mean_image == (
Image *) NULL)
3796 ThrowPSNRSimilarityException();
3797 sum_image=DestroyImage(sum_image);
3798 status=SIMLogImage(mean_image,exception);
3799 if (status == MagickFalse)
3800 ThrowPSNRSimilarityException();
3801 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
3802 if (status == MagickFalse)
3803 ThrowPSNRSimilarityException();
3804 mean_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3805 status=SIMMultiplyImage(mean_image,1.0/48.1647,
3807 if (status == MagickFalse)
3808 ThrowPSNRSimilarityException();
3812 SetGeometry(image,&geometry);
3813 geometry.width=image->columns;
3814 geometry.height=image->rows;
3815 (void) ResetImagePage(mean_image,
"0x0+0+0");
3816 psnr_image=CropImage(mean_image,&geometry,exception);
3817 mean_image=DestroyImage(mean_image);
3818 if (psnr_image == (
Image *) NULL)
3819 ThrowPSNRSimilarityException();
3823 (void) ResetImagePage(psnr_image,
"0x0+0+0");
3824 (void) EvaluateImage(psnr_image,MaxEvaluateOperator,0.0,exception);
3825 status=SIMMinimaImage(psnr_image,&minima,offset,exception);
3826 if (status == MagickFalse)
3827 ThrowPSNRSimilarityException();
3828 status=NegateImage(psnr_image,MagickFalse,exception);
3829 if (status == MagickFalse)
3830 ThrowPSNRSimilarityException();
3831 *similarity_metric=QuantumScale*minima;
3832 alpha_image=DestroyImage(alpha_image);
3833 beta_image=DestroyImage(beta_image);
3837static Image *RMSESimilarityImage(
const Image *image,
const Image *reconstruct,
3840#define ThrowRMSESimilarityException() \
3842 if (alpha_image != (Image *) NULL) \
3843 alpha_image=DestroyImage(alpha_image); \
3844 if (beta_image != (Image *) NULL) \
3845 beta_image=DestroyImage(beta_image); \
3846 if (channel_statistics != (ChannelStatistics *) NULL) \
3847 channel_statistics=(ChannelStatistics *) \
3848 RelinquishMagickMemory(channel_statistics); \
3849 if (mean_image != (Image *) NULL) \
3850 mean_image=DestroyImage(mean_image); \
3851 if (rmse_image != (Image *) NULL) \
3852 rmse_image=DestroyImage(rmse_image); \
3853 if (reconstruct_image != (Image *) NULL) \
3854 reconstruct_image=DestroyImage(reconstruct_image); \
3855 if (sum_image != (Image *) NULL) \
3856 sum_image=DestroyImage(sum_image); \
3857 if (alpha_image != (Image *) NULL) \
3858 alpha_image=DestroyImage(alpha_image); \
3859 return((Image *) NULL); \
3869 *alpha_image = (
Image *) NULL,
3870 *beta_image = (
Image *) NULL,
3871 *mean_image = (
Image *) NULL,
3872 *reconstruct_image = (
Image *) NULL,
3873 *rmse_image = (
Image *) NULL,
3874 *sum_image = (
Image *) NULL,
3875 *test_image = (
Image *) NULL;
3886 test_image=SIMSquareImage(image,exception);
3887 if (test_image == (
Image *) NULL)
3888 ThrowRMSESimilarityException();
3889 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3890 if (reconstruct_image == (
Image *) NULL)
3891 ThrowRMSESimilarityException();
3895 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3896 test_image=DestroyImage(test_image);
3897 if (alpha_image == (
Image *) NULL)
3898 ThrowRMSESimilarityException();
3899 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3901 if (status == MagickFalse)
3902 ThrowRMSESimilarityException();
3906 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3907 MagickTrue,0,0,exception);
3908 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3909 reconstruct_image=DestroyImage(reconstruct_image);
3910 if (beta_image == (
Image *) NULL)
3911 ThrowRMSESimilarityException();
3912 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3914 if (status == MagickFalse)
3915 ThrowRMSESimilarityException();
3919 sum_image=SIMSquareImage(reconstruct,exception);
3920 if (sum_image == (
Image *) NULL)
3921 ThrowRMSESimilarityException();
3922 channel_statistics=GetImageStatistics(sum_image,exception);
3924 ThrowRMSESimilarityException();
3925 status=SetImageExtent(sum_image,image->columns,image->rows,exception);
3926 if (status == MagickFalse)
3927 ThrowRMSESimilarityException();
3928 status=SetImageStorageClass(sum_image,DirectClass,exception);
3929 if (status == MagickFalse)
3930 ThrowRMSESimilarityException();
3931 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3933 RelinquishMagickMemory(channel_statistics);
3934 if (status == MagickFalse)
3935 ThrowRMSESimilarityException();
3939 AppendImageToList(&sum_image,alpha_image);
3940 AppendImageToList(&sum_image,beta_image);
3941 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3942 if (mean_image == (
Image *) NULL)
3943 ThrowRMSESimilarityException();
3944 status=EvaluateImage(mean_image,PowEvaluateOperator,0.5,exception);
3945 if (mean_image == (
Image *) NULL)
3946 ThrowRMSESimilarityException();
3947 sum_image=DestroyImage(sum_image);
3948 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
3949 if (status == MagickFalse)
3950 ThrowRMSESimilarityException();
3951 mean_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3955 SetGeometry(image,&geometry);
3956 geometry.width=image->columns;
3957 geometry.height=image->rows;
3958 (void) ResetImagePage(mean_image,
"0x0+0+0");
3959 rmse_image=CropImage(mean_image,&geometry,exception);
3960 mean_image=DestroyImage(mean_image);
3961 if (rmse_image == (
Image *) NULL)
3962 ThrowRMSESimilarityException();
3966 (void) ResetImagePage(rmse_image,
"0x0+0+0");
3967 status=SIMMinimaImage(rmse_image,&minima,offset,exception);
3968 if (status == MagickFalse)
3969 ThrowRMSESimilarityException();
3970 status=NegateImage(rmse_image,MagickFalse,exception);
3971 if (status == MagickFalse)
3972 ThrowRMSESimilarityException();
3973 *similarity_metric=QuantumScale*minima;
3974 alpha_image=DestroyImage(alpha_image);
3975 beta_image=DestroyImage(beta_image);
3981static double GetSimilarityMetric(
const Image *image,
const Image *reconstruct,
3982 const MetricType metric,
const ssize_t x_offset,
const ssize_t y_offset,
3997 SetGeometry(reconstruct,&geometry);
3998 geometry.x=x_offset;
3999 geometry.y=y_offset;
4000 similarity_image=CropImage(image,&geometry,exception);
4001 if (similarity_image == (
Image *) NULL)
4004 status=GetImageDistortion(similarity_image,reconstruct,metric,&distortion,
4006 similarity_image=DestroyImage(similarity_image);
4007 if (status == MagickFalse)
4012MagickExport
Image *SimilarityImage(
const Image *image,
const Image *reconstruct,
4013 const MetricType metric,
const double similarity_threshold,
4016#define SimilarityImageTag "Similarity/Image"
4022 *similarity_image = (
Image *) NULL;
4036 assert(image != (
const Image *) NULL);
4037 assert(image->signature == MagickCoreSignature);
4039 assert(exception->signature == MagickCoreSignature);
4041 if (IsEventLogging() != MagickFalse)
4042 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4043 SetGeometry(reconstruct,offset);
4044 *similarity_metric=MagickMaximumValue;
4045#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
4047 const char *artifact = GetImageArtifact(image,
"compare:frequency-domain");
4048 if (artifact == (
const char *) NULL)
4049 artifact=GetImageArtifact(image,
"compare:accelerate-ncc");
4050 if ((image->channels & ReadMaskChannel) == 0)
4053 case DotProductCorrelationErrorMetric:
4055 similarity_image=DPCSimilarityImage(image,reconstruct,offset,
4056 similarity_metric,exception);
4057 return(similarity_image);
4059 case MeanSquaredErrorMetric:
4061 if ((artifact != (
const char *) NULL) &&
4062 (IsStringTrue(artifact) == MagickFalse))
4064 similarity_image=MSESimilarityImage(image,reconstruct,offset,
4065 similarity_metric,exception);
4066 return(similarity_image);
4068 case NormalizedCrossCorrelationErrorMetric:
4070 if ((artifact != (
const char *) NULL) &&
4071 (IsStringTrue(artifact) == MagickFalse))
4073 similarity_image=NCCSimilarityImage(image,reconstruct,offset,
4074 similarity_metric,exception);
4075 return(similarity_image);
4077 case PeakSignalToNoiseRatioErrorMetric:
4079 if ((artifact != (
const char *) NULL) &&
4080 (IsStringTrue(artifact) == MagickFalse))
4082 similarity_image=PSNRSimilarityImage(image,reconstruct,offset,
4083 similarity_metric,exception);
4084 return(similarity_image);
4086 case PhaseCorrelationErrorMetric:
4088 similarity_image=PhaseSimilarityImage(image,reconstruct,offset,
4089 similarity_metric,exception);
4090 return(similarity_image);
4092 case RootMeanSquaredErrorMetric:
4094 if ((artifact != (
const char *) NULL) &&
4095 (IsStringTrue(artifact) == MagickFalse))
4097 similarity_image=RMSESimilarityImage(image,reconstruct,offset,
4098 similarity_metric,exception);
4099 return(similarity_image);
4105 if ((metric == DotProductCorrelationErrorMetric) ||
4106 (metric == PhaseCorrelationErrorMetric))
4108 (void) ThrowMagickException(exception,GetMagickModule(),
4109 MissingDelegateError,
"DelegateLibrarySupportNotBuiltIn",
4110 "'%s' (HDRI, FFT)",image->filename);
4111 return((
Image *) NULL);
4114 if ((image->columns < reconstruct->columns) ||
4115 (image->rows < reconstruct->rows))
4117 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
4118 "GeometryDoesNotContainImage",
"`%s'",image->filename);
4119 return((
Image *) NULL);
4121 similarity_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4123 if (similarity_image == (
Image *) NULL)
4124 return((
Image *) NULL);
4125 similarity_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4126 similarity_image->alpha_trait=UndefinedPixelTrait;
4127 similarity_image->type=GrayscaleType;
4128 status=SetImageStorageClass(similarity_image,DirectClass,exception);
4129 if (status == MagickFalse)
4130 return(DestroyImage(similarity_image));
4136 similarity_view=AcquireAuthenticCacheView(similarity_image,exception);
4137 rows=similarity_image->rows;
4138#if defined(MAGICKCORE_OPENMP_SUPPORT)
4139 #pragma omp parallel for schedule(static,1) \
4140 shared(progress,status,similarity_metric) \
4141 magick_number_threads(similarity_image,similarity_image,rows << 3,1)
4143 for (y=0; y < (ssize_t) rows; y++)
4154 if (status == MagickFalse)
4156#if defined(MMAGICKCORE_OPENMP_SUPPORT)
4157 #pragma omp flush(similarity_metric)
4159 if (*similarity_metric <= similarity_threshold)
4161 q=QueueCacheViewAuthenticPixels(similarity_view,0,y,
4162 similarity_image->columns,1,exception);
4163 if (q == (Quantum *) NULL)
4168 for (x=0; x < (ssize_t) similarity_image->columns; x++)
4173#if defined(MAGICKCORE_OPENMP_SUPPORT)
4174 #pragma omp flush(similarity_metric)
4176 if (*similarity_metric <= similarity_threshold)
4178 similarity=GetSimilarityMetric(image,reconstruct,metric,x,y,exception);
4179 if ((metric == DotProductCorrelationErrorMetric) ||
4180 (metric == PhaseCorrelationErrorMetric) ||
4181 (metric == NormalizedCrossCorrelationErrorMetric) ||
4182 (metric == UndefinedErrorMetric))
4183 similarity=1.0-similarity;
4184#if defined(MAGICKCORE_OPENMP_SUPPORT)
4185 #pragma omp critical (MagickCore_SimilarityImage)
4187 if (similarity < *similarity_metric)
4191 *similarity_metric=similarity;
4193 if (metric == PerceptualHashErrorMetric)
4194 similarity=MagickMin(0.01*similarity,1.0);
4195 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4197 PixelChannel channel = GetPixelChannelChannel(image,i);
4198 PixelTrait traits = GetPixelChannelTraits(image,channel);
4199 PixelTrait similarity_traits = GetPixelChannelTraits(similarity_image,
4201 if ((traits == UndefinedPixelTrait) ||
4202 (similarity_traits == UndefinedPixelTrait) ||
4203 ((similarity_traits & UpdatePixelTrait) == 0))
4207 case FuzzErrorMetric:
4208 case MeanAbsoluteErrorMetric:
4209 case MeanSquaredErrorMetric:
4210 case NormalizedCrossCorrelationErrorMetric:
4211 case PerceptualHashErrorMetric:
4212 case RootMeanSquaredErrorMetric:
4213 case StructuralDissimilarityErrorMetric:
4215 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4216 QuantumRange-QuantumRange*similarity),q);
4221 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4222 QuantumRange*similarity),q);
4227 q+=(ptrdiff_t) GetPixelChannels(similarity_image);
4229 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
4231 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4236#if defined(MAGICKCORE_OPENMP_SUPPORT)
4240 proceed=SetImageProgress(image,SimilarityImageTag,progress,image->rows);
4241 if (proceed == MagickFalse)
4245 similarity_view=DestroyCacheView(similarity_view);
4246 if (status == MagickFalse)
4247 similarity_image=DestroyImage(similarity_image);
4248 return(similarity_image);