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 GetMAESimilarity(
const Image *image,
664 const Image *reconstruct_image,
const ChannelType channel,
665 double *similarity,ExceptionInfo *exception)
683 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
684 image_view=AcquireVirtualCacheView(image,exception);
685 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
686#if defined(MAGICKCORE_OPENMP_SUPPORT)
687 #pragma omp parallel for schedule(static) shared(status) \
688 magick_number_threads(image,image,rows,1)
690 for (y=0; y < (ssize_t) rows; y++)
693 channel_similarity[CompositeChannels+1];
696 *magick_restrict indexes,
697 *magick_restrict reconstruct_indexes;
707 if (status == MagickFalse)
709 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
710 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
711 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
716 indexes=GetCacheViewVirtualIndexQueue(image_view);
717 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
718 (void) memset(channel_similarity,0,
sizeof(channel_similarity));
719 for (x=0; x < (ssize_t) columns; x++)
726 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
727 ((double) QuantumRange-(double) OpaqueOpacity));
728 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
729 (double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
731 if ((channel & RedChannel) != 0)
733 distance=QuantumScale*fabs(Sa*(
double) GetPixelRed(p)-Da*
734 (
double) GetPixelRed(q));
735 channel_similarity[RedChannel]+=distance;
736 channel_similarity[CompositeChannels]+=distance;
738 if ((channel & GreenChannel) != 0)
740 distance=QuantumScale*fabs(Sa*(
double) GetPixelGreen(p)-Da*
741 (
double) GetPixelGreen(q));
742 channel_similarity[GreenChannel]+=distance;
743 channel_similarity[CompositeChannels]+=distance;
745 if ((channel & BlueChannel) != 0)
747 distance=QuantumScale*fabs(Sa*(
double) GetPixelBlue(p)-Da*
748 (
double) GetPixelBlue(q));
749 channel_similarity[BlueChannel]+=distance;
750 channel_similarity[CompositeChannels]+=distance;
752 if (((channel & OpacityChannel) != 0) &&
753 (image->matte != MagickFalse))
755 distance=QuantumScale*fabs((double) GetPixelOpacity(p)-(double)
757 channel_similarity[OpacityChannel]+=distance;
758 channel_similarity[CompositeChannels]+=distance;
760 if (((channel & IndexChannel) != 0) &&
761 (image->colorspace == CMYKColorspace))
763 distance=QuantumScale*fabs(Sa*(double) GetPixelIndex(indexes+x)-Da*
764 (double) GetPixelIndex(reconstruct_indexes+x));
765 channel_similarity[BlackChannel]+=distance;
766 channel_similarity[CompositeChannels]+=distance;
771#if defined(MAGICKCORE_OPENMP_SUPPORT)
772 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
774 for (i=0; i <= (ssize_t) CompositeChannels; i++)
775 similarity[i]+=channel_similarity[i];
777 reconstruct_view=DestroyCacheView(reconstruct_view);
778 image_view=DestroyCacheView(image_view);
779 for (i=0; i <= (ssize_t) CompositeChannels; i++)
780 similarity[i]/=((
double) columns*rows);
781 similarity[CompositeChannels]/=(double) GetNumberChannels(image,channel);
785static MagickBooleanType GetMEPPSimilarity(Image *image,
786 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
787 ExceptionInfo *exception)
794 maximum_error = -MagickMaximumValue,
809 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
810 image_view=AcquireVirtualCacheView(image,exception);
811 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
812#if defined(MAGICKCORE_OPENMP_SUPPORT)
813 #pragma omp parallel for schedule(static) shared(maximum_error,status) \
814 magick_number_threads(image,image,rows,1)
816 for (y=0; y < (ssize_t) rows; y++)
819 channel_similarity[CompositeChannels+1] = { 0.0 },
820 local_maximum = maximum_error,
821 local_mean_error = 0.0;
824 *magick_restrict indexes,
825 *magick_restrict reconstruct_indexes;
835 if (status == MagickFalse)
837 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
838 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
839 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
844 indexes=GetCacheViewVirtualIndexQueue(image_view);
845 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
846 (void) memset(channel_similarity,0,
sizeof(channel_similarity));
847 for (x=0; x < (ssize_t) columns; x++)
854 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
855 ((double) QuantumRange-(double) OpaqueOpacity));
856 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
857 (double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
859 if ((channel & RedChannel) != 0)
861 distance=QuantumScale*fabs(Sa*(
double) GetPixelRed(p)-Da*
862 (
double) GetPixelRed(q));
863 channel_similarity[RedChannel]+=distance;
864 channel_similarity[CompositeChannels]+=distance;
865 local_mean_error+=distance*distance;
866 if (distance > local_maximum)
867 local_maximum=distance;
869 if ((channel & GreenChannel) != 0)
871 distance=QuantumScale*fabs(Sa*(
double) GetPixelGreen(p)-Da*
872 (
double) GetPixelGreen(q));
873 channel_similarity[GreenChannel]+=distance;
874 channel_similarity[CompositeChannels]+=distance;
875 local_mean_error+=distance*distance;
876 if (distance > local_maximum)
877 local_maximum=distance;
879 if ((channel & BlueChannel) != 0)
881 distance=QuantumScale*fabs(Sa*(
double) GetPixelBlue(p)-Da*
882 (
double) GetPixelBlue(q));
883 channel_similarity[BlueChannel]+=distance;
884 channel_similarity[CompositeChannels]+=distance;
885 local_mean_error+=distance*distance;
886 if (distance > local_maximum)
887 local_maximum=distance;
889 if (((channel & OpacityChannel) != 0) &&
890 (image->matte != MagickFalse))
892 distance=QuantumScale*fabs((double) GetPixelOpacity(p)-(double)
894 channel_similarity[OpacityChannel]+=distance;
895 channel_similarity[CompositeChannels]+=distance;
896 local_mean_error+=distance*distance;
897 if (distance > local_maximum)
898 local_maximum=distance;
900 if (((channel & IndexChannel) != 0) &&
901 (image->colorspace == CMYKColorspace))
903 distance=QuantumScale*fabs(Sa*(double) GetPixelIndex(indexes+x)-Da*
904 (double) GetPixelIndex(reconstruct_indexes+x));
905 channel_similarity[BlackChannel]+=distance;
906 channel_similarity[CompositeChannels]+=distance;
907 local_mean_error+=distance*distance;
908 if (distance > local_maximum)
909 local_maximum=distance;
914#if defined(MAGICKCORE_OPENMP_SUPPORT)
915 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
918 for (i=0; i <= (ssize_t) CompositeChannels; i++)
919 similarity[i]+=channel_similarity[i];
920 mean_error+=local_mean_error;
921 if (local_maximum > maximum_error)
922 maximum_error=local_maximum;
925 reconstruct_view=DestroyCacheView(reconstruct_view);
926 image_view=DestroyCacheView(image_view);
927 for (i=0; i <= (ssize_t) CompositeChannels; i++)
928 similarity[i]/=((
double) columns*rows);
929 similarity[CompositeChannels]/=(double) GetNumberChannels(image,channel);
930 image->error.mean_error_per_pixel=QuantumRange*similarity[CompositeChannels];
931 image->error.normalized_mean_error=mean_error/((double) columns*rows);
932 image->error.normalized_maximum_error=maximum_error;
936static MagickBooleanType GetMSESimilarity(
const Image *image,
937 const Image *reconstruct_image,
const ChannelType channel,
938 double *similarity,ExceptionInfo *exception)
959 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
960 image_view=AcquireVirtualCacheView(image,exception);
961 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
962#if defined(MAGICKCORE_OPENMP_SUPPORT)
963 #pragma omp parallel for schedule(static) shared(similarity,status) \
964 magick_number_threads(image,image,rows,1)
966 for (y=0; y < (ssize_t) rows; y++)
969 channel_similarity[CompositeChannels+1] = { 0.0 };
972 *magick_restrict indexes,
973 *magick_restrict reconstruct_indexes;
983 if (status == MagickFalse)
985 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
986 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
987 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
992 indexes=GetCacheViewVirtualIndexQueue(image_view);
993 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
994 for (x=0; x < (ssize_t) columns; x++)
1001 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
1002 ((double) QuantumRange-(double) OpaqueOpacity));
1003 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
1004 (double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
1006 if ((channel & RedChannel) != 0)
1008 distance=QuantumScale*(Sa*(double) GetPixelRed(p)-Da*(double)
1010 channel_similarity[RedChannel]+=distance*distance;
1011 channel_similarity[CompositeChannels]+=distance*distance;
1013 if ((channel & GreenChannel) != 0)
1015 distance=QuantumScale*(Sa*(double) GetPixelGreen(p)-Da*(double)
1017 channel_similarity[GreenChannel]+=distance*distance;
1018 channel_similarity[CompositeChannels]+=distance*distance;
1020 if ((channel & BlueChannel) != 0)
1022 distance=QuantumScale*(Sa*(double) GetPixelBlue(p)-Da*(double)
1024 channel_similarity[BlueChannel]+=distance*distance;
1025 channel_similarity[CompositeChannels]+=distance*distance;
1027 if (((channel & OpacityChannel) != 0) &&
1028 (image->matte != MagickFalse))
1030 distance=QuantumScale*((double) GetPixelOpacity(p)-(double)
1031 GetPixelOpacity(q));
1032 channel_similarity[OpacityChannel]+=distance*distance;
1033 channel_similarity[CompositeChannels]+=distance*distance;
1035 if (((channel & IndexChannel) != 0) &&
1036 (image->colorspace == CMYKColorspace) &&
1037 (reconstruct_image->colorspace == CMYKColorspace))
1039 distance=QuantumScale*(Sa*(double) GetPixelIndex(indexes+x)-Da*
1040 (double) GetPixelIndex(reconstruct_indexes+x));
1041 channel_similarity[BlackChannel]+=distance*distance;
1042 channel_similarity[CompositeChannels]+=distance*distance;
1047#if defined(MAGICKCORE_OPENMP_SUPPORT)
1048 #pragma omp critical (MagickCore_GetMeanSquaredError)
1050 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1051 similarity[i]+=channel_similarity[i];
1053 reconstruct_view=DestroyCacheView(reconstruct_view);
1054 image_view=DestroyCacheView(image_view);
1055 area=MagickSafeReciprocal((
double) columns*rows);
1056 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1057 similarity[i]*=area;
1058 similarity[CompositeChannels]/=(double) GetNumberChannels(image,channel);
1062static MagickBooleanType GetNCCSimilarity(
const Image *image,
1063 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
1064 ExceptionInfo *exception)
1066#define SimilarityImageTag "Similarity/Image"
1074 *reconstruct_statistics;
1077 alpha_variance[CompositeChannels+1] = { 0.0 },
1078 beta_variance[CompositeChannels+1] = { 0.0 };
1097 image_statistics=GetImageChannelStatistics(image,exception);
1098 reconstruct_statistics=GetImageChannelStatistics(reconstruct_image,exception);
1099 if ((image_statistics == (ChannelStatistics *) NULL) ||
1100 (reconstruct_statistics == (ChannelStatistics *) NULL))
1102 if (image_statistics != (ChannelStatistics *) NULL)
1103 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1105 if (reconstruct_statistics != (ChannelStatistics *) NULL)
1106 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1107 reconstruct_statistics);
1108 return(MagickFalse);
1110 (void) memset(similarity,0,(CompositeChannels+1)*
sizeof(*similarity));
1113 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1114 image_view=AcquireVirtualCacheView(image,exception);
1115 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1116#if defined(MAGICKCORE_OPENMP_SUPPORT)
1117 #pragma omp parallel for schedule(static) shared(status) \
1118 magick_number_threads(image,image,rows,1)
1120 for (y=0; y < (ssize_t) rows; y++)
1123 *magick_restrict indexes,
1124 *magick_restrict reconstruct_indexes;
1131 channel_alpha_variance[CompositeChannels+1] = { 0.0 },
1132 channel_beta_variance[CompositeChannels+1] = { 0.0 },
1133 channel_similarity[CompositeChannels+1] = { 0.0 };
1138 if (status == MagickFalse)
1140 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1141 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1142 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
1147 indexes=GetCacheViewVirtualIndexQueue(image_view);
1148 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
1149 for (x=0; x < (ssize_t) columns; x++)
1157 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
1158 (double) QuantumRange);
1159 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
1160 (double) GetPixelAlpha(q) : (double) QuantumRange);
1161 if ((channel & RedChannel) != 0)
1163 alpha=QuantumScale*(Sa*(double) GetPixelRed(p)-
1164 image_statistics[RedChannel].mean);
1165 beta=QuantumScale*(Da*(double) GetPixelRed(q)-
1166 reconstruct_statistics[RedChannel].mean);
1167 channel_similarity[RedChannel]+=alpha*beta;
1168 channel_similarity[CompositeChannels]+=alpha*beta;
1169 channel_alpha_variance[RedChannel]+=alpha*alpha;
1170 channel_alpha_variance[CompositeChannels]+=alpha*alpha;
1171 channel_beta_variance[RedChannel]+=beta*beta;
1172 channel_beta_variance[CompositeChannels]+=beta*beta;
1174 if ((channel & GreenChannel) != 0)
1176 alpha=QuantumScale*(Sa*(double) GetPixelGreen(p)-
1177 image_statistics[GreenChannel].mean);
1178 beta=QuantumScale*(Da*(double) GetPixelGreen(q)-
1179 reconstruct_statistics[GreenChannel].mean);
1180 channel_similarity[GreenChannel]+=alpha*beta;
1181 channel_similarity[CompositeChannels]+=alpha*beta;
1182 channel_alpha_variance[GreenChannel]+=alpha*alpha;
1183 channel_alpha_variance[CompositeChannels]+=alpha*alpha;
1184 channel_beta_variance[GreenChannel]+=beta*beta;
1185 channel_beta_variance[CompositeChannels]+=beta*beta;
1187 if ((channel & BlueChannel) != 0)
1189 alpha=QuantumScale*(Sa*(double) GetPixelBlue(p)-
1190 image_statistics[BlueChannel].mean);
1191 beta=QuantumScale*(Da*(double) GetPixelBlue(q)-
1192 reconstruct_statistics[BlueChannel].mean);
1193 channel_similarity[BlueChannel]+=alpha*beta;
1194 channel_alpha_variance[BlueChannel]+=alpha*alpha;
1195 channel_beta_variance[BlueChannel]+=beta*beta;
1197 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
1199 alpha=QuantumScale*((double) GetPixelAlpha(p)-
1200 image_statistics[AlphaChannel].mean);
1201 beta=QuantumScale*((double) GetPixelAlpha(q)-
1202 reconstruct_statistics[AlphaChannel].mean);
1203 channel_similarity[OpacityChannel]+=alpha*beta;
1204 channel_similarity[CompositeChannels]+=alpha*beta;
1205 channel_alpha_variance[OpacityChannel]+=alpha*alpha;
1206 channel_alpha_variance[CompositeChannels]+=alpha*alpha;
1207 channel_beta_variance[OpacityChannel]+=beta*beta;
1208 channel_beta_variance[CompositeChannels]+=beta*beta;
1210 if (((channel & IndexChannel) != 0) &&
1211 (image->colorspace == CMYKColorspace) &&
1212 (reconstruct_image->colorspace == CMYKColorspace))
1214 alpha=QuantumScale*(Sa*(double) GetPixelIndex(indexes+x)-
1215 image_statistics[BlackChannel].mean);
1216 beta=QuantumScale*(Da*(double) GetPixelIndex(reconstruct_indexes+
1217 x)-reconstruct_statistics[BlackChannel].mean);
1218 channel_similarity[BlackChannel]+=alpha*beta;
1219 channel_similarity[CompositeChannels]+=alpha*beta;
1220 channel_alpha_variance[BlackChannel]+=alpha*alpha;
1221 channel_alpha_variance[CompositeChannels]+=alpha*alpha;
1222 channel_beta_variance[BlackChannel]+=beta*beta;
1223 channel_beta_variance[CompositeChannels]+=beta*beta;
1228#if defined(MAGICKCORE_OPENMP_SUPPORT)
1229 #pragma omp critical (GetNCCSimilarity)
1235 for (j=0; j <= (ssize_t) CompositeChannels; j++)
1237 similarity[j]+=channel_similarity[j];
1238 alpha_variance[j]+=channel_alpha_variance[j];
1239 beta_variance[j]+=channel_beta_variance[j];
1242 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1247#if defined(MAGICKCORE_OPENMP_SUPPORT)
1251 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
1252 if (proceed == MagickFalse)
1256 reconstruct_view=DestroyCacheView(reconstruct_view);
1257 image_view=DestroyCacheView(image_view);
1261 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1262 similarity[i]*=MagickSafeReciprocal(sqrt(alpha_variance[i])*
1263 sqrt(beta_variance[i]));
1267 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1268 reconstruct_statistics);
1269 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1274static MagickBooleanType GetPASimilarity(
const Image *image,
1275 const Image *reconstruct_image,
const ChannelType channel,
1276 double *similarity,ExceptionInfo *exception)
1293 (void) memset(similarity,0,(CompositeChannels+1)*
sizeof(*similarity));
1294 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1295 image_view=AcquireVirtualCacheView(image,exception);
1296 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1297#if defined(MAGICKCORE_OPENMP_SUPPORT)
1298 #pragma omp parallel for schedule(static) shared(status) \
1299 magick_number_threads(image,image,rows,1)
1301 for (y=0; y < (ssize_t) rows; y++)
1304 channel_similarity[CompositeChannels+1];
1307 *magick_restrict indexes,
1308 *magick_restrict reconstruct_indexes;
1318 if (status == MagickFalse)
1320 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1321 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1322 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
1327 indexes=GetCacheViewVirtualIndexQueue(image_view);
1328 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
1329 (void) memset(channel_similarity,0,(CompositeChannels+1)*
1330 sizeof(*channel_similarity));
1331 for (x=0; x < (ssize_t) columns; x++)
1338 Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
1339 ((double) QuantumRange-(double) OpaqueOpacity));
1340 Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
1341 (double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
1343 if ((channel & RedChannel) != 0)
1345 distance=QuantumScale*fabs(Sa*(
double) GetPixelRed(p)-Da*
1346 (
double) GetPixelRed(q));
1347 if (distance > channel_similarity[RedChannel])
1348 channel_similarity[RedChannel]=distance;
1349 if (distance > channel_similarity[CompositeChannels])
1350 channel_similarity[CompositeChannels]=distance;
1352 if ((channel & GreenChannel) != 0)
1354 distance=QuantumScale*fabs(Sa*(
double) GetPixelGreen(p)-Da*
1355 (
double) GetPixelGreen(q));
1356 if (distance > channel_similarity[GreenChannel])
1357 channel_similarity[GreenChannel]=distance;
1358 if (distance > channel_similarity[CompositeChannels])
1359 channel_similarity[CompositeChannels]=distance;
1361 if ((channel & BlueChannel) != 0)
1363 distance=QuantumScale*fabs(Sa*(
double) GetPixelBlue(p)-Da*
1364 (
double) GetPixelBlue(q));
1365 if (distance > channel_similarity[BlueChannel])
1366 channel_similarity[BlueChannel]=distance;
1367 if (distance > channel_similarity[CompositeChannels])
1368 channel_similarity[CompositeChannels]=distance;
1370 if (((channel & OpacityChannel) != 0) &&
1371 (image->matte != MagickFalse))
1373 distance=QuantumScale*fabs((double) GetPixelOpacity(p)-(double)
1374 GetPixelOpacity(q));
1375 if (distance > channel_similarity[OpacityChannel])
1376 channel_similarity[OpacityChannel]=distance;
1377 if (distance > channel_similarity[CompositeChannels])
1378 channel_similarity[CompositeChannels]=distance;
1380 if (((channel & IndexChannel) != 0) &&
1381 (image->colorspace == CMYKColorspace) &&
1382 (reconstruct_image->colorspace == CMYKColorspace))
1384 distance=QuantumScale*fabs(Sa*(double) GetPixelIndex(indexes+x)-Da*
1385 (double) GetPixelIndex(reconstruct_indexes+x));
1386 if (distance > channel_similarity[BlackChannel])
1387 channel_similarity[BlackChannel]=distance;
1388 if (distance > channel_similarity[CompositeChannels])
1389 channel_similarity[CompositeChannels]=distance;
1394#if defined(MAGICKCORE_OPENMP_SUPPORT)
1395 #pragma omp critical (MagickCore_GetPeakAbsoluteError)
1397 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1398 if (channel_similarity[i] > similarity[i])
1399 similarity[i]=channel_similarity[i];
1401 reconstruct_view=DestroyCacheView(reconstruct_view);
1402 image_view=DestroyCacheView(image_view);
1406static MagickBooleanType GetPSNRSimilarity(
const Image *image,
1407 const Image *reconstruct_image,
const ChannelType channel,
1408 double *similarity,ExceptionInfo *exception)
1413 status=GetMSESimilarity(image,reconstruct_image,channel,similarity,
1415 if ((channel & RedChannel) != 0)
1416 similarity[RedChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1417 similarity[RedChannel]))/MagickSafePSNRRecipicol(10.0);
1418 if ((channel & GreenChannel) != 0)
1419 similarity[GreenChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1420 similarity[GreenChannel]))/MagickSafePSNRRecipicol(10.0);
1421 if ((channel & BlueChannel) != 0)
1422 similarity[BlueChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1423 similarity[BlueChannel]))/MagickSafePSNRRecipicol(10.0);
1424 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
1425 similarity[OpacityChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1426 similarity[OpacityChannel]))/MagickSafePSNRRecipicol(10.0);
1427 if (((channel & IndexChannel) != 0) && (image->colorspace == CMYKColorspace))
1428 similarity[BlackChannel]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1429 similarity[BlackChannel]))/MagickSafePSNRRecipicol(10.0);
1430 similarity[CompositeChannels]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1431 similarity[CompositeChannels]))/MagickSafePSNRRecipicol(10.0);
1435static MagickBooleanType GetPHASHSimilarity(
const Image *image,
1436 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
1437 ExceptionInfo *exception)
1439#define PHASHNormalizationFactor 389.373723242
1441 ChannelPerceptualHash
1455 image_phash=GetImageChannelPerceptualHash(image,exception);
1456 if (image_phash == (ChannelPerceptualHash *) NULL)
1457 return(MagickFalse);
1458 reconstruct_phash=GetImageChannelPerceptualHash(reconstruct_image,exception);
1459 if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
1461 image_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(image_phash);
1462 return(MagickFalse);
1464 for (i=0; i < MaximumNumberOfImageMoments; i++)
1469 if ((channel & RedChannel) != 0)
1471 error=reconstruct_phash[RedChannel].P[i]-image_phash[RedChannel].P[i];
1472 if (IsNaN(error) != 0)
1474 difference=error*error/PHASHNormalizationFactor;
1475 similarity[RedChannel]+=difference;
1476 similarity[CompositeChannels]+=difference;
1478 if ((channel & GreenChannel) != 0)
1480 error=reconstruct_phash[GreenChannel].P[i]-
1481 image_phash[GreenChannel].P[i];
1482 if (IsNaN(error) != 0)
1484 difference=error*error/PHASHNormalizationFactor;
1485 similarity[GreenChannel]+=difference;
1486 similarity[CompositeChannels]+=difference;
1488 if ((channel & BlueChannel) != 0)
1490 error=reconstruct_phash[BlueChannel].P[i]-image_phash[BlueChannel].P[i];
1491 if (IsNaN(error) != 0)
1493 difference=error*error/PHASHNormalizationFactor;
1494 similarity[BlueChannel]+=difference;
1495 similarity[CompositeChannels]+=difference;
1497 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse) &&
1498 (reconstruct_image->matte != MagickFalse))
1500 error=reconstruct_phash[OpacityChannel].P[i]-
1501 image_phash[OpacityChannel].P[i];
1502 if (IsNaN(error) != 0)
1504 difference=error*error/PHASHNormalizationFactor;
1505 similarity[OpacityChannel]+=difference;
1506 similarity[CompositeChannels]+=difference;
1508 if (((channel & IndexChannel) != 0) &&
1509 (image->colorspace == CMYKColorspace) &&
1510 (reconstruct_image->colorspace == CMYKColorspace))
1512 error=reconstruct_phash[IndexChannel].P[i]-
1513 image_phash[IndexChannel].P[i];
1514 if (IsNaN(error) != 0)
1516 difference=error*error/PHASHNormalizationFactor;
1517 similarity[IndexChannel]+=difference;
1518 similarity[CompositeChannels]+=difference;
1524 for (i=0; i < MaximumNumberOfImageMoments; i++)
1529 if ((channel & RedChannel) != 0)
1531 error=reconstruct_phash[RedChannel].Q[i]-image_phash[RedChannel].Q[i];
1532 if (IsNaN(error) != 0)
1534 difference=error*error/PHASHNormalizationFactor;
1535 similarity[RedChannel]+=difference;
1536 similarity[CompositeChannels]+=difference;
1538 if ((channel & GreenChannel) != 0)
1540 error=reconstruct_phash[GreenChannel].Q[i]-
1541 image_phash[GreenChannel].Q[i];
1542 if (IsNaN(error) != 0)
1544 difference=error*error/PHASHNormalizationFactor;
1545 similarity[GreenChannel]+=difference;
1546 similarity[CompositeChannels]+=difference;
1548 if ((channel & BlueChannel) != 0)
1550 error=reconstruct_phash[BlueChannel].Q[i]-image_phash[BlueChannel].Q[i];
1551 if (IsNaN(error) != 0)
1553 difference=error*error/PHASHNormalizationFactor;
1554 similarity[BlueChannel]+=difference;
1555 similarity[CompositeChannels]+=difference;
1557 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse) &&
1558 (reconstruct_image->matte != MagickFalse))
1560 error=reconstruct_phash[OpacityChannel].Q[i]-
1561 image_phash[OpacityChannel].Q[i];
1562 if (IsNaN(error) != 0)
1564 difference=error*error/PHASHNormalizationFactor;
1565 similarity[OpacityChannel]+=difference;
1566 similarity[CompositeChannels]+=difference;
1568 if (((channel & IndexChannel) != 0) &&
1569 (image->colorspace == CMYKColorspace) &&
1570 (reconstruct_image->colorspace == CMYKColorspace))
1572 error=reconstruct_phash[IndexChannel].Q[i]-
1573 image_phash[IndexChannel].Q[i];
1574 if (IsNaN(error) != 0)
1576 difference=error*error/PHASHNormalizationFactor;
1577 similarity[IndexChannel]+=difference;
1578 similarity[CompositeChannels]+=difference;
1581 similarity[CompositeChannels]/=(double) GetNumberChannels(image,channel);
1585 reconstruct_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1587 image_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(image_phash);
1591static MagickBooleanType GetRMSESimilarity(
const Image *image,
1592 const Image *reconstruct_image,
const ChannelType channel,
double *similarity,
1593 ExceptionInfo *exception)
1595#define RMSESquareRoot(x) sqrt((x) < 0.0 ? 0.0 : (x))
1600 status=GetMSESimilarity(image,reconstruct_image,channel,similarity,
1602 if ((channel & RedChannel) != 0)
1603 similarity[RedChannel]=RMSESquareRoot(similarity[RedChannel]);
1604 if ((channel & GreenChannel) != 0)
1605 similarity[GreenChannel]=RMSESquareRoot(similarity[GreenChannel]);
1606 if ((channel & BlueChannel) != 0)
1607 similarity[BlueChannel]=RMSESquareRoot(similarity[BlueChannel]);
1608 if (((channel & OpacityChannel) != 0) &&
1609 (image->matte != MagickFalse))
1610 similarity[OpacityChannel]=RMSESquareRoot(similarity[OpacityChannel]);
1611 if (((channel & IndexChannel) != 0) &&
1612 (image->colorspace == CMYKColorspace))
1613 similarity[BlackChannel]=RMSESquareRoot(similarity[BlackChannel]);
1614 similarity[CompositeChannels]=RMSESquareRoot(similarity[CompositeChannels]);
1618MagickExport MagickBooleanType GetImageChannelDistortion(Image *image,
1619 const Image *reconstruct_image,
const ChannelType channel,
1620 const MetricType metric,
double *distortion,ExceptionInfo *exception)
1623 *channel_similarity;
1631 assert(image != (Image *) NULL);
1632 assert(image->signature == MagickCoreSignature);
1633 assert(reconstruct_image != (
const Image *) NULL);
1634 assert(reconstruct_image->signature == MagickCoreSignature);
1635 assert(distortion != (
double *) NULL);
1636 if (IsEventLogging() != MagickFalse)
1637 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1639 if (metric != PerceptualHashErrorMetric)
1640 if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
1641 ThrowBinaryException(ImageError,
"ImageMorphologyDiffers",image->filename);
1645 length=CompositeChannels+1UL;
1646 channel_similarity=(
double *) AcquireQuantumMemory(length,
1647 sizeof(*channel_similarity));
1648 if (channel_similarity == (
double *) NULL)
1649 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1650 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
1653 case AbsoluteErrorMetric:
1655 status=GetAESimilarity(image,reconstruct_image,channel,
1656 channel_similarity,exception);
1659 case FuzzErrorMetric:
1661 status=GetFUZZSimilarity(image,reconstruct_image,channel,
1662 channel_similarity,exception);
1665 case MeanAbsoluteErrorMetric:
1667 status=GetMAESimilarity(image,reconstruct_image,channel,
1668 channel_similarity,exception);
1671 case MeanErrorPerPixelMetric:
1673 status=GetMEPPSimilarity(image,reconstruct_image,channel,
1674 channel_similarity,exception);
1677 case MeanSquaredErrorMetric:
1679 status=GetMSESimilarity(image,reconstruct_image,channel,
1680 channel_similarity,exception);
1683 case NormalizedCrossCorrelationErrorMetric:
1685 status=GetNCCSimilarity(image,reconstruct_image,channel,
1686 channel_similarity,exception);
1689 case PeakAbsoluteErrorMetric:
1691 status=GetPASimilarity(image,reconstruct_image,channel,
1692 channel_similarity,exception);
1695 case PeakSignalToNoiseRatioMetric:
1697 status=GetPSNRSimilarity(image,reconstruct_image,channel,
1698 channel_similarity,exception);
1701 case PerceptualHashErrorMetric:
1703 status=GetPHASHSimilarity(image,reconstruct_image,channel,
1704 channel_similarity,exception);
1707 case RootMeanSquaredErrorMetric:
1708 case UndefinedErrorMetric:
1711 status=GetRMSESimilarity(image,reconstruct_image,channel,
1712 channel_similarity,exception);
1716 *distortion=channel_similarity[CompositeChannels];
1719 case NormalizedCrossCorrelationErrorMetric:
1721 *distortion=(1.0-(*distortion))/2.0;
1726 if (fabs(*distortion) < MagickEpsilon)
1728 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
1729 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
1766MagickExport
double *GetImageChannelDistortions(Image *image,
1767 const Image *reconstruct_image,
const MetricType metric,
1768 ExceptionInfo *exception)
1783 assert(image != (Image *) NULL);
1784 assert(image->signature == MagickCoreSignature);
1785 assert(reconstruct_image != (
const Image *) NULL);
1786 assert(reconstruct_image->signature == MagickCoreSignature);
1787 if (IsEventLogging() != MagickFalse)
1788 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1789 if (metric != PerceptualHashErrorMetric)
1790 if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
1792 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1793 ImageError,
"ImageMorphologyDiffers",
"`%s'",image->filename);
1794 return((
double *) NULL);
1799 length=CompositeChannels+1UL;
1800 similarity=(
double *) AcquireQuantumMemory(length,
1801 sizeof(*similarity));
1802 if (similarity == (
double *) NULL)
1803 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1804 (void) memset(similarity,0,length*
sizeof(*similarity));
1808 case AbsoluteErrorMetric:
1810 status=GetAESimilarity(image,reconstruct_image,CompositeChannels,
1811 similarity,exception);
1814 case FuzzErrorMetric:
1816 status=GetFUZZSimilarity(image,reconstruct_image,CompositeChannels,
1817 similarity,exception);
1820 case MeanAbsoluteErrorMetric:
1822 status=GetMAESimilarity(image,reconstruct_image,CompositeChannels,
1823 similarity,exception);
1826 case MeanErrorPerPixelMetric:
1828 status=GetMEPPSimilarity(image,reconstruct_image,CompositeChannels,
1829 similarity,exception);
1832 case MeanSquaredErrorMetric:
1834 status=GetMSESimilarity(image,reconstruct_image,CompositeChannels,
1835 similarity,exception);
1838 case NormalizedCrossCorrelationErrorMetric:
1840 status=GetNCCSimilarity(image,reconstruct_image,CompositeChannels,
1841 similarity,exception);
1844 case PeakAbsoluteErrorMetric:
1846 status=GetPASimilarity(image,reconstruct_image,CompositeChannels,
1847 similarity,exception);
1850 case PeakSignalToNoiseRatioMetric:
1852 status=GetPSNRSimilarity(image,reconstruct_image,CompositeChannels,
1853 similarity,exception);
1856 case PerceptualHashErrorMetric:
1858 status=GetPHASHSimilarity(image,reconstruct_image,CompositeChannels,
1859 similarity,exception);
1862 case RootMeanSquaredErrorMetric:
1863 case UndefinedErrorMetric:
1866 status=GetRMSESimilarity(image,reconstruct_image,CompositeChannels,
1867 similarity,exception);
1871 if (status == MagickFalse)
1873 similarity=(
double *) RelinquishMagickMemory(similarity);
1874 return((
double *) NULL);
1876 distortion=similarity;
1879 case NormalizedCrossCorrelationErrorMetric:
1881 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1882 distortion[i]=(1.0-distortion[i])/2.0;
1887 for (i=0; i <= (ssize_t) CompositeChannels; i++)
1888 if (fabs(distortion[i]) < MagickEpsilon)
1940MagickExport MagickBooleanType IsImagesEqual(Image *image,
1941 const Image *reconstruct_image)
1958 mean_error_per_pixel;
1967 assert(image != (Image *) NULL);
1968 assert(image->signature == MagickCoreSignature);
1969 assert(reconstruct_image != (
const Image *) NULL);
1970 assert(reconstruct_image->signature == MagickCoreSignature);
1971 exception=(&image->exception);
1972 if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
1973 ThrowBinaryException(ImageError,
"ImageMorphologyDiffers",image->filename);
1976 mean_error_per_pixel=0.0;
1978 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1979 image_view=AcquireVirtualCacheView(image,exception);
1980 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1981 for (y=0; y < (ssize_t) rows; y++)
1984 *magick_restrict indexes,
1985 *magick_restrict reconstruct_indexes;
1994 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1995 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1996 if ((p == (
const PixelPacket *) NULL) || (q == (
const PixelPacket *) NULL))
1998 indexes=GetCacheViewVirtualIndexQueue(image_view);
1999 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
2000 for (x=0; x < (ssize_t) columns; x++)
2005 distance=fabs((
double) GetPixelRed(p)-(
double) GetPixelRed(q));
2006 mean_error_per_pixel+=distance;
2007 mean_error+=distance*distance;
2008 if (distance > maximum_error)
2009 maximum_error=distance;
2011 distance=fabs((
double) GetPixelGreen(p)-(
double) GetPixelGreen(q));
2012 mean_error_per_pixel+=distance;
2013 mean_error+=distance*distance;
2014 if (distance > maximum_error)
2015 maximum_error=distance;
2017 distance=fabs((
double) GetPixelBlue(p)-(
double) GetPixelBlue(q));
2018 mean_error_per_pixel+=distance;
2019 mean_error+=distance*distance;
2020 if (distance > maximum_error)
2021 maximum_error=distance;
2023 if (image->matte != MagickFalse)
2025 distance=fabs((
double) GetPixelOpacity(p)-(
double)
2026 GetPixelOpacity(q));
2027 mean_error_per_pixel+=distance;
2028 mean_error+=distance*distance;
2029 if (distance > maximum_error)
2030 maximum_error=distance;
2033 if ((image->colorspace == CMYKColorspace) &&
2034 (reconstruct_image->colorspace == CMYKColorspace))
2036 distance=fabs((
double) GetPixelIndex(indexes+x)-(
double)
2037 GetPixelIndex(reconstruct_indexes+x));
2038 mean_error_per_pixel+=distance;
2039 mean_error+=distance*distance;
2040 if (distance > maximum_error)
2041 maximum_error=distance;
2048 reconstruct_view=DestroyCacheView(reconstruct_view);
2049 image_view=DestroyCacheView(image_view);
2050 gamma=MagickSafeReciprocal(area);
2051 image->error.mean_error_per_pixel=gamma*mean_error_per_pixel;
2052 image->error.normalized_mean_error=gamma*QuantumScale*QuantumScale*mean_error;
2053 image->error.normalized_maximum_error=QuantumScale*maximum_error;
2054 status=image->error.mean_error_per_pixel == 0.0 ? MagickTrue : MagickFalse;
2093static double GetSimilarityMetric(
const Image *image,
2094 const Image *reconstruct_image,
const MetricType metric,
2095 const ssize_t x_offset,
const ssize_t y_offset,ExceptionInfo *exception)
2098 *channel_similarity,
2102 *sans_exception = AcquireExceptionInfo();
2108 status = MagickTrue;
2114 length = CompositeChannels+1UL;
2116 SetGeometry(reconstruct_image,&geometry);
2117 geometry.x=x_offset;
2118 geometry.y=y_offset;
2119 similarity_image=CropImage(image,&geometry,sans_exception);
2120 sans_exception=DestroyExceptionInfo(sans_exception);
2121 if (similarity_image == (Image *) NULL)
2126 channel_similarity=(
double *) AcquireQuantumMemory(length,
2127 sizeof(*channel_similarity));
2128 if (channel_similarity == (
double *) NULL)
2129 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2130 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
2133 case AbsoluteErrorMetric:
2135 status=GetAESimilarity(similarity_image,reconstruct_image,
2136 CompositeChannels,channel_similarity,exception);
2139 case FuzzErrorMetric:
2141 status=GetFUZZSimilarity(similarity_image,reconstruct_image,
2142 CompositeChannels,channel_similarity,exception);
2145 case MeanAbsoluteErrorMetric:
2147 status=GetMAESimilarity(similarity_image,reconstruct_image,
2148 CompositeChannels,channel_similarity,exception);
2151 case MeanErrorPerPixelMetric:
2153 status=GetMEPPSimilarity(similarity_image,reconstruct_image,
2154 CompositeChannels,channel_similarity,exception);
2157 case MeanSquaredErrorMetric:
2159 status=GetMSESimilarity(similarity_image,reconstruct_image,
2160 CompositeChannels,channel_similarity,exception);
2163 case NormalizedCrossCorrelationErrorMetric:
2165 status=GetNCCSimilarity(similarity_image,reconstruct_image,
2166 CompositeChannels,channel_similarity,exception);
2169 case PeakAbsoluteErrorMetric:
2171 status=GetPASimilarity(similarity_image,reconstruct_image,
2172 CompositeChannels,channel_similarity,exception);
2175 case PeakSignalToNoiseRatioMetric:
2177 status=GetPSNRSimilarity(similarity_image,reconstruct_image,
2178 CompositeChannels,channel_similarity,exception);
2181 case PerceptualHashErrorMetric:
2183 status=GetPHASHSimilarity(similarity_image,reconstruct_image,
2184 CompositeChannels,channel_similarity,exception);
2187 case RootMeanSquaredErrorMetric:
2188 case UndefinedErrorMetric:
2191 status=GetRMSESimilarity(similarity_image,reconstruct_image,
2192 CompositeChannels,channel_similarity,exception);
2196 similarity_image=DestroyImage(similarity_image);
2197 similarity=channel_similarity[CompositeChannels];
2198 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
2199 if (status == MagickFalse)
2204MagickExport Image *SimilarityImage(Image *image,
const Image *reference,
2205 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
2210 similarity_image=SimilarityMetricImage(image,reference,
2211 RootMeanSquaredErrorMetric,offset,similarity_metric,exception);
2212 return(similarity_image);
2215MagickExport Image *SimilarityMetricImage(Image *image,
const Image *reconstruct,
2216 const MetricType metric,RectangleInfo *offset,
double *similarity_metric,
2217 ExceptionInfo *exception)
2219#define SimilarityImageTag "Similarity/Image"
2238 similarity_threshold;
2241 *similarity_image = (Image *) NULL;
2250 similarity_info = { 0 };
2255 assert(image != (
const Image *) NULL);
2256 assert(image->signature == MagickCoreSignature);
2257 assert(exception != (ExceptionInfo *) NULL);
2258 assert(exception->signature == MagickCoreSignature);
2259 assert(offset != (RectangleInfo *) NULL);
2260 if (IsEventLogging() != MagickFalse)
2261 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2262 SetGeometry(reconstruct,offset);
2263 *similarity_metric=0.0;
2266 if (ValidateImageMorphology(image,reconstruct) == MagickFalse)
2267 ThrowImageException(ImageError,
"ImageMorphologyDiffers");
2268 if ((image->columns < reconstruct->columns) ||
2269 (image->rows < reconstruct->rows))
2271 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2272 OptionWarning,
"GeometryDoesNotContainImage",
"`%s'",image->filename);
2273 return((Image *) NULL);
2275 similarity_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2277 if (similarity_image == (Image *) NULL)
2278 return((Image *) NULL);
2279 similarity_image->depth=32;
2280 similarity_image->colorspace=GRAYColorspace;
2281 similarity_image->matte=MagickFalse;
2282 status=SetImageStorageClass(similarity_image,DirectClass);
2283 if (status == MagickFalse)
2285 InheritException(exception,&similarity_image->exception);
2286 return(DestroyImage(similarity_image));
2291 similarity_threshold=DefaultSimilarityThreshold;
2292 artifact=GetImageArtifact(image,
"compare:similarity-threshold");
2293 if (artifact != (
const char *) NULL)
2294 similarity_threshold=StringToDouble(artifact,(
char **) NULL);
2296 similarity_info.similarity=GetSimilarityMetric(image,reconstruct,metric,
2297 similarity_info.x,similarity_info.y,exception);
2299 similarity_view=AcquireVirtualCacheView(similarity_image,exception);
2300#if defined(MAGICKCORE_OPENMP_SUPPORT)
2301 #pragma omp parallel for schedule(static) shared(status,similarity_info) \
2302 magick_number_threads(image,reconstruct,similarity_image->rows << 2,1)
2304 for (y=0; y < (ssize_t) similarity_image->rows; y++)
2310 threshold_trigger = MagickFalse;
2316 channel_info = similarity_info;
2321 if (status == MagickFalse)
2323 if (threshold_trigger != MagickFalse)
2325 q=QueueCacheViewAuthenticPixels(similarity_view,0,y,
2326 similarity_image->columns,1,exception);
2327 if (q == (PixelPacket *) NULL)
2332 for (x=0; x < (ssize_t) similarity_image->columns; x++)
2335 update = MagickFalse;
2337 similarity=GetSimilarityMetric(image,reconstruct,metric,x,y,exception);
2340 case NormalizedCrossCorrelationErrorMetric:
2341 case PeakSignalToNoiseRatioMetric:
2343 if (similarity > channel_info.similarity)
2349 if (similarity < channel_info.similarity)
2354 if (update != MagickFalse)
2356 channel_info.similarity=similarity;
2362 case NormalizedCrossCorrelationErrorMetric:
2363 case PeakSignalToNoiseRatioMetric:
2365 SetPixelRed(q,ClampToQuantum((
double) QuantumRange*similarity));
2370 SetPixelRed(q,ClampToQuantum((
double) QuantumRange*(1.0-similarity)));
2374 SetPixelGreen(q,GetPixelRed(q));
2375 SetPixelBlue(q,GetPixelRed(q));
2378#if defined(MAGICKCORE_OPENMP_SUPPORT)
2379 #pragma omp critical (MagickCore_SimilarityMetricImage)
2383 case NormalizedCrossCorrelationErrorMetric:
2384 case PeakSignalToNoiseRatioMetric:
2386 if (similarity_threshold != DefaultSimilarityThreshold)
2387 if (channel_info.similarity >= similarity_threshold)
2388 threshold_trigger=MagickTrue;
2389 if (channel_info.similarity >= similarity_info.similarity)
2390 similarity_info=channel_info;
2395 if (similarity_threshold != DefaultSimilarityThreshold)
2396 if (channel_info.similarity < similarity_threshold)
2397 threshold_trigger=MagickTrue;
2398 if (channel_info.similarity < similarity_info.similarity)
2399 similarity_info=channel_info;
2403 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
2405 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2411 proceed=SetImageProgress(image,SimilarityImageTag,progress,image->rows);
2412 if (proceed == MagickFalse)
2416 similarity_view=DestroyCacheView(similarity_view);
2417 if (status == MagickFalse)
2418 similarity_image=DestroyImage(similarity_image);
2419 *similarity_metric=similarity_info.similarity;
2420 if (fabs(*similarity_metric) < MagickEpsilon)
2421 *similarity_metric=0.0;
2422 offset->x=similarity_info.x;
2423 offset->y=similarity_info.y;
2424 (void) FormatImageProperty((Image *) image,
"similarity",
"%.*g",
2425 GetMagickPrecision(),*similarity_metric);
2426 (void) FormatImageProperty((Image *) image,
"similarity.offset.x",
"%.*g",
2427 GetMagickPrecision(),(
double) offset->x);
2428 (void) FormatImageProperty((Image *) image,
"similarity.offset.y",
"%.*g",
2429 GetMagickPrecision(),(
double) offset->y);
2430 return(similarity_image);