43#include "magick/studio.h"
44#include "magick/artifact.h"
45#include "magick/attribute.h"
46#include "magick/cache-view.h"
47#include "magick/channel.h"
48#include "magick/client.h"
49#include "magick/color.h"
50#include "magick/color-private.h"
51#include "magick/colorspace.h"
52#include "magick/colorspace-private.h"
53#include "magick/compare.h"
54#include "magick/compare-private.h"
55#include "magick/composite-private.h"
56#include "magick/constitute.h"
57#include "magick/exception-private.h"
58#include "magick/geometry.h"
59#include "magick/image-private.h"
60#include "magick/list.h"
61#include "magick/log.h"
62#include "magick/memory_.h"
63#include "magick/monitor.h"
64#include "magick/monitor-private.h"
65#include "magick/option.h"
66#include "magick/pixel-private.h"
67#include "magick/property.h"
68#include "magick/resource_.h"
69#include "magick/statistic-private.h"
70#include "magick/string_.h"
71#include "magick/string-private.h"
72#include "magick/statistic.h"
73#include "magick/thread-private.h"
74#include "magick/transform.h"
75#include "magick/utility.h"
76#include "magick/version.h"
114MagickExport Image *CompareImages(Image *image,
const Image *reconstruct_image,
115 const MetricType metric,
double *distortion,ExceptionInfo *exception)
120 highlight_image=CompareImageChannels(image,reconstruct_image,
121 CompositeChannels,metric,distortion,exception);
122 return(highlight_image);
125static size_t GetNumberChannels(
const Image *image,
const ChannelType channel)
131 if ((channel & RedChannel) != 0)
133 if ((channel & GreenChannel) != 0)
135 if ((channel & BlueChannel) != 0)
137 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
139 if (((channel & IndexChannel) != 0) && (image->colorspace == CMYKColorspace))
141 return(channels == 0 ? 1UL : channels);
144static inline MagickBooleanType ValidateImageMorphology(
145 const Image *magick_restrict image,
146 const Image *magick_restrict reconstruct_image)
151 if (GetNumberChannels(image,DefaultChannels) !=
152 GetNumberChannels(reconstruct_image,DefaultChannels))
157MagickExport Image *CompareImageChannels(Image *image,
158 const Image *reconstruct_image,
const ChannelType channel,
159 const MetricType metric,
double *distortion,ExceptionInfo *exception)
189 assert(image != (Image *) NULL);
190 assert(image->signature == MagickCoreSignature);
191 assert(reconstruct_image != (
const Image *) NULL);
192 assert(reconstruct_image->signature == MagickCoreSignature);
193 assert(distortion != (
double *) NULL);
194 if (IsEventLogging() != MagickFalse)
195 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
197 if (metric != PerceptualHashErrorMetric)
198 if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
199 ThrowImageException(ImageError,
"ImageMorphologyDiffers");
200 status=GetImageChannelDistortion(image,reconstruct_image,channel,metric,
201 distortion,exception);
202 if (status == MagickFalse)
203 return((Image *) NULL);
204 clone_image=CloneImage(image,0,0,MagickTrue,exception);
205 if (clone_image == (Image *) NULL)
206 return((Image *) NULL);
207 (void) SetImageMask(clone_image,(Image *) NULL);
208 difference_image=CloneImage(clone_image,0,0,MagickTrue,exception);
209 clone_image=DestroyImage(clone_image);
210 if (difference_image == (Image *) NULL)
211 return((Image *) NULL);
212 (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel);
213 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
214 highlight_image=CloneImage(image,columns,rows,MagickTrue,exception);
215 if (highlight_image == (Image *) NULL)
217 difference_image=DestroyImage(difference_image);
218 return((Image *) NULL);
220 if (SetImageStorageClass(highlight_image,DirectClass) == MagickFalse)
222 InheritException(exception,&highlight_image->exception);
223 difference_image=DestroyImage(difference_image);
224 highlight_image=DestroyImage(highlight_image);
225 return((Image *) NULL);
227 (void) SetImageMask(highlight_image,(Image *) NULL);
228 (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel);
229 (void) QueryMagickColor(
"#f1001ecc",&highlight,exception);
230 artifact=GetImageArtifact(image,
"compare:highlight-color");
231 if (artifact != (
const char *) NULL)
232 (void) QueryMagickColor(artifact,&highlight,exception);
233 (void) QueryMagickColor(
"#ffffffcc",&lowlight,exception);
234 artifact=GetImageArtifact(image,
"compare:lowlight-color");
235 if (artifact != (
const char *) NULL)
236 (void) QueryMagickColor(artifact,&lowlight,exception);
237 if (highlight_image->colorspace == CMYKColorspace)
239 ConvertRGBToCMYK(&highlight);
240 ConvertRGBToCMYK(&lowlight);
245 GetMagickPixelPacket(image,&zero);
246 image_view=AcquireVirtualCacheView(image,exception);
247 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
248 highlight_view=AcquireAuthenticCacheView(highlight_image,exception);
249#if defined(MAGICKCORE_OPENMP_SUPPORT)
250 #pragma omp parallel for schedule(static) shared(status) \
251 magick_number_threads(image,highlight_image,rows,1)
253 for (y=0; y < (ssize_t) rows; y++)
263 *magick_restrict indexes,
264 *magick_restrict reconstruct_indexes;
271 *magick_restrict highlight_indexes;
279 if (status == MagickFalse)
281 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
282 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
283 r=QueueCacheViewAuthenticPixels(highlight_view,0,y,columns,1,exception);
284 if ((p == (
const PixelPacket *) NULL) ||
285 (q == (
const PixelPacket *) NULL) || (r == (PixelPacket *) NULL))
290 indexes=GetCacheViewVirtualIndexQueue(image_view);
291 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
292 highlight_indexes=GetCacheViewAuthenticIndexQueue(highlight_view);
294 reconstruct_pixel=zero;
295 for (x=0; x < (ssize_t) columns; x++)
297 SetMagickPixelPacket(image,p,indexes == (IndexPacket *) NULL ? NULL :
299 SetMagickPixelPacket(reconstruct_image,q,reconstruct_indexes ==
300 (IndexPacket *) NULL ? NULL : reconstruct_indexes+x,&reconstruct_pixel);
301 if (IsMagickColorSimilar(&pixel,&reconstruct_pixel) == MagickFalse)
302 SetPixelPacket(highlight_image,&highlight,r,highlight_indexes ==
303 (IndexPacket *) NULL ? NULL : highlight_indexes+x);
305 SetPixelPacket(highlight_image,&lowlight,r,highlight_indexes ==
306 (IndexPacket *) NULL ? NULL : highlight_indexes+x);
311 sync=SyncCacheViewAuthenticPixels(highlight_view,exception);
312 if (sync == MagickFalse)
315 highlight_view=DestroyCacheView(highlight_view);
316 reconstruct_view=DestroyCacheView(reconstruct_view);
317 image_view=DestroyCacheView(image_view);
318 (void) CompositeImage(difference_image,image->compose,highlight_image,0,0);
319 highlight_image=DestroyImage(highlight_image);
320 if (status == MagickFalse)
321 difference_image=DestroyImage(difference_image);
322 return(difference_image);
361MagickExport MagickBooleanType GetImageDistortion(Image *image,
362 const Image *reconstruct_image,
const MetricType metric,
double *distortion,
363 ExceptionInfo *exception)
368 status=GetImageChannelDistortion(image,reconstruct_image,CompositeChannels,
369 metric,distortion,exception);
373static MagickBooleanType GetAESimilarity(
const Image *image,
374 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
375 ExceptionInfo *exception)
399 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
400 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
401 image_view=AcquireVirtualCacheView(image,exception);
402 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
403#if defined(MAGICKCORE_OPENMP_SUPPORT)
404 #pragma omp parallel for schedule(static) shared(similarity,status) \
405 magick_number_threads(image,image,rows,1)
407 for (y=0; y < (ssize_t) rows; y++)
410 *magick_restrict indexes,
411 *magick_restrict reconstruct_indexes;
418 channel_similarity[CompositeChannels+1] = { 0.0 };
424 if (status == MagickFalse)
426 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
427 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
428 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
433 indexes=GetCacheViewVirtualIndexQueue(image_view);
434 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
435 (void) memset(channel_similarity,0,
sizeof(channel_similarity));
436 for (x=0; x < (ssize_t) columns; x++)
446 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
447 ((double) QuantumRange-(double) OpaqueOpacity));
448 Da=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(q) :
449 ((double) QuantumRange-(double) OpaqueOpacity));
450 if ((channel & RedChannel) != 0)
452 error=Sa*(double) GetPixelRed(p)-Da*(double)
454 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
456 channel_similarity[RedChannel]++;
460 if ((channel & GreenChannel) != 0)
462 error=Sa*(double) GetPixelGreen(p)-Da*(double)
464 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
466 channel_similarity[GreenChannel]++;
470 if ((channel & BlueChannel) != 0)
472 error=Sa*(double) GetPixelBlue(p)-Da*(double)
474 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
476 channel_similarity[BlueChannel]++;
480 if (((channel & OpacityChannel) != 0) &&
481 (image->matte != MagickFalse))
483 error=(double) GetPixelOpacity(p)-(double) GetPixelOpacity(q);
484 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
486 channel_similarity[OpacityChannel]++;
490 if (((channel & IndexChannel) != 0) &&
491 (image->colorspace == CMYKColorspace))
493 error=Sa*(double) indexes[x]-Da*(
double) reconstruct_indexes[x];
494 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
496 channel_similarity[IndexChannel]++;
501 channel_similarity[CompositeChannels]++;
505#if defined(MAGICKCORE_OPENMP_SUPPORT)
506 #pragma omp critical (MagickCore_GetAESimilarity)
508 for (i=0; i <= (ssize_t) CompositeChannels; i++)
509 similarity[i]+=channel_similarity[i];
511 reconstruct_view=DestroyCacheView(reconstruct_view);
512 image_view=DestroyCacheView(image_view);
513 area=MagickSafeReciprocal((
double) columns*rows);
514 for (j=0; j <= CompositeChannels; j++)
519static MagickBooleanType GetFUZZSimilarity(
const Image *image,
520 const Image *reconstruct_image,
const ChannelType channel,
521 double *similarity,ExceptionInfo *exception)
542 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
543 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
544 image_view=AcquireVirtualCacheView(image,exception);
545 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
546#if defined(MAGICKCORE_OPENMP_SUPPORT)
547 #pragma omp parallel for schedule(static) shared(status) \
548 magick_number_threads(image,image,rows,1)
550 for (y=0; y < (ssize_t) rows; y++)
554 channel_similarity[CompositeChannels+1] = { 0.0 };
557 *magick_restrict indexes,
558 *magick_restrict reconstruct_indexes;
568 if (status == MagickFalse)
570 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
571 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
572 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
577 indexes=GetCacheViewVirtualIndexQueue(image_view);
578 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
579 for (x=0; x < (ssize_t) columns; x++)
586 Sa=QuantumScale*(image->matte != MagickFalse ? (double)
587 GetPixelAlpha(p) : ((double) QuantumRange-(double) OpaqueOpacity));
588 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
589 (double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
591 if ((channel & RedChannel) != 0)
593 error=QuantumScale*(Sa*GetPixelRed(p)-Da*GetPixelRed(q));
594 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
596 channel_similarity[RedChannel]+=error*error;
597 channel_similarity[CompositeChannels]+=error*error;
601 if ((channel & GreenChannel) != 0)
603 error=QuantumScale*(Sa*GetPixelGreen(p)-Da*GetPixelGreen(q));
604 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
606 channel_similarity[GreenChannel]+=error*error;
607 channel_similarity[CompositeChannels]+=error*error;
611 if ((channel & BlueChannel) != 0)
613 error=QuantumScale*(Sa*GetPixelBlue(p)-Da*GetPixelBlue(q));
614 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
616 channel_similarity[BlueChannel]+=error*error;
617 channel_similarity[CompositeChannels]+=error*error;
621 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
623 error=QuantumScale*((double) GetPixelOpacity(p)-GetPixelOpacity(q));
624 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
626 channel_similarity[OpacityChannel]+=error*error;
627 channel_similarity[CompositeChannels]+=error*error;
631 if (((channel & IndexChannel) != 0) &&
632 (image->colorspace == CMYKColorspace))
634 error=QuantumScale*(Sa*GetPixelIndex(indexes+x)-Da*
635 GetPixelIndex(reconstruct_indexes+x));
636 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
638 channel_similarity[BlackChannel]+=error*error;
639 channel_similarity[CompositeChannels]+=error*error;
646#if defined(MAGICKCORE_OPENMP_SUPPORT)
647 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
651 for (i=0; i <= (ssize_t) CompositeChannels; i++)
652 similarity[i]+=channel_similarity[i];
655 reconstruct_view=DestroyCacheView(reconstruct_view);
656 image_view=DestroyCacheView(image_view);
657 area=MagickSafeReciprocal(area);
658 for (i=0; i <= (ssize_t) CompositeChannels; i++)
663static MagickBooleanType GetPDCSimilarity(
const Image *image,
664 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
665 ExceptionInfo *exception)
689 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
690 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
691 image_view=AcquireVirtualCacheView(image,exception);
692 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
693#if defined(MAGICKCORE_OPENMP_SUPPORT)
694 #pragma omp parallel for schedule(static) shared(similarity,status) \
695 magick_number_threads(image,image,rows,1)
697 for (y=0; y < (ssize_t) rows; y++)
700 *magick_restrict indexes,
701 *magick_restrict reconstruct_indexes;
708 channel_similarity[CompositeChannels+1] = { 0.0 };
714 if (status == MagickFalse)
716 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
717 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
718 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
723 indexes=GetCacheViewVirtualIndexQueue(image_view);
724 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
725 (void) memset(channel_similarity,0,
sizeof(channel_similarity));
726 for (x=0; x < (ssize_t) columns; x++)
736 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
737 ((double) QuantumRange-(double) OpaqueOpacity));
738 Da=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(q) :
739 ((double) QuantumRange-(double) OpaqueOpacity));
740 if ((channel & RedChannel) != 0)
742 error=Sa*(double) GetPixelRed(p)-Da*(double)
744 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
746 channel_similarity[RedChannel]++;
750 if ((channel & GreenChannel) != 0)
752 error=Sa*(double) GetPixelGreen(p)-Da*(double)
754 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
756 channel_similarity[GreenChannel]++;
760 if ((channel & BlueChannel) != 0)
762 error=Sa*(double) GetPixelBlue(p)-Da*(double)
764 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
766 channel_similarity[BlueChannel]++;
770 if (((channel & OpacityChannel) != 0) &&
771 (image->matte != MagickFalse))
773 error=(double) GetPixelOpacity(p)-(double) GetPixelOpacity(q);
774 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
776 channel_similarity[OpacityChannel]++;
780 if (((channel & IndexChannel) != 0) &&
781 (image->colorspace == CMYKColorspace))
783 error=Sa*(double) indexes[x]-Da*(
double) reconstruct_indexes[x];
784 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
786 channel_similarity[IndexChannel]++;
791 channel_similarity[CompositeChannels]++;
795#if defined(MAGICKCORE_OPENMP_SUPPORT)
796 #pragma omp critical (MagickCore_GetAESimilarity)
798 for (i=0; i <= (ssize_t) CompositeChannels; i++)
799 similarity[i]+=channel_similarity[i];
801 reconstruct_view=DestroyCacheView(reconstruct_view);
802 image_view=DestroyCacheView(image_view);
803 area=MagickSafeReciprocal((
double) columns*rows);
804 for (j=0; j <= CompositeChannels; j++)
809static MagickBooleanType GetMAESimilarity(
const Image *image,
810 const Image *reconstruct_image,
const ChannelType channel,
811 double *similarity,ExceptionInfo *exception)
829 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
830 image_view=AcquireVirtualCacheView(image,exception);
831 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
832#if defined(MAGICKCORE_OPENMP_SUPPORT)
833 #pragma omp parallel for schedule(static) shared(status) \
834 magick_number_threads(image,image,rows,1)
836 for (y=0; y < (ssize_t) rows; y++)
839 channel_similarity[CompositeChannels+1];
842 *magick_restrict indexes,
843 *magick_restrict reconstruct_indexes;
853 if (status == MagickFalse)
855 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
856 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
857 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
862 indexes=GetCacheViewVirtualIndexQueue(image_view);
863 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
864 (void) memset(channel_similarity,0,
sizeof(channel_similarity));
865 for (x=0; x < (ssize_t) columns; x++)
872 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
873 ((double) QuantumRange-(double) OpaqueOpacity));
874 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
875 (double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
877 if ((channel & RedChannel) != 0)
879 distance=QuantumScale*fabs(Sa*(
double) GetPixelRed(p)-Da*
880 (
double) GetPixelRed(q));
881 channel_similarity[RedChannel]+=distance;
882 channel_similarity[CompositeChannels]+=distance;
884 if ((channel & GreenChannel) != 0)
886 distance=QuantumScale*fabs(Sa*(
double) GetPixelGreen(p)-Da*
887 (
double) GetPixelGreen(q));
888 channel_similarity[GreenChannel]+=distance;
889 channel_similarity[CompositeChannels]+=distance;
891 if ((channel & BlueChannel) != 0)
893 distance=QuantumScale*fabs(Sa*(
double) GetPixelBlue(p)-Da*
894 (
double) GetPixelBlue(q));
895 channel_similarity[BlueChannel]+=distance;
896 channel_similarity[CompositeChannels]+=distance;
898 if (((channel & OpacityChannel) != 0) &&
899 (image->matte != MagickFalse))
901 distance=QuantumScale*fabs((double) GetPixelOpacity(p)-(double)
903 channel_similarity[OpacityChannel]+=distance;
904 channel_similarity[CompositeChannels]+=distance;
906 if (((channel & IndexChannel) != 0) &&
907 (image->colorspace == CMYKColorspace))
909 distance=QuantumScale*fabs(Sa*(double) GetPixelIndex(indexes+x)-Da*
910 (double) GetPixelIndex(reconstruct_indexes+x));
911 channel_similarity[BlackChannel]+=distance;
912 channel_similarity[CompositeChannels]+=distance;
917#if defined(MAGICKCORE_OPENMP_SUPPORT)
918 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
920 for (i=0; i <= (ssize_t) CompositeChannels; i++)
921 similarity[i]+=channel_similarity[i];
923 reconstruct_view=DestroyCacheView(reconstruct_view);
924 image_view=DestroyCacheView(image_view);
925 for (i=0; i <= (ssize_t) CompositeChannels; i++)
926 similarity[i]/=((
double) columns*rows);
927 similarity[CompositeChannels]/=(double) GetNumberChannels(image,channel);
931static MagickBooleanType GetMEPPSimilarity(Image *image,
932 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
933 ExceptionInfo *exception)
940 maximum_error = -MagickMaximumValue,
955 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
956 image_view=AcquireVirtualCacheView(image,exception);
957 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
958#if defined(MAGICKCORE_OPENMP_SUPPORT)
959 #pragma omp parallel for schedule(static) shared(maximum_error,status) \
960 magick_number_threads(image,image,rows,1)
962 for (y=0; y < (ssize_t) rows; y++)
965 channel_similarity[CompositeChannels+1] = { 0.0 },
966 local_maximum = maximum_error,
967 local_mean_error = 0.0;
970 *magick_restrict indexes,
971 *magick_restrict reconstruct_indexes;
981 if (status == MagickFalse)
983 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
984 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
985 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
990 indexes=GetCacheViewVirtualIndexQueue(image_view);
991 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
992 (void) memset(channel_similarity,0,
sizeof(channel_similarity));
993 for (x=0; x < (ssize_t) columns; x++)
1000 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
1001 ((double) QuantumRange-(double) OpaqueOpacity));
1002 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
1003 (double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
1005 if ((channel & RedChannel) != 0)
1007 distance=QuantumScale*fabs(Sa*(
double) GetPixelRed(p)-Da*
1008 (
double) GetPixelRed(q));
1009 channel_similarity[RedChannel]+=distance;
1010 channel_similarity[CompositeChannels]+=distance;
1011 local_mean_error+=distance*distance;
1012 if (distance > local_maximum)
1013 local_maximum=distance;
1015 if ((channel & GreenChannel) != 0)
1017 distance=QuantumScale*fabs(Sa*(
double) GetPixelGreen(p)-Da*
1018 (
double) GetPixelGreen(q));
1019 channel_similarity[GreenChannel]+=distance;
1020 channel_similarity[CompositeChannels]+=distance;
1021 local_mean_error+=distance*distance;
1022 if (distance > local_maximum)
1023 local_maximum=distance;
1025 if ((channel & BlueChannel) != 0)
1027 distance=QuantumScale*fabs(Sa*(
double) GetPixelBlue(p)-Da*
1028 (
double) GetPixelBlue(q));
1029 channel_similarity[BlueChannel]+=distance;
1030 channel_similarity[CompositeChannels]+=distance;
1031 local_mean_error+=distance*distance;
1032 if (distance > local_maximum)
1033 local_maximum=distance;
1035 if (((channel & OpacityChannel) != 0) &&
1036 (image->matte != MagickFalse))
1038 distance=QuantumScale*fabs((double) GetPixelOpacity(p)-(double)
1039 GetPixelOpacity(q));
1040 channel_similarity[OpacityChannel]+=distance;
1041 channel_similarity[CompositeChannels]+=distance;
1042 local_mean_error+=distance*distance;
1043 if (distance > local_maximum)
1044 local_maximum=distance;
1046 if (((channel & IndexChannel) != 0) &&
1047 (image->colorspace == CMYKColorspace))
1049 distance=QuantumScale*fabs(Sa*(double) GetPixelIndex(indexes+x)-Da*
1050 (double) GetPixelIndex(reconstruct_indexes+x));
1051 channel_similarity[BlackChannel]+=distance;
1052 channel_similarity[CompositeChannels]+=distance;
1053 local_mean_error+=distance*distance;
1054 if (distance > local_maximum)
1055 local_maximum=distance;
1060#if defined(MAGICKCORE_OPENMP_SUPPORT)
1061 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
1064 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1065 similarity[i]+=channel_similarity[i];
1066 mean_error+=local_mean_error;
1067 if (local_maximum > maximum_error)
1068 maximum_error=local_maximum;
1071 reconstruct_view=DestroyCacheView(reconstruct_view);
1072 image_view=DestroyCacheView(image_view);
1073 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1074 similarity[i]/=((
double) columns*rows);
1075 similarity[CompositeChannels]/=(double) GetNumberChannels(image,channel);
1076 image->error.mean_error_per_pixel=QuantumRange*similarity[CompositeChannels];
1077 image->error.normalized_mean_error=mean_error/((double) columns*rows);
1078 image->error.normalized_maximum_error=maximum_error;
1082static MagickBooleanType GetMSESimilarity(
const Image *image,
1083 const Image *reconstruct_image,
const ChannelType channel,
1084 double *similarity,ExceptionInfo *exception)
1105 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1106 image_view=AcquireVirtualCacheView(image,exception);
1107 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1108#if defined(MAGICKCORE_OPENMP_SUPPORT)
1109 #pragma omp parallel for schedule(static) shared(similarity,status) \
1110 magick_number_threads(image,image,rows,1)
1112 for (y=0; y < (ssize_t) rows; y++)
1115 channel_similarity[CompositeChannels+1] = { 0.0 };
1118 *magick_restrict indexes,
1119 *magick_restrict reconstruct_indexes;
1129 if (status == MagickFalse)
1131 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1132 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1133 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
1138 indexes=GetCacheViewVirtualIndexQueue(image_view);
1139 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
1140 for (x=0; x < (ssize_t) columns; x++)
1147 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
1148 ((double) QuantumRange-(double) OpaqueOpacity));
1149 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
1150 (double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
1152 if ((channel & RedChannel) != 0)
1154 distance=QuantumScale*(Sa*(double) GetPixelRed(p)-Da*(double)
1156 channel_similarity[RedChannel]+=distance*distance;
1157 channel_similarity[CompositeChannels]+=distance*distance;
1159 if ((channel & GreenChannel) != 0)
1161 distance=QuantumScale*(Sa*(double) GetPixelGreen(p)-Da*(double)
1163 channel_similarity[GreenChannel]+=distance*distance;
1164 channel_similarity[CompositeChannels]+=distance*distance;
1166 if ((channel & BlueChannel) != 0)
1168 distance=QuantumScale*(Sa*(double) GetPixelBlue(p)-Da*(double)
1170 channel_similarity[BlueChannel]+=distance*distance;
1171 channel_similarity[CompositeChannels]+=distance*distance;
1173 if (((channel & OpacityChannel) != 0) &&
1174 (image->matte != MagickFalse))
1176 distance=QuantumScale*((double) GetPixelOpacity(p)-(double)
1177 GetPixelOpacity(q));
1178 channel_similarity[OpacityChannel]+=distance*distance;
1179 channel_similarity[CompositeChannels]+=distance*distance;
1181 if (((channel & IndexChannel) != 0) &&
1182 (image->colorspace == CMYKColorspace) &&
1183 (reconstruct_image->colorspace == CMYKColorspace))
1185 distance=QuantumScale*(Sa*(double) GetPixelIndex(indexes+x)-Da*
1186 (double) GetPixelIndex(reconstruct_indexes+x));
1187 channel_similarity[BlackChannel]+=distance*distance;
1188 channel_similarity[CompositeChannels]+=distance*distance;
1193#if defined(MAGICKCORE_OPENMP_SUPPORT)
1194 #pragma omp critical (MagickCore_GetMeanSquaredError)
1196 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1197 similarity[i]+=channel_similarity[i];
1199 reconstruct_view=DestroyCacheView(reconstruct_view);
1200 image_view=DestroyCacheView(image_view);
1201 area=MagickSafeReciprocal((
double) columns*rows);
1202 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1203 similarity[i]*=area;
1204 similarity[CompositeChannels]/=(double) GetNumberChannels(image,channel);
1208static MagickBooleanType GetNCCSimilarity(
const Image *image,
1209 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
1210 ExceptionInfo *exception)
1212#define SimilarityImageTag "Similarity/Image"
1220 *reconstruct_statistics;
1223 alpha_variance[CompositeChannels+1] = { 0.0 },
1224 beta_variance[CompositeChannels+1] = { 0.0 };
1243 image_statistics=GetImageChannelStatistics(image,exception);
1244 reconstruct_statistics=GetImageChannelStatistics(reconstruct_image,exception);
1245 if ((image_statistics == (ChannelStatistics *) NULL) ||
1246 (reconstruct_statistics == (ChannelStatistics *) NULL))
1248 if (image_statistics != (ChannelStatistics *) NULL)
1249 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1251 if (reconstruct_statistics != (ChannelStatistics *) NULL)
1252 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1253 reconstruct_statistics);
1254 return(MagickFalse);
1256 (void) memset(similarity,0,(CompositeChannels+1)*
sizeof(*similarity));
1259 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1260 image_view=AcquireVirtualCacheView(image,exception);
1261 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1262#if defined(MAGICKCORE_OPENMP_SUPPORT)
1263 #pragma omp parallel for schedule(static) shared(status) \
1264 magick_number_threads(image,image,rows,1)
1266 for (y=0; y < (ssize_t) rows; y++)
1269 *magick_restrict indexes,
1270 *magick_restrict reconstruct_indexes;
1277 channel_alpha_variance[CompositeChannels+1] = { 0.0 },
1278 channel_beta_variance[CompositeChannels+1] = { 0.0 },
1279 channel_similarity[CompositeChannels+1] = { 0.0 };
1284 if (status == MagickFalse)
1286 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1287 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1288 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
1293 indexes=GetCacheViewVirtualIndexQueue(image_view);
1294 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
1295 for (x=0; x < (ssize_t) columns; x++)
1303 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
1304 (double) QuantumRange);
1305 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
1306 (double) GetPixelAlpha(q) : (double) QuantumRange);
1307 if ((channel & RedChannel) != 0)
1309 alpha=QuantumScale*(Sa*(double) GetPixelRed(p)-
1310 image_statistics[RedChannel].mean);
1311 beta=QuantumScale*(Da*(double) GetPixelRed(q)-
1312 reconstruct_statistics[RedChannel].mean);
1313 channel_similarity[RedChannel]+=alpha*beta;
1314 channel_similarity[CompositeChannels]+=alpha*beta;
1315 channel_alpha_variance[RedChannel]+=alpha*alpha;
1316 channel_alpha_variance[CompositeChannels]+=alpha*alpha;
1317 channel_beta_variance[RedChannel]+=beta*beta;
1318 channel_beta_variance[CompositeChannels]+=beta*beta;
1320 if ((channel & GreenChannel) != 0)
1322 alpha=QuantumScale*(Sa*(double) GetPixelGreen(p)-
1323 image_statistics[GreenChannel].mean);
1324 beta=QuantumScale*(Da*(double) GetPixelGreen(q)-
1325 reconstruct_statistics[GreenChannel].mean);
1326 channel_similarity[GreenChannel]+=alpha*beta;
1327 channel_similarity[CompositeChannels]+=alpha*beta;
1328 channel_alpha_variance[GreenChannel]+=alpha*alpha;
1329 channel_alpha_variance[CompositeChannels]+=alpha*alpha;
1330 channel_beta_variance[GreenChannel]+=beta*beta;
1331 channel_beta_variance[CompositeChannels]+=beta*beta;
1333 if ((channel & BlueChannel) != 0)
1335 alpha=QuantumScale*(Sa*(double) GetPixelBlue(p)-
1336 image_statistics[BlueChannel].mean);
1337 beta=QuantumScale*(Da*(double) GetPixelBlue(q)-
1338 reconstruct_statistics[BlueChannel].mean);
1339 channel_similarity[BlueChannel]+=alpha*beta;
1340 channel_alpha_variance[BlueChannel]+=alpha*alpha;
1341 channel_beta_variance[BlueChannel]+=beta*beta;
1343 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
1345 alpha=QuantumScale*((double) GetPixelAlpha(p)-
1346 image_statistics[AlphaChannel].mean);
1347 beta=QuantumScale*((double) GetPixelAlpha(q)-
1348 reconstruct_statistics[AlphaChannel].mean);
1349 channel_similarity[OpacityChannel]+=alpha*beta;
1350 channel_similarity[CompositeChannels]+=alpha*beta;
1351 channel_alpha_variance[OpacityChannel]+=alpha*alpha;
1352 channel_alpha_variance[CompositeChannels]+=alpha*alpha;
1353 channel_beta_variance[OpacityChannel]+=beta*beta;
1354 channel_beta_variance[CompositeChannels]+=beta*beta;
1356 if (((channel & IndexChannel) != 0) &&
1357 (image->colorspace == CMYKColorspace) &&
1358 (reconstruct_image->colorspace == CMYKColorspace))
1360 alpha=QuantumScale*(Sa*(double) GetPixelIndex(indexes+x)-
1361 image_statistics[BlackChannel].mean);
1362 beta=QuantumScale*(Da*(double) GetPixelIndex(reconstruct_indexes+
1363 x)-reconstruct_statistics[BlackChannel].mean);
1364 channel_similarity[BlackChannel]+=alpha*beta;
1365 channel_similarity[CompositeChannels]+=alpha*beta;
1366 channel_alpha_variance[BlackChannel]+=alpha*alpha;
1367 channel_alpha_variance[CompositeChannels]+=alpha*alpha;
1368 channel_beta_variance[BlackChannel]+=beta*beta;
1369 channel_beta_variance[CompositeChannels]+=beta*beta;
1374#if defined(MAGICKCORE_OPENMP_SUPPORT)
1375 #pragma omp critical (GetNCCSimilarity)
1381 for (j=0; j <= (ssize_t) CompositeChannels; j++)
1383 similarity[j]+=channel_similarity[j];
1384 alpha_variance[j]+=channel_alpha_variance[j];
1385 beta_variance[j]+=channel_beta_variance[j];
1388 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1393#if defined(MAGICKCORE_OPENMP_SUPPORT)
1397 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
1398 if (proceed == MagickFalse)
1402 reconstruct_view=DestroyCacheView(reconstruct_view);
1403 image_view=DestroyCacheView(image_view);
1407 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1408 similarity[i]*=MagickSafeReciprocal(sqrt(alpha_variance[i])*
1409 sqrt(beta_variance[i]));
1413 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1414 reconstruct_statistics);
1415 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1420static MagickBooleanType GetPASimilarity(
const Image *image,
1421 const Image *reconstruct_image,
const ChannelType channel,
1422 double *similarity,ExceptionInfo *exception)
1439 (void) memset(similarity,0,(CompositeChannels+1)*
sizeof(*similarity));
1440 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1441 image_view=AcquireVirtualCacheView(image,exception);
1442 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1443#if defined(MAGICKCORE_OPENMP_SUPPORT)
1444 #pragma omp parallel for schedule(static) shared(status) \
1445 magick_number_threads(image,image,rows,1)
1447 for (y=0; y < (ssize_t) rows; y++)
1450 channel_similarity[CompositeChannels+1];
1453 *magick_restrict indexes,
1454 *magick_restrict reconstruct_indexes;
1464 if (status == MagickFalse)
1466 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1467 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1468 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
1473 indexes=GetCacheViewVirtualIndexQueue(image_view);
1474 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
1475 (void) memset(channel_similarity,0,(CompositeChannels+1)*
1476 sizeof(*channel_similarity));
1477 for (x=0; x < (ssize_t) columns; x++)
1484 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
1485 ((double) QuantumRange-(double) OpaqueOpacity));
1486 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
1487 (double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
1489 if ((channel & RedChannel) != 0)
1491 distance=QuantumScale*fabs(Sa*(
double) GetPixelRed(p)-Da*
1492 (
double) GetPixelRed(q));
1493 if (distance > channel_similarity[RedChannel])
1494 channel_similarity[RedChannel]=distance;
1495 if (distance > channel_similarity[CompositeChannels])
1496 channel_similarity[CompositeChannels]=distance;
1498 if ((channel & GreenChannel) != 0)
1500 distance=QuantumScale*fabs(Sa*(
double) GetPixelGreen(p)-Da*
1501 (
double) GetPixelGreen(q));
1502 if (distance > channel_similarity[GreenChannel])
1503 channel_similarity[GreenChannel]=distance;
1504 if (distance > channel_similarity[CompositeChannels])
1505 channel_similarity[CompositeChannels]=distance;
1507 if ((channel & BlueChannel) != 0)
1509 distance=QuantumScale*fabs(Sa*(
double) GetPixelBlue(p)-Da*
1510 (
double) GetPixelBlue(q));
1511 if (distance > channel_similarity[BlueChannel])
1512 channel_similarity[BlueChannel]=distance;
1513 if (distance > channel_similarity[CompositeChannels])
1514 channel_similarity[CompositeChannels]=distance;
1516 if (((channel & OpacityChannel) != 0) &&
1517 (image->matte != MagickFalse))
1519 distance=QuantumScale*fabs((double) GetPixelOpacity(p)-(double)
1520 GetPixelOpacity(q));
1521 if (distance > channel_similarity[OpacityChannel])
1522 channel_similarity[OpacityChannel]=distance;
1523 if (distance > channel_similarity[CompositeChannels])
1524 channel_similarity[CompositeChannels]=distance;
1526 if (((channel & IndexChannel) != 0) &&
1527 (image->colorspace == CMYKColorspace) &&
1528 (reconstruct_image->colorspace == CMYKColorspace))
1530 distance=QuantumScale*fabs(Sa*(double) GetPixelIndex(indexes+x)-Da*
1531 (double) GetPixelIndex(reconstruct_indexes+x));
1532 if (distance > channel_similarity[BlackChannel])
1533 channel_similarity[BlackChannel]=distance;
1534 if (distance > channel_similarity[CompositeChannels])
1535 channel_similarity[CompositeChannels]=distance;
1540#if defined(MAGICKCORE_OPENMP_SUPPORT)
1541 #pragma omp critical (MagickCore_GetPeakAbsoluteError)
1543 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1544 if (channel_similarity[i] > similarity[i])
1545 similarity[i]=channel_similarity[i];
1547 reconstruct_view=DestroyCacheView(reconstruct_view);
1548 image_view=DestroyCacheView(image_view);
1552static MagickBooleanType GetPSNRSimilarity(
const Image *image,
1553 const Image *reconstruct_image,
const ChannelType channel,
1554 double *similarity,ExceptionInfo *exception)
1559 status=GetMSESimilarity(image,reconstruct_image,channel,similarity,
1561 if ((channel & RedChannel) != 0)
1562 similarity[RedChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1563 similarity[RedChannel]))/MagickSafePSNRRecipicol(10.0);
1564 if ((channel & GreenChannel) != 0)
1565 similarity[GreenChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1566 similarity[GreenChannel]))/MagickSafePSNRRecipicol(10.0);
1567 if ((channel & BlueChannel) != 0)
1568 similarity[BlueChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1569 similarity[BlueChannel]))/MagickSafePSNRRecipicol(10.0);
1570 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
1571 similarity[OpacityChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1572 similarity[OpacityChannel]))/MagickSafePSNRRecipicol(10.0);
1573 if (((channel & IndexChannel) != 0) && (image->colorspace == CMYKColorspace))
1574 similarity[BlackChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1575 similarity[BlackChannel]))/MagickSafePSNRRecipicol(10.0);
1576 similarity[CompositeChannels]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1577 similarity[CompositeChannels]))/MagickSafePSNRRecipicol(10.0);
1581static MagickBooleanType GetPHASHSimilarity(
const Image *image,
1582 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
1583 ExceptionInfo *exception)
1585#define PHASHNormalizationFactor 389.373723242
1587 ChannelPerceptualHash
1601 image_phash=GetImageChannelPerceptualHash(image,exception);
1602 if (image_phash == (ChannelPerceptualHash *) NULL)
1603 return(MagickFalse);
1604 reconstruct_phash=GetImageChannelPerceptualHash(reconstruct_image,exception);
1605 if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
1607 image_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(image_phash);
1608 return(MagickFalse);
1610 for (i=0; i < MaximumNumberOfImageMoments; i++)
1615 if ((channel & RedChannel) != 0)
1617 error=reconstruct_phash[RedChannel].P[i]-image_phash[RedChannel].P[i];
1618 if (IsNaN(error) != 0)
1620 difference=error*error/PHASHNormalizationFactor;
1621 similarity[RedChannel]+=difference;
1622 similarity[CompositeChannels]+=difference;
1624 if ((channel & GreenChannel) != 0)
1626 error=reconstruct_phash[GreenChannel].P[i]-
1627 image_phash[GreenChannel].P[i];
1628 if (IsNaN(error) != 0)
1630 difference=error*error/PHASHNormalizationFactor;
1631 similarity[GreenChannel]+=difference;
1632 similarity[CompositeChannels]+=difference;
1634 if ((channel & BlueChannel) != 0)
1636 error=reconstruct_phash[BlueChannel].P[i]-image_phash[BlueChannel].P[i];
1637 if (IsNaN(error) != 0)
1639 difference=error*error/PHASHNormalizationFactor;
1640 similarity[BlueChannel]+=difference;
1641 similarity[CompositeChannels]+=difference;
1643 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse) &&
1644 (reconstruct_image->matte != MagickFalse))
1646 error=reconstruct_phash[OpacityChannel].P[i]-
1647 image_phash[OpacityChannel].P[i];
1648 if (IsNaN(error) != 0)
1650 difference=error*error/PHASHNormalizationFactor;
1651 similarity[OpacityChannel]+=difference;
1652 similarity[CompositeChannels]+=difference;
1654 if (((channel & IndexChannel) != 0) &&
1655 (image->colorspace == CMYKColorspace) &&
1656 (reconstruct_image->colorspace == CMYKColorspace))
1658 error=reconstruct_phash[IndexChannel].P[i]-
1659 image_phash[IndexChannel].P[i];
1660 if (IsNaN(error) != 0)
1662 difference=error*error/PHASHNormalizationFactor;
1663 similarity[IndexChannel]+=difference;
1664 similarity[CompositeChannels]+=difference;
1670 for (i=0; i < MaximumNumberOfImageMoments; i++)
1675 if ((channel & RedChannel) != 0)
1677 error=reconstruct_phash[RedChannel].Q[i]-image_phash[RedChannel].Q[i];
1678 if (IsNaN(error) != 0)
1680 difference=error*error/PHASHNormalizationFactor;
1681 similarity[RedChannel]+=difference;
1682 similarity[CompositeChannels]+=difference;
1684 if ((channel & GreenChannel) != 0)
1686 error=reconstruct_phash[GreenChannel].Q[i]-
1687 image_phash[GreenChannel].Q[i];
1688 if (IsNaN(error) != 0)
1690 difference=error*error/PHASHNormalizationFactor;
1691 similarity[GreenChannel]+=difference;
1692 similarity[CompositeChannels]+=difference;
1694 if ((channel & BlueChannel) != 0)
1696 error=reconstruct_phash[BlueChannel].Q[i]-image_phash[BlueChannel].Q[i];
1697 if (IsNaN(error) != 0)
1699 difference=error*error/PHASHNormalizationFactor;
1700 similarity[BlueChannel]+=difference;
1701 similarity[CompositeChannels]+=difference;
1703 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse) &&
1704 (reconstruct_image->matte != MagickFalse))
1706 error=reconstruct_phash[OpacityChannel].Q[i]-
1707 image_phash[OpacityChannel].Q[i];
1708 if (IsNaN(error) != 0)
1710 difference=error*error/PHASHNormalizationFactor;
1711 similarity[OpacityChannel]+=difference;
1712 similarity[CompositeChannels]+=difference;
1714 if (((channel & IndexChannel) != 0) &&
1715 (image->colorspace == CMYKColorspace) &&
1716 (reconstruct_image->colorspace == CMYKColorspace))
1718 error=reconstruct_phash[IndexChannel].Q[i]-
1719 image_phash[IndexChannel].Q[i];
1720 if (IsNaN(error) != 0)
1722 difference=error*error/PHASHNormalizationFactor;
1723 similarity[IndexChannel]+=difference;
1724 similarity[CompositeChannels]+=difference;
1727 similarity[CompositeChannels]/=(double) GetNumberChannels(image,channel);
1731 reconstruct_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1733 image_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(image_phash);
1737static MagickBooleanType GetRMSESimilarity(
const Image *image,
1738 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
1739 ExceptionInfo *exception)
1741#define RMSESquareRoot(x) sqrt((x) < 0.0 ? 0.0 : (x))
1746 status=GetMSESimilarity(image,reconstruct_image,channel,similarity,
1748 if ((channel & RedChannel) != 0)
1749 similarity[RedChannel]=RMSESquareRoot(similarity[RedChannel]);
1750 if ((channel & GreenChannel) != 0)
1751 similarity[GreenChannel]=RMSESquareRoot(similarity[GreenChannel]);
1752 if ((channel & BlueChannel) != 0)
1753 similarity[BlueChannel]=RMSESquareRoot(similarity[BlueChannel]);
1754 if (((channel & OpacityChannel) != 0) &&
1755 (image->matte != MagickFalse))
1756 similarity[OpacityChannel]=RMSESquareRoot(similarity[OpacityChannel]);
1757 if (((channel & IndexChannel) != 0) &&
1758 (image->colorspace == CMYKColorspace))
1759 similarity[BlackChannel]=RMSESquareRoot(similarity[BlackChannel]);
1760 similarity[CompositeChannels]=RMSESquareRoot(similarity[CompositeChannels]);
1764MagickExport MagickBooleanType GetImageChannelDistortion(Image *image,
1765 const Image *reconstruct_image,
const ChannelType channel,
1766 const MetricType metric,
double *distortion,ExceptionInfo *exception)
1769 *channel_similarity;
1777 assert(image != (Image *) NULL);
1778 assert(image->signature == MagickCoreSignature);
1779 assert(reconstruct_image != (
const Image *) NULL);
1780 assert(reconstruct_image->signature == MagickCoreSignature);
1781 assert(distortion != (
double *) NULL);
1782 if (IsEventLogging() != MagickFalse)
1783 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1785 if (metric != PerceptualHashErrorMetric)
1786 if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
1787 ThrowBinaryException(ImageError,
"ImageMorphologyDiffers",image->filename);
1791 length=CompositeChannels+1UL;
1792 channel_similarity=(
double *) AcquireQuantumMemory(length,
1793 sizeof(*channel_similarity));
1794 if (channel_similarity == (
double *) NULL)
1795 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1796 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
1799 case AbsoluteErrorMetric:
1801 status=GetAESimilarity(image,reconstruct_image,channel,
1802 channel_similarity,exception);
1805 case FuzzErrorMetric:
1807 status=GetFUZZSimilarity(image,reconstruct_image,channel,
1808 channel_similarity,exception);
1811 case MeanAbsoluteErrorMetric:
1813 status=GetMAESimilarity(image,reconstruct_image,channel,
1814 channel_similarity,exception);
1817 case MeanErrorPerPixelMetric:
1819 status=GetMEPPSimilarity(image,reconstruct_image,channel,
1820 channel_similarity,exception);
1823 case MeanSquaredErrorMetric:
1825 status=GetMSESimilarity(image,reconstruct_image,channel,
1826 channel_similarity,exception);
1829 case NormalizedCrossCorrelationErrorMetric:
1831 status=GetNCCSimilarity(image,reconstruct_image,channel,
1832 channel_similarity,exception);
1835 case PeakAbsoluteErrorMetric:
1837 status=GetPASimilarity(image,reconstruct_image,channel,
1838 channel_similarity,exception);
1841 case PeakSignalToNoiseRatioMetric:
1843 status=GetPSNRSimilarity(image,reconstruct_image,channel,
1844 channel_similarity,exception);
1847 case PerceptualHashErrorMetric:
1849 status=GetPHASHSimilarity(image,reconstruct_image,channel,
1850 channel_similarity,exception);
1853 case PixelDifferenceCountErrorMetric:
1855 status=GetPDCSimilarity(image,reconstruct_image,channel,
1856 channel_similarity,exception);
1859 case RootMeanSquaredErrorMetric:
1860 case UndefinedErrorMetric:
1863 status=GetRMSESimilarity(image,reconstruct_image,channel,
1864 channel_similarity,exception);
1868 *distortion=channel_similarity[CompositeChannels];
1871 case NormalizedCrossCorrelationErrorMetric:
1873 *distortion=(1.0-(*distortion))/2.0;
1878 if (fabs(*distortion) < MagickEpsilon)
1880 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
1881 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
1918MagickExport
double *GetImageChannelDistortions(Image *image,
1919 const Image *reconstruct_image,
const MetricType metric,
1920 ExceptionInfo *exception)
1935 assert(image != (Image *) NULL);
1936 assert(image->signature == MagickCoreSignature);
1937 assert(reconstruct_image != (
const Image *) NULL);
1938 assert(reconstruct_image->signature == MagickCoreSignature);
1939 if (IsEventLogging() != MagickFalse)
1940 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1941 if (metric != PerceptualHashErrorMetric)
1942 if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
1944 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1945 ImageError,
"ImageMorphologyDiffers",
"`%s'",image->filename);
1946 return((
double *) NULL);
1951 length=CompositeChannels+1UL;
1952 similarity=(
double *) AcquireQuantumMemory(length,
1953 sizeof(*similarity));
1954 if (similarity == (
double *) NULL)
1955 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1956 (void) memset(similarity,0,length*
sizeof(*similarity));
1960 case AbsoluteErrorMetric:
1962 status=GetAESimilarity(image,reconstruct_image,CompositeChannels,
1963 similarity,exception);
1966 case FuzzErrorMetric:
1968 status=GetFUZZSimilarity(image,reconstruct_image,CompositeChannels,
1969 similarity,exception);
1972 case MeanAbsoluteErrorMetric:
1974 status=GetMAESimilarity(image,reconstruct_image,CompositeChannels,
1975 similarity,exception);
1978 case MeanErrorPerPixelMetric:
1980 status=GetMEPPSimilarity(image,reconstruct_image,CompositeChannels,
1981 similarity,exception);
1984 case MeanSquaredErrorMetric:
1986 status=GetMSESimilarity(image,reconstruct_image,CompositeChannels,
1987 similarity,exception);
1990 case NormalizedCrossCorrelationErrorMetric:
1992 status=GetNCCSimilarity(image,reconstruct_image,CompositeChannels,
1993 similarity,exception);
1996 case PeakAbsoluteErrorMetric:
1998 status=GetPASimilarity(image,reconstruct_image,CompositeChannels,
1999 similarity,exception);
2002 case PeakSignalToNoiseRatioMetric:
2004 status=GetPSNRSimilarity(image,reconstruct_image,CompositeChannels,
2005 similarity,exception);
2008 case PerceptualHashErrorMetric:
2010 status=GetPHASHSimilarity(image,reconstruct_image,CompositeChannels,
2011 similarity,exception);
2014 case PixelDifferenceCountErrorMetric:
2016 status=GetPDCSimilarity(image,reconstruct_image,CompositeChannels,
2017 similarity,exception);
2020 case RootMeanSquaredErrorMetric:
2021 case UndefinedErrorMetric:
2024 status=GetRMSESimilarity(image,reconstruct_image,CompositeChannels,
2025 similarity,exception);
2029 if (status == MagickFalse)
2031 similarity=(
double *) RelinquishMagickMemory(similarity);
2032 return((
double *) NULL);
2034 distortion=similarity;
2037 case NormalizedCrossCorrelationErrorMetric:
2039 for (i=0; i <= (ssize_t) CompositeChannels; i++)
2040 distortion[i]=(1.0-distortion[i])/2.0;
2045 for (i=0; i <= (ssize_t) CompositeChannels; i++)
2046 if (fabs(distortion[i]) < MagickEpsilon)
2098MagickExport MagickBooleanType IsImagesEqual(Image *image,
2099 const Image *reconstruct_image)
2116 mean_error_per_pixel;
2125 assert(image != (Image *) NULL);
2126 assert(image->signature == MagickCoreSignature);
2127 assert(reconstruct_image != (
const Image *) NULL);
2128 assert(reconstruct_image->signature == MagickCoreSignature);
2129 exception=(&image->exception);
2130 if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
2131 ThrowBinaryException(ImageError,
"ImageMorphologyDiffers",image->filename);
2134 mean_error_per_pixel=0.0;
2136 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
2137 image_view=AcquireVirtualCacheView(image,exception);
2138 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2139 for (y=0; y < (ssize_t) rows; y++)
2142 *magick_restrict indexes,
2143 *magick_restrict reconstruct_indexes;
2152 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
2153 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
2154 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
2156 indexes=GetCacheViewVirtualIndexQueue(image_view);
2157 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
2158 for (x=0; x < (ssize_t) columns; x++)
2163 distance=fabs((
double) GetPixelRed(p)-(
double) GetPixelRed(q));
2164 mean_error_per_pixel+=distance;
2165 mean_error+=distance*distance;
2166 if (distance > maximum_error)
2167 maximum_error=distance;
2169 distance=fabs((
double) GetPixelGreen(p)-(
double) GetPixelGreen(q));
2170 mean_error_per_pixel+=distance;
2171 mean_error+=distance*distance;
2172 if (distance > maximum_error)
2173 maximum_error=distance;
2175 distance=fabs((
double) GetPixelBlue(p)-(
double) GetPixelBlue(q));
2176 mean_error_per_pixel+=distance;
2177 mean_error+=distance*distance;
2178 if (distance > maximum_error)
2179 maximum_error=distance;
2181 if (image->matte != MagickFalse)
2183 distance=fabs((
double) GetPixelOpacity(p)-(
double)
2184 GetPixelOpacity(q));
2185 mean_error_per_pixel+=distance;
2186 mean_error+=distance*distance;
2187 if (distance > maximum_error)
2188 maximum_error=distance;
2191 if ((image->colorspace == CMYKColorspace) &&
2192 (reconstruct_image->colorspace == CMYKColorspace))
2194 distance=fabs((
double) GetPixelIndex(indexes+x)-(
double)
2195 GetPixelIndex(reconstruct_indexes+x));
2196 mean_error_per_pixel+=distance;
2197 mean_error+=distance*distance;
2198 if (distance > maximum_error)
2199 maximum_error=distance;
2206 reconstruct_view=DestroyCacheView(reconstruct_view);
2207 image_view=DestroyCacheView(image_view);
2208 gamma=MagickSafeReciprocal(area);
2209 image->error.mean_error_per_pixel=gamma*mean_error_per_pixel;
2210 image->error.normalized_mean_error=gamma*QuantumScale*QuantumScale*mean_error;
2211 image->error.normalized_maximum_error=QuantumScale*maximum_error;
2212 status=image->error.mean_error_per_pixel == 0.0 ? MagickTrue : MagickFalse;
2251static double GetSimilarityMetric(
const Image *image,
2252 const Image *reconstruct_image,
const MetricType metric,
2253 const ssize_t x_offset,
const ssize_t y_offset,ExceptionInfo *exception)
2256 *channel_similarity,
2260 *sans_exception = AcquireExceptionInfo();
2266 status = MagickTrue;
2272 length = CompositeChannels+1UL;
2274 SetGeometry(reconstruct_image,&geometry);
2275 geometry.x=x_offset;
2276 geometry.y=y_offset;
2277 similarity_image=CropImage(image,&geometry,sans_exception);
2278 sans_exception=DestroyExceptionInfo(sans_exception);
2279 if (similarity_image == (Image *) NULL)
2284 channel_similarity=(
double *) AcquireQuantumMemory(length,
2285 sizeof(*channel_similarity));
2286 if (channel_similarity == (
double *) NULL)
2287 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2288 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
2291 case AbsoluteErrorMetric:
2293 status=GetAESimilarity(similarity_image,reconstruct_image,
2294 CompositeChannels,channel_similarity,exception);
2297 case FuzzErrorMetric:
2299 status=GetFUZZSimilarity(similarity_image,reconstruct_image,
2300 CompositeChannels,channel_similarity,exception);
2303 case MeanAbsoluteErrorMetric:
2305 status=GetMAESimilarity(similarity_image,reconstruct_image,
2306 CompositeChannels,channel_similarity,exception);
2309 case MeanErrorPerPixelMetric:
2311 status=GetMEPPSimilarity(similarity_image,reconstruct_image,
2312 CompositeChannels,channel_similarity,exception);
2315 case MeanSquaredErrorMetric:
2317 status=GetMSESimilarity(similarity_image,reconstruct_image,
2318 CompositeChannels,channel_similarity,exception);
2321 case NormalizedCrossCorrelationErrorMetric:
2323 status=GetNCCSimilarity(similarity_image,reconstruct_image,
2324 CompositeChannels,channel_similarity,exception);
2327 case PeakAbsoluteErrorMetric:
2329 status=GetPASimilarity(similarity_image,reconstruct_image,
2330 CompositeChannels,channel_similarity,exception);
2333 case PeakSignalToNoiseRatioMetric:
2335 status=GetPSNRSimilarity(similarity_image,reconstruct_image,
2336 CompositeChannels,channel_similarity,exception);
2339 case PerceptualHashErrorMetric:
2341 status=GetPHASHSimilarity(similarity_image,reconstruct_image,
2342 CompositeChannels,channel_similarity,exception);
2345 case PixelDifferenceCountErrorMetric:
2347 status=GetPDCSimilarity(similarity_image,reconstruct_image,
2348 CompositeChannels,channel_similarity,exception);
2351 case RootMeanSquaredErrorMetric:
2352 case UndefinedErrorMetric:
2355 status=GetRMSESimilarity(similarity_image,reconstruct_image,
2356 CompositeChannels,channel_similarity,exception);
2360 similarity_image=DestroyImage(similarity_image);
2361 similarity=channel_similarity[CompositeChannels];
2362 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
2363 if (status == MagickFalse)
2368MagickExport Image *SimilarityImage(Image *image,
const Image *reference,
2369 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
2374 similarity_image=SimilarityMetricImage(image,reference,
2375 RootMeanSquaredErrorMetric,offset,similarity_metric,exception);
2376 return(similarity_image);
2379MagickExport Image *SimilarityMetricImage(Image *image,
const Image *reconstruct,
2380 const MetricType metric,RectangleInfo *offset,
double *similarity_metric,
2381 ExceptionInfo *exception)
2383#define SimilarityImageTag "Similarity/Image"
2402 similarity_threshold;
2405 *similarity_image = (Image *) NULL;
2414 similarity_info = { 0 };
2419 assert(image != (
const Image *) NULL);
2420 assert(image->signature == MagickCoreSignature);
2421 assert(exception != (ExceptionInfo *) NULL);
2422 assert(exception->signature == MagickCoreSignature);
2423 assert(offset != (RectangleInfo *) NULL);
2424 if (IsEventLogging() != MagickFalse)
2425 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2426 SetGeometry(reconstruct,offset);
2427 *similarity_metric=0.0;
2430 if (ValidateImageMorphology(image,reconstruct) == MagickFalse)
2431 ThrowImageException(ImageError,
"ImageMorphologyDiffers");
2432 if ((image->columns < reconstruct->columns) ||
2433 (image->rows < reconstruct->rows))
2435 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2436 OptionWarning,
"GeometryDoesNotContainImage",
"`%s'",image->filename);
2437 return((Image *) NULL);
2439 similarity_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2441 if (similarity_image == (Image *) NULL)
2442 return((Image *) NULL);
2443 similarity_image->depth=32;
2444 similarity_image->colorspace=GRAYColorspace;
2445 similarity_image->matte=MagickFalse;
2446 status=SetImageStorageClass(similarity_image,DirectClass);
2447 if (status == MagickFalse)
2449 InheritException(exception,&similarity_image->exception);
2450 return(DestroyImage(similarity_image));
2455 similarity_threshold=DefaultSimilarityThreshold;
2456 artifact=GetImageArtifact(image,
"compare:similarity-threshold");
2457 if (artifact != (
const char *) NULL)
2458 similarity_threshold=StringToDouble(artifact,(
char **) NULL);
2460 similarity_info.similarity=GetSimilarityMetric(image,reconstruct,metric,
2461 similarity_info.x,similarity_info.y,exception);
2463 similarity_view=AcquireVirtualCacheView(similarity_image,exception);
2464#if defined(MAGICKCORE_OPENMP_SUPPORT)
2465 #pragma omp parallel for schedule(static) shared(status,similarity_info) \
2466 magick_number_threads(image,reconstruct,similarity_image->rows << 2,1)
2468 for (y=0; y < (ssize_t) similarity_image->rows; y++)
2474 threshold_trigger = MagickFalse;
2480 channel_info = similarity_info;
2485 if (status == MagickFalse)
2487 if (threshold_trigger != MagickFalse)
2489 q=QueueCacheViewAuthenticPixels(similarity_view,0,y,
2490 similarity_image->columns,1,exception);
2491 if (q == (PixelPacket *) NULL)
2496 for (x=0; x < (ssize_t) similarity_image->columns; x++)
2499 update = MagickFalse;
2501 similarity=GetSimilarityMetric(image,reconstruct,metric,x,y,exception);
2504 case NormalizedCrossCorrelationErrorMetric:
2505 case PeakSignalToNoiseRatioMetric:
2507 if (similarity > channel_info.similarity)
2513 if (similarity < channel_info.similarity)
2518 if (update != MagickFalse)
2520 channel_info.similarity=similarity;
2526 case NormalizedCrossCorrelationErrorMetric:
2527 case PeakSignalToNoiseRatioMetric:
2529 SetPixelRed(q,ClampToQuantum((
double) QuantumRange*similarity));
2534 SetPixelRed(q,ClampToQuantum((
double) QuantumRange*(1.0-similarity)));
2538 SetPixelGreen(q,GetPixelRed(q));
2539 SetPixelBlue(q,GetPixelRed(q));
2542#if defined(MAGICKCORE_OPENMP_SUPPORT)
2543 #pragma omp critical (MagickCore_SimilarityMetricImage)
2547 case NormalizedCrossCorrelationErrorMetric:
2548 case PeakSignalToNoiseRatioMetric:
2550 if (similarity_threshold != DefaultSimilarityThreshold)
2551 if (channel_info.similarity >= similarity_threshold)
2552 threshold_trigger=MagickTrue;
2553 if (channel_info.similarity >= similarity_info.similarity)
2554 similarity_info=channel_info;
2559 if (similarity_threshold != DefaultSimilarityThreshold)
2560 if (channel_info.similarity < similarity_threshold)
2561 threshold_trigger=MagickTrue;
2562 if (channel_info.similarity < similarity_info.similarity)
2563 similarity_info=channel_info;
2567 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
2569 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2575 proceed=SetImageProgress(image,SimilarityImageTag,progress,image->rows);
2576 if (proceed == MagickFalse)
2580 similarity_view=DestroyCacheView(similarity_view);
2581 if (status == MagickFalse)
2582 similarity_image=DestroyImage(similarity_image);
2583 *similarity_metric=similarity_info.similarity;
2584 if (fabs(*similarity_metric) < MagickEpsilon)
2585 *similarity_metric=0.0;
2586 offset->x=similarity_info.x;
2587 offset->y=similarity_info.y;
2588 (void) FormatImageProperty((Image *) image,
"similarity",
"%.*g",
2589 GetMagickPrecision(),*similarity_metric);
2590 (void) FormatImageProperty((Image *) image,
"similarity.offset.x",
"%.*g",
2591 GetMagickPrecision(),(
double) offset->x);
2592 (void) FormatImageProperty((Image *) image,
"similarity.offset.y",
"%.*g",
2593 GetMagickPrecision(),(
double) offset->y);
2594 return(similarity_image);