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) ResetImagePage(difference_image,
"0x0+0+0");
177 (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel,exception);
178 highlight_image=CloneImage(image,columns,rows,MagickTrue,exception);
179 if (highlight_image == (
Image *) NULL)
181 difference_image=DestroyImage(difference_image);
182 return((
Image *) NULL);
184 status=SetImageStorageClass(highlight_image,DirectClass,exception);
185 if (status == MagickFalse)
187 difference_image=DestroyImage(difference_image);
188 highlight_image=DestroyImage(highlight_image);
189 return((
Image *) NULL);
191 (void) SetImageMask(highlight_image,ReadPixelMask,(
Image *) NULL,exception);
192 (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel,exception);
193 (void) QueryColorCompliance(
"#f1001ecc",AllCompliance,&highlight,exception);
194 artifact=GetImageArtifact(image,
"compare:highlight-color");
195 if (artifact != (
const char *) NULL)
196 (void) QueryColorCompliance(artifact,AllCompliance,&highlight,exception);
197 (void) QueryColorCompliance(
"#ffffffcc",AllCompliance,&lowlight,exception);
198 artifact=GetImageArtifact(image,
"compare:lowlight-color");
199 if (artifact != (
const char *) NULL)
200 (void) QueryColorCompliance(artifact,AllCompliance,&lowlight,exception);
201 (void) QueryColorCompliance(
"#888888cc",AllCompliance,&masklight,exception);
202 artifact=GetImageArtifact(image,
"compare:masklight-color");
203 if (artifact != (
const char *) NULL)
204 (void) QueryColorCompliance(artifact,AllCompliance,&masklight,exception);
209 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
210 image_view=AcquireVirtualCacheView(image,exception);
211 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
212 highlight_view=AcquireAuthenticCacheView(highlight_image,exception);
213#if defined(MAGICKCORE_OPENMP_SUPPORT)
214 #pragma omp parallel for schedule(static) shared(status) \
215 magick_number_threads(image,highlight_image,rows,1)
217 for (y=0; y < (ssize_t) rows; y++)
232 if (status == MagickFalse)
234 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
235 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
236 r=QueueCacheViewAuthenticPixels(highlight_view,0,y,columns,1,exception);
237 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL) ||
238 (r == (Quantum *) NULL))
243 for (x=0; x < (ssize_t) columns; x++)
255 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
256 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
258 SetPixelViaPixelInfo(highlight_image,&masklight,r);
259 p+=(ptrdiff_t) GetPixelChannels(image);
260 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
261 r+=(ptrdiff_t) GetPixelChannels(highlight_image);
264 difference=MagickFalse;
265 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
266 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
267 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
273 PixelChannel channel = GetPixelChannelChannel(image,i);
274 PixelTrait traits = GetPixelChannelTraits(image,channel);
275 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
277 if ((traits == UndefinedPixelTrait) ||
278 (reconstruct_traits == UndefinedPixelTrait) ||
279 ((reconstruct_traits & UpdatePixelTrait) == 0))
281 if (channel == AlphaPixelChannel)
282 pixel=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
285 pixel=Sa*(double) p[i]-Da*(
double)
286 GetPixelChannel(reconstruct_image,channel,q);
287 distance=pixel*pixel;
288 if (distance >= fuzz)
290 difference=MagickTrue;
294 if (difference == MagickFalse)
295 SetPixelViaPixelInfo(highlight_image,&lowlight,r);
297 SetPixelViaPixelInfo(highlight_image,&highlight,r);
298 p+=(ptrdiff_t) GetPixelChannels(image);
299 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
300 r+=(ptrdiff_t) GetPixelChannels(highlight_image);
302 sync=SyncCacheViewAuthenticPixels(highlight_view,exception);
303 if (sync == MagickFalse)
306 highlight_view=DestroyCacheView(highlight_view);
307 reconstruct_view=DestroyCacheView(reconstruct_view);
308 image_view=DestroyCacheView(image_view);
309 (void) CompositeImage(difference_image,highlight_image,image->compose,
310 MagickTrue,0,0,exception);
311 highlight_image=DestroyImage(highlight_image);
312 if (status == MagickFalse)
313 difference_image=DestroyImage(difference_image);
314 return(difference_image);
351static MagickBooleanType GetAbsoluteDistortion(
const Image *image,
375 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
376 rows=MagickMax(image->rows,reconstruct_image->rows);
377 columns=MagickMax(image->columns,reconstruct_image->columns);
378 image_view=AcquireVirtualCacheView(image,exception);
379 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
380#if defined(MAGICKCORE_OPENMP_SUPPORT)
381 #pragma omp parallel for schedule(static) shared(status) \
382 magick_number_threads(image,image,rows,1)
384 for (y=0; y < (ssize_t) rows; y++)
387 channel_distortion[MaxPixelChannels+1];
397 if (status == MagickFalse)
399 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
400 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
401 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
406 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
407 for (x=0; x < (ssize_t) columns; x++)
419 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
420 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
422 p+=(ptrdiff_t) GetPixelChannels(image);
423 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
426 difference=MagickFalse;
427 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
428 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
429 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
435 PixelChannel channel = GetPixelChannelChannel(image,i);
436 PixelTrait traits = GetPixelChannelTraits(image,channel);
437 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
439 if ((traits == UndefinedPixelTrait) ||
440 (reconstruct_traits == UndefinedPixelTrait) ||
441 ((reconstruct_traits & UpdatePixelTrait) == 0))
443 if (channel == AlphaPixelChannel)
444 pixel=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
447 pixel=Sa*(double) p[i]-Da*(
double) GetPixelChannel(reconstruct_image,
449 distance=pixel*pixel;
450 if (distance >= fuzz)
452 channel_distortion[i]++;
453 difference=MagickTrue;
456 if (difference != MagickFalse)
457 channel_distortion[CompositePixelChannel]++;
458 p+=(ptrdiff_t) GetPixelChannels(image);
459 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
461#if defined(MAGICKCORE_OPENMP_SUPPORT)
462 #pragma omp critical (MagickCore_GetAbsoluteDistortion)
464 for (j=0; j <= MaxPixelChannels; j++)
465 distortion[j]+=channel_distortion[j];
467 reconstruct_view=DestroyCacheView(reconstruct_view);
468 image_view=DestroyCacheView(image_view);
472static MagickBooleanType GetFuzzDistortion(
const Image *image,
496 rows=MagickMax(image->rows,reconstruct_image->rows);
497 columns=MagickMax(image->columns,reconstruct_image->columns);
499 image_view=AcquireVirtualCacheView(image,exception);
500 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
501#if defined(MAGICKCORE_OPENMP_SUPPORT)
502 #pragma omp parallel for schedule(static) shared(status) \
503 magick_number_threads(image,image,rows,1)
505 for (y=0; y < (ssize_t) rows; y++)
508 channel_distortion[MaxPixelChannels+1];
520 if (status == MagickFalse)
522 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
523 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
524 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
529 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
530 for (x=0; x < (ssize_t) columns; x++)
539 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
540 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
542 p+=(ptrdiff_t) GetPixelChannels(image);
543 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
546 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
547 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
548 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
553 PixelChannel channel = GetPixelChannelChannel(image,i);
554 PixelTrait traits = GetPixelChannelTraits(image,channel);
555 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
557 if ((traits == UndefinedPixelTrait) ||
558 (reconstruct_traits == UndefinedPixelTrait) ||
559 ((reconstruct_traits & UpdatePixelTrait) == 0))
561 if (channel == AlphaPixelChannel)
562 distance=QuantumScale*((double) p[i]-(double) GetPixelChannel(
563 reconstruct_image,channel,q));
565 distance=QuantumScale*(Sa*(double) p[i]-Da*(double) GetPixelChannel(
566 reconstruct_image,channel,q));
567 channel_distortion[i]+=distance*distance;
568 channel_distortion[CompositePixelChannel]+=distance*distance;
571 p+=(ptrdiff_t) GetPixelChannels(image);
572 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
574#if defined(MAGICKCORE_OPENMP_SUPPORT)
575 #pragma omp critical (MagickCore_GetFuzzDistortion)
579 for (j=0; j <= MaxPixelChannels; j++)
580 distortion[j]+=channel_distortion[j];
583 reconstruct_view=DestroyCacheView(reconstruct_view);
584 image_view=DestroyCacheView(image_view);
585 area=PerceptibleReciprocal(area);
586 for (j=0; j <= MaxPixelChannels; j++)
588 distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
589 distortion[CompositePixelChannel]=sqrt(distortion[CompositePixelChannel]);
593static MagickBooleanType GetMeanAbsoluteDistortion(
const Image *image,
617 rows=MagickMax(image->rows,reconstruct_image->rows);
618 columns=MagickMax(image->columns,reconstruct_image->columns);
620 image_view=AcquireVirtualCacheView(image,exception);
621 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
622#if defined(MAGICKCORE_OPENMP_SUPPORT)
623 #pragma omp parallel for schedule(static) shared(status) \
624 magick_number_threads(image,image,rows,1)
626 for (y=0; y < (ssize_t) rows; y++)
629 channel_distortion[MaxPixelChannels+1];
641 if (status == MagickFalse)
643 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
644 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
645 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
650 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
651 for (x=0; x < (ssize_t) columns; x++)
660 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
661 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
663 p+=(ptrdiff_t) GetPixelChannels(image);
664 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
667 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
668 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
669 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
674 PixelChannel channel = GetPixelChannelChannel(image,i);
675 PixelTrait traits = GetPixelChannelTraits(image,channel);
676 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
678 if ((traits == UndefinedPixelTrait) ||
679 (reconstruct_traits == UndefinedPixelTrait) ||
680 ((reconstruct_traits & UpdatePixelTrait) == 0))
682 if (channel == AlphaPixelChannel)
683 distance=QuantumScale*fabs((
double) p[i]-(
double)
684 GetPixelChannel(reconstruct_image,channel,q));
686 distance=QuantumScale*fabs(Sa*(
double) p[i]-Da*(
double)
687 GetPixelChannel(reconstruct_image,channel,q));
688 channel_distortion[i]+=distance;
689 channel_distortion[CompositePixelChannel]+=distance;
692 p+=(ptrdiff_t) GetPixelChannels(image);
693 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
695#if defined(MAGICKCORE_OPENMP_SUPPORT)
696 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
700 for (j=0; j <= MaxPixelChannels; j++)
701 distortion[j]+=channel_distortion[j];
704 reconstruct_view=DestroyCacheView(reconstruct_view);
705 image_view=DestroyCacheView(image_view);
706 area=PerceptibleReciprocal(area);
707 for (j=0; j <= MaxPixelChannels; j++)
709 distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
713static MagickBooleanType GetMeanErrorPerPixel(
Image *image,
739 rows=MagickMax(image->rows,reconstruct_image->rows);
740 columns=MagickMax(image->columns,reconstruct_image->columns);
741 image_view=AcquireVirtualCacheView(image,exception);
742 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
743 for (y=0; y < (ssize_t) rows; y++)
752 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
753 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
754 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
759 for (x=0; x < (ssize_t) columns; x++)
768 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
769 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
771 p+=(ptrdiff_t) GetPixelChannels(image);
772 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
775 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
776 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
777 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
782 PixelChannel channel = GetPixelChannelChannel(image,i);
783 PixelTrait traits = GetPixelChannelTraits(image,channel);
784 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
786 if ((traits == UndefinedPixelTrait) ||
787 (reconstruct_traits == UndefinedPixelTrait) ||
788 ((reconstruct_traits & UpdatePixelTrait) == 0))
790 if (channel == AlphaPixelChannel)
791 distance=fabs((
double) p[i]-(
double)
792 GetPixelChannel(reconstruct_image,channel,q));
794 distance=fabs(Sa*(
double) p[i]-Da*(
double)
795 GetPixelChannel(reconstruct_image,channel,q));
796 distortion[i]+=distance;
797 distortion[CompositePixelChannel]+=distance;
798 mean_error+=distance*distance;
799 if (distance > maximum_error)
800 maximum_error=distance;
803 p+=(ptrdiff_t) GetPixelChannels(image);
804 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
807 reconstruct_view=DestroyCacheView(reconstruct_view);
808 image_view=DestroyCacheView(image_view);
809 area=PerceptibleReciprocal(area);
810 image->error.mean_error_per_pixel=area*distortion[CompositePixelChannel];
811 image->error.normalized_mean_error=area*QuantumScale*QuantumScale*mean_error;
812 image->error.normalized_maximum_error=QuantumScale*maximum_error;
816static MagickBooleanType GetMeanSquaredDistortion(
const Image *image,
838 rows=MagickMax(image->rows,reconstruct_image->rows);
839 columns=MagickMax(image->columns,reconstruct_image->columns);
841 image_view=AcquireVirtualCacheView(image,exception);
842 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
843#if defined(MAGICKCORE_OPENMP_SUPPORT)
844 #pragma omp parallel for schedule(static) shared(status) \
845 magick_number_threads(image,image,rows,1)
847 for (y=0; y < (ssize_t) rows; y++)
854 channel_distortion[MaxPixelChannels+1];
862 if (status == MagickFalse)
864 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
865 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
866 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
871 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
872 for (x=0; x < (ssize_t) columns; x++)
881 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
882 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
884 p+=(ptrdiff_t) GetPixelChannels(image);
885 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
888 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
889 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
890 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
895 PixelChannel channel = GetPixelChannelChannel(image,i);
896 PixelTrait traits = GetPixelChannelTraits(image,channel);
897 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
899 if ((traits == UndefinedPixelTrait) ||
900 (reconstruct_traits == UndefinedPixelTrait) ||
901 ((reconstruct_traits & UpdatePixelTrait) == 0))
903 if (channel == AlphaPixelChannel)
904 distance=QuantumScale*((double) p[i]-(double) GetPixelChannel(
905 reconstruct_image,channel,q));
907 distance=QuantumScale*(Sa*(double) p[i]-Da*(double) GetPixelChannel(
908 reconstruct_image,channel,q));
909 channel_distortion[i]+=distance*distance;
910 channel_distortion[CompositePixelChannel]+=distance*distance;
913 p+=(ptrdiff_t) GetPixelChannels(image);
914 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
916#if defined(MAGICKCORE_OPENMP_SUPPORT)
917 #pragma omp critical (MagickCore_GetMeanSquaredError)
921 for (j=0; j <= MaxPixelChannels; j++)
922 distortion[j]+=channel_distortion[j];
925 reconstruct_view=DestroyCacheView(reconstruct_view);
926 image_view=DestroyCacheView(image_view);
927 area=PerceptibleReciprocal(area);
928 for (j=0; j <= MaxPixelChannels; j++)
930 distortion[CompositePixelChannel]/=GetImageChannels(image);
934static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
935 const Image *image,
const Image *reconstruct_image,
double *distortion,
938#define SimilarityImageTag "Similarity/Image"
946 *reconstruct_statistics;
949 alpha_variance[MaxPixelChannels+1],
950 beta_variance[MaxPixelChannels+1];
969 image_statistics=GetImageStatistics(image,exception);
970 reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
979 reconstruct_statistics);
982 (void) memset(distortion,0,(MaxPixelChannels+1)*
sizeof(*distortion));
983 (void) memset(alpha_variance,0,(MaxPixelChannels+1)*
sizeof(*alpha_variance));
984 (void) memset(beta_variance,0,(MaxPixelChannels+1)*
sizeof(*beta_variance));
987 columns=MagickMax(image->columns,reconstruct_image->columns);
988 rows=MagickMax(image->rows,reconstruct_image->rows);
989 image_view=AcquireVirtualCacheView(image,exception);
990 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
991 for (y=0; y < (ssize_t) rows; y++)
1000 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1001 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1002 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1007 for (x=0; x < (ssize_t) columns; x++)
1013 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1014 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1016 p+=(ptrdiff_t) GetPixelChannels(image);
1017 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1020 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1021 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1022 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1028 PixelChannel channel = GetPixelChannelChannel(image,i);
1029 PixelTrait traits = GetPixelChannelTraits(image,channel);
1030 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1032 if ((traits == UndefinedPixelTrait) ||
1033 (reconstruct_traits == UndefinedPixelTrait) ||
1034 ((reconstruct_traits & UpdatePixelTrait) == 0))
1036 if (channel == AlphaPixelChannel)
1038 alpha=QuantumScale*((double) p[i]-image_statistics[channel].mean);
1039 beta=QuantumScale*((double) GetPixelChannel(reconstruct_image,
1040 channel,q)-reconstruct_statistics[channel].mean);
1044 alpha=QuantumScale*(Sa*(double) p[i]-
1045 image_statistics[channel].mean);
1046 beta=QuantumScale*(Da*(double) GetPixelChannel(reconstruct_image,
1047 channel,q)-reconstruct_statistics[channel].mean);
1049 distortion[i]+=alpha*beta;
1050 alpha_variance[i]+=alpha*alpha;
1051 beta_variance[i]+=beta*beta;
1053 p+=(ptrdiff_t) GetPixelChannels(image);
1054 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1056 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1061#if defined(MAGICKCORE_OPENMP_SUPPORT)
1065 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
1066 if (proceed == MagickFalse)
1073 reconstruct_view=DestroyCacheView(reconstruct_view);
1074 image_view=DestroyCacheView(image_view);
1078 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1080 distortion[i]/=sqrt(alpha_variance[i]*beta_variance[i]);
1081 if (fabs(distortion[i]) >= MagickEpsilon)
1082 distortion[CompositePixelChannel]+=distortion[i];
1084 distortion[CompositePixelChannel]=distortion[CompositePixelChannel]/
1085 GetImageChannels(image);
1090 reconstruct_statistics);
1096static MagickBooleanType GetPeakAbsoluteDistortion(
const Image *image,
1114 (void) memset(distortion,0,(MaxPixelChannels+1)*
sizeof(*distortion));
1115 rows=MagickMax(image->rows,reconstruct_image->rows);
1116 columns=MagickMax(image->columns,reconstruct_image->columns);
1117 image_view=AcquireVirtualCacheView(image,exception);
1118 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1119#if defined(MAGICKCORE_OPENMP_SUPPORT)
1120 #pragma omp parallel for schedule(static) shared(status) \
1121 magick_number_threads(image,image,rows,1)
1123 for (y=0; y < (ssize_t) rows; y++)
1126 channel_distortion[MaxPixelChannels+1];
1136 if (status == MagickFalse)
1138 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1139 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1140 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1145 (void) memset(channel_distortion,0,(MaxPixelChannels+1)*
1146 sizeof(*channel_distortion));
1147 for (x=0; x < (ssize_t) columns; x++)
1156 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1157 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1159 p+=(ptrdiff_t) GetPixelChannels(image);
1160 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1163 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1164 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1165 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1170 PixelChannel channel = GetPixelChannelChannel(image,i);
1171 PixelTrait traits = GetPixelChannelTraits(image,channel);
1172 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1174 if ((traits == UndefinedPixelTrait) ||
1175 (reconstruct_traits == UndefinedPixelTrait) ||
1176 ((reconstruct_traits & UpdatePixelTrait) == 0))
1178 if (channel == AlphaPixelChannel)
1179 distance=QuantumScale*fabs((
double) p[i]-(
double)
1180 GetPixelChannel(reconstruct_image,channel,q));
1182 distance=QuantumScale*fabs(Sa*(
double) p[i]-Da*(
double)
1183 GetPixelChannel(reconstruct_image,channel,q));
1184 if (distance > channel_distortion[i])
1185 channel_distortion[i]=distance;
1186 if (distance > channel_distortion[CompositePixelChannel])
1187 channel_distortion[CompositePixelChannel]=distance;
1189 p+=(ptrdiff_t) GetPixelChannels(image);
1190 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1192#if defined(MAGICKCORE_OPENMP_SUPPORT)
1193 #pragma omp critical (MagickCore_GetPeakAbsoluteError)
1195 for (j=0; j <= MaxPixelChannels; j++)
1196 if (channel_distortion[j] > distortion[j])
1197 distortion[j]=channel_distortion[j];
1199 reconstruct_view=DestroyCacheView(reconstruct_view);
1200 image_view=DestroyCacheView(image_view);
1204static MagickBooleanType GetPeakSignalToNoiseRatio(
const Image *image,
1213 status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception);
1214 for (i=0; i < MaxPixelChannels; i++)
1215 if ((fabs(distortion[i]) < MagickEpsilon) || (fabs(distortion[i]) >= 1.0))
1216 distortion[i]=fabs(distortion[i]) < MagickEpsilon ? 0.0 : 1.0;
1218 distortion[i]=(-10.0*MagickLog10(PerceptibleReciprocal(distortion[i])))/
1223static MagickBooleanType GetPerceptualHashDistortion(
const Image *image,
1239 channel_phash=GetImagePerceptualHash(image,exception);
1241 return(MagickFalse);
1242 reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
1247 return(MagickFalse);
1249#if defined(MAGICKCORE_OPENMP_SUPPORT)
1250 #pragma omp parallel for schedule(static)
1252 for (channel=0; channel < MaxPixelChannels; channel++)
1261 for (i=0; i < MaximumNumberOfImageMoments; i++)
1270 for (j=0; j < (ssize_t) channel_phash[0].number_colorspaces; j++)
1272 alpha=channel_phash[channel].phash[j][i];
1273 beta=reconstruct_phash[channel].phash[j][i];
1274 difference+=(beta-alpha)*(beta-alpha);
1277 distortion[channel]+=difference;
1278#if defined(MAGICKCORE_OPENMP_SUPPORT)
1279 #pragma omp critical (MagickCore_GetPerceptualHashDistortion)
1281 distortion[CompositePixelChannel]+=difference;
1283 artifact=GetImageArtifact(image,
"phash:normalize");
1284 if ((artifact != (
const char *) NULL) &&
1285 (IsStringTrue(artifact) != MagickFalse))
1290 for (j=0; j <= MaxPixelChannels; j++)
1291 distortion[j]=sqrt(distortion[j]/channel_phash[0].number_channels);
1302static MagickBooleanType GetRootMeanSquaredDistortion(
const Image *image,
1311 status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception);
1312 for (i=0; i <= MaxPixelChannels; i++)
1313 distortion[i]=sqrt(distortion[i]);
1317static MagickBooleanType GetStructuralSimilarityDistortion(
const Image *image,
1320#define SSIMRadius 5.0
1321#define SSIMSigma 1.5
1322#define SSIMBlocksize 8
1332 geometry[MagickPathExtent];
1363 artifact=GetImageArtifact(image,
"compare:ssim-radius");
1364 if (artifact != (
const char *) NULL)
1365 radius=StringToDouble(artifact,(
char **) NULL);
1367 artifact=GetImageArtifact(image,
"compare:ssim-sigma");
1368 if (artifact != (
const char *) NULL)
1369 sigma=StringToDouble(artifact,(
char **) NULL);
1370 (void) FormatLocaleString(geometry,MagickPathExtent,
"gaussian:%.20gx%.20g",
1372 kernel_info=AcquireKernelInfo(geometry,exception);
1374 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1376 c1=pow(SSIMK1*SSIML,2.0);
1377 artifact=GetImageArtifact(image,
"compare:ssim-k1");
1378 if (artifact != (
const char *) NULL)
1379 c1=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
1380 c2=pow(SSIMK2*SSIML,2.0);
1381 artifact=GetImageArtifact(image,
"compare:ssim-k2");
1382 if (artifact != (
const char *) NULL)
1383 c2=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
1386 rows=MagickMax(image->rows,reconstruct_image->rows);
1387 columns=MagickMax(image->columns,reconstruct_image->columns);
1388 image_view=AcquireVirtualCacheView(image,exception);
1389 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1390#if defined(MAGICKCORE_OPENMP_SUPPORT)
1391 #pragma omp parallel for schedule(static,1) shared(area,distortion,status) \
1392 magick_number_threads(image,reconstruct_image,rows,1)
1394 for (y=0; y < (ssize_t) rows; y++)
1401 channel_distortion[MaxPixelChannels+1];
1410 if (status == MagickFalse)
1412 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1413 ((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
1414 kernel_info->height,exception);
1415 q=GetCacheViewVirtualPixels(reconstruct_view,-((ssize_t) kernel_info->width/
1416 2L),y-((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
1417 kernel_info->height,exception);
1418 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1423 (void) memset(channel_distortion,0,
sizeof(channel_distortion));
1424 for (x=0; x < (ssize_t) columns; x++)
1427 *magick_restrict reconstruct,
1428 *magick_restrict test;
1431 x_pixel_mu[MaxPixelChannels+1],
1432 x_pixel_sigma_squared[MaxPixelChannels+1],
1433 xy_sigma[MaxPixelChannels+1],
1434 y_pixel_mu[MaxPixelChannels+1],
1435 y_pixel_sigma_squared[MaxPixelChannels+1];
1443 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1444 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1446 p+=(ptrdiff_t) GetPixelChannels(image);
1447 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1450 (void) memset(x_pixel_mu,0,
sizeof(x_pixel_mu));
1451 (void) memset(x_pixel_sigma_squared,0,
sizeof(x_pixel_sigma_squared));
1452 (void) memset(xy_sigma,0,
sizeof(xy_sigma));
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]*=PerceptibleReciprocal(area);
1549 distortion[CompositePixelChannel]*=PerceptibleReciprocal(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) (-10.0*QuantumRange*MagickLog10(PerceptibleReciprocal(
2347 (
double) q[i])))/48.1647;
2349 q+=(ptrdiff_t) GetPixelChannels(image);
2351 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2354 image_view=DestroyCacheView(image_view);
2375 square_image=CloneImage(image,0,0,MagickTrue,exception);
2376 if (square_image == (
Image *) NULL)
2377 return(square_image);
2379 image_view=AcquireAuthenticCacheView(square_image,exception);
2380#if defined(MAGICKCORE_OPENMP_SUPPORT)
2381 #pragma omp parallel for schedule(static) shared(status) \
2382 magick_number_threads(square_image,square_image,square_image->rows,1)
2384 for (y=0; y < (ssize_t) square_image->rows; y++)
2392 if (status == MagickFalse)
2394 q=GetCacheViewAuthenticPixels(image_view,0,y,square_image->columns,1,
2396 if (q == (Quantum *) NULL)
2401 for (x=0; x < (ssize_t) square_image->columns; x++)
2406 for (i=0; i < (ssize_t) GetPixelChannels(square_image); i++)
2408 PixelChannel channel = GetPixelChannelChannel(square_image,i);
2409 PixelTrait traits = GetPixelChannelTraits(square_image,channel);
2410 if ((traits & UpdatePixelTrait) == 0)
2412 q[i]=(Quantum) ((
double) q[i]*QuantumScale*(
double) q[i]);
2414 q+=(ptrdiff_t) GetPixelChannels(square_image);
2416 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2419 image_view=DestroyCacheView(image_view);
2420 if (status == MagickFalse)
2421 square_image=DestroyImage(square_image);
2422 return(square_image);
2436 (void) SetImageArtifact(alpha_image,
"compose:clamp",
"False");
2437 xsq_image=SIMSquareImage(alpha_image,exception);
2438 if (xsq_image == (
Image *) NULL)
2439 return((
Image *) NULL);
2440 (void) SetImageArtifact(beta_image,
"compose:clamp",
"False");
2441 ysq_image=SIMSquareImage(beta_image,exception);
2442 if (ysq_image == (
Image *) NULL)
2444 xsq_image=DestroyImage(xsq_image);
2445 return((
Image *) NULL);
2447 status=CompositeImage(xsq_image,ysq_image,PlusCompositeOp,MagickTrue,0,0,
2449 magnitude_image=xsq_image;
2450 ysq_image=DestroyImage(ysq_image);
2451 if (status == MagickFalse)
2453 magnitude_image=DestroyImage(magnitude_image);
2454 return((
Image *) NULL);
2456 status=EvaluateImage(magnitude_image,PowEvaluateOperator,0.5,exception);
2457 if (status == MagickFalse)
2459 magnitude_image=DestroyImage(magnitude_image);
2460 return (
Image *) NULL;
2462 return(magnitude_image);
2465static MagickBooleanType SIMMaximaImage(
const Image *image,
double *maxima,
2481 *maxima=MagickMinimumValue;
2484 image_view=AcquireVirtualCacheView(image,exception);
2485#if defined(MAGICKCORE_OPENMP_SUPPORT)
2486 #pragma omp parallel for schedule(static) shared(status) \
2487 magick_number_threads(image,image,image->rows,1)
2489 for (y=0; y < (ssize_t) image->rows; y++)
2501 if (status == MagickFalse)
2503 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2504 if (p == (
const Quantum *) NULL)
2509 row_maxima=(double) p[0];
2511 for (x=0; x < (ssize_t) image->columns; x++)
2516 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2518 PixelChannel channel = GetPixelChannelChannel(image,i);
2519 PixelTrait traits = GetPixelChannelTraits(image,channel);
2520 if ((traits & UpdatePixelTrait) == 0)
2522 if ((
double) p[i] > row_maxima)
2524 row_maxima=(double) p[i];
2528 p+=(ptrdiff_t) GetPixelChannels(image);
2530#if defined(MAGICKCORE_OPENMP_SUPPORT)
2531 #pragma omp critical (MagickCore_SIMMaximaImage)
2533 if (row_maxima > *maxima)
2539 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2542 image_view=DestroyCacheView(image_view);
2546static MagickBooleanType SIMMinimaImage(
const Image *image,
double *minima,
2562 *minima=MagickMaximumValue;
2565 image_view=AcquireVirtualCacheView(image,exception);
2566#if defined(MAGICKCORE_OPENMP_SUPPORT)
2567 #pragma omp parallel for schedule(static) shared(status) \
2568 magick_number_threads(image,image,image->rows,1)
2570 for (y=0; y < (ssize_t) image->rows; y++)
2582 if (status == MagickFalse)
2584 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2585 if (p == (
const Quantum *) NULL)
2590 row_minima=(double) p[0];
2592 for (x=0; x < (ssize_t) image->columns; x++)
2597 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2599 PixelChannel channel = GetPixelChannelChannel(image,i);
2600 PixelTrait traits = GetPixelChannelTraits(image,channel);
2601 if ((traits & UpdatePixelTrait) == 0)
2603 if ((
double) p[i] < row_minima)
2605 row_minima=(double) p[i];
2609 p+=(ptrdiff_t) GetPixelChannels(image);
2611#if defined(MAGICKCORE_OPENMP_SUPPORT)
2612 #pragma omp critical (MagickCore_SIMMinimaImage)
2614 if (row_minima < *minima)
2620 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2623 image_view=DestroyCacheView(image_view);
2627static MagickBooleanType SIMMultiplyImage(
Image *image,
const double factor,
2643 image_view=AcquireAuthenticCacheView(image,exception);
2644#if defined(MAGICKCORE_OPENMP_SUPPORT)
2645 #pragma omp parallel for schedule(static) shared(status) \
2646 magick_number_threads(image,image,image->rows,1)
2648 for (y=0; y < (ssize_t) image->rows; y++)
2656 if (status == MagickFalse)
2658 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2659 if (q == (Quantum *) NULL)
2664 for (x=0; x < (ssize_t) image->columns; x++)
2669 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2671 PixelChannel channel = GetPixelChannelChannel(image,i);
2672 PixelTrait traits = GetPixelChannelTraits(image,channel);
2673 if ((traits & UpdatePixelTrait) == 0)
2676 q[i]=(Quantum) ((
double) q[i]*QuantumScale*
2677 channel_statistics[channel].standard_deviation);
2678 q[i]=(Quantum) ((
double) q[i]*factor);
2680 q+=(ptrdiff_t) GetPixelChannels(image);
2682 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2685 image_view=DestroyCacheView(image_view);
2689static Image *SIMPhaseCorrelationImage(
const Image *alpha_image,
2693 *alpha_fft = (
Image *) NULL,
2694 *beta_fft = (
Image *) NULL,
2695 *complex_multiplication = (
Image *) NULL,
2696 *cross_correlation = (
Image *) NULL;
2701 beta_fft=CloneImage(beta_image,0,0,MagickTrue,exception);
2702 if (beta_fft == NULL)
2703 return((
Image *) NULL);
2704 (void) SetImageArtifact(beta_fft,
"fourier:normalize",
"inverse");
2705 beta_fft=ForwardFourierTransformImage(beta_fft,MagickFalse,exception);
2706 if (beta_fft == NULL)
2707 return((
Image *) NULL);
2711 alpha_fft=CloneImage(alpha_image,0,0,MagickTrue,exception);
2712 if (alpha_fft == (
Image *) NULL)
2714 beta_fft=DestroyImageList(beta_fft);
2715 return((
Image *) NULL);
2717 (void) SetImageArtifact(alpha_fft,
"fourier:normalize",
"inverse");
2718 alpha_fft=ForwardFourierTransformImage(alpha_fft,MagickFalse,exception);
2719 if (alpha_fft == (
Image *) NULL)
2721 beta_fft=DestroyImageList(beta_fft);
2722 return((
Image *) NULL);
2727 beta_fft=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
2728 if (beta_fft == (
Image *) NULL)
2730 alpha_fft=DestroyImageList(alpha_fft);
2731 return((
Image *) NULL);
2736 beta_fft->next->next=alpha_fft;
2737 DisableCompositeClampUnlessSpecified(beta_fft);
2738 DisableCompositeClampUnlessSpecified(beta_fft->next);
2739 complex_multiplication=ComplexImages(beta_fft,MultiplyComplexOperator,
2741 beta_fft=DestroyImageList(beta_fft);
2742 if (complex_multiplication == (
Image *) NULL)
2743 return((
Image *) NULL);
2747 CompositeLayers(complex_multiplication,DivideSrcCompositeOp,(
Image *)
2748 magnitude_image,0,0,exception);
2752 (void) SetImageArtifact(complex_multiplication,
"fourier:normalize",
"inverse");
2753 cross_correlation=InverseFourierTransformImage(complex_multiplication,
2754 complex_multiplication->next,MagickFalse,exception);
2755 complex_multiplication=DestroyImageList(complex_multiplication);
2756 return(cross_correlation);
2759static MagickBooleanType SIMSetImageMean(
const Image *image,
2775 image_view=AcquireAuthenticCacheView(image,exception);
2776#if defined(MAGICKCORE_OPENMP_SUPPORT)
2777 #pragma omp parallel for schedule(static) shared(status) \
2778 magick_number_threads(image,image,image->rows,1)
2780 for (y=0; y < (ssize_t) image->rows; y++)
2788 if (status == MagickFalse)
2790 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2791 if (q == (Quantum *) NULL)
2796 for (x=0; x < (ssize_t) image->columns; x++)
2801 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2803 PixelChannel channel = GetPixelChannelChannel(image,i);
2804 PixelTrait traits = GetPixelChannelTraits(image,channel);
2805 if ((traits & UpdatePixelTrait) == 0)
2807 q[i]=(Quantum) channel_statistics[channel].mean;
2809 q+=(ptrdiff_t) GetPixelChannels(image);
2811 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2814 image_view=DestroyCacheView(image_view);
2818static Image *SIMSubtractImageMean(
const Image *alpha_image,
2838 subtract_image=CloneImage(beta_image,alpha_image->columns,alpha_image->rows,
2839 MagickTrue,exception);
2840 if (subtract_image == (
Image *) NULL)
2841 return(subtract_image);
2843 image_view=AcquireAuthenticCacheView(subtract_image,exception);
2844 beta_view=AcquireVirtualCacheView(beta_image,exception);
2845#if defined(MAGICKCORE_OPENMP_SUPPORT)
2846 #pragma omp parallel for schedule(static) shared(status) \
2847 magick_number_threads(beta_image,subtract_image,subtract_image->rows,1)
2849 for (y=0; y < (ssize_t) subtract_image->rows; y++)
2860 if (status == MagickFalse)
2862 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,exception);
2863 q=GetCacheViewAuthenticPixels(image_view,0,y,subtract_image->columns,1,
2865 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2870 for (x=0; x < (ssize_t) subtract_image->columns; x++)
2875 for (i=0; i < (ssize_t) GetPixelChannels(subtract_image); i++)
2877 PixelChannel channel = GetPixelChannelChannel(subtract_image,i);
2878 PixelTrait traits = GetPixelChannelTraits(subtract_image,channel);
2879 if ((traits & UpdatePixelTrait) == 0)
2881 if ((x >= (ssize_t) beta_image->columns) ||
2882 (y >= (ssize_t) beta_image->rows))
2885 q[i]=(Quantum) ((
double) p[i]-channel_statistics[channel].mean);
2887 p+=(ptrdiff_t) GetPixelChannels(beta_image);
2888 q+=(ptrdiff_t) GetPixelChannels(subtract_image);
2890 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2893 beta_view=DestroyCacheView(beta_view);
2894 image_view=DestroyCacheView(image_view);
2895 if (status == MagickFalse)
2896 subtract_image=DestroyImage(subtract_image);
2897 return(subtract_image);
2900static Image *SIMUnityImage(
const Image *alpha_image,
const Image *beta_image,
2918 unity_image=CloneImage(alpha_image,alpha_image->columns,alpha_image->rows,
2919 MagickTrue,exception);
2920 if (unity_image == (
Image *) NULL)
2921 return(unity_image);
2922 if (SetImageStorageClass(unity_image,DirectClass,exception) == MagickFalse)
2923 return(DestroyImage(unity_image));
2925 image_view=AcquireAuthenticCacheView(unity_image,exception);
2926#if defined(MAGICKCORE_OPENMP_SUPPORT)
2927 #pragma omp parallel for schedule(static) shared(status) \
2928 magick_number_threads(unity_image,unity_image,unity_image->rows,1)
2930 for (y=0; y < (ssize_t) unity_image->rows; y++)
2938 if (status == MagickFalse)
2940 q=GetCacheViewAuthenticPixels(image_view,0,y,unity_image->columns,1,
2942 if (q == (Quantum *) NULL)
2947 for (x=0; x < (ssize_t) unity_image->columns; x++)
2952 for (i=0; i < (ssize_t) GetPixelChannels(unity_image); i++)
2954 PixelChannel channel = GetPixelChannelChannel(unity_image,i);
2955 PixelTrait traits = GetPixelChannelTraits(unity_image,channel);
2956 if ((traits & UpdatePixelTrait) == 0)
2958 if ((x >= (ssize_t) beta_image->columns) ||
2959 (y >= (ssize_t) beta_image->rows))
2964 q+=(ptrdiff_t) GetPixelChannels(unity_image);
2966 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2969 image_view=DestroyCacheView(image_view);
2970 if (status == MagickFalse)
2971 unity_image=DestroyImage(unity_image);
2972 return(unity_image);
2975static Image *SIMVarianceImage(
Image *alpha_image,
const Image *beta_image,
2994 variance_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
2995 if (variance_image == (
Image *) NULL)
2996 return(variance_image);
2998 image_view=AcquireAuthenticCacheView(variance_image,exception);
2999 beta_view=AcquireVirtualCacheView(beta_image,exception);
3000#if defined(MAGICKCORE_OPENMP_SUPPORT)
3001 #pragma omp parallel for schedule(static) shared(status) \
3002 magick_number_threads(beta_image,variance_image,variance_image->rows,1)
3004 for (y=0; y < (ssize_t) variance_image->rows; y++)
3015 if (status == MagickFalse)
3017 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,
3019 q=GetCacheViewAuthenticPixels(image_view,0,y,variance_image->columns,1,
3021 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3026 for (x=0; x < (ssize_t) variance_image->columns; x++)
3031 for (i=0; i < (ssize_t) GetPixelChannels(variance_image); i++)
3033 PixelChannel channel = GetPixelChannelChannel(variance_image,i);
3034 PixelTrait traits = GetPixelChannelTraits(variance_image,channel);
3035 if ((traits & UpdatePixelTrait) == 0)
3037 q[i]=(Quantum) ((
double) ClampToQuantum((
double) QuantumRange*
3038 sqrt(fabs(QuantumScale*((
double) q[i]-(
double) p[i]))))/
3039 sqrt((
double) QuantumRange));
3041 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3042 q+=(ptrdiff_t) GetPixelChannels(variance_image);
3044 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3047 beta_view=DestroyCacheView(beta_view);
3048 image_view=DestroyCacheView(image_view);
3049 if (status == MagickFalse)
3050 variance_image=DestroyImage(variance_image);
3051 return(variance_image);
3054static Image *DPCSimilarityImage(
const Image *image,
const Image *reconstruct,
3057#define ThrowDPCSimilarityException() \
3059 if (dot_product_image != (Image *) NULL) \
3060 dot_product_image=DestroyImage(dot_product_image); \
3061 if (magnitude_image != (Image *) NULL) \
3062 magnitude_image=DestroyImage(magnitude_image); \
3063 if (reconstruct_image != (Image *) NULL) \
3064 reconstruct_image=DestroyImage(reconstruct_image); \
3065 if (rx_image != (Image *) NULL) \
3066 rx_image=DestroyImage(rx_image); \
3067 if (ry_image != (Image *) NULL) \
3068 ry_image=DestroyImage(ry_image); \
3069 if (test_image != (Image *) NULL) \
3070 test_image=DestroyImage(test_image); \
3071 if (threshold_image != (Image *) NULL) \
3072 threshold_image=DestroyImage(threshold_image); \
3073 if (trx_image != (Image *) NULL) \
3074 trx_image=DestroyImage(trx_image); \
3075 if (try_image != (Image *) NULL) \
3076 try_image=DestroyImage(try_image); \
3077 if (tx_image != (Image *) NULL) \
3078 tx_image=DestroyImage(tx_image); \
3079 if (ty_image != (Image *) NULL) \
3080 ty_image=DestroyImage(ty_image); \
3081 return((Image *) NULL); \
3088 standard_deviation = 0.0;
3091 *dot_product_image = (
Image *) NULL,
3092 *magnitude_image = (
Image *) NULL,
3093 *reconstruct_image = (
Image *) NULL,
3094 *rx_image = (
Image *) NULL,
3095 *ry_image = (
Image *) NULL,
3096 *trx_image = (
Image *) NULL,
3097 *temp_image = (
Image *) NULL,
3098 *test_image = (
Image *) NULL,
3099 *threshold_image = (
Image *) NULL,
3100 *try_image = (
Image *) NULL,
3101 *tx_image = (
Image *) NULL,
3102 *ty_image = (
Image *) NULL;
3113 test_image=CloneImage(image,0,0,MagickTrue,exception);
3114 if (test_image == (
Image *) NULL)
3115 return((
Image *) NULL);
3116 GetPixelInfoRGBA(0,0,0,0,&test_image->background_color);
3117 (void) ResetImagePage(test_image,
"0x0+0+0");
3118 status=SetImageExtent(test_image,2*(
size_t) ceil((
double) image->columns/2.0),
3119 2*(
size_t) ceil((
double) image->rows/2.0),exception);
3120 if (status == MagickFalse)
3121 ThrowDPCSimilarityException();
3122 (void) SetImageAlphaChannel(test_image,OffAlphaChannel,exception);
3126 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3127 if (reconstruct_image == (
Image *) NULL)
3128 ThrowDPCSimilarityException();
3129 GetPixelInfoRGBA(0,0,0,0,&reconstruct_image->background_color);
3130 (void) ResetImagePage(reconstruct_image,
"0x0+0+0");
3131 (void) SetImageAlphaChannel(reconstruct_image,OffAlphaChannel,exception);
3135 (void) SetImageVirtualPixelMethod(reconstruct_image,EdgeVirtualPixelMethod,
3137 rx_image=SIMDerivativeImage(reconstruct_image,
"Sobel",exception);
3138 if (rx_image == (
Image *) NULL)
3139 ThrowDPCSimilarityException();
3140 ry_image=SIMDerivativeImage(reconstruct_image,
"Sobel:90",exception);
3141 reconstruct_image=DestroyImage(reconstruct_image);
3142 if (ry_image == (
Image *) NULL)
3143 ThrowDPCSimilarityException();
3147 magnitude_image=SIMMagnitudeImage(rx_image,ry_image,exception);
3148 if (magnitude_image == (
Image *) NULL)
3149 ThrowDPCSimilarityException();
3153 threshold_image=CloneImage(magnitude_image,0,0,MagickTrue,exception);
3154 if (threshold_image == (
Image *) NULL)
3155 ThrowDPCSimilarityException();
3156 status=BilevelImage(threshold_image,0.0,exception);
3157 if (status == MagickFalse)
3158 ThrowDPCSimilarityException();
3159 status=GetImageMean(threshold_image,&mean,&standard_deviation,exception);
3160 threshold_image=DestroyImage(threshold_image);
3161 if (status == MagickFalse)
3162 ThrowDPCSimilarityException();
3163 edge_factor=1.0/(QuantumScale*mean)/reconstruct->columns/reconstruct->rows;
3167 temp_image=SIMDivideByMagnitude(rx_image,magnitude_image,image,exception);
3168 rx_image=DestroyImage(rx_image);
3169 if (temp_image == (
Image *) NULL)
3170 ThrowDPCSimilarityException();
3171 rx_image=temp_image;
3172 try_image=SIMDivideByMagnitude(ry_image,magnitude_image,image,exception);
3173 magnitude_image=DestroyImage(magnitude_image);
3174 ry_image=DestroyImage(ry_image);
3175 if (try_image == (
Image *) NULL)
3176 ThrowDPCSimilarityException();
3181 (void) SetImageVirtualPixelMethod(test_image,EdgeVirtualPixelMethod,
3183 tx_image=SIMDerivativeImage(test_image,
"Sobel",exception);
3184 if (tx_image == (
Image *) NULL)
3185 ThrowDPCSimilarityException();
3186 ty_image=SIMDerivativeImage(test_image,
"Sobel:90",exception);
3187 test_image=DestroyImage(test_image);
3188 if (ty_image == (
Image *) NULL)
3189 ThrowDPCSimilarityException();
3193 magnitude_image=SIMMagnitudeImage(tx_image,ty_image,exception);
3194 if (magnitude_image == (
Image *) NULL)
3195 ThrowDPCSimilarityException();
3199 temp_image=SIMDivideByMagnitude(tx_image,magnitude_image,image,exception);
3200 tx_image=DestroyImage(tx_image);
3201 if (temp_image == (
Image *) NULL)
3202 ThrowDPCSimilarityException();
3203 tx_image=temp_image;
3204 try_image=SIMDivideByMagnitude(ty_image,magnitude_image,image,exception);
3205 ty_image=DestroyImage(ty_image);
3206 magnitude_image=DestroyImage(magnitude_image);
3207 if (try_image == (
Image *) NULL)
3208 ThrowDPCSimilarityException();
3213 trx_image=SIMCrossCorrelationImage(tx_image,rx_image,exception);
3214 rx_image=DestroyImage(rx_image);
3215 tx_image=DestroyImage(tx_image);
3216 if (trx_image == (
Image *) NULL)
3217 ThrowDPCSimilarityException();
3218 try_image=SIMCrossCorrelationImage(ty_image,ry_image,exception);
3219 ry_image=DestroyImage(ry_image);
3220 ty_image=DestroyImage(ty_image);
3221 if (try_image == (
Image *) NULL)
3222 ThrowDPCSimilarityException();
3226 (void) SetImageArtifact(try_image,
"compose:clamp",
"False");
3227 status=CompositeImage(trx_image,try_image,PlusCompositeOp,MagickTrue,0,0,
3229 try_image=DestroyImage(try_image);
3230 if (status == MagickFalse)
3231 ThrowDPCSimilarityException();
3232 status=SIMMultiplyImage(trx_image,edge_factor,
3234 if (status == MagickFalse)
3235 ThrowDPCSimilarityException();
3239 SetGeometry(image,&geometry);
3240 geometry.width=image->columns;
3241 geometry.height=image->rows;
3242 (void) ResetImagePage(trx_image,
"0x0+0+0");
3243 dot_product_image=CropImage(trx_image,&geometry,exception);
3244 trx_image=DestroyImage(trx_image);
3245 if (dot_product_image == (
Image *) NULL)
3246 ThrowDPCSimilarityException();
3247 (void) ResetImagePage(dot_product_image,
"0x0+0+0");
3251 status=GrayscaleImage(dot_product_image,AveragePixelIntensityMethod,
3253 if (status == MagickFalse)
3254 ThrowDPCSimilarityException();
3255 dot_product_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3256 status=SIMMaximaImage(dot_product_image,&maxima,offset,exception);
3257 if (status == MagickFalse)
3258 ThrowDPCSimilarityException();
3259 *similarity_metric=1.0-QuantumScale*maxima;
3260 return(dot_product_image);
3263static Image *MSESimilarityImage(
const Image *image,
const Image *reconstruct,
3266#define ThrowMSESimilarityException() \
3268 if (alpha_image != (Image *) NULL) \
3269 alpha_image=DestroyImage(alpha_image); \
3270 if (beta_image != (Image *) NULL) \
3271 beta_image=DestroyImage(beta_image); \
3272 if (channel_statistics != (ChannelStatistics *) NULL) \
3273 channel_statistics=(ChannelStatistics *) \
3274 RelinquishMagickMemory(channel_statistics); \
3275 if (mean_image != (Image *) NULL) \
3276 mean_image=DestroyImage(mean_image); \
3277 if (mse_image != (Image *) NULL) \
3278 mse_image=DestroyImage(mse_image); \
3279 if (reconstruct_image != (Image *) NULL) \
3280 reconstruct_image=DestroyImage(reconstruct_image); \
3281 if (sum_image != (Image *) NULL) \
3282 sum_image=DestroyImage(sum_image); \
3283 if (alpha_image != (Image *) NULL) \
3284 alpha_image=DestroyImage(alpha_image); \
3285 return((Image *) NULL); \
3295 *alpha_image = (
Image *) NULL,
3296 *beta_image = (
Image *) NULL,
3297 *mean_image = (
Image *) NULL,
3298 *mse_image = (
Image *) NULL,
3299 *reconstruct_image = (
Image *) NULL,
3300 *sum_image = (
Image *) NULL,
3301 *test_image = (
Image *) NULL;
3312 test_image=SIMSquareImage(image,exception);
3313 if (test_image == (
Image *) NULL)
3314 ThrowMSESimilarityException();
3315 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3316 if (reconstruct_image == (
Image *) NULL)
3317 ThrowMSESimilarityException();
3321 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3322 test_image=DestroyImage(test_image);
3323 if (alpha_image == (
Image *) NULL)
3324 ThrowMSESimilarityException();
3325 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3327 if (status == MagickFalse)
3328 ThrowMSESimilarityException();
3332 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3333 MagickTrue,0,0,exception);
3334 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3335 reconstruct_image=DestroyImage(reconstruct_image);
3336 if (beta_image == (
Image *) NULL)
3337 ThrowMSESimilarityException();
3338 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3340 if (status == MagickFalse)
3341 ThrowMSESimilarityException();
3345 sum_image=SIMSquareImage(reconstruct,exception);
3346 if (sum_image == (
Image *) NULL)
3347 ThrowMSESimilarityException();
3348 channel_statistics=GetImageStatistics(sum_image,exception);
3350 ThrowMSESimilarityException();
3351 status=SetImageExtent(sum_image,image->columns,image->rows,exception);
3352 if (status == MagickFalse)
3353 ThrowMSESimilarityException();
3354 status=SetImageStorageClass(sum_image,DirectClass,exception);
3355 if (status == MagickFalse)
3356 ThrowMSESimilarityException();
3357 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3359 RelinquishMagickMemory(channel_statistics);
3360 if (status == MagickFalse)
3361 ThrowMSESimilarityException();
3365 AppendImageToList(&sum_image,alpha_image);
3366 AppendImageToList(&sum_image,beta_image);
3367 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3368 if (mean_image == (
Image *) NULL)
3369 ThrowMSESimilarityException();
3370 sum_image=DestroyImage(sum_image);
3371 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
3372 if (status == MagickFalse)
3373 ThrowMSESimilarityException();
3374 mean_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3378 SetGeometry(image,&geometry);
3379 geometry.width=image->columns;
3380 geometry.height=image->rows;
3381 (void) ResetImagePage(mean_image,
"0x0+0+0");
3382 mse_image=CropImage(mean_image,&geometry,exception);
3383 mean_image=DestroyImage(mean_image);
3384 if (mse_image == (
Image *) NULL)
3385 ThrowMSESimilarityException();
3389 (void) ResetImagePage(mse_image,
"0x0+0+0");
3390 (void) ClampImage(mse_image,exception);
3391 status=SIMMinimaImage(mse_image,&minima,offset,exception);
3392 if (status == MagickFalse)
3393 ThrowMSESimilarityException();
3394 status=NegateImage(mse_image,MagickFalse,exception);
3395 if (status == MagickFalse)
3396 ThrowMSESimilarityException();
3397 *similarity_metric=QuantumScale*minima;
3398 alpha_image=DestroyImage(alpha_image);
3399 beta_image=DestroyImage(beta_image);
3403static Image *NCCSimilarityImage(
const Image *image,
const Image *reconstruct,
3406#define ThrowNCCSimilarityException() \
3408 if (alpha_image != (Image *) NULL) \
3409 alpha_image=DestroyImage(alpha_image); \
3410 if (beta_image != (Image *) NULL) \
3411 beta_image=DestroyImage(beta_image); \
3412 if (channel_statistics != (ChannelStatistics *) NULL) \
3413 channel_statistics=(ChannelStatistics *) \
3414 RelinquishMagickMemory(channel_statistics); \
3415 if (correlation_image != (Image *) NULL) \
3416 correlation_image=DestroyImage(correlation_image); \
3417 if (divide_image != (Image *) NULL) \
3418 divide_image=DestroyImage(divide_image); \
3419 if (ncc_image != (Image *) NULL) \
3420 ncc_image=DestroyImage(ncc_image); \
3421 if (normalize_image != (Image *) NULL) \
3422 normalize_image=DestroyImage(normalize_image); \
3423 if (reconstruct_image != (Image *) NULL) \
3424 reconstruct_image=DestroyImage(reconstruct_image); \
3425 if (test_image != (Image *) NULL) \
3426 test_image=DestroyImage(test_image); \
3427 if (variance_image != (Image *) NULL) \
3428 variance_image=DestroyImage(variance_image); \
3429 return((Image *) NULL); \
3439 *alpha_image = (
Image *) NULL,
3440 *beta_image = (
Image *) NULL,
3441 *correlation_image = (
Image *) NULL,
3442 *divide_image = (
Image *) NULL,
3443 *ncc_image = (
Image *) NULL,
3444 *normalize_image = (
Image *) NULL,
3445 *reconstruct_image = (
Image *) NULL,
3446 *test_image = (
Image *) NULL,
3447 *variance_image = (
Image *) NULL;
3458 test_image=SIMSquareImage(image,exception);
3459 if (test_image == (
Image *) NULL)
3460 ThrowNCCSimilarityException();
3461 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3462 if (reconstruct_image == (
Image *) NULL)
3463 ThrowNCCSimilarityException();
3467 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3468 test_image=DestroyImage(test_image);
3469 if (alpha_image == (
Image *) NULL)
3470 ThrowNCCSimilarityException();
3471 status=SIMMultiplyImage(alpha_image,(
double) QuantumRange*
3474 if (status == MagickFalse)
3475 ThrowNCCSimilarityException();
3479 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3480 reconstruct_image=DestroyImage(reconstruct_image);
3481 if (beta_image == (
Image *) NULL)
3482 ThrowNCCSimilarityException();
3483 test_image=SIMSquareImage(beta_image,exception);
3484 beta_image=DestroyImage(beta_image);
3485 if (test_image == (
Image *) NULL)
3486 ThrowNCCSimilarityException();
3487 status=SIMMultiplyImage(test_image,(
double) QuantumRange,
3489 if (status == MagickFalse)
3490 ThrowNCCSimilarityException();
3494 variance_image=SIMVarianceImage(alpha_image,test_image,exception);
3495 test_image=DestroyImage(test_image);
3496 alpha_image=DestroyImage(alpha_image);
3497 if (variance_image == (
Image *) NULL)
3498 ThrowNCCSimilarityException();
3502 channel_statistics=GetImageStatistics(reconstruct,exception);
3504 ThrowNCCSimilarityException();
3505 status=SIMMultiplyImage(variance_image,1.0,channel_statistics,exception);
3506 if (status == MagickFalse)
3507 ThrowNCCSimilarityException();
3508 normalize_image=SIMSubtractImageMean(image,reconstruct,channel_statistics,
3511 RelinquishMagickMemory(channel_statistics);
3512 if (normalize_image == (
Image *) NULL)
3513 ThrowNCCSimilarityException();
3514 correlation_image=SIMCrossCorrelationImage(image,normalize_image,exception);
3515 normalize_image=DestroyImage(normalize_image);
3516 if (correlation_image == (
Image *) NULL)
3517 ThrowNCCSimilarityException();
3521 divide_image=SIMDivideImage(correlation_image,variance_image,exception);
3522 correlation_image=DestroyImage(correlation_image);
3523 variance_image=DestroyImage(variance_image);
3524 if (divide_image == (
Image *) NULL)
3525 ThrowNCCSimilarityException();
3529 SetGeometry(image,&geometry);
3530 geometry.width=image->columns;
3531 geometry.height=image->rows;
3532 (void) ResetImagePage(divide_image,
"0x0+0+0");
3533 ncc_image=CropImage(divide_image,&geometry,exception);
3534 divide_image=DestroyImage(divide_image);
3535 if (ncc_image == (
Image *) NULL)
3536 ThrowNCCSimilarityException();
3540 (void) ResetImagePage(ncc_image,
"0x0+0+0");
3541 status=GrayscaleImage(ncc_image,AveragePixelIntensityMethod,exception);
3542 if (status == MagickFalse)
3543 ThrowNCCSimilarityException();
3544 ncc_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3545 status=SIMMaximaImage(ncc_image,&maxima,offset,exception);
3546 if (status == MagickFalse)
3547 ThrowNCCSimilarityException();
3548 *similarity_metric=1.0-QuantumScale*maxima;
3552static Image *PhaseSimilarityImage(
const Image *image,
const Image *reconstruct,
3555#define ThrowPhaseSimilarityException() \
3557 if (correlation_image != (Image *) NULL) \
3558 correlation_image=DestroyImage(correlation_image); \
3559 if (fft_images != (Image *) NULL) \
3560 fft_images=DestroyImageList(fft_images); \
3561 if (gamma_image != (Image *) NULL) \
3562 gamma_image=DestroyImage(gamma_image); \
3563 if (magnitude_image != (Image *) NULL) \
3564 magnitude_image=DestroyImage(magnitude_image); \
3565 if (phase_image != (Image *) NULL) \
3566 phase_image=DestroyImage(phase_image); \
3567 if (reconstruct_image != (Image *) NULL) \
3568 reconstruct_image=DestroyImage(reconstruct_image); \
3569 if (reconstruct_magnitude != (Image *) NULL) \
3570 reconstruct_magnitude=DestroyImage(reconstruct_magnitude); \
3571 if (test_image != (Image *) NULL) \
3572 test_image=DestroyImage(test_image); \
3573 if (test_magnitude != (Image *) NULL) \
3574 test_magnitude=DestroyImage(test_magnitude); \
3575 return((Image *) NULL); \
3582 *correlation_image = (
Image *) NULL,
3583 *fft_images = (
Image *) NULL,
3584 *gamma_image = (
Image *) NULL,
3585 *magnitude_image = (
Image *) NULL,
3586 *phase_image = (
Image *) NULL,
3587 *reconstruct_image = (
Image *) NULL,
3588 *reconstruct_magnitude = (
Image *) NULL,
3589 *test_image = (
Image *) NULL,
3590 *test_magnitude = (
Image *) NULL;
3601 test_image=CloneImage(image,0,0,MagickTrue,exception);
3602 if (test_image == (
Image *) NULL)
3603 ThrowPhaseSimilarityException();
3604 GetPixelInfoRGBA(0,0,0,0,&test_image->background_color);
3605 (void) ResetImagePage(test_image,
"0x0+0+0");
3606 status=SetImageExtent(test_image,2*(
size_t) ceil((
double) image->columns/2.0),
3607 2*(
size_t) ceil((
double) image->rows/2.0),exception);
3608 if (status == MagickFalse)
3609 ThrowPhaseSimilarityException();
3610 (void) SetImageAlphaChannel(test_image,OffAlphaChannel,exception);
3614 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3615 if (reconstruct_image == (
Image *) NULL)
3616 ThrowPhaseSimilarityException();
3617 GetPixelInfoRGBA(0,0,0,0,&reconstruct_image->background_color);
3618 (void) ResetImagePage(reconstruct_image,
"0x0+0+0");
3619 status=SetImageExtent(reconstruct_image,2*(
size_t) ceil((
double)
3620 image->columns/2.0),2*(
size_t) ceil((
double) image->rows/2.0),exception);
3621 if (status == MagickFalse)
3622 ThrowPhaseSimilarityException();
3623 (void) SetImageAlphaChannel(reconstruct_image,OffAlphaChannel,exception);
3624 (void) SetImageArtifact(test_image,
"fourier:normalize",
"inverse");
3625 fft_images=ForwardFourierTransformImage(test_image,MagickTrue,exception);
3626 if (fft_images == (
Image *) NULL)
3627 ThrowPhaseSimilarityException();
3628 test_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
3629 fft_images=DestroyImageList(fft_images);
3630 if (test_magnitude == (
Image *) NULL)
3631 ThrowPhaseSimilarityException();
3632 (void) SetImageArtifact(reconstruct_image,
"fourier:normalize",
"inverse");
3633 fft_images=ForwardFourierTransformImage(reconstruct_image,MagickTrue,
3635 if (fft_images == (
Image *) NULL)
3636 ThrowPhaseSimilarityException();
3637 reconstruct_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
3638 fft_images=DestroyImageList(fft_images);
3639 if (reconstruct_magnitude == (
Image *) NULL)
3640 ThrowPhaseSimilarityException();
3641 magnitude_image=CloneImage(reconstruct_magnitude,0,0,MagickTrue,exception);
3642 if (magnitude_image == (
Image *) NULL)
3643 ThrowPhaseSimilarityException();
3644 DisableCompositeClampUnlessSpecified(magnitude_image);
3645 (void) CompositeImage(magnitude_image,test_magnitude,MultiplyCompositeOp,
3646 MagickTrue,0,0,exception);
3650 correlation_image=SIMPhaseCorrelationImage(test_image,reconstruct_image,
3651 magnitude_image,exception);
3652 test_image=DestroyImage(test_image);
3653 reconstruct_image=DestroyImage(reconstruct_image);
3654 test_magnitude=DestroyImage(test_magnitude);
3655 reconstruct_magnitude=DestroyImage(reconstruct_magnitude);
3656 if (correlation_image == (
Image *) NULL)
3657 ThrowPhaseSimilarityException();
3661 gamma_image=CloneImage(correlation_image,0,0,MagickTrue,exception);
3662 correlation_image=DestroyImage(correlation_image);
3663 if (gamma_image == (
Image *) NULL)
3664 ThrowPhaseSimilarityException();
3668 SetGeometry(image,&geometry);
3669 geometry.width=image->columns;
3670 geometry.height=image->rows;
3671 (void) ResetImagePage(gamma_image,
"0x0+0+0");
3672 phase_image=CropImage(gamma_image,&geometry,exception);
3673 gamma_image=DestroyImage(gamma_image);
3674 if (phase_image == (
Image *) NULL)
3675 ThrowPhaseSimilarityException();
3676 (void) ResetImagePage(phase_image,
"0x0+0+0");
3680 status=GrayscaleImage(phase_image,AveragePixelIntensityMethod,exception);
3681 if (status == MagickFalse)
3682 ThrowPhaseSimilarityException();
3683 phase_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3684 status=SIMMaximaImage(phase_image,&maxima,offset,exception);
3685 if (status == MagickFalse)
3686 ThrowPhaseSimilarityException();
3687 *similarity_metric=QuantumScale*maxima;
3688 magnitude_image=DestroyImage(magnitude_image);
3689 return(phase_image);
3692static Image *PSNRSimilarityImage(
const Image *image,
const Image *reconstruct,
3695#define ThrowPSNRSimilarityException() \
3697 if (alpha_image != (Image *) NULL) \
3698 alpha_image=DestroyImage(alpha_image); \
3699 if (beta_image != (Image *) NULL) \
3700 beta_image=DestroyImage(beta_image); \
3701 if (channel_statistics != (ChannelStatistics *) NULL) \
3702 channel_statistics=(ChannelStatistics *) \
3703 RelinquishMagickMemory(channel_statistics); \
3704 if (mean_image != (Image *) NULL) \
3705 mean_image=DestroyImage(mean_image); \
3706 if (psnr_image != (Image *) NULL) \
3707 psnr_image=DestroyImage(psnr_image); \
3708 if (reconstruct_image != (Image *) NULL) \
3709 reconstruct_image=DestroyImage(reconstruct_image); \
3710 if (sum_image != (Image *) NULL) \
3711 sum_image=DestroyImage(sum_image); \
3712 if (test_image != (Image *) NULL) \
3713 test_image=DestroyImage(test_image); \
3714 return((Image *) NULL); \
3724 *alpha_image = (
Image *) NULL,
3725 *beta_image = (
Image *) NULL,
3726 *mean_image = (
Image *) NULL,
3727 *psnr_image = (
Image *) NULL,
3728 *reconstruct_image = (
Image *) NULL,
3729 *sum_image = (
Image *) NULL,
3730 *test_image = (
Image *) NULL;
3741 test_image=SIMSquareImage(image,exception);
3742 if (test_image == (
Image *) NULL)
3743 ThrowPSNRSimilarityException();
3744 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3745 if (reconstruct_image == (
Image *) NULL)
3746 ThrowPSNRSimilarityException();
3750 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3751 test_image=DestroyImage(test_image);
3752 if (alpha_image == (
Image *) NULL)
3753 ThrowPSNRSimilarityException();
3754 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3756 if (status == MagickFalse)
3757 ThrowPSNRSimilarityException();
3761 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3762 MagickTrue,0,0,exception);
3763 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3764 reconstruct_image=DestroyImage(reconstruct_image);
3765 if (beta_image == (
Image *) NULL)
3766 ThrowPSNRSimilarityException();
3767 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3769 if (status == MagickFalse)
3770 ThrowPSNRSimilarityException();
3774 sum_image=SIMSquareImage(reconstruct,exception);
3775 if (sum_image == (
Image *) NULL)
3776 ThrowPSNRSimilarityException();
3777 channel_statistics=GetImageStatistics(sum_image,exception);
3779 ThrowPSNRSimilarityException();
3780 status=SetImageExtent(sum_image,image->columns,image->rows,exception);
3781 if (status == MagickFalse)
3782 ThrowPSNRSimilarityException();
3783 status=SetImageStorageClass(sum_image,DirectClass,exception);
3784 if (status == MagickFalse)
3785 ThrowPSNRSimilarityException();
3786 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3788 RelinquishMagickMemory(channel_statistics);
3789 if (status == MagickFalse)
3790 ThrowPSNRSimilarityException();
3794 AppendImageToList(&sum_image,alpha_image);
3795 AppendImageToList(&sum_image,beta_image);
3796 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3797 if (mean_image == (
Image *) NULL)
3798 ThrowPSNRSimilarityException();
3799 sum_image=DestroyImage(sum_image);
3800 status=SIMLogImage(mean_image,exception);
3801 if (status == MagickFalse)
3802 ThrowPSNRSimilarityException();
3803 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
3804 if (status == MagickFalse)
3805 ThrowPSNRSimilarityException();
3806 mean_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3809 if (status == MagickFalse)
3810 ThrowPSNRSimilarityException();
3814 SetGeometry(image,&geometry);
3815 geometry.width=image->columns;
3816 geometry.height=image->rows;
3817 (void) ResetImagePage(mean_image,
"0x0+0+0");
3818 psnr_image=CropImage(mean_image,&geometry,exception);
3819 mean_image=DestroyImage(mean_image);
3820 if (psnr_image == (
Image *) NULL)
3821 ThrowPSNRSimilarityException();
3825 (void) ResetImagePage(psnr_image,
"0x0+0+0");
3826 (void) EvaluateImage(psnr_image,MaxEvaluateOperator,0.0,exception);
3827 status=SIMMinimaImage(psnr_image,&minima,offset,exception);
3828 if (status == MagickFalse)
3829 ThrowPSNRSimilarityException();
3830 status=NegateImage(psnr_image,MagickFalse,exception);
3831 if (status == MagickFalse)
3832 ThrowPSNRSimilarityException();
3833 *similarity_metric=(-1.0+QuantumScale*minima);
3834 alpha_image=DestroyImage(alpha_image);
3835 beta_image=DestroyImage(beta_image);
3839static Image *RMSESimilarityImage(
const Image *image,
const Image *reconstruct,
3842#define ThrowRMSESimilarityException() \
3844 if (alpha_image != (Image *) NULL) \
3845 alpha_image=DestroyImage(alpha_image); \
3846 if (beta_image != (Image *) NULL) \
3847 beta_image=DestroyImage(beta_image); \
3848 if (channel_statistics != (ChannelStatistics *) NULL) \
3849 channel_statistics=(ChannelStatistics *) \
3850 RelinquishMagickMemory(channel_statistics); \
3851 if (mean_image != (Image *) NULL) \
3852 mean_image=DestroyImage(mean_image); \
3853 if (rmse_image != (Image *) NULL) \
3854 rmse_image=DestroyImage(rmse_image); \
3855 if (reconstruct_image != (Image *) NULL) \
3856 reconstruct_image=DestroyImage(reconstruct_image); \
3857 if (sum_image != (Image *) NULL) \
3858 sum_image=DestroyImage(sum_image); \
3859 if (alpha_image != (Image *) NULL) \
3860 alpha_image=DestroyImage(alpha_image); \
3861 return((Image *) NULL); \
3871 *alpha_image = (
Image *) NULL,
3872 *beta_image = (
Image *) NULL,
3873 *mean_image = (
Image *) NULL,
3874 *reconstruct_image = (
Image *) NULL,
3875 *rmse_image = (
Image *) NULL,
3876 *sum_image = (
Image *) NULL,
3877 *test_image = (
Image *) NULL;
3888 test_image=SIMSquareImage(image,exception);
3889 if (test_image == (
Image *) NULL)
3890 ThrowRMSESimilarityException();
3891 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3892 if (reconstruct_image == (
Image *) NULL)
3893 ThrowRMSESimilarityException();
3897 alpha_image=SIMCrossCorrelationImage(test_image,reconstruct_image,exception);
3898 test_image=DestroyImage(test_image);
3899 if (alpha_image == (
Image *) NULL)
3900 ThrowRMSESimilarityException();
3901 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3903 if (status == MagickFalse)
3904 ThrowRMSESimilarityException();
3908 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3909 MagickTrue,0,0,exception);
3910 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3911 reconstruct_image=DestroyImage(reconstruct_image);
3912 if (beta_image == (
Image *) NULL)
3913 ThrowRMSESimilarityException();
3914 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3916 if (status == MagickFalse)
3917 ThrowRMSESimilarityException();
3921 sum_image=SIMSquareImage(reconstruct,exception);
3922 if (sum_image == (
Image *) NULL)
3923 ThrowRMSESimilarityException();
3924 channel_statistics=GetImageStatistics(sum_image,exception);
3926 ThrowRMSESimilarityException();
3927 status=SetImageExtent(sum_image,image->columns,image->rows,exception);
3928 if (status == MagickFalse)
3929 ThrowRMSESimilarityException();
3930 status=SetImageStorageClass(sum_image,DirectClass,exception);
3931 if (status == MagickFalse)
3932 ThrowRMSESimilarityException();
3933 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3935 RelinquishMagickMemory(channel_statistics);
3936 if (status == MagickFalse)
3937 ThrowRMSESimilarityException();
3941 AppendImageToList(&sum_image,alpha_image);
3942 AppendImageToList(&sum_image,beta_image);
3943 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3944 if (mean_image == (
Image *) NULL)
3945 ThrowRMSESimilarityException();
3946 status=EvaluateImage(mean_image,PowEvaluateOperator,0.5,exception);
3947 if (mean_image == (
Image *) NULL)
3948 ThrowRMSESimilarityException();
3949 sum_image=DestroyImage(sum_image);
3950 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
3951 if (status == MagickFalse)
3952 ThrowRMSESimilarityException();
3953 mean_image->depth=MAGICKCORE_QUANTUM_DEPTH;
3957 SetGeometry(image,&geometry);
3958 geometry.width=image->columns;
3959 geometry.height=image->rows;
3960 (void) ResetImagePage(mean_image,
"0x0+0+0");
3961 rmse_image=CropImage(mean_image,&geometry,exception);
3962 mean_image=DestroyImage(mean_image);
3963 if (rmse_image == (
Image *) NULL)
3964 ThrowRMSESimilarityException();
3968 (void) ResetImagePage(rmse_image,
"0x0+0+0");
3969 status=SIMMinimaImage(rmse_image,&minima,offset,exception);
3970 if (status == MagickFalse)
3971 ThrowRMSESimilarityException();
3972 status=NegateImage(rmse_image,MagickFalse,exception);
3973 if (status == MagickFalse)
3974 ThrowRMSESimilarityException();
3975 *similarity_metric=QuantumScale*minima;
3976 alpha_image=DestroyImage(alpha_image);
3977 beta_image=DestroyImage(beta_image);
3983static double GetSimilarityMetric(
const Image *image,
const Image *reconstruct,
3984 const MetricType metric,
const ssize_t x_offset,
const ssize_t y_offset,
3991 *sans_exception = AcquireExceptionInfo();
4002 SetGeometry(reconstruct,&geometry);
4003 geometry.x=x_offset;
4004 geometry.y=y_offset;
4005 similarity_image=CropImage(image,&geometry,sans_exception);
4006 sans_exception=DestroyExceptionInfo(sans_exception);
4007 if (similarity_image == (
Image *) NULL)
4010 status=GetImageDistortion(similarity_image,reconstruct,metric,&distortion,
4012 similarity_image=DestroyImage(similarity_image);
4013 if (status == MagickFalse)
4018MagickExport
Image *SimilarityImage(
const Image *image,
const Image *reconstruct,
4019 const MetricType metric,
const double similarity_threshold,
4022#define SimilarityImageTag "Similarity/Image"
4028 *similarity_image = (
Image *) NULL;
4042 assert(image != (
const Image *) NULL);
4043 assert(image->signature == MagickCoreSignature);
4045 assert(exception->signature == MagickCoreSignature);
4047 if (IsEventLogging() != MagickFalse)
4048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4049 SetGeometry(reconstruct,offset);
4050 *similarity_metric=MagickMaximumValue;
4051#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
4053 const char *artifact = GetImageArtifact(image,
"compare:frequency-domain");
4054 if (artifact == (
const char *) NULL)
4055 artifact=GetImageArtifact(image,
"compare:accelerate-ncc");
4056 if ((image->channels & ReadMaskChannel) == 0)
4059 case DotProductCorrelationErrorMetric:
4061 similarity_image=DPCSimilarityImage(image,reconstruct,offset,
4062 similarity_metric,exception);
4063 return(similarity_image);
4065 case MeanSquaredErrorMetric:
4067 if ((artifact != (
const char *) NULL) &&
4068 (IsStringTrue(artifact) == MagickFalse))
4070 similarity_image=MSESimilarityImage(image,reconstruct,offset,
4071 similarity_metric,exception);
4072 return(similarity_image);
4074 case NormalizedCrossCorrelationErrorMetric:
4076 if ((artifact != (
const char *) NULL) &&
4077 (IsStringTrue(artifact) == MagickFalse))
4079 similarity_image=NCCSimilarityImage(image,reconstruct,offset,
4080 similarity_metric,exception);
4081 return(similarity_image);
4083 case PeakSignalToNoiseRatioErrorMetric:
4085 if ((artifact != (
const char *) NULL) &&
4086 (IsStringTrue(artifact) == MagickFalse))
4088 similarity_image=PSNRSimilarityImage(image,reconstruct,offset,
4089 similarity_metric,exception);
4090 return(similarity_image);
4092 case PhaseCorrelationErrorMetric:
4094 similarity_image=PhaseSimilarityImage(image,reconstruct,offset,
4095 similarity_metric,exception);
4096 return(similarity_image);
4098 case RootMeanSquaredErrorMetric:
4100 if ((artifact != (
const char *) NULL) &&
4101 (IsStringTrue(artifact) == MagickFalse))
4103 similarity_image=RMSESimilarityImage(image,reconstruct,offset,
4104 similarity_metric,exception);
4105 return(similarity_image);
4111 if ((metric == DotProductCorrelationErrorMetric) ||
4112 (metric == PhaseCorrelationErrorMetric))
4114 (void) ThrowMagickException(exception,GetMagickModule(),
4115 MissingDelegateError,
"DelegateLibrarySupportNotBuiltIn",
4116 "'%s' (HDRI, FFT)",image->filename);
4117 return((
Image *) NULL);
4120 if ((image->columns < reconstruct->columns) ||
4121 (image->rows < reconstruct->rows))
4123 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
4124 "GeometryDoesNotContainImage",
"`%s'",image->filename);
4125 return((
Image *) NULL);
4127 similarity_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4129 if (similarity_image == (
Image *) NULL)
4130 return((
Image *) NULL);
4131 similarity_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4132 similarity_image->alpha_trait=UndefinedPixelTrait;
4133 status=SetImageStorageClass(similarity_image,DirectClass,exception);
4134 if (status == MagickFalse)
4135 return(DestroyImage(similarity_image));
4141 similarity_view=AcquireAuthenticCacheView(similarity_image,exception);
4142 rows=similarity_image->rows;
4143#if defined(MAGICKCORE_OPENMP_SUPPORT)
4144 #pragma omp parallel for schedule(static,1) \
4145 shared(progress,similarity_metric,status) \
4146 magick_number_threads(similarity_image,similarity_image,rows << 3,1)
4148 for (y=0; y < (ssize_t) rows; y++)
4159 if (status == MagickFalse)
4161#if defined(MAGICKCORE_OPENMP_SUPPORT)
4162 #pragma omp flush(similarity_metric)
4164 if (*similarity_metric <= similarity_threshold)
4166 q=QueueCacheViewAuthenticPixels(similarity_view,0,y,
4167 similarity_image->columns,1,exception);
4168 if (q == (Quantum *) NULL)
4173 for (x=0; x < (ssize_t) similarity_image->columns; x++)
4178#if defined(MAGICKCORE_OPENMP_SUPPORT)
4179 #pragma omp flush(similarity_metric)
4181 if (*similarity_metric <= similarity_threshold)
4183 similarity=GetSimilarityMetric(image,reconstruct,metric,x,y,exception);
4184 if ((metric == DotProductCorrelationErrorMetric) ||
4185 (metric == PhaseCorrelationErrorMetric) ||
4186 (metric == NormalizedCrossCorrelationErrorMetric) ||
4187 (metric == StructuralSimilarityErrorMetric) ||
4188 (metric == UndefinedErrorMetric))
4189 similarity=1.0-similarity;
4190 if (metric == PerceptualHashErrorMetric)
4191 similarity=MagickMin(0.01*similarity,1.0);
4192#if defined(MAGICKCORE_OPENMP_SUPPORT)
4193 #pragma omp critical (MagickCore_SimilarityImage)
4195 if (similarity < *similarity_metric)
4199 *similarity_metric=similarity;
4201 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4203 PixelChannel channel = GetPixelChannelChannel(image,i);
4204 PixelTrait traits = GetPixelChannelTraits(image,channel);
4205 PixelTrait similarity_traits = GetPixelChannelTraits(similarity_image,
4207 if ((traits == UndefinedPixelTrait) ||
4208 (similarity_traits == UndefinedPixelTrait) ||
4209 ((similarity_traits & UpdatePixelTrait) == 0))
4213 case AbsoluteErrorMetric:
4214 case FuzzErrorMetric:
4215 case MeanAbsoluteErrorMetric:
4216 case MeanErrorPerPixelErrorMetric:
4217 case MeanSquaredErrorMetric:
4218 case NormalizedCrossCorrelationErrorMetric:
4219 case PeakAbsoluteErrorMetric:
4220 case PerceptualHashErrorMetric:
4221 case RootMeanSquaredErrorMetric:
4222 case StructuralSimilarityErrorMetric:
4223 case StructuralDissimilarityErrorMetric:
4225 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4226 QuantumRange-QuantumRange*similarity),q);
4231 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4232 QuantumRange*similarity),q);
4237 q+=(ptrdiff_t) GetPixelChannels(similarity_image);
4239 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
4241 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4246#if defined(MAGICKCORE_OPENMP_SUPPORT)
4250 proceed=SetImageProgress(image,SimilarityImageTag,progress,image->rows);
4251 if (proceed == MagickFalse)
4255 similarity_view=DestroyCacheView(similarity_view);
4256 (void) SetImageType(similarity_image,GrayscaleType,exception);
4257 if (status == MagickFalse)
4258 similarity_image=DestroyImage(similarity_image);
4259 return(similarity_image);