Skip to content

Commit d739d80

Browse files
author
Cristy
committed
correct fuzz metric
1 parent d1b5f9e commit d739d80

File tree

1 file changed

+73
-52
lines changed

1 file changed

+73
-52
lines changed

magick/compare.c

Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
443443
*reconstruct_view;
444444

445445
double
446-
area,
447-
fuzz;
446+
area;
448447

449448
MagickBooleanType
450449
status;
@@ -461,7 +460,6 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
461460
Compute the absolute difference in pixels between two images.
462461
*/
463462
status=MagickTrue;
464-
fuzz=GetFuzzyColorDistance(image,reconstruct_image);
465463
SetImageDistortionBounds(image,reconstruct_image,&columns,&rows);
466464
image_view=AcquireVirtualCacheView(image,exception);
467465
reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
@@ -516,7 +514,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
516514
{
517515
delta=Sa*(double) GetPixelRed(p)-Da*(double)
518516
GetPixelRed(q);
519-
if ((delta*delta) > fuzz)
517+
if ((delta*delta) >= MagickEpsilon)
520518
{
521519
channel_distortion[RedChannel]++;
522520
count++;
@@ -526,7 +524,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
526524
{
527525
delta=Sa*(double) GetPixelGreen(p)-Da*(double)
528526
GetPixelGreen(q);
529-
if ((delta*delta) > fuzz)
527+
if ((delta*delta) >= MagickEpsilon)
530528
{
531529
channel_distortion[GreenChannel]++;
532530
count++;
@@ -536,7 +534,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
536534
{
537535
delta=Sa*(double) GetPixelBlue(p)-Da*(double)
538536
GetPixelBlue(q);
539-
if ((delta*delta) > fuzz)
537+
if ((delta*delta) >= MagickEpsilon)
540538
{
541539
channel_distortion[BlueChannel]++;
542540
count++;
@@ -547,7 +545,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
547545
{
548546
delta=(double) GetPixelOpacity(p)-(double)
549547
GetPixelOpacity(q);
550-
if ((delta*delta) > fuzz)
548+
if ((delta*delta) >= MagickEpsilon)
551549
{
552550
channel_distortion[OpacityChannel]++;
553551
count++;
@@ -558,7 +556,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
558556
{
559557
delta=Sa*(double) indexes[x]-Da*(double)
560558
reconstruct_indexes[x];
561-
if ((delta*delta) > fuzz)
559+
if ((delta*delta) >= MagickEpsilon)
562560
{
563561
channel_distortion[IndexChannel]++;
564562
count++;
@@ -584,27 +582,33 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
584582
}
585583

586584
static MagickBooleanType GetFuzzDistortion(const Image *image,
587-
const Image *reconstruct_image,const ChannelType channel,
588-
double *distortion,ExceptionInfo *exception)
585+
const Image *reconstruct_image,const ChannelType channel,double *distortion,
586+
ExceptionInfo *exception)
589587
{
590588
CacheView
591589
*image_view,
592590
*reconstruct_view;
593591

592+
double
593+
area,
594+
fuzz;
595+
594596
MagickBooleanType
595597
status;
596598

597-
ssize_t
598-
i;
599-
600599
size_t
601600
columns,
602601
rows;
603602

604603
ssize_t
604+
j,
605605
y;
606606

607+
/*
608+
Compute the absolute difference in pixels between two images.
609+
*/
607610
status=MagickTrue;
611+
fuzz=GetFuzzyColorDistance(image,reconstruct_image);
608612
SetImageDistortionBounds(image,reconstruct_image,&columns,&rows);
609613
image_view=AcquireVirtualCacheView(image,exception);
610614
reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
@@ -614,9 +618,6 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
614618
#endif
615619
for (y=0; y < (ssize_t) rows; y++)
616620
{
617-
double
618-
channel_distortion[CompositeChannels+1];
619-
620621
const IndexPacket
621622
*magick_restrict indexes,
622623
*magick_restrict reconstruct_indexes;
@@ -625,6 +626,9 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
625626
*magick_restrict p,
626627
*magick_restrict q;
627628

629+
double
630+
channel_distortion[CompositeChannels+1] = { 0.0 };
631+
628632
ssize_t
629633
i,
630634
x;
@@ -643,74 +647,91 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
643647
(void) memset(channel_distortion,0,sizeof(channel_distortion));
644648
for (x=0; x < (ssize_t) columns; x++)
645649
{
646-
MagickRealType
647-
distance,
650+
double
648651
Da,
652+
delta,
649653
Sa;
650654

655+
size_t
656+
count = 0;
657+
651658
Sa=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(p) :
652659
((double) QuantumRange-(double) OpaqueOpacity));
653-
Da=QuantumScale*(reconstruct_image->matte != MagickFalse ?
654-
(double) GetPixelAlpha(q) : ((double) QuantumRange-(double)
655-
OpaqueOpacity));
660+
Da=QuantumScale*(image->matte != MagickFalse ? (double) GetPixelAlpha(q) :
661+
((double) QuantumRange-(double) OpaqueOpacity));
656662
if ((channel & RedChannel) != 0)
657663
{
658-
distance=QuantumScale*(Sa*(double) GetPixelRed(p)-Da*(double)
659-
GetPixelRed(q));
660-
channel_distortion[RedChannel]+=distance*distance;
661-
channel_distortion[CompositeChannels]+=distance*distance;
664+
delta=Sa*(double) GetPixelRed(p)-Da*(double)
665+
GetPixelRed(q);
666+
if ((delta*delta) > fuzz)
667+
{
668+
channel_distortion[RedChannel]++;
669+
count++;
670+
}
662671
}
663672
if ((channel & GreenChannel) != 0)
664673
{
665-
distance=QuantumScale*(Sa*(double) GetPixelGreen(p)-Da*(double)
666-
GetPixelGreen(q));
667-
channel_distortion[GreenChannel]+=distance*distance;
668-
channel_distortion[CompositeChannels]+=distance*distance;
674+
delta=Sa*(double) GetPixelGreen(p)-Da*(double)
675+
GetPixelGreen(q);
676+
if ((delta*delta) > fuzz)
677+
{
678+
channel_distortion[GreenChannel]++;
679+
count++;
680+
}
669681
}
670682
if ((channel & BlueChannel) != 0)
671683
{
672-
distance=QuantumScale*(Sa*(double) GetPixelBlue(p)-Da*(double)
673-
GetPixelBlue(q));
674-
channel_distortion[BlueChannel]+=distance*distance;
675-
channel_distortion[CompositeChannels]+=distance*distance;
684+
delta=Sa*(double) GetPixelBlue(p)-Da*(double)
685+
GetPixelBlue(q);
686+
if ((delta*delta) > fuzz)
687+
{
688+
channel_distortion[BlueChannel]++;
689+
count++;
690+
}
676691
}
677-
if (((channel & OpacityChannel) != 0) && ((image->matte != MagickFalse) ||
678-
(reconstruct_image->matte != MagickFalse)))
692+
if (((channel & OpacityChannel) != 0) &&
693+
(image->matte != MagickFalse))
679694
{
680-
distance=QuantumScale*((image->matte != MagickFalse ? (double)
681-
GetPixelOpacity(p) : (double) OpaqueOpacity)-
682-
(reconstruct_image->matte != MagickFalse ?
683-
(double) GetPixelOpacity(q): (double) OpaqueOpacity));
684-
channel_distortion[OpacityChannel]+=distance*distance;
685-
channel_distortion[CompositeChannels]+=distance*distance;
695+
delta=(double) GetPixelOpacity(p)-(double)
696+
GetPixelOpacity(q);
697+
if ((delta*delta) > fuzz)
698+
{
699+
channel_distortion[OpacityChannel]++;
700+
count++;
701+
}
686702
}
687703
if (((channel & IndexChannel) != 0) &&
688-
(image->colorspace == CMYKColorspace) &&
689-
(reconstruct_image->colorspace == CMYKColorspace))
704+
(image->colorspace == CMYKColorspace))
690705
{
691-
distance=QuantumScale*(Sa*(double) GetPixelIndex(indexes+x)-
692-
Da*(double) GetPixelIndex(reconstruct_indexes+x));
693-
channel_distortion[BlackChannel]+=distance*distance;
694-
channel_distortion[CompositeChannels]+=distance*distance;
706+
delta=Sa*(double) indexes[x]-Da*(double)
707+
reconstruct_indexes[x];
708+
if ((delta*delta) > fuzz)
709+
{
710+
channel_distortion[IndexChannel]++;
711+
count++;
712+
}
695713
}
714+
if (count != 0)
715+
channel_distortion[CompositeChannels]++;
696716
p++;
697717
q++;
698718
}
699719
#if defined(MAGICKCORE_OPENMP_SUPPORT)
700-
#pragma omp critical (MagickCore_GetFuzzDistortion)
720+
#pragma omp critical (MagickCore_GetAbsoluteDistortion)
701721
#endif
702722
for (i=0; i <= (ssize_t) CompositeChannels; i++)
703723
distortion[i]+=channel_distortion[i];
704724
}
705725
reconstruct_view=DestroyCacheView(reconstruct_view);
706726
image_view=DestroyCacheView(image_view);
707-
distortion[CompositeChannels]/=(double) GetNumberChannels(image,channel);
708-
for (i=0; i <= (ssize_t) CompositeChannels; i++)
709-
distortion[i]/=((double) columns*rows);
710-
distortion[CompositeChannels]=sqrt(distortion[CompositeChannels]);
727+
area=PerceptibleReciprocal((double) columns*rows);
728+
for (j=0; j <= CompositeChannels; j++)
729+
distortion[j]*=area;
711730
return(status);
712731
}
713732

733+
734+
714735
static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
715736
const Image *reconstruct_image,const ChannelType channel,
716737
double *distortion,ExceptionInfo *exception)

0 commit comments

Comments
 (0)