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-2010 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 % http://www.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 "magick/studio.h"
44 #include "magick/artifact.h"
45 #include "magick/cache-view.h"
46 #include "magick/client.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/colorspace-private.h"
51 #include "magick/compare.h"
52 #include "magick/composite-private.h"
53 #include "magick/constitute.h"
54 #include "magick/exception-private.h"
55 #include "magick/geometry.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/log.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/option.h"
63 #include "magick/pixel-private.h"
64 #include "magick/resource_.h"
65 #include "magick/string_.h"
66 #include "magick/utility.h"
67 #include "magick/version.h"
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % C o m p a r e I m a g e C h a n n e l s %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % CompareImageChannels() compares one or more image channels of an image
81 % to a reconstructed image and returns the difference image.
83 % The format of the CompareImageChannels method is:
85 % Image *CompareImageChannels(const Image *image,
86 % const Image *reconstruct_image,const ChannelType channel,
87 % const MetricType metric,double *distortion,ExceptionInfo *exception)
89 % A description of each parameter follows:
93 % o reconstruct_image: the reconstruct image.
95 % o channel: the channel.
97 % o metric: the metric.
99 % o distortion: the computed distortion between the images.
101 % o exception: return any errors or warnings in this structure.
105 MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
106 const MetricType metric,double *distortion,ExceptionInfo *exception)
111 highlight_image=CompareImageChannels(image,reconstruct_image,AllChannels,
112 metric,distortion,exception);
113 return(highlight_image);
116 MagickExport Image *CompareImageChannels(Image *image,
117 const Image *reconstruct_image,const ChannelType channel,
118 const MetricType metric,double *distortion,ExceptionInfo *exception)
143 assert(image != (Image *) NULL);
144 assert(image->signature == MagickSignature);
145 if (image->debug != MagickFalse)
146 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
147 assert(reconstruct_image != (const Image *) NULL);
148 assert(reconstruct_image->signature == MagickSignature);
149 assert(distortion != (double *) NULL);
151 if (image->debug != MagickFalse)
152 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
153 if ((reconstruct_image->columns != image->columns) ||
154 (reconstruct_image->rows != image->rows))
155 ThrowImageException(ImageError,"ImageSizeDiffers");
156 status=GetImageChannelDistortion(image,reconstruct_image,channel,metric,
157 distortion,exception);
158 if (status == MagickFalse)
159 return((Image *) NULL);
160 difference_image=CloneImage(image,0,0,MagickTrue,exception);
161 if (difference_image == (Image *) NULL)
162 return((Image *) NULL);
163 (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel);
164 highlight_image=CloneImage(image,image->columns,image->rows,MagickTrue,
166 if (highlight_image == (Image *) NULL)
168 difference_image=DestroyImage(difference_image);
169 return((Image *) NULL);
171 if (SetImageStorageClass(highlight_image,DirectClass) == MagickFalse)
173 InheritException(exception,&highlight_image->exception);
174 difference_image=DestroyImage(difference_image);
175 highlight_image=DestroyImage(highlight_image);
176 return((Image *) NULL);
178 (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel);
179 (void) QueryMagickColor("#f1001ecc",&highlight,exception);
180 artifact=GetImageArtifact(image,"highlight-color");
181 if (artifact != (const char *) NULL)
182 (void) QueryMagickColor(artifact,&highlight,exception);
183 (void) QueryMagickColor("#ffffffcc",&lowlight,exception);
184 artifact=GetImageArtifact(image,"lowlight-color");
185 if (artifact != (const char *) NULL)
186 (void) QueryMagickColor(artifact,&lowlight,exception);
187 if (highlight_image->colorspace == CMYKColorspace)
189 ConvertRGBToCMYK(&highlight);
190 ConvertRGBToCMYK(&lowlight);
193 Generate difference image.
196 GetMagickPixelPacket(image,&zero);
197 image_view=AcquireCacheView(image);
198 reconstruct_view=AcquireCacheView(reconstruct_image);
199 highlight_view=AcquireCacheView(highlight_image);
200 #if defined(MAGICKCORE_OPENMP_SUPPORT)
201 #pragma omp parallel for schedule(dynamic,4) shared(status)
203 for (y=0; y < (long) image->rows; y++)
212 register const IndexPacket
214 *restrict reconstruct_indexes;
216 register const PixelPacket
221 *restrict highlight_indexes;
229 if (status == MagickFalse)
231 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
232 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
234 r=QueueCacheViewAuthenticPixels(highlight_view,0,y,highlight_image->columns,
236 if ((p == (const PixelPacket *) NULL) ||
237 (q == (const PixelPacket *) NULL) || (r == (PixelPacket *) NULL))
242 indexes=GetCacheViewVirtualIndexQueue(image_view);
243 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
244 highlight_indexes=GetCacheViewAuthenticIndexQueue(highlight_view);
246 reconstruct_pixel=zero;
247 for (x=0; x < (long) image->columns; x++)
252 SetMagickPixelPacket(image,p,indexes+x,&pixel);
253 SetMagickPixelPacket(reconstruct_image,q,reconstruct_indexes+x,
255 difference=MagickFalse;
256 if (channel == AllChannels)
258 if (IsMagickColorSimilar(&pixel,&reconstruct_pixel) == MagickFalse)
259 difference=MagickTrue;
263 if (((channel & RedChannel) != 0) && (p->red != q->red))
264 difference=MagickTrue;
265 if (((channel & GreenChannel) != 0) && (p->green != q->green))
266 difference=MagickTrue;
267 if (((channel & BlueChannel) != 0) && (p->blue != q->blue))
268 difference=MagickTrue;
269 if (((channel & OpacityChannel) != 0) &&
270 (image->matte != MagickFalse) && (p->opacity != q->opacity))
271 difference=MagickTrue;
272 if ((((channel & IndexChannel) != 0) &&
273 (image->colorspace == CMYKColorspace) &&
274 (reconstruct_image->colorspace == CMYKColorspace)) &&
275 (indexes[x] != reconstruct_indexes[x]))
276 difference=MagickTrue;
278 if (difference != MagickFalse)
279 SetPixelPacket(highlight_image,&highlight,r,highlight_indexes+x);
281 SetPixelPacket(highlight_image,&lowlight,r,highlight_indexes+x);
286 sync=SyncCacheViewAuthenticPixels(highlight_view,exception);
287 if (sync == MagickFalse)
290 highlight_view=DestroyCacheView(highlight_view);
291 reconstruct_view=DestroyCacheView(reconstruct_view);
292 image_view=DestroyCacheView(image_view);
293 (void) CompositeImage(difference_image,image->compose,highlight_image,0,0);
294 highlight_image=DestroyImage(highlight_image);
295 if (status == MagickFalse)
296 difference_image=DestroyImage(difference_image);
297 return(difference_image);
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 % G e t I m a g e C h a n n e l D i s t o r t i o n %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 % GetImageChannelDistortion() compares one or more image channels of an image
312 % to a reconstructed image and returns the specified distortion metric.
314 % The format of the CompareImageChannels method is:
316 % MagickBooleanType GetImageChannelDistortion(const Image *image,
317 % const Image *reconstruct_image,const ChannelType channel,
318 % const MetricType metric,double *distortion,ExceptionInfo *exception)
320 % A description of each parameter follows:
322 % o image: the image.
324 % o reconstruct_image: the reconstruct image.
326 % o channel: the channel.
328 % o metric: the metric.
330 % o distortion: the computed distortion between the images.
332 % o exception: return any errors or warnings in this structure.
336 MagickExport MagickBooleanType GetImageDistortion(Image *image,
337 const Image *reconstruct_image,const MetricType metric,double *distortion,
338 ExceptionInfo *exception)
343 status=GetImageChannelDistortion(image,reconstruct_image,AllChannels,
344 metric,distortion,exception);
348 static MagickBooleanType GetAbsoluteError(const Image *image,
349 const Image *reconstruct_image,const ChannelType channel,double *distortion,
350 ExceptionInfo *exception)
366 Compute the absolute difference in pixels between two images.
369 GetMagickPixelPacket(image,&zero);
370 image_view=AcquireCacheView(image);
371 reconstruct_view=AcquireCacheView(reconstruct_image);
372 #if defined(MAGICKCORE_OPENMP_SUPPORT)
373 #pragma omp parallel for schedule(dynamic,4) shared(status)
375 for (y=0; y < (long) image->rows; y++)
378 channel_distortion[AllChannels+1];
384 register const IndexPacket
386 *restrict reconstruct_indexes;
388 register const PixelPacket
396 if (status == MagickFalse)
398 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
399 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
401 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
406 indexes=GetCacheViewVirtualIndexQueue(image_view);
407 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
409 reconstruct_pixel=pixel;
410 (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
411 for (x=0; x < (long) image->columns; x++)
413 SetMagickPixelPacket(image,p,indexes+x,&pixel);
414 SetMagickPixelPacket(reconstruct_image,q,reconstruct_indexes+x,
416 if (IsMagickColorSimilar(&pixel,&reconstruct_pixel) == MagickFalse)
418 if ((channel & RedChannel) != 0)
419 channel_distortion[RedChannel]++;
420 if ((channel & GreenChannel) != 0)
421 channel_distortion[GreenChannel]++;
422 if ((channel & BlueChannel) != 0)
423 channel_distortion[BlueChannel]++;
424 if (((channel & OpacityChannel) != 0) &&
425 (image->matte != MagickFalse))
426 channel_distortion[OpacityChannel]++;
427 if (((channel & IndexChannel) != 0) &&
428 (image->colorspace == CMYKColorspace))
429 channel_distortion[BlackChannel]++;
430 channel_distortion[AllChannels]++;
435 #if defined(MAGICKCORE_OPENMP_SUPPORT)
436 #pragma omp critical (MagickCore_GetAbsoluteError)
438 for (i=0; i <= (long) AllChannels; i++)
439 distortion[i]+=channel_distortion[i];
441 reconstruct_view=DestroyCacheView(reconstruct_view);
442 image_view=DestroyCacheView(image_view);
446 static unsigned long GetNumberChannels(const Image *image,
447 const ChannelType channel)
453 if ((channel & RedChannel) != 0)
455 if ((channel & GreenChannel) != 0)
457 if ((channel & BlueChannel) != 0)
459 if (((channel & OpacityChannel) != 0) &&
460 (image->matte != MagickFalse))
462 if (((channel & IndexChannel) != 0) &&
463 (image->colorspace == CMYKColorspace))
468 static MagickBooleanType GetMeanAbsoluteError(const Image *image,
469 const Image *reconstruct_image,const ChannelType channel,
470 double *distortion,ExceptionInfo *exception)
486 image_view=AcquireCacheView(image);
487 reconstruct_view=AcquireCacheView(reconstruct_image);
488 #if defined(MAGICKCORE_OPENMP_SUPPORT)
489 #pragma omp parallel for schedule(dynamic,4) shared(status)
491 for (y=0; y < (long) image->rows; y++)
494 channel_distortion[AllChannels+1];
496 register const IndexPacket
498 *restrict reconstruct_indexes;
500 register const PixelPacket
508 if (status == MagickFalse)
510 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
511 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,
512 reconstruct_image->columns,1,exception);
513 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
518 indexes=GetCacheViewVirtualIndexQueue(image_view);
519 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
520 (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
521 for (x=0; x < (long) image->columns; x++)
526 if ((channel & RedChannel) != 0)
528 distance=QuantumScale*fabs(p->red-(double) q->red);
529 channel_distortion[RedChannel]+=distance;
530 channel_distortion[AllChannels]+=distance;
532 if ((channel & GreenChannel) != 0)
534 distance=QuantumScale*fabs(p->green-(double) q->green);
535 channel_distortion[GreenChannel]+=distance;
536 channel_distortion[AllChannels]+=distance;
538 if ((channel & BlueChannel) != 0)
540 distance=QuantumScale*fabs(p->blue-(double) q->blue);
541 channel_distortion[BlueChannel]+=distance;
542 channel_distortion[AllChannels]+=distance;
544 if (((channel & OpacityChannel) != 0) &&
545 (image->matte != MagickFalse))
547 distance=QuantumScale*fabs(p->opacity-(double) q->opacity);
548 channel_distortion[OpacityChannel]+=distance;
549 channel_distortion[AllChannels]+=distance;
551 if (((channel & IndexChannel) != 0) &&
552 (image->colorspace == CMYKColorspace))
554 distance=QuantumScale*fabs(indexes[x]-(double)
555 reconstruct_indexes[x]);
556 channel_distortion[BlackChannel]+=distance;
557 channel_distortion[AllChannels]+=distance;
562 #if defined(MAGICKCORE_OPENMP_SUPPORT)
563 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
565 for (i=0; i <= (long) AllChannels; i++)
566 distortion[i]+=channel_distortion[i];
568 reconstruct_view=DestroyCacheView(reconstruct_view);
569 image_view=DestroyCacheView(image_view);
570 for (i=0; i <= (long) AllChannels; i++)
571 distortion[i]/=((double) image->columns*image->rows);
572 distortion[AllChannels]/=(double) GetNumberChannels(image,channel);
576 static MagickBooleanType GetMeanErrorPerPixel(Image *image,
577 const Image *reconstruct_image,const ChannelType channel,double *distortion,
578 ExceptionInfo *exception)
603 image_view=AcquireCacheView(image);
604 reconstruct_view=AcquireCacheView(reconstruct_image);
605 for (y=0; y < (long) image->rows; y++)
607 register const IndexPacket
609 *restrict reconstruct_indexes;
611 register const PixelPacket
618 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
619 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
621 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
626 indexes=GetCacheViewVirtualIndexQueue(image_view);
627 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
628 for (x=0; x < (long) image->columns; x++)
633 if ((channel & OpacityChannel) != 0)
635 if (image->matte != MagickFalse)
636 alpha=(MagickRealType) (QuantumScale*(GetAlphaPixelComponent(p)));
637 if (reconstruct_image->matte != MagickFalse)
638 beta=(MagickRealType) (QuantumScale*GetAlphaPixelComponent(q));
640 if ((channel & RedChannel) != 0)
642 distance=fabs(alpha*p->red-beta*q->red);
643 distortion[RedChannel]+=distance;
644 distortion[AllChannels]+=distance;
645 mean_error+=distance*distance;
646 if (distance > maximum_error)
647 maximum_error=distance;
650 if ((channel & GreenChannel) != 0)
652 distance=fabs(alpha*p->green-beta*q->green);
653 distortion[GreenChannel]+=distance;
654 distortion[AllChannels]+=distance;
655 mean_error+=distance*distance;
656 if (distance > maximum_error)
657 maximum_error=distance;
660 if ((channel & BlueChannel) != 0)
662 distance=fabs(alpha*p->blue-beta*q->blue);
663 distortion[BlueChannel]+=distance;
664 distortion[AllChannels]+=distance;
665 mean_error+=distance*distance;
666 if (distance > maximum_error)
667 maximum_error=distance;
670 if (((channel & OpacityChannel) != 0) &&
671 (image->matte != MagickFalse))
673 distance=fabs((double) p->opacity-q->opacity);
674 distortion[OpacityChannel]+=distance;
675 distortion[AllChannels]+=distance;
676 mean_error+=distance*distance;
677 if (distance > maximum_error)
678 maximum_error=distance;
681 if (((channel & IndexChannel) != 0) &&
682 (image->colorspace == CMYKColorspace) &&
683 (reconstruct_image->colorspace == CMYKColorspace))
685 distance=fabs(alpha*indexes[x]-beta*reconstruct_indexes[x]);
686 distortion[BlackChannel]+=distance;
687 distortion[AllChannels]+=distance;
688 mean_error+=distance*distance;
689 if (distance > maximum_error)
690 maximum_error=distance;
697 reconstruct_view=DestroyCacheView(reconstruct_view);
698 image_view=DestroyCacheView(image_view);
699 image->error.mean_error_per_pixel=distortion[AllChannels]/area;
700 image->error.normalized_mean_error=QuantumScale*QuantumScale*mean_error/area;
701 image->error.normalized_maximum_error=QuantumScale*maximum_error;
705 static MagickBooleanType GetMeanSquaredError(const Image *image,
706 const Image *reconstruct_image,const ChannelType channel,
707 double *distortion,ExceptionInfo *exception)
723 image_view=AcquireCacheView(image);
724 reconstruct_view=AcquireCacheView(reconstruct_image);
725 #if defined(MAGICKCORE_OPENMP_SUPPORT)
726 #pragma omp parallel for schedule(dynamic,4) shared(status)
728 for (y=0; y < (long) image->rows; y++)
731 channel_distortion[AllChannels+1];
733 register const IndexPacket
735 *restrict reconstruct_indexes;
737 register const PixelPacket
745 if (status == MagickFalse)
747 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
748 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,
749 reconstruct_image->columns,1,exception);
750 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
755 indexes=GetCacheViewVirtualIndexQueue(image_view);
756 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
757 (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
758 for (x=0; x < (long) image->columns; x++)
763 if ((channel & RedChannel) != 0)
765 distance=QuantumScale*(p->red-(MagickRealType) q->red);
766 channel_distortion[RedChannel]+=distance*distance;
767 channel_distortion[AllChannels]+=distance*distance;
769 if ((channel & GreenChannel) != 0)
771 distance=QuantumScale*(p->green-(MagickRealType) q->green);
772 channel_distortion[GreenChannel]+=distance*distance;
773 channel_distortion[AllChannels]+=distance*distance;
775 if ((channel & BlueChannel) != 0)
777 distance=QuantumScale*(p->blue-(MagickRealType) q->blue);
778 channel_distortion[BlueChannel]+=distance*distance;
779 channel_distortion[AllChannels]+=distance*distance;
781 if (((channel & OpacityChannel) != 0) &&
782 (image->matte != MagickFalse))
784 distance=QuantumScale*(p->opacity-(MagickRealType) q->opacity);
785 channel_distortion[OpacityChannel]+=distance*distance;
786 channel_distortion[AllChannels]+=distance*distance;
788 if (((channel & IndexChannel) != 0) &&
789 (image->colorspace == CMYKColorspace) &&
790 (reconstruct_image->colorspace == CMYKColorspace))
792 distance=QuantumScale*(indexes[x]-(MagickRealType)
793 reconstruct_indexes[x]);
794 channel_distortion[BlackChannel]+=distance*distance;
795 channel_distortion[AllChannels]+=distance*distance;
800 #if defined(MAGICKCORE_OPENMP_SUPPORT)
801 #pragma omp critical (MagickCore_GetMeanSquaredError)
803 for (i=0; i <= (long) AllChannels; i++)
804 distortion[i]+=channel_distortion[i];
806 reconstruct_view=DestroyCacheView(reconstruct_view);
807 image_view=DestroyCacheView(image_view);
808 for (i=0; i <= (long) AllChannels; i++)
809 distortion[i]/=((double) image->columns*image->rows);
810 distortion[AllChannels]/=(double) GetNumberChannels(image,channel);
814 static MagickBooleanType GetPeakAbsoluteError(const Image *image,
815 const Image *reconstruct_image,const ChannelType channel,
816 double *distortion,ExceptionInfo *exception)
829 image_view=AcquireCacheView(image);
830 reconstruct_view=AcquireCacheView(reconstruct_image);
831 #if defined(MAGICKCORE_OPENMP_SUPPORT)
832 #pragma omp parallel for schedule(dynamic,4) shared(status)
834 for (y=0; y < (long) image->rows; y++)
837 channel_distortion[AllChannels+1];
839 register const IndexPacket
841 *restrict reconstruct_indexes;
843 register const PixelPacket
851 if (status == MagickFalse)
853 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
854 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,
855 reconstruct_image->columns,1,exception);
856 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
861 indexes=GetCacheViewVirtualIndexQueue(image_view);
862 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
863 (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
864 for (x=0; x < (long) image->columns; x++)
869 if ((channel & RedChannel) != 0)
871 distance=QuantumScale*fabs(p->red-(double) q->red);
872 if (distance > channel_distortion[RedChannel])
873 channel_distortion[RedChannel]=distance;
874 if (distance > channel_distortion[AllChannels])
875 channel_distortion[AllChannels]=distance;
877 if ((channel & GreenChannel) != 0)
879 distance=QuantumScale*fabs(p->green-(double) q->green);
880 if (distance > channel_distortion[GreenChannel])
881 channel_distortion[GreenChannel]=distance;
882 if (distance > channel_distortion[AllChannels])
883 channel_distortion[AllChannels]=distance;
885 if ((channel & BlueChannel) != 0)
887 distance=QuantumScale*fabs(p->blue-(double) q->blue);
888 if (distance > channel_distortion[BlueChannel])
889 channel_distortion[BlueChannel]=distance;
890 if (distance > channel_distortion[AllChannels])
891 channel_distortion[AllChannels]=distance;
893 if (((channel & OpacityChannel) != 0) &&
894 (image->matte != MagickFalse))
896 distance=QuantumScale*fabs(p->opacity-(double) q->opacity);
897 if (distance > channel_distortion[OpacityChannel])
898 channel_distortion[OpacityChannel]=distance;
899 if (distance > channel_distortion[AllChannels])
900 channel_distortion[AllChannels]=distance;
902 if (((channel & IndexChannel) != 0) &&
903 (image->colorspace == CMYKColorspace) &&
904 (reconstruct_image->colorspace == CMYKColorspace))
906 distance=QuantumScale*fabs(indexes[x]-(double)
907 reconstruct_indexes[x]);
908 if (distance > channel_distortion[BlackChannel])
909 channel_distortion[BlackChannel]=distance;
910 if (distance > channel_distortion[AllChannels])
911 channel_distortion[AllChannels]=distance;
916 #if defined(MAGICKCORE_OPENMP_SUPPORT)
917 #pragma omp critical (MagickCore_GetPeakAbsoluteError)
919 for (i=0; i <= (long) AllChannels; i++)
920 if (channel_distortion[i] > distortion[i])
921 distortion[i]=channel_distortion[i];
923 reconstruct_view=DestroyCacheView(reconstruct_view);
924 image_view=DestroyCacheView(image_view);
928 static MagickBooleanType GetPeakSignalToNoiseRatio(const Image *image,
929 const Image *reconstruct_image,const ChannelType channel,
930 double *distortion,ExceptionInfo *exception)
935 status=GetMeanSquaredError(image,reconstruct_image,channel,distortion,
937 if ((channel & RedChannel) != 0)
938 distortion[RedChannel]=20.0*log10((double) 1.0/sqrt(
939 distortion[RedChannel]));
940 if ((channel & GreenChannel) != 0)
941 distortion[GreenChannel]=20.0*log10((double) 1.0/sqrt(
942 distortion[GreenChannel]));
943 if ((channel & BlueChannel) != 0)
944 distortion[BlueChannel]=20.0*log10((double) 1.0/sqrt(
945 distortion[BlueChannel]));
946 if (((channel & OpacityChannel) != 0) &&
947 (image->matte != MagickFalse))
948 distortion[OpacityChannel]=20.0*log10((double) 1.0/sqrt(
949 distortion[OpacityChannel]));
950 if (((channel & IndexChannel) != 0) &&
951 (image->colorspace == CMYKColorspace))
952 distortion[BlackChannel]=20.0*log10((double) 1.0/sqrt(
953 distortion[BlackChannel]));
954 distortion[AllChannels]=20.0*log10((double) 1.0/sqrt(
955 distortion[AllChannels]));
959 static MagickBooleanType GetRootMeanSquaredError(const Image *image,
960 const Image *reconstruct_image,const ChannelType channel,
961 double *distortion,ExceptionInfo *exception)
966 status=GetMeanSquaredError(image,reconstruct_image,channel,distortion,
968 if ((channel & RedChannel) != 0)
969 distortion[RedChannel]=sqrt(distortion[RedChannel]);
970 if ((channel & GreenChannel) != 0)
971 distortion[GreenChannel]=sqrt(distortion[GreenChannel]);
972 if ((channel & BlueChannel) != 0)
973 distortion[BlueChannel]=sqrt(distortion[BlueChannel]);
974 if (((channel & OpacityChannel) != 0) &&
975 (image->matte != MagickFalse))
976 distortion[OpacityChannel]=sqrt(distortion[OpacityChannel]);
977 if (((channel & IndexChannel) != 0) &&
978 (image->colorspace == CMYKColorspace))
979 distortion[BlackChannel]=sqrt(distortion[BlackChannel]);
980 distortion[AllChannels]=sqrt(distortion[AllChannels]);
984 MagickExport MagickBooleanType GetImageChannelDistortion(Image *image,
985 const Image *reconstruct_image,const ChannelType channel,
986 const MetricType metric,double *distortion,ExceptionInfo *exception)
997 assert(image != (Image *) NULL);
998 assert(image->signature == MagickSignature);
999 if (image->debug != MagickFalse)
1000 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1001 assert(reconstruct_image != (const Image *) NULL);
1002 assert(reconstruct_image->signature == MagickSignature);
1003 assert(distortion != (double *) NULL);
1005 if (image->debug != MagickFalse)
1006 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1007 if ((reconstruct_image->columns != image->columns) ||
1008 (reconstruct_image->rows != image->rows))
1009 ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename);
1011 Get image distortion.
1013 length=AllChannels+1UL;
1014 channel_distortion=(double *) AcquireQuantumMemory(length,
1015 sizeof(*channel_distortion));
1016 if (channel_distortion == (double *) NULL)
1017 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1018 (void) ResetMagickMemory(channel_distortion,0,length*
1019 sizeof(*channel_distortion));
1022 case AbsoluteErrorMetric:
1024 status=GetAbsoluteError(image,reconstruct_image,channel,
1025 channel_distortion,exception);
1028 case MeanAbsoluteErrorMetric:
1030 status=GetMeanAbsoluteError(image,reconstruct_image,channel,
1031 channel_distortion,exception);
1034 case MeanErrorPerPixelMetric:
1036 status=GetMeanErrorPerPixel(image,reconstruct_image,channel,
1037 channel_distortion,exception);
1040 case MeanSquaredErrorMetric:
1042 status=GetMeanSquaredError(image,reconstruct_image,channel,
1043 channel_distortion,exception);
1046 case PeakAbsoluteErrorMetric:
1049 status=GetPeakAbsoluteError(image,reconstruct_image,channel,
1050 channel_distortion,exception);
1053 case PeakSignalToNoiseRatioMetric:
1055 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,channel,
1056 channel_distortion,exception);
1059 case RootMeanSquaredErrorMetric:
1061 status=GetRootMeanSquaredError(image,reconstruct_image,channel,
1062 channel_distortion,exception);
1066 *distortion=channel_distortion[AllChannels];
1067 channel_distortion=(double *) RelinquishMagickMemory(channel_distortion);
1072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1076 % G e t I m a g e C h a n n e l D i s t o r t i o n s %
1080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082 % GetImageChannelDistrortion() compares the image channels of an image to a
1083 % reconstructed image and returns the specified distortion metric for each
1086 % The format of the CompareImageChannels method is:
1088 % double *GetImageChannelDistortions(const Image *image,
1089 % const Image *reconstruct_image,const MetricType metric,
1090 % ExceptionInfo *exception)
1092 % A description of each parameter follows:
1094 % o image: the image.
1096 % o reconstruct_image: the reconstruct image.
1098 % o metric: the metric.
1100 % o exception: return any errors or warnings in this structure.
1103 MagickExport double *GetImageChannelDistortions(Image *image,
1104 const Image *reconstruct_image,const MetricType metric,
1105 ExceptionInfo *exception)
1108 *channel_distortion;
1116 assert(image != (Image *) NULL);
1117 assert(image->signature == MagickSignature);
1118 if (image->debug != MagickFalse)
1119 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1120 assert(reconstruct_image != (const Image *) NULL);
1121 assert(reconstruct_image->signature == MagickSignature);
1122 if (image->debug != MagickFalse)
1123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1124 if ((reconstruct_image->columns != image->columns) ||
1125 (reconstruct_image->rows != image->rows))
1127 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1128 ImageError,"ImageSizeDiffers","`%s'",image->filename);
1129 return((double *) NULL);
1132 Get image distortion.
1134 length=AllChannels+1UL;
1135 channel_distortion=(double *) AcquireQuantumMemory(length,
1136 sizeof(*channel_distortion));
1137 if (channel_distortion == (double *) NULL)
1138 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1139 (void) ResetMagickMemory(channel_distortion,0,length*
1140 sizeof(*channel_distortion));
1143 case AbsoluteErrorMetric:
1145 status=GetAbsoluteError(image,reconstruct_image,AllChannels,
1146 channel_distortion,exception);
1149 case MeanAbsoluteErrorMetric:
1151 status=GetMeanAbsoluteError(image,reconstruct_image,AllChannels,
1152 channel_distortion,exception);
1155 case MeanErrorPerPixelMetric:
1157 status=GetMeanErrorPerPixel(image,reconstruct_image,AllChannels,
1158 channel_distortion,exception);
1161 case MeanSquaredErrorMetric:
1163 status=GetMeanSquaredError(image,reconstruct_image,AllChannels,
1164 channel_distortion,exception);
1167 case PeakAbsoluteErrorMetric:
1170 status=GetPeakAbsoluteError(image,reconstruct_image,AllChannels,
1171 channel_distortion,exception);
1174 case PeakSignalToNoiseRatioMetric:
1176 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,AllChannels,
1177 channel_distortion,exception);
1180 case RootMeanSquaredErrorMetric:
1182 status=GetRootMeanSquaredError(image,reconstruct_image,AllChannels,
1183 channel_distortion,exception);
1187 return(channel_distortion);
1191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1195 % I s I m a g e s E q u a l %
1199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201 % IsImagesEqual() measures the difference between colors at each pixel
1202 % location of two images. A value other than 0 means the colors match
1203 % exactly. Otherwise an error measure is computed by summing over all
1204 % pixels in an image the distance squared in RGB space between each image
1205 % pixel and its corresponding pixel in the reconstruct image. The error
1206 % measure is assigned to these image members:
1208 % o mean_error_per_pixel: The mean error for any single pixel in
1211 % o normalized_mean_error: The normalized mean quantization error for
1212 % any single pixel in the image. This distance measure is normalized to
1213 % a range between 0 and 1. It is independent of the range of red, green,
1214 % and blue values in the image.
1216 % o normalized_maximum_error: The normalized maximum quantization
1217 % error for any single pixel in the image. This distance measure is
1218 % normalized to a range between 0 and 1. It is independent of the range
1219 % of red, green, and blue values in your image.
1221 % A small normalized mean square error, accessed as
1222 % image->normalized_mean_error, suggests the images are very similar in
1223 % spatial layout and color.
1225 % The format of the IsImagesEqual method is:
1227 % MagickBooleanType IsImagesEqual(Image *image,
1228 % const Image *reconstruct_image)
1230 % A description of each parameter follows.
1232 % o image: the image.
1234 % o reconstruct_image: the reconstruct image.
1237 MagickExport MagickBooleanType IsImagesEqual(Image *image,
1238 const Image *reconstruct_image)
1257 mean_error_per_pixel;
1259 assert(image != (Image *) NULL);
1260 assert(image->signature == MagickSignature);
1261 assert(reconstruct_image != (const Image *) NULL);
1262 assert(reconstruct_image->signature == MagickSignature);
1263 if ((reconstruct_image->columns != image->columns) ||
1264 (reconstruct_image->rows != image->rows))
1265 ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename);
1268 mean_error_per_pixel=0.0;
1270 exception=(&image->exception);
1271 image_view=AcquireCacheView(image);
1272 reconstruct_view=AcquireCacheView(reconstruct_image);
1273 for (y=0; y < (long) image->rows; y++)
1275 register const IndexPacket
1277 *restrict reconstruct_indexes;
1279 register const PixelPacket
1286 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1287 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
1289 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
1291 indexes=GetCacheViewVirtualIndexQueue(image_view);
1292 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
1293 for (x=0; x < (long) image->columns; x++)
1298 distance=fabs(p->red-(double) q->red);
1299 mean_error_per_pixel+=distance;
1300 mean_error+=distance*distance;
1301 if (distance > maximum_error)
1302 maximum_error=distance;
1304 distance=fabs(p->green-(double) q->green);
1305 mean_error_per_pixel+=distance;
1306 mean_error+=distance*distance;
1307 if (distance > maximum_error)
1308 maximum_error=distance;
1310 distance=fabs(p->blue-(double) q->blue);
1311 mean_error_per_pixel+=distance;
1312 mean_error+=distance*distance;
1313 if (distance > maximum_error)
1314 maximum_error=distance;
1316 if (image->matte != MagickFalse)
1318 distance=fabs(p->opacity-(double) q->opacity);
1319 mean_error_per_pixel+=distance;
1320 mean_error+=distance*distance;
1321 if (distance > maximum_error)
1322 maximum_error=distance;
1325 if ((image->colorspace == CMYKColorspace) &&
1326 (reconstruct_image->colorspace == CMYKColorspace))
1328 distance=fabs(indexes[x]-(double) reconstruct_indexes[x]);
1329 mean_error_per_pixel+=distance;
1330 mean_error+=distance*distance;
1331 if (distance > maximum_error)
1332 maximum_error=distance;
1339 reconstruct_view=DestroyCacheView(reconstruct_view);
1340 image_view=DestroyCacheView(image_view);
1341 image->error.mean_error_per_pixel=(double) (mean_error_per_pixel/area);
1342 image->error.normalized_mean_error=(double) (QuantumScale*QuantumScale*
1344 image->error.normalized_maximum_error=(double) (QuantumScale*maximum_error);
1345 status=image->error.mean_error_per_pixel == 0.0 ? MagickTrue : MagickFalse;
1350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354 % S i m i l a r i t y I m a g e %
1358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 % SimilarityImage() compares the reference image of the image and returns the
1361 % best match offset. In addition, it returns a similarity image such that an
1362 % exact match location is completely white and if none of the pixels match,
1363 % black, otherwise some gray level in-between.
1365 % The format of the SimilarityImageImage method is:
1367 % Image *SimilarityImage(const Image *image,const Image *reference,
1368 % RectangleInfo *offset,double *similarity,ExceptionInfo *exception)
1370 % A description of each parameter follows:
1372 % o image: the image.
1374 % o reference: find an area of the image that closely resembles this image.
1376 % o the best match offset of the reference image within the image.
1378 % o similarity: the computed similarity between the images.
1380 % o exception: return any errors or warnings in this structure.
1384 static double GetSimilarityMetric(const Image *image,const Image *reference,
1385 const long x_offset,const long y_offset,ExceptionInfo *exception)
1401 Compute the similarity in pixels between two images.
1405 image_view=AcquireCacheView(image);
1406 reference_view=AcquireCacheView(reference);
1407 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1408 #pragma omp parallel for schedule(dynamic,4) shared(status)
1410 for (y=0; y < (long) reference->rows; y++)
1412 register const IndexPacket
1414 *restrict reference_indexes;
1416 register const PixelPacket
1423 if (status == MagickFalse)
1425 p=GetCacheViewVirtualPixels(image_view,x_offset,y_offset+y,
1426 reference->columns,1,exception);
1427 q=GetCacheViewVirtualPixels(reference_view,0,y,reference->columns,1,
1429 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
1434 indexes=GetCacheViewVirtualIndexQueue(image_view);
1435 reference_indexes=GetCacheViewVirtualIndexQueue(reference_view);
1436 for (x=0; x < (long) reference->columns; x++)
1444 thread_similarity=0.0;
1445 distance=QuantumScale*(p->red-(MagickRealType) q->red);
1446 thread_similarity+=distance*distance;
1447 distance=QuantumScale*(p->green-(MagickRealType) q->green);
1448 thread_similarity+=distance*distance;
1449 distance=QuantumScale*(p->blue-(MagickRealType) q->blue);
1450 thread_similarity+=distance*distance;
1451 if ((image->matte != MagickFalse) && (reference->matte != MagickFalse))
1453 distance=QuantumScale*(p->opacity-(MagickRealType) q->opacity);
1454 thread_similarity+=distance*distance;
1456 if ((image->colorspace == CMYKColorspace) &&
1457 (reference->colorspace == CMYKColorspace))
1459 distance=QuantumScale*(indexes[x]-(MagickRealType)
1460 reference_indexes[x]);
1461 thread_similarity+=distance*distance;
1463 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1464 #pragma omp critical (MagickCore_GetSimilarityMetric)
1466 similarity+=thread_similarity;
1471 reference_view=DestroyCacheView(reference_view);
1472 image_view=DestroyCacheView(image_view);
1473 if (status == MagickFalse)
1475 similarity/=((double) reference->columns*reference->rows);
1476 similarity/=(double) GetNumberChannels(reference,AllChannels);
1477 return(sqrt(similarity));
1480 MagickExport Image *SimilarityImage(Image *image,const Image *reference,
1481 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
1483 #define SimilarityImageTag "Similarity/Image"
1498 assert(image != (const Image *) NULL);
1499 assert(image->signature == MagickSignature);
1500 if (image->debug != MagickFalse)
1501 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1502 assert(exception != (ExceptionInfo *) NULL);
1503 assert(exception->signature == MagickSignature);
1504 assert(offset != (RectangleInfo *) NULL);
1505 SetGeometry(reference,offset);
1506 *similarity_metric=1.0;
1507 if ((reference->columns > image->columns) ||
1508 (reference->rows > image->rows))
1509 ThrowImageException(ImageError,"ImageSizeDiffers");
1510 similarity_image=CloneImage(image,image->columns-reference->columns+1,
1511 image->rows-reference->rows+1,MagickTrue,exception);
1512 if (similarity_image == (Image *) NULL)
1513 return((Image *) NULL);
1514 if (SetImageStorageClass(similarity_image,DirectClass) == MagickFalse)
1516 InheritException(exception,&similarity_image->exception);
1517 similarity_image=DestroyImage(similarity_image);
1518 return((Image *) NULL);
1521 Measure similarity of reference image against image.
1525 similarity_view=AcquireCacheView(similarity_image);
1526 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1527 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1529 for (y=0; y < (long) (image->rows-reference->rows+1); y++)
1537 register PixelPacket
1540 if (status == MagickFalse)
1542 q=GetCacheViewAuthenticPixels(similarity_view,0,y,
1543 similarity_image->columns,1,exception);
1544 if (q == (const PixelPacket *) NULL)
1549 for (x=0; x < (long) (image->columns-reference->columns+1); x++)
1551 similarity=GetSimilarityMetric(image,reference,x,y,exception);
1552 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1553 #pragma omp critical (MagickCore_SimilarityImage)
1555 if (similarity < *similarity_metric)
1557 *similarity_metric=similarity;
1561 q->red=ClampToQuantum(QuantumRange-QuantumRange*similarity);
1566 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
1568 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1573 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1574 #pragma omp critical (MagickCore_SimilarityImage)
1576 proceed=SetImageProgress(image,SimilarityImageTag,progress++,
1578 if (proceed == MagickFalse)
1582 similarity_view=DestroyCacheView(similarity_view);
1583 return(similarity_image);