2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO M M PPPP AAA RRRR EEEEE %
7 % C O O MM MM P P A A R R E %
8 % C O O M M M PPPP AAAAA RRRR EEE %
9 % C O O M M P A A R R E %
10 % CCCC OOO M M P A A R R EEEEE %
13 % MagickCore Image Comparison Methods %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/channel.h"
48 #include "MagickCore/client.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/compare.h"
54 #include "MagickCore/composite-private.h"
55 #include "MagickCore/constitute.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/log.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/monitor.h"
63 #include "MagickCore/monitor-private.h"
64 #include "MagickCore/option.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/property.h"
67 #include "MagickCore/resource_.h"
68 #include "MagickCore/string_.h"
69 #include "MagickCore/statistic.h"
70 #include "MagickCore/string-private.h"
71 #include "MagickCore/thread-private.h"
72 #include "MagickCore/transform.h"
73 #include "MagickCore/utility.h"
74 #include "MagickCore/version.h"
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % C o m p a r e I m a g e %
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % CompareImages() compares one or more pixel channels of an image to a
88 % reconstructed image and returns the difference image.
90 % The format of the CompareImages method is:
92 % Image *CompareImages(const Image *image,const Image *reconstruct_image,
93 % const MetricType metric,double *distortion,ExceptionInfo *exception)
95 % A description of each parameter follows:
99 % o reconstruct_image: the reconstruct image.
101 % o metric: the metric.
103 % o distortion: the computed distortion between the images.
105 % o exception: return any errors or warnings in this structure.
109 static size_t GetImageChannels(const Image *image)
118 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
120 PixelChannel channel = GetPixelChannelChannel(image,i);
121 PixelTrait traits = GetPixelChannelTraits(image,channel);
122 if ((traits & UpdatePixelTrait) != 0)
125 return(channels == 0 ? (size_t) 1 : channels);
128 MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
129 const MetricType metric,double *distortion,ExceptionInfo *exception)
165 assert(image != (Image *) NULL);
166 assert(image->signature == MagickCoreSignature);
167 if (image->debug != MagickFalse)
168 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
169 assert(reconstruct_image != (const Image *) NULL);
170 assert(reconstruct_image->signature == MagickCoreSignature);
171 assert(distortion != (double *) NULL);
173 if (image->debug != MagickFalse)
174 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
175 status=GetImageDistortion(image,reconstruct_image,metric,distortion,
177 if (status == MagickFalse)
178 return((Image *) NULL);
179 columns=MagickMax(image->columns,reconstruct_image->columns);
180 rows=MagickMax(image->rows,reconstruct_image->rows);
181 SetGeometry(image,&geometry);
182 geometry.width=columns;
183 geometry.height=rows;
184 clone_image=CloneImage(image,0,0,MagickTrue,exception);
185 if (clone_image == (Image *) NULL)
186 return((Image *) NULL);
187 (void) SetImageMask(clone_image,ReadPixelMask,(Image *) NULL,exception);
188 difference_image=ExtentImage(clone_image,&geometry,exception);
189 clone_image=DestroyImage(clone_image);
190 if (difference_image == (Image *) NULL)
191 return((Image *) NULL);
192 (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel,exception);
193 highlight_image=CloneImage(image,columns,rows,MagickTrue,exception);
194 if (highlight_image == (Image *) NULL)
196 difference_image=DestroyImage(difference_image);
197 return((Image *) NULL);
199 status=SetImageStorageClass(highlight_image,DirectClass,exception);
200 if (status == MagickFalse)
202 difference_image=DestroyImage(difference_image);
203 highlight_image=DestroyImage(highlight_image);
204 return((Image *) NULL);
206 (void) SetImageMask(highlight_image,ReadPixelMask,(Image *) NULL,exception);
207 (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel,exception);
208 (void) QueryColorCompliance("#f1001ecc",AllCompliance,&highlight,exception);
209 artifact=GetImageArtifact(image,"compare:highlight-color");
210 if (artifact != (const char *) NULL)
211 (void) QueryColorCompliance(artifact,AllCompliance,&highlight,exception);
212 (void) QueryColorCompliance("#ffffffcc",AllCompliance,&lowlight,exception);
213 artifact=GetImageArtifact(image,"compare:lowlight-color");
214 if (artifact != (const char *) NULL)
215 (void) QueryColorCompliance(artifact,AllCompliance,&lowlight,exception);
216 (void) QueryColorCompliance("#888888cc",AllCompliance,&masklight,exception);
217 artifact=GetImageArtifact(image,"compare:masklight-color");
218 if (artifact != (const char *) NULL)
219 (void) QueryColorCompliance(artifact,AllCompliance,&masklight,exception);
221 Generate difference image.
224 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
225 image_view=AcquireVirtualCacheView(image,exception);
226 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
227 highlight_view=AcquireAuthenticCacheView(highlight_image,exception);
228 #if defined(MAGICKCORE_OPENMP_SUPPORT)
229 #pragma omp parallel for schedule(static) shared(status) \
230 magick_number_threads(image,highlight_image,rows,1)
232 for (y=0; y < (ssize_t) rows; y++)
237 register const Quantum
247 if (status == MagickFalse)
249 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
250 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
251 r=QueueCacheViewAuthenticPixels(highlight_view,0,y,columns,1,exception);
252 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL) ||
253 (r == (Quantum *) NULL))
258 for (x=0; x < (ssize_t) columns; x++)
270 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
271 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
273 SetPixelViaPixelInfo(highlight_image,&masklight,r);
274 p+=GetPixelChannels(image);
275 q+=GetPixelChannels(reconstruct_image);
276 r+=GetPixelChannels(highlight_image);
279 difference=MagickFalse;
280 Sa=QuantumScale*GetPixelAlpha(image,p);
281 Da=QuantumScale*GetPixelAlpha(reconstruct_image,q);
282 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
287 PixelChannel channel = GetPixelChannelChannel(image,i);
288 PixelTrait traits = GetPixelChannelTraits(image,channel);
289 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
291 if ((traits == UndefinedPixelTrait) ||
292 (reconstruct_traits == UndefinedPixelTrait) ||
293 ((reconstruct_traits & UpdatePixelTrait) == 0))
295 if (channel == AlphaPixelChannel)
296 distance=(double) p[i]-GetPixelChannel(reconstruct_image,channel,q);
298 distance=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
299 if ((distance*distance) > fuzz)
301 difference=MagickTrue;
305 if (difference == MagickFalse)
306 SetPixelViaPixelInfo(highlight_image,&lowlight,r);
308 SetPixelViaPixelInfo(highlight_image,&highlight,r);
309 p+=GetPixelChannels(image);
310 q+=GetPixelChannels(reconstruct_image);
311 r+=GetPixelChannels(highlight_image);
313 sync=SyncCacheViewAuthenticPixels(highlight_view,exception);
314 if (sync == MagickFalse)
317 highlight_view=DestroyCacheView(highlight_view);
318 reconstruct_view=DestroyCacheView(reconstruct_view);
319 image_view=DestroyCacheView(image_view);
320 (void) CompositeImage(difference_image,highlight_image,image->compose,
321 MagickTrue,0,0,exception);
322 highlight_image=DestroyImage(highlight_image);
323 if (status == MagickFalse)
324 difference_image=DestroyImage(difference_image);
325 return(difference_image);
329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 % G e t I m a g e D i s t o r t i o n %
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 % GetImageDistortion() compares one or more pixel channels of an image to a
340 % reconstructed image and returns the specified distortion metric.
342 % The format of the GetImageDistortion method is:
344 % MagickBooleanType GetImageDistortion(const Image *image,
345 % const Image *reconstruct_image,const MetricType metric,
346 % double *distortion,ExceptionInfo *exception)
348 % A description of each parameter follows:
350 % o image: the image.
352 % o reconstruct_image: the reconstruct image.
354 % o metric: the metric.
356 % o distortion: the computed distortion between the images.
358 % o exception: return any errors or warnings in this structure.
362 static MagickBooleanType GetAbsoluteDistortion(const Image *image,
363 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
383 Compute the absolute difference in pixels between two images.
386 fuzz=(double) MagickMin(GetPixelChannels(image),
387 GetPixelChannels(reconstruct_image))*
388 GetFuzzyColorDistance(image,reconstruct_image);
389 rows=MagickMax(image->rows,reconstruct_image->rows);
390 columns=MagickMax(image->columns,reconstruct_image->columns);
391 image_view=AcquireVirtualCacheView(image,exception);
392 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
393 #if defined(MAGICKCORE_OPENMP_SUPPORT)
394 #pragma omp parallel for schedule(static) shared(status) \
395 magick_number_threads(image,image,rows,1)
397 for (y=0; y < (ssize_t) rows; y++)
400 channel_distortion[MaxPixelChannels+1];
402 register const Quantum
410 if (status == MagickFalse)
412 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
413 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
414 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
419 (void) memset(channel_distortion,0,sizeof(channel_distortion));
420 for (x=0; x < (ssize_t) columns; x++)
433 difference=MagickFalse;
435 Sa=QuantumScale*GetPixelAlpha(image,p);
436 Da=QuantumScale*GetPixelAlpha(reconstruct_image,q);
437 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
442 PixelChannel channel = GetPixelChannelChannel(image,i);
443 PixelTrait traits = GetPixelChannelTraits(image,channel);
444 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
446 if ((traits == UndefinedPixelTrait) ||
447 (reconstruct_traits == UndefinedPixelTrait) ||
448 ((reconstruct_traits & UpdatePixelTrait) == 0))
450 if (channel == AlphaPixelChannel)
451 pixel=(double) p[i]-GetPixelChannel(reconstruct_image,channel,q);
453 pixel=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
454 distance+=pixel*pixel;
457 channel_distortion[i]++;
458 difference=MagickTrue;
461 if (difference != MagickFalse)
462 channel_distortion[CompositePixelChannel]++;
463 p+=GetPixelChannels(image);
464 q+=GetPixelChannels(reconstruct_image);
466 #if defined(MAGICKCORE_OPENMP_SUPPORT)
467 #pragma omp critical (MagickCore_GetAbsoluteDistortion)
469 for (j=0; j <= MaxPixelChannels; j++)
470 distortion[j]+=channel_distortion[j];
472 reconstruct_view=DestroyCacheView(reconstruct_view);
473 image_view=DestroyCacheView(image_view);
477 static MagickBooleanType GetFuzzDistortion(const Image *image,
478 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
501 rows=MagickMax(image->rows,reconstruct_image->rows);
502 columns=MagickMax(image->columns,reconstruct_image->columns);
504 image_view=AcquireVirtualCacheView(image,exception);
505 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
506 #if defined(MAGICKCORE_OPENMP_SUPPORT)
507 #pragma omp parallel for schedule(static) shared(status) \
508 magick_number_threads(image,image,rows,1) reduction(+:area)
510 for (y=0; y < (ssize_t) rows; y++)
513 channel_distortion[MaxPixelChannels+1];
515 register const Quantum
522 if (status == MagickFalse)
524 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
525 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
526 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
531 (void) memset(channel_distortion,0,sizeof(channel_distortion));
532 for (x=0; x < (ssize_t) columns; x++)
541 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
542 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
544 p+=GetPixelChannels(image);
545 q+=GetPixelChannels(reconstruct_image);
548 Sa=QuantumScale*GetPixelAlpha(image,p);
549 Da=QuantumScale*GetPixelAlpha(reconstruct_image,q);
550 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
555 PixelChannel channel = GetPixelChannelChannel(image,i);
556 PixelTrait traits = GetPixelChannelTraits(image,channel);
557 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
559 if ((traits == UndefinedPixelTrait) ||
560 (reconstruct_traits == UndefinedPixelTrait) ||
561 ((reconstruct_traits & UpdatePixelTrait) == 0))
563 if (channel == AlphaPixelChannel)
564 distance=QuantumScale*(p[i]-GetPixelChannel(reconstruct_image,
567 distance=QuantumScale*(Sa*p[i]-Da*GetPixelChannel(reconstruct_image,
569 channel_distortion[i]+=distance*distance;
570 channel_distortion[CompositePixelChannel]+=distance*distance;
573 p+=GetPixelChannels(image);
574 q+=GetPixelChannels(reconstruct_image);
576 #if defined(MAGICKCORE_OPENMP_SUPPORT)
577 #pragma omp critical (MagickCore_GetFuzzDistortion)
579 for (j=0; j <= MaxPixelChannels; j++)
580 distortion[j]+=channel_distortion[j];
582 reconstruct_view=DestroyCacheView(reconstruct_view);
583 image_view=DestroyCacheView(image_view);
584 area=PerceptibleReciprocal(area);
585 for (j=0; j <= MaxPixelChannels; j++)
587 distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
588 distortion[CompositePixelChannel]=sqrt(distortion[CompositePixelChannel]);
592 static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
593 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
616 rows=MagickMax(image->rows,reconstruct_image->rows);
617 columns=MagickMax(image->columns,reconstruct_image->columns);
619 image_view=AcquireVirtualCacheView(image,exception);
620 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
621 #if defined(MAGICKCORE_OPENMP_SUPPORT)
622 #pragma omp parallel for schedule(static) shared(status) \
623 magick_number_threads(image,image,rows,1) reduction(+:area)
625 for (y=0; y < (ssize_t) rows; y++)
628 channel_distortion[MaxPixelChannels+1];
630 register const Quantum
637 if (status == MagickFalse)
639 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
640 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
641 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
646 (void) memset(channel_distortion,0,sizeof(channel_distortion));
647 for (x=0; x < (ssize_t) columns; x++)
656 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
657 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
659 p+=GetPixelChannels(image);
660 q+=GetPixelChannels(reconstruct_image);
663 Sa=QuantumScale*GetPixelAlpha(image,p);
664 Da=QuantumScale*GetPixelAlpha(reconstruct_image,q);
665 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
670 PixelChannel channel = GetPixelChannelChannel(image,i);
671 PixelTrait traits = GetPixelChannelTraits(image,channel);
672 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
674 if ((traits == UndefinedPixelTrait) ||
675 (reconstruct_traits == UndefinedPixelTrait) ||
676 ((reconstruct_traits & UpdatePixelTrait) == 0))
678 if (channel == AlphaPixelChannel)
679 distance=QuantumScale*fabs((double) p[i]-
680 GetPixelChannel(reconstruct_image,channel,q));
682 distance=QuantumScale*fabs(Sa*p[i]-Da*
683 GetPixelChannel(reconstruct_image,channel,q));
684 channel_distortion[i]+=distance;
685 channel_distortion[CompositePixelChannel]+=distance;
688 p+=GetPixelChannels(image);
689 q+=GetPixelChannels(reconstruct_image);
691 #if defined(MAGICKCORE_OPENMP_SUPPORT)
692 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
694 for (j=0; j <= MaxPixelChannels; j++)
695 distortion[j]+=channel_distortion[j];
697 reconstruct_view=DestroyCacheView(reconstruct_view);
698 image_view=DestroyCacheView(image_view);
699 area=PerceptibleReciprocal(area);
700 for (j=0; j <= MaxPixelChannels; j++)
702 distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
706 static MagickBooleanType GetMeanErrorPerPixel(Image *image,
707 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
732 rows=MagickMax(image->rows,reconstruct_image->rows);
733 columns=MagickMax(image->columns,reconstruct_image->columns);
734 image_view=AcquireVirtualCacheView(image,exception);
735 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
736 for (y=0; y < (ssize_t) rows; y++)
738 register const Quantum
745 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
746 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
747 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
752 for (x=0; x < (ssize_t) columns; x++)
761 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
762 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
764 p+=GetPixelChannels(image);
765 q+=GetPixelChannels(reconstruct_image);
768 Sa=QuantumScale*GetPixelAlpha(image,p);
769 Da=QuantumScale*GetPixelAlpha(reconstruct_image,q);
770 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
775 PixelChannel channel = GetPixelChannelChannel(image,i);
776 PixelTrait traits = GetPixelChannelTraits(image,channel);
777 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
779 if ((traits == UndefinedPixelTrait) ||
780 (reconstruct_traits == UndefinedPixelTrait) ||
781 ((reconstruct_traits & UpdatePixelTrait) == 0))
783 if (channel == AlphaPixelChannel)
784 distance=fabs((double) p[i]-
785 GetPixelChannel(reconstruct_image,channel,q));
787 distance=fabs(Sa*p[i]-Da*
788 GetPixelChannel(reconstruct_image,channel,q));
789 distortion[i]+=distance;
790 distortion[CompositePixelChannel]+=distance;
791 mean_error+=distance*distance;
792 if (distance > maximum_error)
793 maximum_error=distance;
796 p+=GetPixelChannels(image);
797 q+=GetPixelChannels(reconstruct_image);
800 reconstruct_view=DestroyCacheView(reconstruct_view);
801 image_view=DestroyCacheView(image_view);
802 image->error.mean_error_per_pixel=distortion[CompositePixelChannel]/area;
803 image->error.normalized_mean_error=QuantumScale*QuantumScale*mean_error/area;
804 image->error.normalized_maximum_error=QuantumScale*maximum_error;
808 static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
809 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
832 rows=MagickMax(image->rows,reconstruct_image->rows);
833 columns=MagickMax(image->columns,reconstruct_image->columns);
835 image_view=AcquireVirtualCacheView(image,exception);
836 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
837 #if defined(MAGICKCORE_OPENMP_SUPPORT)
838 #pragma omp parallel for schedule(static) shared(status) \
839 magick_number_threads(image,image,rows,1) reduction(+:area)
841 for (y=0; y < (ssize_t) rows; y++)
844 channel_distortion[MaxPixelChannels+1];
846 register const Quantum
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 Quantum *) NULL) || (q == (const Quantum *) NULL))
862 (void) memset(channel_distortion,0,sizeof(channel_distortion));
863 for (x=0; x < (ssize_t) columns; x++)
872 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
873 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
875 p+=GetPixelChannels(image);
876 q+=GetPixelChannels(reconstruct_image);
879 Sa=QuantumScale*GetPixelAlpha(image,p);
880 Da=QuantumScale*GetPixelAlpha(reconstruct_image,q);
881 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
886 PixelChannel channel = GetPixelChannelChannel(image,i);
887 PixelTrait traits = GetPixelChannelTraits(image,channel);
888 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
890 if ((traits == UndefinedPixelTrait) ||
891 (reconstruct_traits == UndefinedPixelTrait) ||
892 ((reconstruct_traits & UpdatePixelTrait) == 0))
894 if (channel == AlphaPixelChannel)
895 distance=QuantumScale*(p[i]-GetPixelChannel(reconstruct_image,
898 distance=QuantumScale*(Sa*p[i]-Da*GetPixelChannel(reconstruct_image,
900 channel_distortion[i]+=distance*distance;
901 channel_distortion[CompositePixelChannel]+=distance*distance;
904 p+=GetPixelChannels(image);
905 q+=GetPixelChannels(reconstruct_image);
907 #if defined(MAGICKCORE_OPENMP_SUPPORT)
908 #pragma omp critical (MagickCore_GetMeanSquaredError)
910 for (j=0; j <= MaxPixelChannels; j++)
911 distortion[j]+=channel_distortion[j];
913 reconstruct_view=DestroyCacheView(reconstruct_view);
914 image_view=DestroyCacheView(image_view);
915 area=PerceptibleReciprocal(area);
916 for (j=0; j <= MaxPixelChannels; j++)
918 distortion[CompositePixelChannel]/=GetImageChannels(image);
922 static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
923 const Image *image,const Image *reconstruct_image,double *distortion,
924 ExceptionInfo *exception)
926 #define SimilarityImageTag "Similarity/Image"
934 *reconstruct_statistics;
956 Normalize to account for variation due to lighting and exposure condition.
958 image_statistics=GetImageStatistics(image,exception);
959 reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
960 if ((image_statistics == (ChannelStatistics *) NULL) ||
961 (reconstruct_statistics == (ChannelStatistics *) NULL))
963 if (image_statistics != (ChannelStatistics *) NULL)
964 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
966 if (reconstruct_statistics != (ChannelStatistics *) NULL)
967 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
968 reconstruct_statistics);
973 for (i=0; i <= MaxPixelChannels; i++)
975 rows=MagickMax(image->rows,reconstruct_image->rows);
976 columns=MagickMax(image->columns,reconstruct_image->columns);
978 image_view=AcquireVirtualCacheView(image,exception);
979 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
980 for (y=0; y < (ssize_t) rows; y++)
982 register const Quantum
989 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
990 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
991 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
996 for (x=0; x < (ssize_t) columns; x++)
998 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
999 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1001 p+=GetPixelChannels(image);
1002 q+=GetPixelChannels(reconstruct_image);
1006 p+=GetPixelChannels(image);
1007 q+=GetPixelChannels(reconstruct_image);
1010 area=PerceptibleReciprocal(area);
1011 for (y=0; y < (ssize_t) rows; y++)
1013 register const Quantum
1020 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1021 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1022 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
1027 for (x=0; x < (ssize_t) columns; x++)
1033 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1034 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1036 p+=GetPixelChannels(image);
1037 q+=GetPixelChannels(reconstruct_image);
1040 Sa=QuantumScale*GetPixelAlpha(image,p);
1041 Da=QuantumScale*GetPixelAlpha(reconstruct_image,q);
1042 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1044 PixelChannel channel = GetPixelChannelChannel(image,i);
1045 PixelTrait traits = GetPixelChannelTraits(image,channel);
1046 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1048 if ((traits == UndefinedPixelTrait) ||
1049 (reconstruct_traits == UndefinedPixelTrait) ||
1050 ((reconstruct_traits & UpdatePixelTrait) == 0))
1052 if (channel == AlphaPixelChannel)
1054 distortion[i]+=area*QuantumScale*(p[i]-
1055 image_statistics[channel].mean)*(GetPixelChannel(
1056 reconstruct_image,channel,q)-
1057 reconstruct_statistics[channel].mean);
1061 distortion[i]+=area*QuantumScale*(Sa*p[i]-
1062 image_statistics[channel].mean)*(Da*GetPixelChannel(
1063 reconstruct_image,channel,q)-
1064 reconstruct_statistics[channel].mean);
1067 p+=GetPixelChannels(image);
1068 q+=GetPixelChannels(reconstruct_image);
1070 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1075 proceed=SetImageProgress(image,SimilarityImageTag,progress++,rows);
1076 if (proceed == MagickFalse)
1083 reconstruct_view=DestroyCacheView(reconstruct_view);
1084 image_view=DestroyCacheView(image_view);
1086 Divide by the standard deviation.
1088 distortion[CompositePixelChannel]=0.0;
1089 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1094 PixelChannel channel = GetPixelChannelChannel(image,i);
1095 gamma=image_statistics[channel].standard_deviation*
1096 reconstruct_statistics[channel].standard_deviation;
1097 gamma=PerceptibleReciprocal(gamma);
1098 distortion[i]=QuantumRange*gamma*distortion[i];
1099 distortion[CompositePixelChannel]+=distortion[i]*distortion[i];
1101 distortion[CompositePixelChannel]=sqrt(distortion[CompositePixelChannel]/
1102 GetImageChannels(image));
1106 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1107 reconstruct_statistics);
1108 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1113 static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
1114 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
1131 rows=MagickMax(image->rows,reconstruct_image->rows);
1132 columns=MagickMax(image->columns,reconstruct_image->columns);
1133 image_view=AcquireVirtualCacheView(image,exception);
1134 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1135 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1136 #pragma omp parallel for schedule(static) shared(status) \
1137 magick_number_threads(image,image,rows,1)
1139 for (y=0; y < (ssize_t) rows; y++)
1142 channel_distortion[MaxPixelChannels+1];
1144 register const Quantum
1152 if (status == MagickFalse)
1154 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1155 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1156 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
1161 (void) memset(channel_distortion,0,sizeof(channel_distortion));
1162 for (x=0; x < (ssize_t) columns; x++)
1171 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1172 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1174 p+=GetPixelChannels(image);
1175 q+=GetPixelChannels(reconstruct_image);
1178 Sa=QuantumScale*GetPixelAlpha(image,p);
1179 Da=QuantumScale*GetPixelAlpha(reconstruct_image,q);
1180 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1185 PixelChannel channel = GetPixelChannelChannel(image,i);
1186 PixelTrait traits = GetPixelChannelTraits(image,channel);
1187 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1189 if ((traits == UndefinedPixelTrait) ||
1190 (reconstruct_traits == UndefinedPixelTrait) ||
1191 ((reconstruct_traits & UpdatePixelTrait) == 0))
1193 if (channel == AlphaPixelChannel)
1194 distance=QuantumScale*fabs((double) p[i]-
1195 GetPixelChannel(reconstruct_image,channel,q));
1197 distance=QuantumScale*fabs(Sa*p[i]-Da*
1198 GetPixelChannel(reconstruct_image,channel,q));
1199 if (distance > channel_distortion[i])
1200 channel_distortion[i]=distance;
1201 if (distance > channel_distortion[CompositePixelChannel])
1202 channel_distortion[CompositePixelChannel]=distance;
1204 p+=GetPixelChannels(image);
1205 q+=GetPixelChannels(reconstruct_image);
1207 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1208 #pragma omp critical (MagickCore_GetPeakAbsoluteError)
1210 for (j=0; j <= MaxPixelChannels; j++)
1211 if (channel_distortion[j] > distortion[j])
1212 distortion[j]=channel_distortion[j];
1214 reconstruct_view=DestroyCacheView(reconstruct_view);
1215 image_view=DestroyCacheView(image_view);
1219 static inline double MagickLog10(const double x)
1221 #define Log10Epsilon (1.0e-11)
1223 if (fabs(x) < Log10Epsilon)
1224 return(log10(Log10Epsilon));
1225 return(log10(fabs(x)));
1228 static MagickBooleanType GetPeakSignalToNoiseRatio(const Image *image,
1229 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
1237 status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception);
1238 for (i=0; i <= MaxPixelChannels; i++)
1239 if (fabs(distortion[i]) < MagickEpsilon)
1240 distortion[i]=INFINITY;
1242 distortion[i]=20.0*MagickLog10(1.0/sqrt(distortion[i]));
1246 static MagickBooleanType GetPerceptualHashDistortion(const Image *image,
1247 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
1249 ChannelPerceptualHash
1263 Compute perceptual hash in the sRGB colorspace.
1265 channel_phash=GetImagePerceptualHash(image,exception);
1266 if (channel_phash == (ChannelPerceptualHash *) NULL)
1267 return(MagickFalse);
1268 reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
1269 if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
1271 channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1273 return(MagickFalse);
1275 artifact=GetImageArtifact(image,"phash:normalize");
1276 normalize=(artifact == (const char *) NULL) ||
1277 (IsStringTrue(artifact) == MagickFalse) ? MagickFalse : MagickTrue;
1278 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1279 #pragma omp parallel for schedule(static)
1281 for (channel=0; channel < MaxPixelChannels; channel++)
1290 for (i=0; i < MaximumNumberOfImageMoments; i++)
1299 for (j=0; j < (ssize_t) channel_phash[0].number_colorspaces; j++)
1301 alpha=channel_phash[channel].phash[j][i];
1302 beta=reconstruct_phash[channel].phash[j][i];
1303 if (normalize == MagickFalse)
1304 difference+=(beta-alpha)*(beta-alpha);
1306 difference=sqrt((beta-alpha)*(beta-alpha)/
1307 channel_phash[0].number_channels);
1310 distortion[channel]+=difference;
1311 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1312 #pragma omp critical (MagickCore_GetPerceptualHashDistortion)
1314 distortion[CompositePixelChannel]+=difference;
1319 reconstruct_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1321 channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(channel_phash);
1325 static MagickBooleanType GetRootMeanSquaredDistortion(const Image *image,
1326 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
1334 status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception);
1335 for (i=0; i <= MaxPixelChannels; i++)
1336 distortion[i]=sqrt(distortion[i]);
1340 static MagickBooleanType GetStructuralSimilarityDistortion(const Image *image,
1341 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
1343 #define SSIMRadius 5.0
1344 #define SSIMSigma 1.5
1345 #define SSIMBlocksize 8
1355 geometry[MagickPathExtent];
1383 Compute structural similarity index @
1384 https://en.wikipedia.org/wiki/Structural_similarity.
1387 artifact=GetImageArtifact(image,"compare:ssim-radius");
1388 if (artifact != (const char *) NULL)
1389 radius=StringToDouble(artifact,(char **) NULL);
1391 artifact=GetImageArtifact(image,"compare:ssim-sigma");
1392 if (artifact != (const char *) NULL)
1393 sigma=StringToDouble(artifact,(char **) NULL);
1394 (void) FormatLocaleString(geometry,MagickPathExtent,"gaussian:%.20gx%.20g",
1396 kernel_info=AcquireKernelInfo(geometry,exception);
1397 if (kernel_info == (KernelInfo *) NULL)
1398 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1400 c1=pow(SSIMK1*SSIML,2.0);
1401 artifact=GetImageArtifact(image,"compare:ssim-k1");
1402 if (artifact != (const char *) NULL)
1403 c1=pow(StringToDouble(artifact,(char **) NULL)*SSIML,2.0);
1404 c2=pow(SSIMK2*SSIML,2.0);
1405 artifact=GetImageArtifact(image,"compare:ssim-k2");
1406 if (artifact != (const char *) NULL)
1407 c2=pow(StringToDouble(artifact,(char **) NULL)*SSIML,2.0);
1409 rows=MagickMax(image->rows,reconstruct_image->rows);
1410 columns=MagickMax(image->columns,reconstruct_image->columns);
1411 image_view=AcquireVirtualCacheView(image,exception);
1412 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1413 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1414 #pragma omp parallel for schedule(static) shared(status) \
1415 magick_number_threads(image,reconstruct_image,rows,1)
1417 for (y=0; y < (ssize_t) rows; y++)
1420 channel_distortion[MaxPixelChannels+1];
1422 register const Quantum
1430 if (status == MagickFalse)
1432 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1433 ((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
1434 kernel_info->height,exception);
1435 q=GetCacheViewVirtualPixels(reconstruct_view,-((ssize_t) kernel_info->width/
1436 2L),y-((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
1437 kernel_info->height,exception);
1438 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
1443 (void) memset(channel_distortion,0,sizeof(channel_distortion));
1444 for (x=0; x < (ssize_t) columns; x++)
1447 x_pixel_mu[MaxPixelChannels+1],
1448 x_pixel_sigma_squared[MaxPixelChannels+1],
1449 xy_sigma[MaxPixelChannels+1],
1450 y_pixel_mu[MaxPixelChannels+1],
1451 y_pixel_sigma_squared[MaxPixelChannels+1];
1453 register const Quantum
1454 *magick_restrict reference,
1455 *magick_restrict target;
1457 register MagickRealType
1463 (void) memset(x_pixel_mu,0,sizeof(x_pixel_mu));
1464 (void) memset(x_pixel_sigma_squared,0,sizeof(x_pixel_sigma_squared));
1465 (void) memset(xy_sigma,0,sizeof(xy_sigma));
1466 (void) memset(x_pixel_sigma_squared,0,sizeof(y_pixel_sigma_squared));
1467 (void) memset(y_pixel_mu,0,sizeof(y_pixel_mu));
1468 (void) memset(y_pixel_sigma_squared,0,sizeof(y_pixel_sigma_squared));
1469 k=kernel_info->values;
1472 for (v=0; v < (ssize_t) kernel_info->height; v++)
1477 for (u=0; u < (ssize_t) kernel_info->width; u++)
1479 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1485 PixelChannel channel = GetPixelChannelChannel(image,i);
1486 PixelTrait traits = GetPixelChannelTraits(image,channel);
1487 PixelTrait reconstruct_traits = GetPixelChannelTraits(
1488 reconstruct_image,channel);
1489 if ((traits == UndefinedPixelTrait) ||
1490 (reconstruct_traits == UndefinedPixelTrait) ||
1491 ((reconstruct_traits & UpdatePixelTrait) == 0))
1493 x_pixel=QuantumScale*reference[i];
1494 x_pixel_mu[i]+=(*k)*x_pixel;
1495 x_pixel_sigma_squared[i]+=(*k)*x_pixel*x_pixel;
1496 y_pixel=QuantumScale*
1497 GetPixelChannel(reconstruct_image,channel,target);
1498 y_pixel_mu[i]+=(*k)*y_pixel;
1499 y_pixel_sigma_squared[i]+=(*k)*y_pixel*y_pixel;
1500 xy_sigma[i]+=(*k)*x_pixel*y_pixel;
1503 reference+=GetPixelChannels(image);
1504 target+=GetPixelChannels(reconstruct_image);
1506 reference+=GetPixelChannels(image)*columns;
1507 target+=GetPixelChannels(reconstruct_image)*columns;
1509 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1514 x_pixel_sigmas_squared,
1518 y_pixel_sigmas_squared;
1520 PixelChannel channel = GetPixelChannelChannel(image,i);
1521 PixelTrait traits = GetPixelChannelTraits(image,channel);
1522 PixelTrait reconstruct_traits = GetPixelChannelTraits(
1523 reconstruct_image,channel);
1524 if ((traits == UndefinedPixelTrait) ||
1525 (reconstruct_traits == UndefinedPixelTrait) ||
1526 ((reconstruct_traits & UpdatePixelTrait) == 0))
1528 x_pixel_mu_squared=x_pixel_mu[i]*x_pixel_mu[i];
1529 y_pixel_mu_squared=y_pixel_mu[i]*y_pixel_mu[i];
1530 xy_mu=x_pixel_mu[i]*y_pixel_mu[i];
1531 xy_sigmas=xy_sigma[i]-xy_mu;
1532 x_pixel_sigmas_squared=x_pixel_sigma_squared[i]-x_pixel_mu_squared;
1533 y_pixel_sigmas_squared=y_pixel_sigma_squared[i]-y_pixel_mu_squared;
1534 ssim=((2.0*xy_mu+c1)*(2.0*xy_sigmas+c2))/
1535 ((x_pixel_mu_squared+y_pixel_mu_squared+c1)*
1536 (x_pixel_sigmas_squared+y_pixel_sigmas_squared+c2));
1537 channel_distortion[i]+=ssim;
1538 channel_distortion[CompositePixelChannel]+=ssim;
1540 p+=GetPixelChannels(image);
1541 q+=GetPixelChannels(reconstruct_image);
1543 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1544 #pragma omp critical (MagickCore_GetStructuralSimilarityDistortion)
1546 for (i=0; i <= MaxPixelChannels; i++)
1547 distortion[i]+=channel_distortion[i];
1549 image_view=DestroyCacheView(image_view);
1550 reconstruct_view=DestroyCacheView(reconstruct_view);
1551 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1553 PixelChannel channel = GetPixelChannelChannel(image,i);
1554 PixelTrait traits = GetPixelChannelTraits(image,channel);
1555 if ((traits == UndefinedPixelTrait) || ((traits & UpdatePixelTrait) == 0))
1557 distortion[i]/=((double) columns*rows);
1559 distortion[CompositePixelChannel]/=((double) columns*rows);
1560 distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
1561 kernel_info=DestroyKernelInfo(kernel_info);
1565 static MagickBooleanType GetStructuralDisimilarityDistortion(const Image *image,
1566 const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
1574 status=GetStructuralSimilarityDistortion(image,reconstruct_image,
1575 distortion,exception);
1576 for (i=0; i <= MaxPixelChannels; i++)
1577 distortion[i]=(1.0-(distortion[i]))/2.0;
1581 MagickExport MagickBooleanType GetImageDistortion(Image *image,
1582 const Image *reconstruct_image,const MetricType metric,double *distortion,
1583 ExceptionInfo *exception)
1586 *channel_distortion;
1594 assert(image != (Image *) NULL);
1595 assert(image->signature == MagickCoreSignature);
1596 if (image->debug != MagickFalse)
1597 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1598 assert(reconstruct_image != (const Image *) NULL);
1599 assert(reconstruct_image->signature == MagickCoreSignature);
1600 assert(distortion != (double *) NULL);
1602 if (image->debug != MagickFalse)
1603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1605 Get image distortion.
1607 length=MaxPixelChannels+1;
1608 channel_distortion=(double *) AcquireQuantumMemory(length,
1609 sizeof(*channel_distortion));
1610 if (channel_distortion == (double *) NULL)
1611 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1612 (void) memset(channel_distortion,0,length*
1613 sizeof(*channel_distortion));
1616 case AbsoluteErrorMetric:
1618 status=GetAbsoluteDistortion(image,reconstruct_image,channel_distortion,
1622 case FuzzErrorMetric:
1624 status=GetFuzzDistortion(image,reconstruct_image,channel_distortion,
1628 case MeanAbsoluteErrorMetric:
1630 status=GetMeanAbsoluteDistortion(image,reconstruct_image,
1631 channel_distortion,exception);
1634 case MeanErrorPerPixelErrorMetric:
1636 status=GetMeanErrorPerPixel(image,reconstruct_image,channel_distortion,
1640 case MeanSquaredErrorMetric:
1642 status=GetMeanSquaredDistortion(image,reconstruct_image,
1643 channel_distortion,exception);
1646 case NormalizedCrossCorrelationErrorMetric:
1649 status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image,
1650 channel_distortion,exception);
1653 case PeakAbsoluteErrorMetric:
1655 status=GetPeakAbsoluteDistortion(image,reconstruct_image,
1656 channel_distortion,exception);
1659 case PeakSignalToNoiseRatioErrorMetric:
1661 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,
1662 channel_distortion,exception);
1665 case PerceptualHashErrorMetric:
1667 status=GetPerceptualHashDistortion(image,reconstruct_image,
1668 channel_distortion,exception);
1671 case RootMeanSquaredErrorMetric:
1673 status=GetRootMeanSquaredDistortion(image,reconstruct_image,
1674 channel_distortion,exception);
1677 case StructuralSimilarityErrorMetric:
1679 status=GetStructuralSimilarityDistortion(image,reconstruct_image,
1680 channel_distortion,exception);
1683 case StructuralDissimilarityErrorMetric:
1685 status=GetStructuralDisimilarityDistortion(image,reconstruct_image,
1686 channel_distortion,exception);
1690 *distortion=channel_distortion[CompositePixelChannel];
1691 channel_distortion=(double *) RelinquishMagickMemory(channel_distortion);
1692 (void) FormatImageProperty(image,"distortion","%.*g",GetMagickPrecision(),
1698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1702 % G e t I m a g e D i s t o r t i o n s %
1706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1708 % GetImageDistortions() compares the pixel channels of an image to a
1709 % reconstructed image and returns the specified distortion metric for each
1712 % The format of the GetImageDistortions method is:
1714 % double *GetImageDistortions(const Image *image,
1715 % const Image *reconstruct_image,const MetricType metric,
1716 % ExceptionInfo *exception)
1718 % A description of each parameter follows:
1720 % o image: the image.
1722 % o reconstruct_image: the reconstruct image.
1724 % o metric: the metric.
1726 % o exception: return any errors or warnings in this structure.
1729 MagickExport double *GetImageDistortions(Image *image,
1730 const Image *reconstruct_image,const MetricType metric,
1731 ExceptionInfo *exception)
1734 *channel_distortion;
1742 assert(image != (Image *) NULL);
1743 assert(image->signature == MagickCoreSignature);
1744 if (image->debug != MagickFalse)
1745 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1746 assert(reconstruct_image != (const Image *) NULL);
1747 assert(reconstruct_image->signature == MagickCoreSignature);
1748 if (image->debug != MagickFalse)
1749 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1751 Get image distortion.
1753 length=MaxPixelChannels+1UL;
1754 channel_distortion=(double *) AcquireQuantumMemory(length,
1755 sizeof(*channel_distortion));
1756 if (channel_distortion == (double *) NULL)
1757 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1758 (void) memset(channel_distortion,0,length*
1759 sizeof(*channel_distortion));
1763 case AbsoluteErrorMetric:
1765 status=GetAbsoluteDistortion(image,reconstruct_image,channel_distortion,
1769 case FuzzErrorMetric:
1771 status=GetFuzzDistortion(image,reconstruct_image,channel_distortion,
1775 case MeanAbsoluteErrorMetric:
1777 status=GetMeanAbsoluteDistortion(image,reconstruct_image,
1778 channel_distortion,exception);
1781 case MeanErrorPerPixelErrorMetric:
1783 status=GetMeanErrorPerPixel(image,reconstruct_image,channel_distortion,
1787 case MeanSquaredErrorMetric:
1789 status=GetMeanSquaredDistortion(image,reconstruct_image,
1790 channel_distortion,exception);
1793 case NormalizedCrossCorrelationErrorMetric:
1796 status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image,
1797 channel_distortion,exception);
1800 case PeakAbsoluteErrorMetric:
1802 status=GetPeakAbsoluteDistortion(image,reconstruct_image,
1803 channel_distortion,exception);
1806 case PeakSignalToNoiseRatioErrorMetric:
1808 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,
1809 channel_distortion,exception);
1812 case PerceptualHashErrorMetric:
1814 status=GetRootMeanSquaredDistortion(image,reconstruct_image,
1815 channel_distortion,exception);
1818 case RootMeanSquaredErrorMetric:
1820 status=GetRootMeanSquaredDistortion(image,reconstruct_image,
1821 channel_distortion,exception);
1824 case StructuralSimilarityErrorMetric:
1826 status=GetStructuralSimilarityDistortion(image,reconstruct_image,
1827 channel_distortion,exception);
1830 case StructuralDissimilarityErrorMetric:
1832 status=GetStructuralDisimilarityDistortion(image,reconstruct_image,
1833 channel_distortion,exception);
1837 if (status == MagickFalse)
1839 channel_distortion=(double *) RelinquishMagickMemory(channel_distortion);
1840 return((double *) NULL);
1842 return(channel_distortion);
1846 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1850 % I s I m a g e s E q u a l %
1854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1856 % IsImagesEqual() compare the pixels of two images and returns immediately
1857 % if any pixel is not identical.
1859 % The format of the IsImagesEqual method is:
1861 % MagickBooleanType IsImagesEqual(const Image *image,
1862 % const Image *reconstruct_image,ExceptionInfo *exception)
1864 % A description of each parameter follows.
1866 % o image: the image.
1868 % o reconstruct_image: the reconstruct image.
1870 % o exception: return any errors or warnings in this structure.
1873 MagickExport MagickBooleanType IsImagesEqual(const Image *image,
1874 const Image *reconstruct_image,ExceptionInfo *exception)
1887 assert(image != (Image *) NULL);
1888 assert(image->signature == MagickCoreSignature);
1889 assert(reconstruct_image != (const Image *) NULL);
1890 assert(reconstruct_image->signature == MagickCoreSignature);
1891 rows=MagickMax(image->rows,reconstruct_image->rows);
1892 columns=MagickMax(image->columns,reconstruct_image->columns);
1893 image_view=AcquireVirtualCacheView(image,exception);
1894 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1895 for (y=0; y < (ssize_t) rows; y++)
1897 register const Quantum
1904 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1905 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1906 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1908 for (x=0; x < (ssize_t) columns; x++)
1913 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1918 PixelChannel channel = GetPixelChannelChannel(image,i);
1919 PixelTrait traits = GetPixelChannelTraits(image,channel);
1920 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1922 if ((traits == UndefinedPixelTrait) ||
1923 (reconstruct_traits == UndefinedPixelTrait) ||
1924 ((reconstruct_traits & UpdatePixelTrait) == 0))
1926 distance=fabs(p[i]-(double) GetPixelChannel(reconstruct_image,
1928 if (distance >= MagickEpsilon)
1931 if (i < (ssize_t) GetPixelChannels(image))
1933 p+=GetPixelChannels(image);
1934 q+=GetPixelChannels(reconstruct_image);
1936 if (x < (ssize_t) columns)
1939 reconstruct_view=DestroyCacheView(reconstruct_view);
1940 image_view=DestroyCacheView(image_view);
1941 return(y < (ssize_t) rows ? MagickFalse : MagickTrue);
1945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1949 % S e t I m a g e C o l o r M e t r i c %
1953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1955 % SetImageColorMetric() measures the difference between colors at each pixel
1956 % location of two images. A value other than 0 means the colors match
1957 % exactly. Otherwise an error measure is computed by summing over all
1958 % pixels in an image the distance squared in RGB space between each image
1959 % pixel and its corresponding pixel in the reconstruct image. The error
1960 % measure is assigned to these image members:
1962 % o mean_error_per_pixel: The mean error for any single pixel in
1965 % o normalized_mean_error: The normalized mean quantization error for
1966 % any single pixel in the image. This distance measure is normalized to
1967 % a range between 0 and 1. It is independent of the range of red, green,
1968 % and blue values in the image.
1970 % o normalized_maximum_error: The normalized maximum quantization
1971 % error for any single pixel in the image. This distance measure is
1972 % normalized to a range between 0 and 1. It is independent of the range
1973 % of red, green, and blue values in your image.
1975 % A small normalized mean square error, accessed as
1976 % image->normalized_mean_error, suggests the images are very similar in
1977 % spatial layout and color.
1979 % The format of the SetImageColorMetric method is:
1981 % MagickBooleanType SetImageColorMetric(Image *image,
1982 % const Image *reconstruct_image,ExceptionInfo *exception)
1984 % A description of each parameter follows.
1986 % o image: the image.
1988 % o reconstruct_image: the reconstruct image.
1990 % o exception: return any errors or warnings in this structure.
1993 MagickExport MagickBooleanType SetImageColorMetric(Image *image,
1994 const Image *reconstruct_image,ExceptionInfo *exception)
2004 mean_error_per_pixel;
2016 assert(image != (Image *) NULL);
2017 assert(image->signature == MagickCoreSignature);
2018 assert(reconstruct_image != (const Image *) NULL);
2019 assert(reconstruct_image->signature == MagickCoreSignature);
2022 mean_error_per_pixel=0.0;
2024 rows=MagickMax(image->rows,reconstruct_image->rows);
2025 columns=MagickMax(image->columns,reconstruct_image->columns);
2026 image_view=AcquireVirtualCacheView(image,exception);
2027 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2028 for (y=0; y < (ssize_t) rows; y++)
2030 register const Quantum
2037 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
2038 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
2039 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2041 for (x=0; x < (ssize_t) columns; x++)
2046 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2051 PixelChannel channel = GetPixelChannelChannel(image,i);
2052 PixelTrait traits = GetPixelChannelTraits(image,channel);
2053 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2055 if ((traits == UndefinedPixelTrait) ||
2056 (reconstruct_traits == UndefinedPixelTrait) ||
2057 ((reconstruct_traits & UpdatePixelTrait) == 0))
2059 distance=fabs(p[i]-(double) GetPixelChannel(reconstruct_image,
2061 if (distance >= MagickEpsilon)
2063 mean_error_per_pixel+=distance;
2064 mean_error+=distance*distance;
2065 if (distance > maximum_error)
2066 maximum_error=distance;
2070 p+=GetPixelChannels(image);
2071 q+=GetPixelChannels(reconstruct_image);
2074 reconstruct_view=DestroyCacheView(reconstruct_view);
2075 image_view=DestroyCacheView(image_view);
2076 image->error.mean_error_per_pixel=(double) (mean_error_per_pixel/area);
2077 image->error.normalized_mean_error=(double) (QuantumScale*QuantumScale*
2079 image->error.normalized_maximum_error=(double) (QuantumScale*maximum_error);
2080 status=image->error.mean_error_per_pixel == 0.0 ? MagickTrue : MagickFalse;
2085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2089 % S i m i l a r i t y I m a g e %
2093 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2095 % SimilarityImage() compares the reference image of the image and returns the
2096 % best match offset. In addition, it returns a similarity image such that an
2097 % exact match location is completely white and if none of the pixels match,
2098 % black, otherwise some gray level in-between.
2100 % The format of the SimilarityImageImage method is:
2102 % Image *SimilarityImage(const Image *image,const Image *reference,
2103 % const MetricType metric,const double similarity_threshold,
2104 % RectangleInfo *offset,double *similarity,ExceptionInfo *exception)
2106 % A description of each parameter follows:
2108 % o image: the image.
2110 % o reference: find an area of the image that closely resembles this image.
2112 % o metric: the metric.
2114 % o similarity_threshold: minimum distortion for (sub)image match.
2116 % o offset: the best match offset of the reference image within the image.
2118 % o similarity: the computed similarity between the images.
2120 % o exception: return any errors or warnings in this structure.
2124 static double GetSimilarityMetric(const Image *image,const Image *reference,
2125 const MetricType metric,const ssize_t x_offset,const ssize_t y_offset,
2126 ExceptionInfo *exception)
2140 SetGeometry(reference,&geometry);
2141 geometry.x=x_offset;
2142 geometry.y=y_offset;
2143 similarity_image=CropImage(image,&geometry,exception);
2144 if (similarity_image == (Image *) NULL)
2147 status=GetImageDistortion(similarity_image,reference,metric,&distortion,
2149 similarity_image=DestroyImage(similarity_image);
2150 if (status == MagickFalse)
2155 MagickExport Image *SimilarityImage(const Image *image,const Image *reference,
2156 const MetricType metric,const double similarity_threshold,
2157 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
2159 #define SimilarityImageTag "Similarity/Image"
2176 assert(image != (const Image *) NULL);
2177 assert(image->signature == MagickCoreSignature);
2178 if (image->debug != MagickFalse)
2179 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2180 assert(exception != (ExceptionInfo *) NULL);
2181 assert(exception->signature == MagickCoreSignature);
2182 assert(offset != (RectangleInfo *) NULL);
2183 SetGeometry(reference,offset);
2184 *similarity_metric=MagickMaximumValue;
2185 similarity_image=CloneImage(image,image->columns-reference->columns+1,
2186 image->rows-reference->rows+1,MagickTrue,exception);
2187 if (similarity_image == (Image *) NULL)
2188 return((Image *) NULL);
2189 status=SetImageStorageClass(similarity_image,DirectClass,exception);
2190 if (status == MagickFalse)
2192 similarity_image=DestroyImage(similarity_image);
2193 return((Image *) NULL);
2195 (void) SetImageAlphaChannel(similarity_image,DeactivateAlphaChannel,
2198 Measure similarity of reference image against image.
2202 similarity_view=AcquireAuthenticCacheView(similarity_image,exception);
2203 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2204 #pragma omp parallel for schedule(static) \
2205 shared(progress,status,similarity_metric) \
2206 magick_number_threads(image,image,image->rows-reference->rows+1,1)
2208 for (y=0; y < (ssize_t) (image->rows-reference->rows+1); y++)
2219 if (status == MagickFalse)
2221 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2222 #pragma omp flush(similarity_metric)
2224 if (*similarity_metric <= similarity_threshold)
2226 q=GetCacheViewAuthenticPixels(similarity_view,0,y,similarity_image->columns,
2228 if (q == (Quantum *) NULL)
2233 for (x=0; x < (ssize_t) (image->columns-reference->columns+1); x++)
2238 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2239 #pragma omp flush(similarity_metric)
2241 if (*similarity_metric <= similarity_threshold)
2243 similarity=GetSimilarityMetric(image,reference,metric,x,y,exception);
2244 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2245 #pragma omp critical (MagickCore_SimilarityImage)
2247 if ((metric == NormalizedCrossCorrelationErrorMetric) ||
2248 (metric == UndefinedErrorMetric))
2249 similarity=1.0-similarity;
2250 if (similarity < *similarity_metric)
2254 *similarity_metric=similarity;
2256 if (metric == PerceptualHashErrorMetric)
2257 similarity=MagickMin(0.01*similarity,1.0);
2258 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2260 PixelChannel channel = GetPixelChannelChannel(image,i);
2261 PixelTrait traits = GetPixelChannelTraits(image,channel);
2262 PixelTrait similarity_traits=GetPixelChannelTraits(similarity_image,
2264 if ((traits == UndefinedPixelTrait) ||
2265 (similarity_traits == UndefinedPixelTrait) ||
2266 ((similarity_traits & UpdatePixelTrait) == 0))
2268 SetPixelChannel(similarity_image,channel,ClampToQuantum(QuantumRange-
2269 QuantumRange*similarity),q);
2271 q+=GetPixelChannels(similarity_image);
2273 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
2275 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2280 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2281 #pragma omp critical (MagickCore_SimilarityImage)
2283 proceed=SetImageProgress(image,SimilarityImageTag,progress++,
2285 if (proceed == MagickFalse)
2289 similarity_view=DestroyCacheView(similarity_view);
2290 if (status == MagickFalse)
2291 similarity_image=DestroyImage(similarity_image);
2292 return(similarity_image);