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) shared(status) \
1391 magick_number_threads(image,reconstruct_image,rows,1)
1393 for (y=0; y < (ssize_t) rows; y++)
1396 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 x_pixel_mu[MaxPixelChannels+1],
1427 x_pixel_sigma_squared[MaxPixelChannels+1],
1428 xy_sigma[MaxPixelChannels+1],
1429 y_pixel_mu[MaxPixelChannels+1],
1430 y_pixel_sigma_squared[MaxPixelChannels+1];
1433 *magick_restrict reconstruct,
1434 *magick_restrict test;
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(x_pixel_sigma_squared,0,
sizeof(y_pixel_sigma_squared));
1453 (void) memset(y_pixel_mu,0,
sizeof(y_pixel_mu));
1454 (void) memset(y_pixel_sigma_squared,0,
sizeof(y_pixel_sigma_squared));
1455 k=kernel_info->values;
1458 for (v=0; v < (ssize_t) kernel_info->height; v++)
1463 for (u=0; u < (ssize_t) kernel_info->width; u++)
1465 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1471 PixelChannel channel = GetPixelChannelChannel(image,i);
1472 PixelTrait traits = GetPixelChannelTraits(image,channel);
1473 PixelTrait reconstruct_traits = GetPixelChannelTraits(
1474 reconstruct_image,channel);
1475 if ((traits == UndefinedPixelTrait) ||
1476 (reconstruct_traits == UndefinedPixelTrait) ||
1477 ((reconstruct_traits & UpdatePixelTrait) == 0))
1479 x_pixel=QuantumScale*(double) reconstruct[i];
1480 x_pixel_mu[i]+=(*k)*x_pixel;
1481 x_pixel_sigma_squared[i]+=(*k)*x_pixel*x_pixel;
1482 y_pixel=QuantumScale*(double)
1483 GetPixelChannel(reconstruct_image,channel,test);
1484 y_pixel_mu[i]+=(*k)*y_pixel;
1485 y_pixel_sigma_squared[i]+=(*k)*y_pixel*y_pixel;
1486 xy_sigma[i]+=(*k)*x_pixel*y_pixel;
1489 reconstruct+=(ptrdiff_t) GetPixelChannels(image);
1490 test+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1492 reconstruct+=(ptrdiff_t) GetPixelChannels(image)*columns;
1493 test+=(ptrdiff_t) GetPixelChannels(reconstruct_image)*columns;
1495 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1500 x_pixel_sigmas_squared,
1504 y_pixel_sigmas_squared;
1506 PixelChannel channel = GetPixelChannelChannel(image,i);
1507 PixelTrait traits = GetPixelChannelTraits(image,channel);
1508 PixelTrait reconstruct_traits = GetPixelChannelTraits(
1509 reconstruct_image,channel);
1510 if ((traits == UndefinedPixelTrait) ||
1511 (reconstruct_traits == UndefinedPixelTrait) ||
1512 ((reconstruct_traits & UpdatePixelTrait) == 0))
1514 x_pixel_mu_squared=x_pixel_mu[i]*x_pixel_mu[i];
1515 y_pixel_mu_squared=y_pixel_mu[i]*y_pixel_mu[i];
1516 xy_mu=x_pixel_mu[i]*y_pixel_mu[i];
1517 xy_sigmas=xy_sigma[i]-xy_mu;
1518 x_pixel_sigmas_squared=x_pixel_sigma_squared[i]-x_pixel_mu_squared;
1519 y_pixel_sigmas_squared=y_pixel_sigma_squared[i]-y_pixel_mu_squared;
1520 ssim=((2.0*xy_mu+c1)*(2.0*xy_sigmas+c2))/
1521 ((x_pixel_mu_squared+y_pixel_mu_squared+c1)*
1522 (x_pixel_sigmas_squared+y_pixel_sigmas_squared+c2));
1523 channel_distortion[i]+=ssim;
1524 channel_distortion[CompositePixelChannel]+=ssim;
1526 p+=(ptrdiff_t) GetPixelChannels(image);
1527 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1530#if defined(MAGICKCORE_OPENMP_SUPPORT)
1531 #pragma omp critical (MagickCore_GetStructuralSimilarityDistortion)
1535 for (i=0; i <= MaxPixelChannels; i++)
1536 distortion[i]+=channel_distortion[i];
1539 image_view=DestroyCacheView(image_view);
1540 reconstruct_view=DestroyCacheView(reconstruct_view);
1541 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1543 PixelChannel channel = GetPixelChannelChannel(image,j);
1544 PixelTrait traits = GetPixelChannelTraits(image,channel);
1545 if ((traits == UndefinedPixelTrait) || ((traits & UpdatePixelTrait) == 0))
1547 distortion[j]/=area;
1549 distortion[CompositePixelChannel]/=area;
1550 distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
1551 kernel_info=DestroyKernelInfo(kernel_info);
1555static MagickBooleanType GetStructuralDisimilarityDistortion(
const Image *image,
1564 status=GetStructuralSimilarityDistortion(image,reconstruct_image,
1565 distortion,exception);
1566 for (i=0; i <= MaxPixelChannels; i++)
1567 distortion[i]=(1.0-(distortion[i]))/2.0;
1571MagickExport MagickBooleanType GetImageDistortion(
Image *image,
1572 const Image *reconstruct_image,
const MetricType metric,
double *distortion,
1576 *channel_distortion;
1584 assert(image != (
Image *) NULL);
1585 assert(image->signature == MagickCoreSignature);
1586 assert(reconstruct_image != (
const Image *) NULL);
1587 assert(reconstruct_image->signature == MagickCoreSignature);
1588 assert(distortion != (
double *) NULL);
1589 if (IsEventLogging() != MagickFalse)
1590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1595 length=MaxPixelChannels+1UL;
1596 channel_distortion=(
double *) AcquireQuantumMemory(length,
1597 sizeof(*channel_distortion));
1598 if (channel_distortion == (
double *) NULL)
1599 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1600 (void) memset(channel_distortion,0,length*
1601 sizeof(*channel_distortion));
1604 case AbsoluteErrorMetric:
1606 status=GetAbsoluteDistortion(image,reconstruct_image,channel_distortion,
1610 case FuzzErrorMetric:
1612 status=GetFuzzDistortion(image,reconstruct_image,channel_distortion,
1616 case MeanAbsoluteErrorMetric:
1618 status=GetMeanAbsoluteDistortion(image,reconstruct_image,
1619 channel_distortion,exception);
1622 case MeanErrorPerPixelErrorMetric:
1624 status=GetMeanErrorPerPixel(image,reconstruct_image,channel_distortion,
1628 case MeanSquaredErrorMetric:
1630 status=GetMeanSquaredDistortion(image,reconstruct_image,
1631 channel_distortion,exception);
1634 case NormalizedCrossCorrelationErrorMetric:
1637 status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image,
1638 channel_distortion,exception);
1641 case PeakAbsoluteErrorMetric:
1643 status=GetPeakAbsoluteDistortion(image,reconstruct_image,
1644 channel_distortion,exception);
1647 case PeakSignalToNoiseRatioErrorMetric:
1649 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,
1650 channel_distortion,exception);
1653 case PerceptualHashErrorMetric:
1655 status=GetPerceptualHashDistortion(image,reconstruct_image,
1656 channel_distortion,exception);
1659 case RootMeanSquaredErrorMetric:
1661 status=GetRootMeanSquaredDistortion(image,reconstruct_image,
1662 channel_distortion,exception);
1665 case StructuralSimilarityErrorMetric:
1667 status=GetStructuralSimilarityDistortion(image,reconstruct_image,
1668 channel_distortion,exception);
1671 case StructuralDissimilarityErrorMetric:
1673 status=GetStructuralDisimilarityDistortion(image,reconstruct_image,
1674 channel_distortion,exception);
1678 *distortion=channel_distortion[CompositePixelChannel];
1679 channel_distortion=(
double *) RelinquishMagickMemory(channel_distortion);
1680 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
1717MagickExport
double *GetImageDistortions(
Image *image,
1718 const Image *reconstruct_image,
const MetricType metric,
1722 *channel_distortion;
1730 assert(image != (
Image *) NULL);
1731 assert(image->signature == MagickCoreSignature);
1732 assert(reconstruct_image != (
const Image *) NULL);
1733 assert(reconstruct_image->signature == MagickCoreSignature);
1734 if (IsEventLogging() != MagickFalse)
1735 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1739 length=MaxPixelChannels+1UL;
1740 channel_distortion=(
double *) AcquireQuantumMemory(length,
1741 sizeof(*channel_distortion));
1742 if (channel_distortion == (
double *) NULL)
1743 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1744 (void) memset(channel_distortion,0,length*
1745 sizeof(*channel_distortion));
1749 case AbsoluteErrorMetric:
1751 status=GetAbsoluteDistortion(image,reconstruct_image,channel_distortion,
1755 case FuzzErrorMetric:
1757 status=GetFuzzDistortion(image,reconstruct_image,channel_distortion,
1761 case MeanAbsoluteErrorMetric:
1763 status=GetMeanAbsoluteDistortion(image,reconstruct_image,
1764 channel_distortion,exception);
1767 case MeanErrorPerPixelErrorMetric:
1769 status=GetMeanErrorPerPixel(image,reconstruct_image,channel_distortion,
1773 case MeanSquaredErrorMetric:
1775 status=GetMeanSquaredDistortion(image,reconstruct_image,
1776 channel_distortion,exception);
1779 case NormalizedCrossCorrelationErrorMetric:
1782 status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image,
1783 channel_distortion,exception);
1786 case PeakAbsoluteErrorMetric:
1788 status=GetPeakAbsoluteDistortion(image,reconstruct_image,
1789 channel_distortion,exception);
1792 case PeakSignalToNoiseRatioErrorMetric:
1794 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,
1795 channel_distortion,exception);
1798 case PerceptualHashErrorMetric:
1800 status=GetPerceptualHashDistortion(image,reconstruct_image,
1801 channel_distortion,exception);
1804 case RootMeanSquaredErrorMetric:
1806 status=GetRootMeanSquaredDistortion(image,reconstruct_image,
1807 channel_distortion,exception);
1810 case StructuralSimilarityErrorMetric:
1812 status=GetStructuralSimilarityDistortion(image,reconstruct_image,
1813 channel_distortion,exception);
1816 case StructuralDissimilarityErrorMetric:
1818 status=GetStructuralDisimilarityDistortion(image,reconstruct_image,
1819 channel_distortion,exception);
1823 if (status == MagickFalse)
1825 channel_distortion=(
double *) RelinquishMagickMemory(channel_distortion);
1826 return((
double *) NULL);
1828 return(channel_distortion);
1859MagickExport MagickBooleanType IsImagesEqual(
const Image *image,
1873 assert(image != (
Image *) NULL);
1874 assert(image->signature == MagickCoreSignature);
1875 assert(reconstruct_image != (
const Image *) NULL);
1876 assert(reconstruct_image->signature == MagickCoreSignature);
1877 rows=MagickMax(image->rows,reconstruct_image->rows);
1878 columns=MagickMax(image->columns,reconstruct_image->columns);
1879 image_view=AcquireVirtualCacheView(image,exception);
1880 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1881 for (y=0; y < (ssize_t) rows; y++)
1890 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1891 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1892 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1894 for (x=0; x < (ssize_t) columns; x++)
1899 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1904 PixelChannel channel = GetPixelChannelChannel(image,i);
1905 PixelTrait traits = GetPixelChannelTraits(image,channel);
1906 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1908 if ((traits == UndefinedPixelTrait) ||
1909 (reconstruct_traits == UndefinedPixelTrait) ||
1910 ((reconstruct_traits & UpdatePixelTrait) == 0))
1912 distance=fabs((
double) p[i]-(
double) GetPixelChannel(reconstruct_image,
1914 if (distance >= MagickEpsilon)
1917 if (i < (ssize_t) GetPixelChannels(image))
1919 p+=(ptrdiff_t) GetPixelChannels(image);
1920 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1922 if (x < (ssize_t) columns)
1925 reconstruct_view=DestroyCacheView(reconstruct_view);
1926 image_view=DestroyCacheView(image_view);
1927 return(y < (ssize_t) rows ? MagickFalse : MagickTrue);
1979MagickExport MagickBooleanType SetImageColorMetric(
Image *image,
1990 mean_error_per_pixel;
2002 assert(image != (
Image *) NULL);
2003 assert(image->signature == MagickCoreSignature);
2004 assert(reconstruct_image != (
const Image *) NULL);
2005 assert(reconstruct_image->signature == MagickCoreSignature);
2008 mean_error_per_pixel=0.0;
2010 rows=MagickMax(image->rows,reconstruct_image->rows);
2011 columns=MagickMax(image->columns,reconstruct_image->columns);
2012 image_view=AcquireVirtualCacheView(image,exception);
2013 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2014 for (y=0; y < (ssize_t) rows; y++)
2023 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
2024 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
2025 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2027 for (x=0; x < (ssize_t) columns; x++)
2032 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2037 PixelChannel channel = GetPixelChannelChannel(image,i);
2038 PixelTrait traits = GetPixelChannelTraits(image,channel);
2039 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2041 if ((traits == UndefinedPixelTrait) ||
2042 (reconstruct_traits == UndefinedPixelTrait) ||
2043 ((reconstruct_traits & UpdatePixelTrait) == 0))
2045 distance=fabs((
double) p[i]-(
double) GetPixelChannel(reconstruct_image,
2047 if (distance >= MagickEpsilon)
2049 mean_error_per_pixel+=distance;
2050 mean_error+=distance*distance;
2051 if (distance > maximum_error)
2052 maximum_error=distance;
2056 p+=(ptrdiff_t) GetPixelChannels(image);
2057 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2060 reconstruct_view=DestroyCacheView(reconstruct_view);
2061 image_view=DestroyCacheView(image_view);
2062 image->error.mean_error_per_pixel=(double) (mean_error_per_pixel/area);
2063 image->error.normalized_mean_error=(double) (QuantumScale*QuantumScale*
2065 image->error.normalized_maximum_error=(double) (QuantumScale*maximum_error);
2066 status=image->error.mean_error_per_pixel == 0.0 ? MagickTrue : MagickFalse;
2113#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
2114static Image *SIMCrossCorrelationImage(
const Image *alpha_image,
2118 *alpha_fft = (
Image *) NULL,
2119 *beta_fft = (
Image *) NULL,
2120 *complex_conjugate = (
Image *) NULL,
2121 *complex_multiplication = (
Image *) NULL,
2122 *cross_correlation = (
Image *) NULL,
2123 *temp_image = (
Image *) NULL;
2128 temp_image=CloneImage(beta_image,0,0,MagickTrue,exception);
2129 if (temp_image == (
Image *) NULL)
2130 return((
Image *) NULL);
2131 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2132 beta_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2133 temp_image=DestroyImageList(temp_image);
2134 if (beta_fft == (
Image *) NULL)
2135 return((
Image *) NULL);
2139 complex_conjugate=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
2140 beta_fft=DestroyImageList(beta_fft);
2141 if (complex_conjugate == (
Image *) NULL)
2142 return((
Image *) NULL);
2146 temp_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
2147 if (temp_image == (
Image *) NULL)
2149 complex_conjugate=DestroyImageList(complex_conjugate);
2150 return((
Image *) NULL);
2152 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2153 alpha_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2154 temp_image=DestroyImageList(temp_image);
2155 if (alpha_fft == (
Image *) NULL)
2157 complex_conjugate=DestroyImageList(complex_conjugate);
2158 return((
Image *) NULL);
2163 DisableCompositeClampUnlessSpecified(complex_conjugate);
2164 DisableCompositeClampUnlessSpecified(complex_conjugate->next);
2165 complex_conjugate->next->next=alpha_fft;
2166 complex_multiplication=ComplexImages(complex_conjugate,
2167 MultiplyComplexOperator,exception);
2168 complex_conjugate=DestroyImageList(complex_conjugate);
2169 if (complex_multiplication == (
Image *) NULL)
2170 return((
Image *) NULL);
2174 cross_correlation=InverseFourierTransformImage(complex_multiplication,
2175 complex_multiplication->next,MagickFalse,exception);
2176 complex_multiplication=DestroyImageList(complex_multiplication);
2177 return(cross_correlation);
2180static Image *SIMDerivativeImage(
const Image *image,
const char *kernel,
2189 kernel_info=AcquireKernelInfo(kernel,exception);
2191 return((
Image *) NULL);
2192 derivative_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
2194 kernel_info=DestroyKernelInfo(kernel_info);
2195 return(derivative_image);
2198static Image *SIMDivideImage(
const Image *numerator_image,
2217 divide_image=CloneImage(numerator_image,0,0,MagickTrue,exception);
2218 if (divide_image == (
Image *) NULL)
2219 return(divide_image);
2221 numerator_view=AcquireAuthenticCacheView(divide_image,exception);
2222 denominator_view=AcquireVirtualCacheView(denominator_image,exception);
2223#if defined(MAGICKCORE_OPENMP_SUPPORT)
2224 #pragma omp parallel for schedule(static) shared(status) \
2225 magick_number_threads(denominator_image,divide_image,divide_image->rows,1)
2227 for (y=0; y < (ssize_t) divide_image->rows; y++)
2238 if (status == MagickFalse)
2240 p=GetCacheViewVirtualPixels(denominator_view,0,y,
2241 denominator_image->columns,1,exception);
2242 q=GetCacheViewAuthenticPixels(numerator_view,0,y,divide_image->columns,1,
2244 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2249 for (x=0; x < (ssize_t) divide_image->columns; x++)
2254 for (i=0; i < (ssize_t) GetPixelChannels(divide_image); i++)
2256 PixelChannel channel = GetPixelChannelChannel(divide_image,i);
2257 PixelTrait traits = GetPixelChannelTraits(divide_image,channel);
2258 if ((traits & UpdatePixelTrait) == 0)
2260 if (fabs(p[i]) >= MagickEpsilon)
2261 q[i]=(Quantum) ((
double) q[i]*PerceptibleReciprocal(QuantumScale*
2264 p+=(ptrdiff_t) GetPixelChannels(denominator_image);
2265 q+=(ptrdiff_t) GetPixelChannels(divide_image);
2267 if (SyncCacheViewAuthenticPixels(numerator_view,exception) == MagickFalse)
2270 denominator_view=DestroyCacheView(denominator_view);
2271 numerator_view=DestroyCacheView(numerator_view);
2272 if (status == MagickFalse)
2273 divide_image=DestroyImage(divide_image);
2274 return(divide_image);
2277static Image *SIMDivideByMagnitude(
Image *image,
Image *magnitude_image,
2287 divide_image=SIMDivideImage(image,magnitude_image,exception);
2288 if (divide_image == (
Image *) NULL)
2289 return((
Image *) NULL);
2290 GetPixelInfoRGBA(0,0,0,0,÷_image->background_color);
2291 SetGeometry(source_image,&geometry);
2292 geometry.width=MagickMax(source_image->columns,divide_image->columns);
2293 geometry.height=MagickMax(source_image->rows,divide_image->rows);
2294 result_image=ExtentImage(divide_image,&geometry,exception);
2295 divide_image=DestroyImage(divide_image);
2296 return(result_image);
2314 image_view=AcquireAuthenticCacheView(image,exception);
2315#if defined(MAGICKCORE_OPENMP_SUPPORT)
2316 #pragma omp parallel for schedule(static) shared(status) \
2317 magick_number_threads(image,image,image->rows,1)
2319 for (y=0; y < (ssize_t) image->rows; y++)
2327 if (status == MagickFalse)
2329 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2330 if (q == (Quantum *) NULL)
2335 for (x=0; x < (ssize_t) image->columns; x++)
2340 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2342 PixelChannel channel = GetPixelChannelChannel(image,i);
2343 PixelTrait traits = GetPixelChannelTraits(image,channel);
2344 if ((traits & UpdatePixelTrait) == 0)
2346 q[i]=(Quantum) (QuantumRange*10.0*MagickLog10((
double) q[i]));
2348 q+=(ptrdiff_t) GetPixelChannels(image);
2350 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2353 image_view=DestroyCacheView(image_view);
2374 square_image=CloneImage(image,0,0,MagickTrue,exception);
2375 if (square_image == (
Image *) NULL)
2376 return(square_image);
2378 image_view=AcquireAuthenticCacheView(square_image,exception);
2379#if defined(MAGICKCORE_OPENMP_SUPPORT)
2380 #pragma omp parallel for schedule(static) shared(status) \
2381 magick_number_threads(square_image,square_image,square_image->rows,1)
2383 for (y=0; y < (ssize_t) square_image->rows; y++)
2391 if (status == MagickFalse)
2393 q=GetCacheViewAuthenticPixels(image_view,0,y,square_image->columns,1,
2395 if (q == (Quantum *) NULL)
2400 for (x=0; x < (ssize_t) square_image->columns; x++)
2405 for (i=0; i < (ssize_t) GetPixelChannels(square_image); i++)
2407 PixelChannel channel = GetPixelChannelChannel(square_image,i);
2408 PixelTrait traits = GetPixelChannelTraits(square_image,channel);
2409 if ((traits & UpdatePixelTrait) == 0)
2411 q[i]=(Quantum) ((
double) q[i]*QuantumScale*(
double) q[i]);
2413 q+=(ptrdiff_t) GetPixelChannels(square_image);
2415 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2418 image_view=DestroyCacheView(image_view);
2419 if (status == MagickFalse)
2420 square_image=DestroyImage(square_image);
2421 return(square_image);
2435 (void) SetImageArtifact(alpha_image,
"compose:clamp",
"False");
2436 xsq_image=SIMSquareImage(alpha_image,exception);
2437 if (xsq_image == (
Image *) NULL)
2438 return((
Image *) NULL);
2439 (void) SetImageArtifact(beta_image,
"compose:clamp",
"False");
2440 ysq_image=SIMSquareImage(beta_image,exception);
2441 if (ysq_image == (
Image *) NULL)
2443 xsq_image=DestroyImage(xsq_image);
2444 return((
Image *) NULL);
2446 status=CompositeImage(xsq_image,ysq_image,PlusCompositeOp,MagickTrue,0,0,
2448 magnitude_image=xsq_image;
2449 ysq_image=DestroyImage(ysq_image);
2450 if (status == MagickFalse)
2452 magnitude_image=DestroyImage(magnitude_image);
2453 return((
Image *) NULL);
2455 status=EvaluateImage(magnitude_image,PowEvaluateOperator,0.5,exception);
2456 if (status == MagickFalse)
2458 magnitude_image=DestroyImage(magnitude_image);
2459 return (
Image *) NULL;
2461 return(magnitude_image);
2464static MagickBooleanType SIMMaximaImage(
const Image *image,
double *maxima,
2480 *maxima=MagickMinimumValue;
2483 image_view=AcquireVirtualCacheView(image,exception);
2484#if defined(MAGICKCORE_OPENMP_SUPPORT)
2485 #pragma omp parallel for schedule(static) shared(status) \
2486 magick_number_threads(image,image,image->rows,1)
2488 for (y=0; y < (ssize_t) image->rows; y++)
2500 if (status == MagickFalse)
2502 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2503 if (p == (
const Quantum *) NULL)
2508 row_maxima=(double) p[0];
2510 for (x=0; x < (ssize_t) image->columns; x++)
2515 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2517 PixelChannel channel = GetPixelChannelChannel(image,i);
2518 PixelTrait traits = GetPixelChannelTraits(image,channel);
2519 if ((traits & UpdatePixelTrait) == 0)
2521 if ((
double) p[i] > row_maxima)
2523 row_maxima=(double) p[i];
2527 p+=(ptrdiff_t) GetPixelChannels(image);
2529#if defined(MAGICKCORE_OPENMP_SUPPORT)
2530 #pragma omp critical (MagickCore_SIMMaximaImage)
2532 if (row_maxima > *maxima)
2538 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2541 image_view=DestroyCacheView(image_view);
2545static MagickBooleanType SIMMinimaImage(
const Image *image,
double *minima,
2561 *minima=MagickMaximumValue;
2564 image_view=AcquireVirtualCacheView(image,exception);
2565#if defined(MAGICKCORE_OPENMP_SUPPORT)
2566 #pragma omp parallel for schedule(static) shared(status) \
2567 magick_number_threads(image,image,image->rows,1)
2569 for (y=0; y < (ssize_t) image->rows; y++)
2581 if (status == MagickFalse)
2583 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2584 if (p == (
const Quantum *) NULL)
2589 row_minima=(double) p[0];
2591 for (x=0; x < (ssize_t) image->columns; x++)
2596 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2598 PixelChannel channel = GetPixelChannelChannel(image,i);
2599 PixelTrait traits = GetPixelChannelTraits(image,channel);
2600 if ((traits & UpdatePixelTrait) == 0)
2602 if ((
double) p[i] < row_minima)
2604 row_minima=(double) p[i];
2608 p+=(ptrdiff_t) GetPixelChannels(image);
2610#if defined(MAGICKCORE_OPENMP_SUPPORT)
2611 #pragma omp critical (MagickCore_SIMMinimaImage)
2613 if (row_minima < *minima)
2619 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2622 image_view=DestroyCacheView(image_view);
2626static MagickBooleanType SIMMultiplyImage(
Image *image,
const double factor,
2642 image_view=AcquireAuthenticCacheView(image,exception);
2643#if defined(MAGICKCORE_OPENMP_SUPPORT)
2644 #pragma omp parallel for schedule(static) shared(status) \
2645 magick_number_threads(image,image,image->rows,1)
2647 for (y=0; y < (ssize_t) image->rows; y++)
2655 if (status == MagickFalse)
2657 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2658 if (q == (Quantum *) NULL)
2663 for (x=0; x < (ssize_t) image->columns; x++)
2668 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2670 PixelChannel channel = GetPixelChannelChannel(image,i);
2671 PixelTrait traits = GetPixelChannelTraits(image,channel);
2672 if ((traits & UpdatePixelTrait) == 0)
2675 q[i]=(Quantum) ((
double) q[i]*QuantumScale*
2676 channel_statistics[channel].standard_deviation);
2677 q[i]=(Quantum) ((
double) q[i]*factor);
2679 q+=(ptrdiff_t) GetPixelChannels(image);
2681 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2684 image_view=DestroyCacheView(image_view);
2688static Image *SIMPhaseCorrelationImage(
const Image *alpha_image,
2692 *alpha_fft = (
Image *) NULL,
2693 *beta_fft = (
Image *) NULL,
2694 *complex_multiplication = (
Image *) NULL,
2695 *cross_correlation = (
Image *) NULL;
2700 beta_fft=CloneImage(beta_image,0,0,MagickTrue,exception);
2701 if (beta_fft == NULL)
2702 return((
Image *) NULL);
2703 (void) SetImageArtifact(beta_fft,
"fourier:normalize",
"inverse");
2704 beta_fft=ForwardFourierTransformImage(beta_fft,MagickFalse,exception);
2705 if (beta_fft == NULL)
2706 return((
Image *) NULL);
2710 alpha_fft=CloneImage(alpha_image,0,0,MagickTrue,exception);
2711 if (alpha_fft == (
Image *) NULL)
2713 beta_fft=DestroyImageList(beta_fft);
2714 return((
Image *) NULL);
2716 (void) SetImageArtifact(alpha_fft,
"fourier:normalize",
"inverse");
2717 alpha_fft=ForwardFourierTransformImage(alpha_fft,MagickFalse,exception);
2718 if (alpha_fft == (
Image *) NULL)
2720 beta_fft=DestroyImageList(beta_fft);
2721 return((
Image *) NULL);
2726 beta_fft=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
2727 if (beta_fft == (
Image *) NULL)
2729 alpha_fft=DestroyImageList(alpha_fft);
2730 return((
Image *) NULL);
2735 beta_fft->next->next=alpha_fft;
2736 DisableCompositeClampUnlessSpecified(beta_fft);
2737 DisableCompositeClampUnlessSpecified(beta_fft->next);
2738 complex_multiplication=ComplexImages(beta_fft,MultiplyComplexOperator,
2740 beta_fft=DestroyImageList(beta_fft);
2741 if (complex_multiplication == (
Image *) NULL)
2742 return((
Image *) NULL);
2746 CompositeLayers(complex_multiplication,DivideSrcCompositeOp,(
Image *)
2747 magnitude_image,0,0,exception);
2751 (void) SetImageArtifact(complex_multiplication,
"fourier:normalize",
"inverse");
2752 cross_correlation=InverseFourierTransformImage(complex_multiplication,
2753 complex_multiplication->next,MagickFalse,exception);
2754 complex_multiplication=DestroyImageList(complex_multiplication);
2755 return(cross_correlation);
2758static MagickBooleanType SIMSetImageMean(
const Image *image,
2774 image_view=AcquireAuthenticCacheView(image,exception);
2775#if defined(MAGICKCORE_OPENMP_SUPPORT)
2776 #pragma omp parallel for schedule(static) shared(status) \
2777 magick_number_threads(image,image,image->rows,1)
2779 for (y=0; y < (ssize_t) image->rows; y++)
2787 if (status == MagickFalse)
2789 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2790 if (q == (Quantum *) NULL)
2795 for (x=0; x < (ssize_t) image->columns; x++)
2800 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2802 PixelChannel channel = GetPixelChannelChannel(image,i);
2803 PixelTrait traits = GetPixelChannelTraits(image,channel);
2804 if ((traits & UpdatePixelTrait) == 0)
2806 q[i]=(Quantum) channel_statistics[channel].mean;
2808 q+=(ptrdiff_t) GetPixelChannels(image);
2810 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2813 image_view=DestroyCacheView(image_view);
2817static Image *SIMSubtractImageMean(
const Image *alpha_image,
2837 subtract_image=CloneImage(beta_image,alpha_image->columns,alpha_image->rows,
2838 MagickTrue,exception);
2839 if (subtract_image == (
Image *) NULL)
2840 return(subtract_image);
2842 image_view=AcquireAuthenticCacheView(subtract_image,exception);
2843 beta_view=AcquireVirtualCacheView(beta_image,exception);
2844#if defined(MAGICKCORE_OPENMP_SUPPORT)
2845 #pragma omp parallel for schedule(static) shared(status) \
2846 magick_number_threads(beta_image,subtract_image,subtract_image->rows,1)
2848 for (y=0; y < (ssize_t) subtract_image->rows; y++)
2859 if (status == MagickFalse)
2861 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,exception);
2862 q=GetCacheViewAuthenticPixels(image_view,0,y,subtract_image->columns,1,
2864 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2869 for (x=0; x < (ssize_t) subtract_image->columns; x++)
2874 for (i=0; i < (ssize_t) GetPixelChannels(subtract_image); i++)
2876 PixelChannel channel = GetPixelChannelChannel(subtract_image,i);
2877 PixelTrait traits = GetPixelChannelTraits(subtract_image,channel);
2878 if ((traits & UpdatePixelTrait) == 0)
2880 if ((x >= (ssize_t) beta_image->columns) ||
2881 (y >= (ssize_t) beta_image->rows))
2884 q[i]=(Quantum) ((
double) p[i]-channel_statistics[channel].mean);
2886 p+=(ptrdiff_t) GetPixelChannels(beta_image);
2887 q+=(ptrdiff_t) GetPixelChannels(subtract_image);
2889 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2892 beta_view=DestroyCacheView(beta_view);
2893 image_view=DestroyCacheView(image_view);
2894 if (status == MagickFalse)
2895 subtract_image=DestroyImage(subtract_image);
2896 return(subtract_image);
2899static Image *SIMUnityImage(
const Image *alpha_image,
const Image *beta_image,
2917 unity_image=CloneImage(alpha_image,alpha_image->columns,alpha_image->rows,
2918 MagickTrue,exception);
2919 if (unity_image == (
Image *) NULL)
2920 return(unity_image);
2921 if (SetImageStorageClass(unity_image,DirectClass,exception) == MagickFalse)
2922 return(DestroyImage(unity_image));
2924 image_view=AcquireAuthenticCacheView(unity_image,exception);
2925#if defined(MAGICKCORE_OPENMP_SUPPORT)
2926 #pragma omp parallel for schedule(static) shared(status) \
2927 magick_number_threads(unity_image,unity_image,unity_image->rows,1)
2929 for (y=0; y < (ssize_t) unity_image->rows; y++)
2937 if (status == MagickFalse)
2939 q=GetCacheViewAuthenticPixels(image_view,0,y,unity_image->columns,1,
2941 if (q == (Quantum *) NULL)
2946 for (x=0; x < (ssize_t) unity_image->columns; x++)
2951 for (i=0; i < (ssize_t) GetPixelChannels(unity_image); i++)
2953 PixelChannel channel = GetPixelChannelChannel(unity_image,i);
2954 PixelTrait traits = GetPixelChannelTraits(unity_image,channel);
2955 if ((traits & UpdatePixelTrait) == 0)
2957 if ((x >= (ssize_t) beta_image->columns) ||
2958 (y >= (ssize_t) beta_image->rows))
2963 q+=(ptrdiff_t) GetPixelChannels(unity_image);
2965 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2968 image_view=DestroyCacheView(image_view);
2969 if (status == MagickFalse)
2970 unity_image=DestroyImage(unity_image);
2971 return(unity_image);
2974static Image *SIMVarianceImage(
Image *alpha_image,
const Image *beta_image,
2993 variance_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
2994 if (variance_image == (
Image *) NULL)
2995 return(variance_image);
2997 image_view=AcquireAuthenticCacheView(variance_image,exception);
2998 beta_view=AcquireVirtualCacheView(beta_image,exception);
2999#if defined(MAGICKCORE_OPENMP_SUPPORT)
3000 #pragma omp parallel for schedule(static) shared(status) \
3001 magick_number_threads(beta_image,variance_image,variance_image->rows,1)
3003 for (y=0; y < (ssize_t) variance_image->rows; y++)
3014 if (status == MagickFalse)
3016 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,
3018 q=GetCacheViewAuthenticPixels(image_view,0,y,variance_image->columns,1,
3020 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3025 for (x=0; x < (ssize_t) variance_image->columns; x++)
3030 for (i=0; i < (ssize_t) GetPixelChannels(variance_image); i++)
3032 PixelChannel channel = GetPixelChannelChannel(variance_image,i);
3033 PixelTrait traits = GetPixelChannelTraits(variance_image,channel);
3034 if ((traits & UpdatePixelTrait) == 0)
3036 q[i]=(Quantum) ((
double) ClampToQuantum((
double) QuantumRange*
3037 sqrt(fabs(QuantumScale*((
double) q[i]-(
double) p[i]))))/
3038 sqrt((
double) QuantumRange));
3040 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3041 q+=(ptrdiff_t) GetPixelChannels(variance_image);
3043 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3046 beta_view=DestroyCacheView(beta_view);
3047 image_view=DestroyCacheView(image_view);
3048 if (status == MagickFalse)
3049 variance_image=DestroyImage(variance_image);
3050 return(variance_image);
3053static Image *DPCSimilarityImage(
const Image *image,
const Image *reconstruct,
3056#define ThrowDPCSimilarityException() \
3058 if (dot_product_image != (Image *) NULL) \
3059 dot_product_image=DestroyImage(dot_product_image); \
3060 if (magnitude_image != (Image *) NULL) \
3061 magnitude_image=DestroyImage(magnitude_image); \
3062 if (reconstruct_image != (Image *) NULL) \
3063 reconstruct_image=DestroyImage(reconstruct_image); \
3064 if (rx_image != (Image *) NULL) \
3065 rx_image=DestroyImage(rx_image); \
3066 if (ry_image != (Image *) NULL) \
3067 ry_image=DestroyImage(ry_image); \
3068 if (test_image != (Image *) NULL) \
3069 test_image=DestroyImage(test_image); \
3070 if (threshold_image != (Image *) NULL) \
3071 threshold_image=DestroyImage(threshold_image); \
3072 if (trx_image != (Image *) NULL) \
3073 trx_image=DestroyImage(trx_image); \
3074 if (try_image != (Image *) NULL) \
3075 try_image=DestroyImage(try_image); \
3076 if (tx_image != (Image *) NULL) \
3077 tx_image=DestroyImage(tx_image); \
3078 if (ty_image != (Image *) NULL) \
3079 ty_image=DestroyImage(ty_image); \
3080 return((Image *) NULL); \
3087 standard_deviation = 0.0;
3090 *dot_product_image = (
Image *) NULL,
3091 *magnitude_image = (
Image *) NULL,
3092 *reconstruct_image = (
Image *) NULL,
3093 *rx_image = (
Image *) NULL,
3094 *ry_image = (
Image *) NULL,
3095 *trx_image = (
Image *) NULL,
3096 *temp_image = (
Image *) NULL,
3097 *test_image = (
Image *) NULL,
3098 *threshold_image = (
Image *) NULL,
3099 *try_image = (
Image *) NULL,
3100 *tx_image = (
Image *) NULL,
3101 *ty_image = (
Image *) NULL;
3112 test_image=CloneImage(image,0,0,MagickTrue,exception);
3113 if (test_image == (
Image *) NULL)
3114 return((
Image *) NULL);
3115 GetPixelInfoRGBA(0,0,0,0,&test_image->background_color);
3116 (void) ResetImagePage(test_image,
"0x0+0+0");
3117 status=SetImageExtent(test_image,2*(
size_t) ceil((
double) image->columns/2.0),
3118 2*(
size_t) ceil((
double) image->rows/2.0),exception);
3119 if (status == MagickFalse)
3120 ThrowDPCSimilarityException();
3121 (void) SetImageAlphaChannel(test_image,OffAlphaChannel,exception);
3125 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3126 if (reconstruct_image == (
Image *) NULL)
3127 ThrowDPCSimilarityException();
3128 GetPixelInfoRGBA(0,0,0,0,&reconstruct_image->background_color);
3129 (void) ResetImagePage(reconstruct_image,
"0x0+0+0");
3130 (void) SetImageAlphaChannel(reconstruct_image,OffAlphaChannel,exception);
3134 (void) SetImageVirtualPixelMethod(reconstruct_image,EdgeVirtualPixelMethod,
3136 rx_image=SIMDerivativeImage(reconstruct_image,
"Sobel",exception);
3137 if (rx_image == (
Image *) NULL)
3138 ThrowDPCSimilarityException();
3139 ry_image=SIMDerivativeImage(reconstruct_image,
"Sobel:90",exception);
3140 reconstruct_image=DestroyImage(reconstruct_image);
3141 if (ry_image == (
Image *) NULL)
3142 ThrowDPCSimilarityException();
3146 magnitude_image=SIMMagnitudeImage(rx_image,ry_image,exception);
3147 if (magnitude_image == (
Image *) NULL)
3148 ThrowDPCSimilarityException();
3152 threshold_image=CloneImage(magnitude_image,0,0,MagickTrue,exception);
3153 if (threshold_image == (
Image *) NULL)
3154 ThrowDPCSimilarityException();
3155 status=BilevelImage(threshold_image,0.0,exception);
3156 if (status == MagickFalse)
3157 ThrowDPCSimilarityException();
3158 status=GetImageMean(threshold_image,&mean,&standard_deviation,exception);
3159 threshold_image=DestroyImage(threshold_image);
3160 if (status == MagickFalse)
3161 ThrowDPCSimilarityException();
3162 edge_factor=1.0/(QuantumScale*mean)/reconstruct->columns/reconstruct->rows;
3166 temp_image=SIMDivideByMagnitude(rx_image,magnitude_image,image,exception);
3167 rx_image=DestroyImage(rx_image);
3168 if (temp_image == (
Image *) NULL)
3169 ThrowDPCSimilarityException();
3170 rx_image=temp_image;
3171 try_image=SIMDivideByMagnitude(ry_image,magnitude_image,image,exception);
3172 magnitude_image=DestroyImage(magnitude_image);
3173 ry_image=DestroyImage(ry_image);
3174 if (try_image == (
Image *) NULL)
3175 ThrowDPCSimilarityException();
3180 (void) SetImageVirtualPixelMethod(test_image,EdgeVirtualPixelMethod,
3182 tx_image=SIMDerivativeImage(test_image,
"Sobel",exception);
3183 if (tx_image == (
Image *) NULL)
3184 ThrowDPCSimilarityException();
3185 ty_image=SIMDerivativeImage(test_image,
"Sobel:90",exception);
3186 test_image=DestroyImage(test_image);
3187 if (ty_image == (
Image *) NULL)
3188 ThrowDPCSimilarityException();
3192 magnitude_image=SIMMagnitudeImage(tx_image,ty_image,exception);
3193 if (magnitude_image == (
Image *) NULL)
3194 ThrowDPCSimilarityException();
3198 temp_image=SIMDivideByMagnitude(tx_image,magnitude_image,image,exception);
3199 tx_image=DestroyImage(tx_image);
3200 if (temp_image == (
Image *) NULL)
3201 ThrowDPCSimilarityException();
3202 tx_image=temp_image;
3203 try_image=SIMDivideByMagnitude(ty_image,magnitude_image,image,exception);
3204 ty_image=DestroyImage(ty_image);
3205 magnitude_image=DestroyImage(magnitude_image);
3206 if (try_image == (
Image *) NULL)
3207 ThrowDPCSimilarityException();
3212 trx_image=SIMCrossCorrelationImage(tx_image,rx_image,exception);
3213 rx_image=DestroyImage(rx_image);
3214 tx_image=DestroyImage(tx_image);
3215 if (trx_image == (
Image *) NULL)
3216 ThrowDPCSimilarityException();
3217 try_image=SIMCrossCorrelationImage(ty_image,ry_image,exception);
3218 ry_image=DestroyImage(ry_image);
3219 ty_image=DestroyImage(ty_image);
3220 if (try_image == (
Image *) NULL)
3221 ThrowDPCSimilarityException();
3225 (void) SetImageArtifact(try_image,
"compose:clamp",
"False");
3226 status=CompositeImage(trx_image,try_image,PlusCompositeOp,MagickTrue,0,0,
3228 try_image=DestroyImage(try_image);
3229 if (status == MagickFalse)
3230 ThrowDPCSimilarityException();
3231 status=SIMMultiplyImage(trx_image,edge_factor,
3233 if (status == MagickFalse)
3234 ThrowDPCSimilarityException();
3238 SetGeometry(image,&geometry);
3239 geometry.width=image->columns;
3240 geometry.height=image->rows;
3241 (void) ResetImagePage(trx_image,
"0x0+0+0");
3242 dot_product_image=CropImage(trx_image,&geometry,exception);
3243 trx_image=DestroyImage(trx_image);
3244 if (dot_product_image == (
Image *) NULL)
3245 ThrowDPCSimilarityException();
3246 (void) ResetImagePage(dot_product_image,
"0x0+0+0");
3250 status=GrayscaleImage(dot_product_image,AveragePixelIntensityMethod,
3252 if (status == MagickFalse)
3253 ThrowDPCSimilarityException();
3254 dot_product_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3255 status=SIMMaximaImage(dot_product_image,&maxima,offset,exception);
3256 if (status == MagickFalse)
3257 ThrowDPCSimilarityException();
3258 *similarity_metric=1.0-QuantumScale*maxima;
3259 return(dot_product_image);
3262static Image *MSESimilarityImage(
const Image *image,
const Image *reconstruct,
3265#define ThrowMSESimilarityException() \
3267 if (alpha_image != (Image *) NULL) \
3268 alpha_image=DestroyImage(alpha_image); \
3269 if (beta_image != (Image *) NULL) \
3270 beta_image=DestroyImage(beta_image); \
3271 if (channel_statistics != (ChannelStatistics *) NULL) \
3272 channel_statistics=(ChannelStatistics *) \
3273 RelinquishMagickMemory(channel_statistics); \
3274 if (mean_image != (Image *) NULL) \
3275 mean_image=DestroyImage(mean_image); \
3276 if (mse_image != (Image *) NULL) \
3277 mse_image=DestroyImage(mse_image); \
3278 if (reconstruct_image != (Image *) NULL) \
3279 reconstruct_image=DestroyImage(reconstruct_image); \
3280 if (sum_image != (Image *) NULL) \
3281 sum_image=DestroyImage(sum_image); \
3282 if (alpha_image != (Image *) NULL) \
3283 alpha_image=DestroyImage(alpha_image); \
3284 return((Image *) NULL); \
3294 *alpha_image = (
Image *) NULL,
3295 *beta_image = (
Image *) NULL,
3296 *mean_image = (
Image *) NULL,
3297 *mse_image = (
Image *) NULL,
3298 *reconstruct_image = (
Image *) NULL,
3299 *sum_image = (
Image *) NULL,
3300 *test_image = (
Image *) NULL;
3311 test_image=SIMSquareImage(image,exception);
3312 if (test_image == (
Image *) NULL)
3313 ThrowMSESimilarityException();
3314 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3315 if (reconstruct_image == (
Image *) NULL)
3316 ThrowMSESimilarityException();
3320 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3321 test_image=DestroyImage(test_image);
3322 if (alpha_image == (
Image *) NULL)
3323 ThrowMSESimilarityException();
3324 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3326 if (status == MagickFalse)
3327 ThrowMSESimilarityException();
3331 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3332 MagickTrue,0,0,exception);
3333 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3334 reconstruct_image=DestroyImage(reconstruct_image);
3335 if (beta_image == (
Image *) NULL)
3336 ThrowMSESimilarityException();
3337 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3339 if (status == MagickFalse)
3340 ThrowMSESimilarityException();
3344 sum_image=SIMSquareImage(reconstruct,exception);
3345 if (sum_image == (
Image *) NULL)
3346 ThrowMSESimilarityException();
3347 channel_statistics=GetImageStatistics(sum_image,exception);
3349 ThrowMSESimilarityException();
3350 status=SetImageExtent(sum_image,image->columns,image->rows,exception);
3351 if (status == MagickFalse)
3352 ThrowMSESimilarityException();
3353 status=SetImageStorageClass(sum_image,DirectClass,exception);
3354 if (status == MagickFalse)
3355 ThrowMSESimilarityException();
3356 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3358 RelinquishMagickMemory(channel_statistics);
3359 if (status == MagickFalse)
3360 ThrowMSESimilarityException();
3364 AppendImageToList(&sum_image,alpha_image);
3365 AppendImageToList(&sum_image,beta_image);
3366 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3367 if (mean_image == (
Image *) NULL)
3368 ThrowMSESimilarityException();
3369 sum_image=DestroyImage(sum_image);
3370 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
3371 if (status == MagickFalse)
3372 ThrowMSESimilarityException();
3373 mean_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3377 SetGeometry(image,&geometry);
3378 geometry.width=image->columns;
3379 geometry.height=image->rows;
3380 (void) ResetImagePage(mean_image,
"0x0+0+0");
3381 mse_image=CropImage(mean_image,&geometry,exception);
3382 mean_image=DestroyImage(mean_image);
3383 if (mse_image == (
Image *) NULL)
3384 ThrowMSESimilarityException();
3388 (void) ResetImagePage(mse_image,
"0x0+0+0");
3389 (void) ClampImage(mse_image,exception);
3390 status=SIMMinimaImage(mse_image,&minima,offset,exception);
3391 if (status == MagickFalse)
3392 ThrowMSESimilarityException();
3393 status=NegateImage(mse_image,MagickFalse,exception);
3394 if (status == MagickFalse)
3395 ThrowMSESimilarityException();
3396 *similarity_metric=QuantumScale*minima;
3397 alpha_image=DestroyImage(alpha_image);
3398 beta_image=DestroyImage(beta_image);
3402static Image *NCCSimilarityImage(
const Image *image,
const Image *reconstruct,
3405#define ThrowNCCSimilarityException() \
3407 if (alpha_image != (Image *) NULL) \
3408 alpha_image=DestroyImage(alpha_image); \
3409 if (beta_image != (Image *) NULL) \
3410 beta_image=DestroyImage(beta_image); \
3411 if (channel_statistics != (ChannelStatistics *) NULL) \
3412 channel_statistics=(ChannelStatistics *) \
3413 RelinquishMagickMemory(channel_statistics); \
3414 if (correlation_image != (Image *) NULL) \
3415 correlation_image=DestroyImage(correlation_image); \
3416 if (divide_image != (Image *) NULL) \
3417 divide_image=DestroyImage(divide_image); \
3418 if (ncc_image != (Image *) NULL) \
3419 ncc_image=DestroyImage(ncc_image); \
3420 if (normalize_image != (Image *) NULL) \
3421 normalize_image=DestroyImage(normalize_image); \
3422 if (reconstruct_image != (Image *) NULL) \
3423 reconstruct_image=DestroyImage(reconstruct_image); \
3424 if (test_image != (Image *) NULL) \
3425 test_image=DestroyImage(test_image); \
3426 if (variance_image != (Image *) NULL) \
3427 variance_image=DestroyImage(variance_image); \
3428 return((Image *) NULL); \
3438 *alpha_image = (
Image *) NULL,
3439 *beta_image = (
Image *) NULL,
3440 *correlation_image = (
Image *) NULL,
3441 *divide_image = (
Image *) NULL,
3442 *ncc_image = (
Image *) NULL,
3443 *normalize_image = (
Image *) NULL,
3444 *reconstruct_image = (
Image *) NULL,
3445 *test_image = (
Image *) NULL,
3446 *variance_image = (
Image *) NULL;
3457 test_image=SIMSquareImage(image,exception);
3458 if (test_image == (
Image *) NULL)
3459 ThrowNCCSimilarityException();
3460 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3461 if (reconstruct_image == (
Image *) NULL)
3462 ThrowNCCSimilarityException();
3466 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3467 test_image=DestroyImage(test_image);
3468 if (alpha_image == (
Image *) NULL)
3469 ThrowNCCSimilarityException();
3470 status=SIMMultiplyImage(alpha_image,(
double) QuantumRange*
3473 if (status == MagickFalse)
3474 ThrowNCCSimilarityException();
3478 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3479 reconstruct_image=DestroyImage(reconstruct_image);
3480 if (beta_image == (
Image *) NULL)
3481 ThrowNCCSimilarityException();
3482 test_image=SIMSquareImage(beta_image,exception);
3483 beta_image=DestroyImage(beta_image);
3484 if (test_image == (
Image *) NULL)
3485 ThrowNCCSimilarityException();
3486 status=SIMMultiplyImage(test_image,(
double) QuantumRange,
3488 if (status == MagickFalse)
3489 ThrowNCCSimilarityException();
3493 variance_image=SIMVarianceImage(alpha_image,test_image,exception);
3494 test_image=DestroyImage(test_image);
3495 alpha_image=DestroyImage(alpha_image);
3496 if (variance_image == (
Image *) NULL)
3497 ThrowNCCSimilarityException();
3501 channel_statistics=GetImageStatistics(reconstruct,exception);
3503 ThrowNCCSimilarityException();
3504 status=SIMMultiplyImage(variance_image,1.0,channel_statistics,exception);
3505 if (status == MagickFalse)
3506 ThrowNCCSimilarityException();
3507 normalize_image=SIMSubtractImageMean(image,reconstruct,channel_statistics,
3510 RelinquishMagickMemory(channel_statistics);
3511 if (normalize_image == (
Image *) NULL)
3512 ThrowNCCSimilarityException();
3513 correlation_image=SIMCrossCorrelationImage(image,normalize_image,exception);
3514 normalize_image=DestroyImage(normalize_image);
3515 if (correlation_image == (
Image *) NULL)
3516 ThrowNCCSimilarityException();
3520 divide_image=SIMDivideImage(correlation_image,variance_image,exception);
3521 correlation_image=DestroyImage(correlation_image);
3522 variance_image=DestroyImage(variance_image);
3523 if (divide_image == (
Image *) NULL)
3524 ThrowNCCSimilarityException();
3528 SetGeometry(image,&geometry);
3529 geometry.width=image->columns;
3530 geometry.height=image->rows;
3531 (void) ResetImagePage(divide_image,
"0x0+0+0");
3532 ncc_image=CropImage(divide_image,&geometry,exception);
3533 divide_image=DestroyImage(divide_image);
3534 if (ncc_image == (
Image *) NULL)
3535 ThrowNCCSimilarityException();
3539 (void) ResetImagePage(ncc_image,
"0x0+0+0");
3540 status=GrayscaleImage(ncc_image,AveragePixelIntensityMethod,exception);
3541 if (status == MagickFalse)
3542 ThrowNCCSimilarityException();
3543 ncc_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3544 status=SIMMaximaImage(ncc_image,&maxima,offset,exception);
3545 if (status == MagickFalse)
3546 ThrowNCCSimilarityException();
3547 *similarity_metric=1.0-QuantumScale*maxima;
3551static Image *PhaseSimilarityImage(
const Image *image,
const Image *reconstruct,
3554#define ThrowPhaseSimilarityException() \
3556 if (correlation_image != (Image *) NULL) \
3557 correlation_image=DestroyImage(correlation_image); \
3558 if (fft_images != (Image *) NULL) \
3559 fft_images=DestroyImageList(fft_images); \
3560 if (gamma_image != (Image *) NULL) \
3561 gamma_image=DestroyImage(gamma_image); \
3562 if (magnitude_image != (Image *) NULL) \
3563 magnitude_image=DestroyImage(magnitude_image); \
3564 if (phase_image != (Image *) NULL) \
3565 phase_image=DestroyImage(phase_image); \
3566 if (reconstruct_image != (Image *) NULL) \
3567 reconstruct_image=DestroyImage(reconstruct_image); \
3568 if (reconstruct_magnitude != (Image *) NULL) \
3569 reconstruct_magnitude=DestroyImage(reconstruct_magnitude); \
3570 if (test_image != (Image *) NULL) \
3571 test_image=DestroyImage(test_image); \
3572 if (test_magnitude != (Image *) NULL) \
3573 test_magnitude=DestroyImage(test_magnitude); \
3574 return((Image *) NULL); \
3581 *correlation_image = (
Image *) NULL,
3582 *fft_images = (
Image *) NULL,
3583 *gamma_image = (
Image *) NULL,
3584 *magnitude_image = (
Image *) NULL,
3585 *phase_image = (
Image *) NULL,
3586 *reconstruct_image = (
Image *) NULL,
3587 *reconstruct_magnitude = (
Image *) NULL,
3588 *test_image = (
Image *) NULL,
3589 *test_magnitude = (
Image *) NULL;
3600 test_image=CloneImage(image,0,0,MagickTrue,exception);
3601 if (test_image == (
Image *) NULL)
3602 ThrowPhaseSimilarityException();
3603 GetPixelInfoRGBA(0,0,0,0,&test_image->background_color);
3604 (void) ResetImagePage(test_image,
"0x0+0+0");
3605 status=SetImageExtent(test_image,2*(
size_t) ceil((
double) image->columns/2.0),
3606 2*(
size_t) ceil((
double) image->rows/2.0),exception);
3607 if (status == MagickFalse)
3608 ThrowPhaseSimilarityException();
3609 (void) SetImageAlphaChannel(test_image,OffAlphaChannel,exception);
3613 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3614 if (reconstruct_image == (
Image *) NULL)
3615 ThrowPhaseSimilarityException();
3616 GetPixelInfoRGBA(0,0,0,0,&reconstruct_image->background_color);
3617 (void) ResetImagePage(reconstruct_image,
"0x0+0+0");
3618 status=SetImageExtent(reconstruct_image,2*(
size_t) ceil((
double)
3619 image->columns/2.0),2*(
size_t) ceil((
double) image->rows/2.0),exception);
3620 if (status == MagickFalse)
3621 ThrowPhaseSimilarityException();
3622 (void) SetImageAlphaChannel(reconstruct_image,OffAlphaChannel,exception);
3623 (void) SetImageArtifact(test_image,
"fourier:normalize",
"inverse");
3624 fft_images=ForwardFourierTransformImage(test_image,MagickTrue,exception);
3625 if (fft_images == (
Image *) NULL)
3626 ThrowPhaseSimilarityException();
3627 test_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
3628 fft_images=DestroyImageList(fft_images);
3629 if (test_magnitude == (
Image *) NULL)
3630 ThrowPhaseSimilarityException();
3631 (void) SetImageArtifact(reconstruct_image,
"fourier:normalize",
"inverse");
3632 fft_images=ForwardFourierTransformImage(reconstruct_image,MagickTrue,
3634 if (fft_images == (
Image *) NULL)
3635 ThrowPhaseSimilarityException();
3636 reconstruct_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
3637 fft_images=DestroyImageList(fft_images);
3638 if (reconstruct_magnitude == (
Image *) NULL)
3639 ThrowPhaseSimilarityException();
3640 magnitude_image=CloneImage(reconstruct_magnitude,0,0,MagickTrue,exception);
3641 if (magnitude_image == (
Image *) NULL)
3642 ThrowPhaseSimilarityException();
3643 DisableCompositeClampUnlessSpecified(magnitude_image);
3644 (void) CompositeImage(magnitude_image,test_magnitude,MultiplyCompositeOp,
3645 MagickTrue,0,0,exception);
3649 correlation_image=SIMPhaseCorrelationImage(test_image,reconstruct_image,
3650 magnitude_image,exception);
3651 test_image=DestroyImage(test_image);
3652 reconstruct_image=DestroyImage(reconstruct_image);
3653 test_magnitude=DestroyImage(test_magnitude);
3654 reconstruct_magnitude=DestroyImage(reconstruct_magnitude);
3655 if (correlation_image == (
Image *) NULL)
3656 ThrowPhaseSimilarityException();
3660 gamma_image=CloneImage(correlation_image,0,0,MagickTrue,exception);
3661 correlation_image=DestroyImage(correlation_image);
3662 if (gamma_image == (
Image *) NULL)
3663 ThrowPhaseSimilarityException();
3667 SetGeometry(image,&geometry);
3668 geometry.width=image->columns;
3669 geometry.height=image->rows;
3670 (void) ResetImagePage(gamma_image,
"0x0+0+0");
3671 phase_image=CropImage(gamma_image,&geometry,exception);
3672 gamma_image=DestroyImage(gamma_image);
3673 if (phase_image == (
Image *) NULL)
3674 ThrowPhaseSimilarityException();
3675 (void) ResetImagePage(phase_image,
"0x0+0+0");
3679 status=GrayscaleImage(phase_image,AveragePixelIntensityMethod,exception);
3680 if (status == MagickFalse)
3681 ThrowPhaseSimilarityException();
3682 phase_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3683 status=SIMMaximaImage(phase_image,&maxima,offset,exception);
3684 if (status == MagickFalse)
3685 ThrowPhaseSimilarityException();
3686 *similarity_metric=QuantumScale*maxima;
3687 magnitude_image=DestroyImage(magnitude_image);
3688 return(phase_image);
3691static Image *PSNRSimilarityImage(
const Image *image,
const Image *reconstruct,
3694#define ThrowPSNRSimilarityException() \
3696 if (alpha_image != (Image *) NULL) \
3697 alpha_image=DestroyImage(alpha_image); \
3698 if (beta_image != (Image *) NULL) \
3699 beta_image=DestroyImage(beta_image); \
3700 if (channel_statistics != (ChannelStatistics *) NULL) \
3701 channel_statistics=(ChannelStatistics *) \
3702 RelinquishMagickMemory(channel_statistics); \
3703 if (mean_image != (Image *) NULL) \
3704 mean_image=DestroyImage(mean_image); \
3705 if (psnr_image != (Image *) NULL) \
3706 psnr_image=DestroyImage(psnr_image); \
3707 if (reconstruct_image != (Image *) NULL) \
3708 reconstruct_image=DestroyImage(reconstruct_image); \
3709 if (sum_image != (Image *) NULL) \
3710 sum_image=DestroyImage(sum_image); \
3711 if (test_image != (Image *) NULL) \
3712 test_image=DestroyImage(test_image); \
3713 return((Image *) NULL); \
3723 *alpha_image = (
Image *) NULL,
3724 *beta_image = (
Image *) NULL,
3725 *mean_image = (
Image *) NULL,
3726 *psnr_image = (
Image *) NULL,
3727 *reconstruct_image = (
Image *) NULL,
3728 *sum_image = (
Image *) NULL,
3729 *test_image = (
Image *) NULL;
3740 test_image=SIMSquareImage(image,exception);
3741 if (test_image == (
Image *) NULL)
3742 ThrowPSNRSimilarityException();
3743 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3744 if (reconstruct_image == (
Image *) NULL)
3745 ThrowPSNRSimilarityException();
3749 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3750 test_image=DestroyImage(test_image);
3751 if (alpha_image == (
Image *) NULL)
3752 ThrowPSNRSimilarityException();
3753 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3755 if (status == MagickFalse)
3756 ThrowPSNRSimilarityException();
3760 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3761 MagickTrue,0,0,exception);
3762 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3763 reconstruct_image=DestroyImage(reconstruct_image);
3764 if (beta_image == (
Image *) NULL)
3765 ThrowPSNRSimilarityException();
3766 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3768 if (status == MagickFalse)
3769 ThrowPSNRSimilarityException();
3773 sum_image=SIMSquareImage(reconstruct,exception);
3774 if (sum_image == (
Image *) NULL)
3775 ThrowPSNRSimilarityException();
3776 channel_statistics=GetImageStatistics(sum_image,exception);
3778 ThrowPSNRSimilarityException();
3779 status=SetImageExtent(sum_image,image->columns,image->rows,exception);
3780 if (status == MagickFalse)
3781 ThrowPSNRSimilarityException();
3782 status=SetImageStorageClass(sum_image,DirectClass,exception);
3783 if (status == MagickFalse)
3784 ThrowPSNRSimilarityException();
3785 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3787 RelinquishMagickMemory(channel_statistics);
3788 if (status == MagickFalse)
3789 ThrowPSNRSimilarityException();
3793 AppendImageToList(&sum_image,alpha_image);
3794 AppendImageToList(&sum_image,beta_image);
3795 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3796 if (mean_image == (
Image *) NULL)
3797 ThrowPSNRSimilarityException();
3798 sum_image=DestroyImage(sum_image);
3799 status=SIMLogImage(mean_image,exception);
3800 if (status == MagickFalse)
3801 ThrowPSNRSimilarityException();
3802 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
3803 if (status == MagickFalse)
3804 ThrowPSNRSimilarityException();
3805 mean_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3806 status=SIMMultiplyImage(mean_image,1.0/48.1647,
3808 if (status == MagickFalse)
3809 ThrowPSNRSimilarityException();
3813 SetGeometry(image,&geometry);
3814 geometry.width=image->columns;
3815 geometry.height=image->rows;
3816 (void) ResetImagePage(mean_image,
"0x0+0+0");
3817 psnr_image=CropImage(mean_image,&geometry,exception);
3818 mean_image=DestroyImage(mean_image);
3819 if (psnr_image == (
Image *) NULL)
3820 ThrowPSNRSimilarityException();
3824 (void) ResetImagePage(psnr_image,
"0x0+0+0");
3825 (void) EvaluateImage(psnr_image,MaxEvaluateOperator,0.0,exception);
3826 status=SIMMinimaImage(psnr_image,&minima,offset,exception);
3827 if (status == MagickFalse)
3828 ThrowPSNRSimilarityException();
3829 status=NegateImage(psnr_image,MagickFalse,exception);
3830 if (status == MagickFalse)
3831 ThrowPSNRSimilarityException();
3832 *similarity_metric=QuantumScale*minima;
3833 alpha_image=DestroyImage(alpha_image);
3834 beta_image=DestroyImage(beta_image);
3838static Image *RMSESimilarityImage(
const Image *image,
const Image *reconstruct,
3841#define ThrowRMSESimilarityException() \
3843 if (alpha_image != (Image *) NULL) \
3844 alpha_image=DestroyImage(alpha_image); \
3845 if (beta_image != (Image *) NULL) \
3846 beta_image=DestroyImage(beta_image); \
3847 if (channel_statistics != (ChannelStatistics *) NULL) \
3848 channel_statistics=(ChannelStatistics *) \
3849 RelinquishMagickMemory(channel_statistics); \
3850 if (mean_image != (Image *) NULL) \
3851 mean_image=DestroyImage(mean_image); \
3852 if (rmse_image != (Image *) NULL) \
3853 rmse_image=DestroyImage(rmse_image); \
3854 if (reconstruct_image != (Image *) NULL) \
3855 reconstruct_image=DestroyImage(reconstruct_image); \
3856 if (sum_image != (Image *) NULL) \
3857 sum_image=DestroyImage(sum_image); \
3858 if (alpha_image != (Image *) NULL) \
3859 alpha_image=DestroyImage(alpha_image); \
3860 return((Image *) NULL); \
3870 *alpha_image = (
Image *) NULL,
3871 *beta_image = (
Image *) NULL,
3872 *mean_image = (
Image *) NULL,
3873 *reconstruct_image = (
Image *) NULL,
3874 *rmse_image = (
Image *) NULL,
3875 *sum_image = (
Image *) NULL,
3876 *test_image = (
Image *) NULL;
3887 test_image=SIMSquareImage(image,exception);
3888 if (test_image == (
Image *) NULL)
3889 ThrowRMSESimilarityException();
3890 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3891 if (reconstruct_image == (
Image *) NULL)
3892 ThrowRMSESimilarityException();
3896 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3897 test_image=DestroyImage(test_image);
3898 if (alpha_image == (
Image *) NULL)
3899 ThrowRMSESimilarityException();
3900 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3902 if (status == MagickFalse)
3903 ThrowRMSESimilarityException();
3907 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3908 MagickTrue,0,0,exception);
3909 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3910 reconstruct_image=DestroyImage(reconstruct_image);
3911 if (beta_image == (
Image *) NULL)
3912 ThrowRMSESimilarityException();
3913 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3915 if (status == MagickFalse)
3916 ThrowRMSESimilarityException();
3920 sum_image=SIMSquareImage(reconstruct,exception);
3921 if (sum_image == (
Image *) NULL)
3922 ThrowRMSESimilarityException();
3923 channel_statistics=GetImageStatistics(sum_image,exception);
3925 ThrowRMSESimilarityException();
3926 status=SetImageExtent(sum_image,image->columns,image->rows,exception);
3927 if (status == MagickFalse)
3928 ThrowRMSESimilarityException();
3929 status=SetImageStorageClass(sum_image,DirectClass,exception);
3930 if (status == MagickFalse)
3931 ThrowRMSESimilarityException();
3932 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3934 RelinquishMagickMemory(channel_statistics);
3935 if (status == MagickFalse)
3936 ThrowRMSESimilarityException();
3940 AppendImageToList(&sum_image,alpha_image);
3941 AppendImageToList(&sum_image,beta_image);
3942 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3943 if (mean_image == (
Image *) NULL)
3944 ThrowRMSESimilarityException();
3945 status=EvaluateImage(mean_image,PowEvaluateOperator,0.5,exception);
3946 if (mean_image == (
Image *) NULL)
3947 ThrowRMSESimilarityException();
3948 sum_image=DestroyImage(sum_image);
3949 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
3950 if (status == MagickFalse)
3951 ThrowRMSESimilarityException();
3952 mean_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3956 SetGeometry(image,&geometry);
3957 geometry.width=image->columns;
3958 geometry.height=image->rows;
3959 (void) ResetImagePage(mean_image,
"0x0+0+0");
3960 rmse_image=CropImage(mean_image,&geometry,exception);
3961 mean_image=DestroyImage(mean_image);
3962 if (rmse_image == (
Image *) NULL)
3963 ThrowRMSESimilarityException();
3967 (void) ResetImagePage(rmse_image,
"0x0+0+0");
3968 status=SIMMinimaImage(rmse_image,&minima,offset,exception);
3969 if (status == MagickFalse)
3970 ThrowRMSESimilarityException();
3971 status=NegateImage(rmse_image,MagickFalse,exception);
3972 if (status == MagickFalse)
3973 ThrowRMSESimilarityException();
3974 *similarity_metric=QuantumScale*minima;
3975 alpha_image=DestroyImage(alpha_image);
3976 beta_image=DestroyImage(beta_image);
3982static double GetSimilarityMetric(
const Image *image,
const Image *reconstruct,
3983 const MetricType metric,
const ssize_t x_offset,
const ssize_t y_offset,
3998 SetGeometry(reconstruct,&geometry);
3999 geometry.x=x_offset;
4000 geometry.y=y_offset;
4001 similarity_image=CropImage(image,&geometry,exception);
4002 if (similarity_image == (
Image *) NULL)
4005 status=GetImageDistortion(similarity_image,reconstruct,metric,&distortion,
4007 similarity_image=DestroyImage(similarity_image);
4008 if (status == MagickFalse)
4013MagickExport
Image *SimilarityImage(
const Image *image,
const Image *reconstruct,
4014 const MetricType metric,
const double similarity_threshold,
4017#define SimilarityImageTag "Similarity/Image"
4023 *similarity_image = (
Image *) NULL;
4037 assert(image != (
const Image *) NULL);
4038 assert(image->signature == MagickCoreSignature);
4040 assert(exception->signature == MagickCoreSignature);
4042 if (IsEventLogging() != MagickFalse)
4043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4044 SetGeometry(reconstruct,offset);
4045 *similarity_metric=MagickMaximumValue;
4046#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
4048 const char *artifact = GetImageArtifact(image,
"compare:frequency-domain");
4049 if (artifact == (
const char *) NULL)
4050 artifact=GetImageArtifact(image,
"compare:accelerate-ncc");
4051 if ((image->channels & ReadMaskChannel) == 0)
4054 case DotProductCorrelationErrorMetric:
4056 similarity_image=DPCSimilarityImage(image,reconstruct,offset,
4057 similarity_metric,exception);
4058 return(similarity_image);
4060 case MeanSquaredErrorMetric:
4062 if ((artifact != (
const char *) NULL) &&
4063 (IsStringTrue(artifact) == MagickFalse))
4065 similarity_image=MSESimilarityImage(image,reconstruct,offset,
4066 similarity_metric,exception);
4067 return(similarity_image);
4069 case NormalizedCrossCorrelationErrorMetric:
4071 if ((artifact != (
const char *) NULL) &&
4072 (IsStringTrue(artifact) == MagickFalse))
4074 similarity_image=NCCSimilarityImage(image,reconstruct,offset,
4075 similarity_metric,exception);
4076 return(similarity_image);
4078 case PeakSignalToNoiseRatioErrorMetric:
4080 if ((artifact != (
const char *) NULL) &&
4081 (IsStringTrue(artifact) == MagickFalse))
4083 similarity_image=PSNRSimilarityImage(image,reconstruct,offset,
4084 similarity_metric,exception);
4085 return(similarity_image);
4087 case PhaseCorrelationErrorMetric:
4089 similarity_image=PhaseSimilarityImage(image,reconstruct,offset,
4090 similarity_metric,exception);
4091 return(similarity_image);
4093 case RootMeanSquaredErrorMetric:
4095 if ((artifact != (
const char *) NULL) &&
4096 (IsStringTrue(artifact) == MagickFalse))
4098 similarity_image=RMSESimilarityImage(image,reconstruct,offset,
4099 similarity_metric,exception);
4100 return(similarity_image);
4106 if ((metric == DotProductCorrelationErrorMetric) ||
4107 (metric == PhaseCorrelationErrorMetric))
4109 (void) ThrowMagickException(exception,GetMagickModule(),
4110 MissingDelegateError,
"DelegateLibrarySupportNotBuiltIn",
4111 "'%s' (HDRI, FFT)",image->filename);
4112 return((
Image *) NULL);
4115 if ((image->columns < reconstruct->columns) ||
4116 (image->rows < reconstruct->rows))
4118 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
4119 "GeometryDoesNotContainImage",
"`%s'",image->filename);
4120 return((
Image *) NULL);
4122 similarity_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4124 if (similarity_image == (
Image *) NULL)
4125 return((
Image *) NULL);
4126 similarity_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4127 similarity_image->alpha_trait=UndefinedPixelTrait;
4128 similarity_image->type=GrayscaleType;
4129 status=SetImageStorageClass(similarity_image,DirectClass,exception);
4130 if (status == MagickFalse)
4131 return(DestroyImage(similarity_image));
4137 similarity_view=AcquireAuthenticCacheView(similarity_image,exception);
4138 rows=similarity_image->rows;
4139#if defined(MAGICKCORE_OPENMP_SUPPORT)
4140 #pragma omp parallel for schedule(static,1) \
4141 shared(progress,status,similarity_metric) \
4142 magick_number_threads(similarity_image,similarity_image,rows << 3,1)
4144 for (y=0; y < (ssize_t) rows; y++)
4155 if (status == MagickFalse)
4157#if defined(MMAGICKCORE_OPENMP_SUPPORT)
4158 #pragma omp flush(similarity_metric)
4160 if (*similarity_metric <= similarity_threshold)
4162 q=QueueCacheViewAuthenticPixels(similarity_view,0,y,
4163 similarity_image->columns,1,exception);
4164 if (q == (Quantum *) NULL)
4169 for (x=0; x < (ssize_t) similarity_image->columns; x++)
4174#if defined(MAGICKCORE_OPENMP_SUPPORT)
4175 #pragma omp flush(similarity_metric)
4177 if (*similarity_metric <= similarity_threshold)
4179 similarity=GetSimilarityMetric(image,reconstruct,metric,x,y,exception);
4180 if ((metric == DotProductCorrelationErrorMetric) ||
4181 (metric == PhaseCorrelationErrorMetric) ||
4182 (metric == NormalizedCrossCorrelationErrorMetric) ||
4183 (metric == UndefinedErrorMetric))
4184 similarity=1.0-similarity;
4185#if defined(MAGICKCORE_OPENMP_SUPPORT)
4186 #pragma omp critical (MagickCore_SimilarityImage)
4188 if (similarity < *similarity_metric)
4192 *similarity_metric=similarity;
4194 if (metric == PerceptualHashErrorMetric)
4195 similarity=MagickMin(0.01*similarity,1.0);
4196 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4198 PixelChannel channel = GetPixelChannelChannel(image,i);
4199 PixelTrait traits = GetPixelChannelTraits(image,channel);
4200 PixelTrait similarity_traits=GetPixelChannelTraits(similarity_image,
4202 if ((traits == UndefinedPixelTrait) ||
4203 (similarity_traits == UndefinedPixelTrait) ||
4204 ((similarity_traits & UpdatePixelTrait) == 0))
4206 if ((metric == MeanSquaredErrorMetric) ||
4207 (metric == NormalizedCrossCorrelationErrorMetric) ||
4208 (metric == RootMeanSquaredErrorMetric))
4210 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4211 QuantumRange-QuantumRange*similarity),q);
4214 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4215 QuantumRange*similarity),q);
4217 q+=(ptrdiff_t) GetPixelChannels(similarity_image);
4219 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
4221 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4226#if defined(MAGICKCORE_OPENMP_SUPPORT)
4230 proceed=SetImageProgress(image,SimilarityImageTag,progress,image->rows);
4231 if (proceed == MagickFalse)
4235 similarity_view=DestroyCacheView(similarity_view);
4236 if (status == MagickFalse)
4237 similarity_image=DestroyImage(similarity_image);
4238 return(similarity_image);