MagickCore 7.1.2-37
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
compare.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
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 %
11% %
12% %
13% MagickCore Image Comparison Methods %
14% %
15% Software Design %
16% Cristy %
17% December 2003 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/license/ %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39␌
40/*
41 Include declarations.
42*/
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/compare-private.h"
55#include "MagickCore/composite-private.h"
56#include "MagickCore/constitute.h"
57#include "MagickCore/distort.h"
58#include "MagickCore/exception-private.h"
59#include "MagickCore/enhance.h"
60#include "MagickCore/fourier.h"
61#include "MagickCore/geometry.h"
62#include "MagickCore/image-private.h"
63#include "MagickCore/list.h"
64#include "MagickCore/log.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/monitor.h"
67#include "MagickCore/monitor-private.h"
68#include "MagickCore/option.h"
69#include "MagickCore/pixel-accessor.h"
70#include "MagickCore/property.h"
71#include "MagickCore/registry.h"
72#include "MagickCore/resource_.h"
73#include "MagickCore/string_.h"
74#include "MagickCore/statistic.h"
75#include "MagickCore/statistic-private.h"
76#include "MagickCore/string-private.h"
77#include "MagickCore/thread-private.h"
78#include "MagickCore/threshold.h"
79#include "MagickCore/transform.h"
80#include "MagickCore/utility.h"
81#include "MagickCore/version.h"
82␌
83/*
84%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85% %
86% %
87% %
88% C o m p a r e I m a g e s %
89% %
90% %
91% %
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93%
94% CompareImages() compares one or more pixel channels of an image to a
95% reconstructed image and returns the difference image.
96%
97% The format of the CompareImages method is:
98%
99% Image *CompareImages(const Image *image,const Image *reconstruct_image,
100% const MetricType metric,double *distortion,ExceptionInfo *exception)
101%
102% A description of each parameter follows:
103%
104% o image: the image.
105%
106% o reconstruct_image: the reconstruction image.
107%
108% o metric: the metric.
109%
110% o distortion: the computed distortion between the images.
111%
112% o exception: return any errors or warnings in this structure.
113%
114*/
115MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
116 const MetricType metric,double *distortion,ExceptionInfo *exception)
117{
118 CacheView
119 *highlight_view,
120 *image_view,
121 *reconstruct_view;
122
123 const char
124 *artifact;
125
126 Image
127 *clone_image,
128 *difference_image,
129 *highlight_image;
130
131 MagickBooleanType
132 status = MagickTrue;
133
134 PixelInfo
135 highlight,
136 lowlight,
137 masklight;
138
139 RectangleInfo
140 geometry;
141
142 size_t
143 columns,
144 rows;
145
146 ssize_t
147 y;
148
149 assert(image != (Image *) NULL);
150 assert(image->signature == MagickCoreSignature);
151 assert(reconstruct_image != (const Image *) NULL);
152 assert(reconstruct_image->signature == MagickCoreSignature);
153 assert(distortion != (double *) NULL);
154 if (IsEventLogging() != MagickFalse)
155 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
156 *distortion=0.0;
157 status=GetImageDistortion(image,reconstruct_image,metric,distortion,
158 exception);
159 if (status == MagickFalse)
160 return((Image *) NULL);
161 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
162 SetGeometry(image,&geometry);
163 geometry.width=columns;
164 geometry.height=rows;
165 clone_image=CloneImage(image,0,0,MagickTrue,exception);
166 if (clone_image == (Image *) NULL)
167 return((Image *) NULL);
168 (void) SetImageMask(clone_image,ReadPixelMask,(Image *) NULL,exception);
169 difference_image=ExtentImage(clone_image,&geometry,exception);
170 clone_image=DestroyImage(clone_image);
171 if (difference_image == (Image *) NULL)
172 return((Image *) NULL);
173 (void) ResetImagePage(difference_image,"0x0+0+0");
174 (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel,exception);
175 highlight_image=CloneImage(image,columns,rows,MagickTrue,exception);
176 if (highlight_image == (Image *) NULL)
177 {
178 difference_image=DestroyImage(difference_image);
179 return((Image *) NULL);
180 }
181 status=SetImageStorageClass(highlight_image,DirectClass,exception);
182 if (status == MagickFalse)
183 {
184 difference_image=DestroyImage(difference_image);
185 highlight_image=DestroyImage(highlight_image);
186 return((Image *) NULL);
187 }
188 (void) SetImageMask(highlight_image,ReadPixelMask,(Image *) NULL,exception);
189 (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel,exception);
190 (void) QueryColorCompliance("#f1001ecc",AllCompliance,&highlight,exception);
191 artifact=GetImageArtifact(image,"compare:highlight-color");
192 if (artifact != (const char *) NULL)
193 (void) QueryColorCompliance(artifact,AllCompliance,&highlight,exception);
194 (void) QueryColorCompliance("#ffffffcc",AllCompliance,&lowlight,exception);
195 artifact=GetImageArtifact(image,"compare:lowlight-color");
196 if (artifact != (const char *) NULL)
197 (void) QueryColorCompliance(artifact,AllCompliance,&lowlight,exception);
198 (void) QueryColorCompliance("#888888cc",AllCompliance,&masklight,exception);
199 artifact=GetImageArtifact(image,"compare:masklight-color");
200 if (artifact != (const char *) NULL)
201 (void) QueryColorCompliance(artifact,AllCompliance,&masklight,exception);
202 /*
203 Generate difference image.
204 */
205 image_view=AcquireVirtualCacheView(image,exception);
206 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
207 highlight_view=AcquireAuthenticCacheView(highlight_image,exception);
208#if defined(MAGICKCORE_OPENMP_SUPPORT)
209 #pragma omp parallel for schedule(static) shared(status) \
210 magick_number_threads(image,highlight_image,rows,1)
211#endif
212 for (y=0; y < (ssize_t) rows; y++)
213 {
214 const Quantum
215 *magick_restrict p,
216 *magick_restrict q;
217
218 MagickBooleanType
219 sync;
220
221 Quantum
222 *magick_restrict r;
223
224 ssize_t
225 x;
226
227 if (status == MagickFalse)
228 continue;
229 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
230 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
231 r=QueueCacheViewAuthenticPixels(highlight_view,0,y,columns,1,exception);
232 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL) ||
233 (r == (Quantum *) NULL))
234 {
235 status=MagickFalse;
236 continue;
237 }
238 for (x=0; x < (ssize_t) columns; x++)
239 {
240 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
241 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
242 {
243 SetPixelViaPixelInfo(highlight_image,&masklight,r);
244 p+=(ptrdiff_t) GetPixelChannels(image);
245 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
246 r+=(ptrdiff_t) GetPixelChannels(highlight_image);
247 continue;
248 }
249 if (IsFuzzyEquivalencePixel(image,p,reconstruct_image,q) == MagickFalse)
250 SetPixelViaPixelInfo(highlight_image,&highlight,r);
251 else
252 SetPixelViaPixelInfo(highlight_image,&lowlight,r);
253 p+=(ptrdiff_t) GetPixelChannels(image);
254 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
255 r+=(ptrdiff_t) GetPixelChannels(highlight_image);
256 }
257 sync=SyncCacheViewAuthenticPixels(highlight_view,exception);
258 if (sync == MagickFalse)
259 status=MagickFalse;
260 }
261 highlight_view=DestroyCacheView(highlight_view);
262 reconstruct_view=DestroyCacheView(reconstruct_view);
263 image_view=DestroyCacheView(image_view);
264 if ((status != MagickFalse) && (difference_image != (Image *) NULL))
265 status=CompositeImage(difference_image,highlight_image,image->compose,
266 MagickTrue,0,0,exception);
267 highlight_image=DestroyImage(highlight_image);
268 if (status == MagickFalse)
269 difference_image=DestroyImage(difference_image);
270 return(difference_image);
271}
272␌
273/*
274%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275% %
276% %
277% %
278% G e t I m a g e D i s t o r t i o n %
279% %
280% %
281% %
282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283%
284% GetImageDistortion() compares one or more pixel channels of an image to a
285% reconstructed image and returns the specified distortion metric.
286%
287% The format of the GetImageDistortion method is:
288%
289% MagickBooleanType GetImageDistortion(const Image *image,
290% const Image *reconstruct_image,const MetricType metric,
291% double *distortion,ExceptionInfo *exception)
292%
293% A description of each parameter follows:
294%
295% o image: the image.
296%
297% o reconstruct_image: the reconstruction image.
298%
299% o metric: the metric.
300%
301% o distortion: the computed distortion between the images.
302%
303% o exception: return any errors or warnings in this structure.
304%
305*/
306
307static MagickBooleanType GetAESimilarity(const Image *image,
308 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
309{
310 CacheView
311 *image_view,
312 *reconstruct_view;
313
314 double
315 area,
316 fuzz;
317
318 MagickBooleanType
319 status = MagickTrue;
320
321 size_t
322 columns,
323 rows;
324
325 ssize_t
326 channels = 0,
327 k,
328 y;
329
330 /*
331 Compute the absolute error similarity.
332 */
333 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
334 (void) memset(similarity,0,(MaxPixelChannels+1)*sizeof(*similarity));
335 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
336 image_view=AcquireVirtualCacheView(image,exception);
337 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
338#if defined(MAGICKCORE_OPENMP_SUPPORT)
339 #pragma omp parallel for schedule(static) shared(similarity,status) \
340 magick_number_threads(image,image,rows,1)
341#endif
342 for (y=0; y < (ssize_t) rows; y++)
343 {
344 const Quantum
345 *magick_restrict p,
346 *magick_restrict q;
347
348 double
349 channel_similarity[MaxPixelChannels+1] = { 0.0 };
350
351 ssize_t
352 x;
353
354 if (status == MagickFalse)
355 continue;
356 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
357 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
358 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
359 {
360 status=MagickFalse;
361 continue;
362 }
363 for (x=0; x < (ssize_t) columns; x++)
364 {
365 double
366 Da,
367 Sa;
368
369 ssize_t
370 i;
371
372 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
373 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
374 {
375 p+=(ptrdiff_t) GetPixelChannels(image);
376 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
377 continue;
378 }
379 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
380 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
381 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
382 {
383 double
384 error;
385
386 PixelChannel channel = GetPixelChannelChannel(image,i);
387 PixelTrait traits = GetPixelChannelTraits(image,channel);
388 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
389 channel);
390 if (((traits & UpdatePixelTrait) == 0) ||
391 ((reconstruct_traits & UpdatePixelTrait) == 0))
392 continue;
393 if (channel == AlphaPixelChannel)
394 error=(double) p[i]-(double) GetPixelChannel(reconstruct_image,
395 channel,q);
396 else
397 error=Sa*(double) p[i]-Da*(double) GetPixelChannel(reconstruct_image,channel,
398 q);
399 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
400 {
401 double ae = fabs(QuantumScale*error);
402 channel_similarity[i]+=ae;
403 channel_similarity[CompositePixelChannel]+=ae;
404 }
405 }
406 p+=(ptrdiff_t) GetPixelChannels(image);
407 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
408 }
409#if defined(MAGICKCORE_OPENMP_SUPPORT)
410 #pragma omp critical (MagickCore_GetAESimilarity)
411#endif
412 {
413 ssize_t
414 j;
415
416 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
417 {
418 PixelChannel channel = GetPixelChannelChannel(image,j);
419 PixelTrait traits = GetPixelChannelTraits(image,channel);
420 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
421 channel);
422 if (((traits & UpdatePixelTrait) == 0) ||
423 ((reconstruct_traits & UpdatePixelTrait) == 0))
424 continue;
425 similarity[j]+=channel_similarity[j];
426 }
427 similarity[CompositePixelChannel]+=
428 channel_similarity[CompositePixelChannel];
429 }
430 }
431 reconstruct_view=DestroyCacheView(reconstruct_view);
432 image_view=DestroyCacheView(image_view);
433 area=MagickSafeReciprocal((double) columns*(double) rows);
434 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
435 {
436 PixelChannel channel = GetPixelChannelChannel(image,k);
437 PixelTrait traits = GetPixelChannelTraits(image,channel);
438 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
439 channel);
440 if (((traits & UpdatePixelTrait) == 0) ||
441 ((reconstruct_traits & UpdatePixelTrait) == 0))
442 continue;
443 similarity[k]*=area;
444 channels++;
445 }
446 similarity[CompositePixelChannel]*=area;
447 if (channels != 0)
448 similarity[CompositePixelChannel]/=(double) channels;
449 return(status);
450}
451
452static MagickBooleanType GetDPCSimilarity(const Image *image,
453 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
454{
455#define SimilarityImageTag "Similarity/Image"
456
457 CacheView
458 *image_view,
459 *reconstruct_view;
460
461 ChannelStatistics
462 *image_statistics,
463 *reconstruct_statistics;
464
465 double
466 norm[MaxPixelChannels+1] = { 0.0 },
467 reconstruct_norm[MaxPixelChannels+1] = { 0.0 };
468
469 MagickBooleanType
470 status = MagickTrue;
471
472 MagickOffsetType
473 progress = 0;
474
475 size_t
476 columns,
477 rows;
478
479 ssize_t
480 k,
481 y;
482
483 /*
484 Compute the dot product correlation similarity.
485 */
486 image_statistics=GetImageStatistics(image,exception);
487 reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
488 if ((image_statistics == (ChannelStatistics *) NULL) ||
489 (reconstruct_statistics == (ChannelStatistics *) NULL))
490 {
491 if (image_statistics != (ChannelStatistics *) NULL)
492 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
493 image_statistics);
494 if (reconstruct_statistics != (ChannelStatistics *) NULL)
495 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
496 reconstruct_statistics);
497 return(MagickFalse);
498 }
499 (void) memset(similarity,0,(MaxPixelChannels+1)*sizeof(*similarity));
500 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
501 image_view=AcquireVirtualCacheView(image,exception);
502 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
503#if defined(MAGICKCORE_OPENMP_SUPPORT)
504 #pragma omp parallel for schedule(static) shared(norm,reconstruct_norm,similarity,status) \
505 magick_number_threads(image,image,rows,1)
506#endif
507 for (y=0; y < (ssize_t) rows; y++)
508 {
509 const Quantum
510 *magick_restrict p,
511 *magick_restrict q;
512
513 double
514 channel_norm[MaxPixelChannels+1] = { 0.0 },
515 channel_reconstruct_norm[MaxPixelChannels+1] = { 0.0 },
516 channel_similarity[MaxPixelChannels+1] = { 0.0 };
517
518 ssize_t
519 x;
520
521 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
522 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
523 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
524 {
525 status=MagickFalse;
526 continue;
527 }
528 for (x=0; x < (ssize_t) columns; x++)
529 {
530 double
531 Da,
532 Sa;
533
534 ssize_t
535 i;
536
537 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
538 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
539 {
540 p+=(ptrdiff_t) GetPixelChannels(image);
541 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
542 continue;
543 }
544 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
545 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
546 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
547 {
548 double
549 alpha,
550 beta;
551
552 PixelChannel channel = GetPixelChannelChannel(image,i);
553 PixelTrait traits = GetPixelChannelTraits(image,channel);
554 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
555 channel);
556 if (((traits & UpdatePixelTrait) == 0) ||
557 ((reconstruct_traits & UpdatePixelTrait) == 0))
558 continue;
559 if (channel == AlphaPixelChannel)
560 {
561 alpha=QuantumScale*((double) p[i]-image_statistics[channel].mean);
562 beta=QuantumScale*((double) GetPixelChannel(reconstruct_image,
563 channel,q)-reconstruct_statistics[channel].mean);
564 }
565 else
566 {
567 alpha=QuantumScale*(Sa*(double) p[i]-
568 image_statistics[channel].mean);
569 beta=QuantumScale*(Da*(double) GetPixelChannel(reconstruct_image,channel,
570 q)-reconstruct_statistics[channel].mean);
571 }
572 channel_similarity[i]+=alpha*beta;
573 channel_norm[i]+=alpha*alpha;
574 channel_reconstruct_norm[i]+=beta*beta;
575 }
576 p+=(ptrdiff_t) GetPixelChannels(image);
577 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
578 }
579#if defined(MAGICKCORE_OPENMP_SUPPORT)
580 #pragma omp critical (MagickCore_GetDPCSimilarity)
581#endif
582 {
583 ssize_t
584 j;
585
586 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
587 {
588 PixelChannel channel = GetPixelChannelChannel(image,j);
589 PixelTrait traits = GetPixelChannelTraits(image,channel);
590 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
591 channel);
592 if (((traits & UpdatePixelTrait) == 0) ||
593 ((reconstruct_traits & UpdatePixelTrait) == 0))
594 continue;
595 similarity[j]+=channel_similarity[j];
596 similarity[CompositePixelChannel]+=channel_similarity[j];
597 norm[j]+=channel_norm[j];
598 norm[CompositePixelChannel]+=channel_norm[j];
599 reconstruct_norm[j]+=channel_reconstruct_norm[j];
600 reconstruct_norm[CompositePixelChannel]+=channel_reconstruct_norm[j];
601 }
602 }
603 if (image->progress_monitor != (MagickProgressMonitor) NULL)
604 {
605 MagickBooleanType
606 proceed;
607
608#if defined(MAGICKCORE_OPENMP_SUPPORT)
609 #pragma omp atomic
610#endif
611 progress++;
612 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
613 if (proceed == MagickFalse)
614 {
615 status=MagickFalse;
616 continue;
617 }
618 }
619 }
620 reconstruct_view=DestroyCacheView(reconstruct_view);
621 image_view=DestroyCacheView(image_view);
622 /*
623 Compute dot product correlation: divide by mean.
624 */
625 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
626 {
627 PixelChannel channel = GetPixelChannelChannel(image,k);
628 PixelTrait traits = GetPixelChannelTraits(image,channel);
629 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
630 channel);
631 if (((traits & UpdatePixelTrait) == 0) ||
632 ((reconstruct_traits & UpdatePixelTrait) == 0))
633 continue;
634 similarity[k]*=MagickSafeReciprocal(sqrt(norm[k]*reconstruct_norm[k]));
635 }
636 similarity[CompositePixelChannel]*=MagickSafeReciprocal(sqrt(
637 norm[CompositePixelChannel]*reconstruct_norm[CompositePixelChannel]));
638 /*
639 Free resources.
640 */
641 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
642 reconstruct_statistics);
643 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
644 image_statistics);
645 return(status);
646}
647
648static MagickBooleanType GetFUZZSimilarity(const Image *image,
649 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
650{
651 CacheView
652 *image_view,
653 *reconstruct_view;
654
655 double
656 area = 0.0,
657 fuzz = 0.0;
658
659 MagickBooleanType
660 status = MagickTrue;
661
662 size_t
663 columns,
664 rows;
665
666 ssize_t
667 k,
668 y;
669
670 /*
671 Compute the MSE similarity within tolerance (fuzz).
672 */
673 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
674 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
675 image_view=AcquireVirtualCacheView(image,exception);
676 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
677#if defined(MAGICKCORE_OPENMP_SUPPORT)
678 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
679 magick_number_threads(image,image,rows,1)
680#endif
681 for (y=0; y < (ssize_t) rows; y++)
682 {
683 const Quantum
684 *magick_restrict p,
685 *magick_restrict q;
686
687 double
688 channel_area = 0.0,
689 channel_similarity[MaxPixelChannels+1] = { 0.0 };
690
691 ssize_t
692 x;
693
694 if (status == MagickFalse)
695 continue;
696 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
697 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
698 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
699 {
700 status=MagickFalse;
701 continue;
702 }
703 for (x=0; x < (ssize_t) columns; x++)
704 {
705 double
706 Da,
707 Sa;
708
709 ssize_t
710 i;
711
712 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
713 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
714 {
715 p+=(ptrdiff_t) GetPixelChannels(image);
716 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
717 continue;
718 }
719 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
720 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
721 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
722 {
723 double
724 error;
725
726 PixelChannel channel = GetPixelChannelChannel(image,i);
727 PixelTrait traits = GetPixelChannelTraits(image,channel);
728 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
729 channel);
730 if (((traits & UpdatePixelTrait) == 0) ||
731 ((reconstruct_traits & UpdatePixelTrait) == 0))
732 continue;
733 if (channel == AlphaPixelChannel)
734 error=(double) p[i]-(double) GetPixelChannel(reconstruct_image,
735 channel,q);
736 else
737 error=Sa*(double) p[i]-Da*(double) GetPixelChannel(reconstruct_image,
738 channel,q);
739 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
740 {
741 channel_similarity[i]+=QuantumScale*error*QuantumScale*error;
742 channel_similarity[CompositePixelChannel]+=QuantumScale*error*
743 QuantumScale*error;
744 channel_area++;
745 }
746 }
747 p+=(ptrdiff_t) GetPixelChannels(image);
748 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
749 }
750#if defined(MAGICKCORE_OPENMP_SUPPORT)
751 #pragma omp critical (MagickCore_GetFUZZSimilarity)
752#endif
753 {
754 ssize_t
755 j;
756
757 area+=channel_area;
758 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
759 {
760 PixelChannel channel = GetPixelChannelChannel(image,j);
761 PixelTrait traits = GetPixelChannelTraits(image,channel);
762 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
763 channel);
764 if (((traits & UpdatePixelTrait) == 0) ||
765 ((reconstruct_traits & UpdatePixelTrait) == 0))
766 continue;
767 similarity[j]+=channel_similarity[j];
768 }
769 similarity[CompositePixelChannel]+=
770 channel_similarity[CompositePixelChannel];
771 }
772 }
773 reconstruct_view=DestroyCacheView(reconstruct_view);
774 image_view=DestroyCacheView(image_view);
775 area=MagickSafeReciprocal(area);
776 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
777 {
778 PixelChannel channel = GetPixelChannelChannel(image,k);
779 PixelTrait traits = GetPixelChannelTraits(image,channel);
780 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
781 channel);
782 if (((traits & UpdatePixelTrait) == 0) ||
783 ((reconstruct_traits & UpdatePixelTrait) == 0))
784 continue;
785 similarity[k]*=area;
786 }
787 similarity[CompositePixelChannel]*=area;
788 return(status);
789}
790
791static MagickBooleanType GetMAESimilarity(const Image *image,
792 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
793{
794 CacheView
795 *image_view,
796 *reconstruct_view;
797
798 double
799 area = 0.0;
800
801 MagickBooleanType
802 status = MagickTrue;
803
804 size_t
805 columns,
806 rows;
807
808 ssize_t
809 channels = 0,
810 k,
811 y;
812
813 /*
814 Compute the mean absolute error similarity.
815 */
816 (void) memset(similarity,0,(MaxPixelChannels+1)*sizeof(*similarity));
817 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
818 image_view=AcquireVirtualCacheView(image,exception);
819 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
820#if defined(MAGICKCORE_OPENMP_SUPPORT)
821 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
822 magick_number_threads(image,image,rows,1)
823#endif
824 for (y=0; y < (ssize_t) rows; y++)
825 {
826 const Quantum
827 *magick_restrict p,
828 *magick_restrict q;
829
830 double
831 channel_area = 0.0,
832 channel_similarity[MaxPixelChannels+1] = { 0.0 };
833
834 ssize_t
835 x;
836
837 if (status == MagickFalse)
838 continue;
839 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
840 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
841 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
842 {
843 status=MagickFalse;
844 continue;
845 }
846 for (x=0; x < (ssize_t) columns; x++)
847 {
848 double
849 Da,
850 Sa;
851
852 ssize_t
853 i;
854
855 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
856 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
857 {
858 p+=(ptrdiff_t) GetPixelChannels(image);
859 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
860 continue;
861 }
862 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
863 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
864 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
865 {
866 double
867 error;
868
869 PixelChannel channel = GetPixelChannelChannel(image,i);
870 PixelTrait traits = GetPixelChannelTraits(image,channel);
871 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
872 channel);
873 if (((traits & UpdatePixelTrait) == 0) ||
874 ((reconstruct_traits & UpdatePixelTrait) == 0))
875 continue;
876 if (channel == AlphaPixelChannel)
877 error=QuantumScale*fabs((double) p[i]-(double) GetPixelChannel(
878 reconstruct_image,channel,q));
879 else
880 error=QuantumScale*fabs(Sa*(double) p[i]-Da*(double)
881 GetPixelChannel(reconstruct_image,channel,q));
882 channel_similarity[i]+=error;
883 channel_similarity[CompositePixelChannel]+=error;
884 }
885 channel_area++;
886 p+=(ptrdiff_t) GetPixelChannels(image);
887 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
888 }
889#if defined(MAGICKCORE_OPENMP_SUPPORT)
890 #pragma omp critical (MagickCore_GetMAESimilarity)
891#endif
892 {
893 ssize_t
894 j;
895
896 area+=channel_area;
897 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
898 {
899 PixelChannel channel = GetPixelChannelChannel(image,j);
900 PixelTrait traits = GetPixelChannelTraits(image,channel);
901 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
902 channel);
903 if (((traits & UpdatePixelTrait) == 0) ||
904 ((reconstruct_traits & UpdatePixelTrait) == 0))
905 continue;
906 similarity[j]+=channel_similarity[j];
907 }
908 similarity[CompositePixelChannel]+=
909 channel_similarity[CompositePixelChannel];
910 }
911 }
912 reconstruct_view=DestroyCacheView(reconstruct_view);
913 image_view=DestroyCacheView(image_view);
914 area=MagickSafeReciprocal(area);
915 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
916 {
917 PixelChannel channel = GetPixelChannelChannel(image,k);
918 PixelTrait traits = GetPixelChannelTraits(image,channel);
919 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
920 channel);
921 if (((traits & UpdatePixelTrait) == 0) ||
922 ((reconstruct_traits & UpdatePixelTrait) == 0))
923 continue;
924 similarity[k]*=area;
925 channels++;
926 }
927 similarity[CompositePixelChannel]*=area;
928 if (channels != 0)
929 similarity[CompositePixelChannel]/=(double) channels;
930 return(status);
931}
932
933static MagickBooleanType GetMEPPSimilarity(Image *image,
934 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
935{
936 CacheView
937 *image_view,
938 *reconstruct_view;
939
940 double
941 area = 0.0,
942 maximum_error = -MagickMaximumValue,
943 mean_error = 0.0;
944
945 MagickBooleanType
946 status = MagickTrue;
947
948 size_t
949 columns,
950 rows;
951
952 ssize_t
953 channels = 0,
954 k,
955 y;
956
957 /*
958 Compute the mean error per pixel similarity.
959 */
960 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
961 image_view=AcquireVirtualCacheView(image,exception);
962 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
963#if defined(MAGICKCORE_OPENMP_SUPPORT)
964 #pragma omp parallel for schedule(static) shared(area,similarity,maximum_error,mean_error,status) \
965 magick_number_threads(image,image,rows,1)
966#endif
967 for (y=0; y < (ssize_t) rows; y++)
968 {
969 const Quantum
970 *magick_restrict p,
971 *magick_restrict q;
972
973 double
974 channel_area = 0.0,
975 channel_similarity[MaxPixelChannels+1] = { 0.0 },
976 channel_maximum_error = maximum_error,
977 channel_mean_error = 0.0;
978
979 ssize_t
980 x;
981
982 if (status == MagickFalse)
983 continue;
984 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
985 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
986 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
987 {
988 status=MagickFalse;
989 continue;
990 }
991 for (x=0; x < (ssize_t) columns; x++)
992 {
993 double
994 Da,
995 Sa;
996
997 ssize_t
998 i;
999
1000 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1001 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1002 {
1003 p+=(ptrdiff_t) GetPixelChannels(image);
1004 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1005 continue;
1006 }
1007 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1008 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1009 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1010 {
1011 double
1012 error;
1013
1014 PixelChannel channel = GetPixelChannelChannel(image,i);
1015 PixelTrait traits = GetPixelChannelTraits(image,channel);
1016 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1017 channel);
1018 if (((traits & UpdatePixelTrait) == 0) ||
1019 ((reconstruct_traits & UpdatePixelTrait) == 0))
1020 continue;
1021 if (channel == AlphaPixelChannel)
1022 error=QuantumScale*fabs((double) p[i]-(double) GetPixelChannel(
1023 reconstruct_image,channel,q));
1024 else
1025 error=QuantumScale*fabs(Sa*(double) p[i]-Da*(double)
1026 GetPixelChannel(reconstruct_image,channel,q));
1027 channel_similarity[i]+=error;
1028 channel_similarity[CompositePixelChannel]+=error;
1029 channel_mean_error+=error*error;
1030 if (error > channel_maximum_error)
1031 channel_maximum_error=error;
1032 }
1033 channel_area++;
1034 p+=(ptrdiff_t) GetPixelChannels(image);
1035 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1036 }
1037#if defined(MAGICKCORE_OPENMP_SUPPORT)
1038 #pragma omp critical (MagickCore_GetMEPPSimilarity)
1039#endif
1040 {
1041 ssize_t
1042 j;
1043
1044 area+=channel_area;
1045 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1046 {
1047 PixelChannel channel = GetPixelChannelChannel(image,j);
1048 PixelTrait traits = GetPixelChannelTraits(image,channel);
1049 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1050 channel);
1051 if (((traits & UpdatePixelTrait) == 0) ||
1052 ((reconstruct_traits & UpdatePixelTrait) == 0))
1053 continue;
1054 similarity[j]+=channel_similarity[j];
1055 }
1056 similarity[CompositePixelChannel]+=
1057 channel_similarity[CompositePixelChannel];
1058 mean_error+=channel_mean_error;
1059 if (channel_maximum_error > maximum_error)
1060 maximum_error=channel_maximum_error;
1061 }
1062 }
1063 reconstruct_view=DestroyCacheView(reconstruct_view);
1064 image_view=DestroyCacheView(image_view);
1065 area=MagickSafeReciprocal(area);
1066 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1067 {
1068 PixelChannel channel = GetPixelChannelChannel(image,k);
1069 PixelTrait traits = GetPixelChannelTraits(image,channel);
1070 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1071 channel);
1072 if (((traits & UpdatePixelTrait) == 0) ||
1073 ((reconstruct_traits & UpdatePixelTrait) == 0))
1074 continue;
1075 similarity[k]*=area;
1076 channels++;
1077 }
1078 similarity[CompositePixelChannel]*=area;
1079 if (channels != 0)
1080 similarity[CompositePixelChannel]/=(double) channels;
1081 image->error.mean_error_per_pixel=(double) QuantumRange*
1082 similarity[CompositePixelChannel];
1083 image->error.normalized_mean_error=mean_error*area;
1084 image->error.normalized_maximum_error=maximum_error;
1085 return(status);
1086}
1087
1088static MagickBooleanType GetMSESimilarity(const Image *image,
1089 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
1090{
1091 CacheView
1092 *image_view,
1093 *reconstruct_view;
1094
1095 double
1096 area = 0.0;
1097
1098 MagickBooleanType
1099 status = MagickTrue;
1100
1101 size_t
1102 columns,
1103 rows;
1104
1105 ssize_t
1106 channels = 0,
1107 k,
1108 y;
1109
1110 /*
1111 Compute the mean sequared error similarity.
1112 */
1113 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1114 image_view=AcquireVirtualCacheView(image,exception);
1115 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1116#if defined(MAGICKCORE_OPENMP_SUPPORT)
1117 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
1118 magick_number_threads(image,image,rows,1)
1119#endif
1120 for (y=0; y < (ssize_t) rows; y++)
1121 {
1122 const Quantum
1123 *magick_restrict p,
1124 *magick_restrict q;
1125
1126 double
1127 channel_area = 0.0,
1128 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1129
1130 ssize_t
1131 x;
1132
1133 if (status == MagickFalse)
1134 continue;
1135 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1136 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1137 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
1138 {
1139 status=MagickFalse;
1140 continue;
1141 }
1142 for (x=0; x < (ssize_t) columns; x++)
1143 {
1144 double
1145 Da,
1146 Sa;
1147
1148 ssize_t
1149 i;
1150
1151 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1152 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1153 {
1154 p+=(ptrdiff_t) GetPixelChannels(image);
1155 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1156 continue;
1157 }
1158 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1159 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1160 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1161 {
1162 double
1163 error;
1164
1165 PixelChannel channel = GetPixelChannelChannel(image,i);
1166 PixelTrait traits = GetPixelChannelTraits(image,channel);
1167 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1168 channel);
1169 if (((traits & UpdatePixelTrait) == 0) ||
1170 ((reconstruct_traits & UpdatePixelTrait) == 0))
1171 continue;
1172 if (channel == AlphaPixelChannel)
1173 error=QuantumScale*((double) p[i]-(double) GetPixelChannel(
1174 reconstruct_image,channel,q));
1175 else
1176 error=QuantumScale*(Sa*(double) p[i]-Da*(double) GetPixelChannel(
1177 reconstruct_image,channel,q));
1178 channel_similarity[i]+=error*error;
1179 channel_similarity[CompositePixelChannel]+=error*error;
1180 }
1181 channel_area++;
1182 p+=(ptrdiff_t) GetPixelChannels(image);
1183 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1184 }
1185#if defined(MAGICKCORE_OPENMP_SUPPORT)
1186 #pragma omp critical (MagickCore_GetMSESimilarity)
1187#endif
1188 {
1189 ssize_t
1190 j;
1191
1192 area+=channel_area;
1193 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1194 {
1195 PixelChannel channel = GetPixelChannelChannel(image,j);
1196 PixelTrait traits = GetPixelChannelTraits(image,channel);
1197 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1198 channel);
1199 if (((traits & UpdatePixelTrait) == 0) ||
1200 ((reconstruct_traits & UpdatePixelTrait) == 0))
1201 continue;
1202 similarity[j]+=channel_similarity[j];
1203 }
1204 similarity[CompositePixelChannel]+=
1205 channel_similarity[CompositePixelChannel];
1206 }
1207 }
1208 reconstruct_view=DestroyCacheView(reconstruct_view);
1209 image_view=DestroyCacheView(image_view);
1210 area=MagickSafeReciprocal(area);
1211 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1212 {
1213 PixelChannel channel = GetPixelChannelChannel(image,k);
1214 PixelTrait traits = GetPixelChannelTraits(image,channel);
1215 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1216 channel);
1217 if (((traits & UpdatePixelTrait) == 0) ||
1218 ((reconstruct_traits & UpdatePixelTrait) == 0))
1219 continue;
1220 similarity[k]*=area;
1221 channels++;
1222 }
1223 similarity[CompositePixelChannel]*=area;
1224 if (channels != 0)
1225 similarity[CompositePixelChannel]/=(double) channels;
1226 return(status);
1227}
1228
1229static MagickBooleanType GetNCCSimilarity(const Image *image,
1230 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
1231{
1232 CacheView
1233 *image_view,
1234 *reconstruct_view;
1235
1236 ChannelStatistics
1237 *image_statistics,
1238 *reconstruct_statistics;
1239
1240 double
1241 reconstruct_variance[MaxPixelChannels+1] = { 0.0 },
1242 variance[MaxPixelChannels+1] = { 0.0 };
1243
1244 MagickBooleanType
1245 status = MagickTrue;
1246
1247 MagickOffsetType
1248 progress = 0;
1249
1250 size_t
1251 columns,
1252 rows;
1253
1254 ssize_t
1255 k,
1256 y;
1257
1258 /*
1259 Compute the normalized criss-correlation similarity.
1260 */
1261 image_statistics=GetImageStatistics(image,exception);
1262 reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
1263 if ((image_statistics == (ChannelStatistics *) NULL) ||
1264 (reconstruct_statistics == (ChannelStatistics *) NULL))
1265 {
1266 if (image_statistics != (ChannelStatistics *) NULL)
1267 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1268 image_statistics);
1269 if (reconstruct_statistics != (ChannelStatistics *) NULL)
1270 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1271 reconstruct_statistics);
1272 return(MagickFalse);
1273 }
1274 (void) memset(similarity,0,(MaxPixelChannels+1)*sizeof(*similarity));
1275 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1276 image_view=AcquireVirtualCacheView(image,exception);
1277 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1278#if defined(MAGICKCORE_OPENMP_SUPPORT)
1279 #pragma omp parallel for schedule(static) shared(variance,reconstruct_variance,similarity,status) \
1280 magick_number_threads(image,image,rows,1)
1281#endif
1282 for (y=0; y < (ssize_t) rows; y++)
1283 {
1284 const Quantum
1285 *magick_restrict p,
1286 *magick_restrict q;
1287
1288 double
1289 channel_reconstruct_variance[MaxPixelChannels+1] = { 0.0 },
1290 channel_similarity[MaxPixelChannels+1] = { 0.0 },
1291 channel_variance[MaxPixelChannels+1] = { 0.0 };
1292
1293 ssize_t
1294 x;
1295
1296 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1297 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1298 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
1299 {
1300 status=MagickFalse;
1301 continue;
1302 }
1303 for (x=0; x < (ssize_t) columns; x++)
1304 {
1305 double
1306 Da,
1307 Sa;
1308
1309 ssize_t
1310 i;
1311
1312 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1313 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1314 {
1315 p+=(ptrdiff_t) GetPixelChannels(image);
1316 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1317 continue;
1318 }
1319 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1320 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1321 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1322 {
1323 double
1324 alpha,
1325 beta;
1326
1327 PixelChannel channel = GetPixelChannelChannel(image,i);
1328 PixelTrait traits = GetPixelChannelTraits(image,channel);
1329 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1330 channel);
1331 if (((traits & UpdatePixelTrait) == 0) ||
1332 ((reconstruct_traits & UpdatePixelTrait) == 0))
1333 continue;
1334 if (channel == AlphaPixelChannel)
1335 {
1336 alpha=QuantumScale*((double) p[i]-image_statistics[channel].mean);
1337 beta=QuantumScale*((double) GetPixelChannel(reconstruct_image,
1338 channel,q)-reconstruct_statistics[channel].mean);
1339 }
1340 else
1341 {
1342 alpha=QuantumScale*(Sa*(double) p[i]-
1343 image_statistics[channel].mean);
1344 beta=QuantumScale*(Da*(double) GetPixelChannel(reconstruct_image,channel,
1345 q)-reconstruct_statistics[channel].mean);
1346 }
1347 channel_similarity[i]+=alpha*beta;
1348 channel_variance[i]+=alpha*alpha;
1349 channel_reconstruct_variance[i]+=beta*beta;
1350 }
1351 p+=(ptrdiff_t) GetPixelChannels(image);
1352 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1353 }
1354#if defined(MAGICKCORE_OPENMP_SUPPORT)
1355 #pragma omp critical (MagickCore_GetNCCSimilarity)
1356#endif
1357 {
1358 ssize_t
1359 j;
1360
1361 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1362 {
1363 PixelChannel channel = GetPixelChannelChannel(image,j);
1364 PixelTrait traits = GetPixelChannelTraits(image,channel);
1365 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1366 channel);
1367 if (((traits & UpdatePixelTrait) == 0) ||
1368 ((reconstruct_traits & UpdatePixelTrait) == 0))
1369 continue;
1370 similarity[j]+=channel_similarity[j];
1371 similarity[CompositePixelChannel]+=channel_similarity[j];
1372 variance[j]+=channel_variance[j];
1373 variance[CompositePixelChannel]+=channel_variance[j];
1374 reconstruct_variance[j]+=channel_reconstruct_variance[j];
1375 reconstruct_variance[CompositePixelChannel]+=
1376 channel_reconstruct_variance[j];
1377 }
1378 }
1379 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1380 {
1381 MagickBooleanType
1382 proceed;
1383
1384#if defined(MAGICKCORE_OPENMP_SUPPORT)
1385 #pragma omp atomic
1386#endif
1387 progress++;
1388 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
1389 if (proceed == MagickFalse)
1390 {
1391 status=MagickFalse;
1392 continue;
1393 }
1394 }
1395 }
1396 reconstruct_view=DestroyCacheView(reconstruct_view);
1397 image_view=DestroyCacheView(image_view);
1398 /*
1399 Compute normalized cross correlation: divide by standard deviation.
1400 */
1401 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1402 {
1403 PixelChannel channel = GetPixelChannelChannel(image,k);
1404 PixelTrait traits = GetPixelChannelTraits(image,channel);
1405 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1406 channel);
1407 if (((traits & UpdatePixelTrait) == 0) ||
1408 ((reconstruct_traits & UpdatePixelTrait) == 0))
1409 continue;
1410 similarity[k]*=MagickSafeReciprocal(sqrt(variance[k])*
1411 sqrt(reconstruct_variance[k]));
1412 }
1413 similarity[CompositePixelChannel]*=MagickSafeReciprocal(sqrt(
1414 variance[CompositePixelChannel])*sqrt(
1415 reconstruct_variance[CompositePixelChannel]));
1416 /*
1417 Free resources.
1418 */
1419 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1420 reconstruct_statistics);
1421 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1422 image_statistics);
1423 return(status);
1424}
1425
1426static MagickBooleanType GetPASimilarity(const Image *image,
1427 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
1428{
1429 CacheView
1430 *image_view,
1431 *reconstruct_view;
1432
1433 MagickBooleanType
1434 status = MagickTrue;
1435
1436 size_t
1437 columns,
1438 rows;
1439
1440 ssize_t
1441 y;
1442
1443 /*
1444 Compute the peak absolute similarity.
1445 */
1446 (void) memset(similarity,0,(MaxPixelChannels+1)*sizeof(*similarity));
1447 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1448 image_view=AcquireVirtualCacheView(image,exception);
1449 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1450#if defined(MAGICKCORE_OPENMP_SUPPORT)
1451 #pragma omp parallel for schedule(static) shared(similarity,status) \
1452 magick_number_threads(image,image,rows,1)
1453#endif
1454 for (y=0; y < (ssize_t) rows; y++)
1455 {
1456 const Quantum
1457 *magick_restrict p,
1458 *magick_restrict q;
1459
1460 double
1461 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1462
1463 ssize_t
1464 x;
1465
1466 if (status == MagickFalse)
1467 continue;
1468 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1469 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1470 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
1471 {
1472 status=MagickFalse;
1473 continue;
1474 }
1475 for (x=0; x < (ssize_t) columns; x++)
1476 {
1477 double
1478 Da,
1479 Sa;
1480
1481 ssize_t
1482 i;
1483
1484 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1485 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1486 {
1487 p+=(ptrdiff_t) GetPixelChannels(image);
1488 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1489 continue;
1490 }
1491 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1492 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1493 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1494 {
1495 double
1496 distance;
1497
1498 PixelChannel channel = GetPixelChannelChannel(image,i);
1499 PixelTrait traits = GetPixelChannelTraits(image,channel);
1500 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1501 channel);
1502 if (((traits & UpdatePixelTrait) == 0) ||
1503 ((reconstruct_traits & UpdatePixelTrait) == 0))
1504 continue;
1505 if (channel == AlphaPixelChannel)
1506 distance=QuantumScale*fabs((double) p[i]-(double)
1507 GetPixelChannel(reconstruct_image,channel,q));
1508 else
1509 distance=QuantumScale*fabs(Sa*(double) p[i]-Da*(double) GetPixelChannel(
1510 reconstruct_image,channel,q));
1511 if (distance > channel_similarity[i])
1512 channel_similarity[i]=distance;
1513 if (distance > channel_similarity[CompositePixelChannel])
1514 channel_similarity[CompositePixelChannel]=distance;
1515 }
1516 p+=(ptrdiff_t) GetPixelChannels(image);
1517 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1518 }
1519#if defined(MAGICKCORE_OPENMP_SUPPORT)
1520 #pragma omp critical (MagickCore_GetPASimilarity)
1521#endif
1522 {
1523 ssize_t
1524 j;
1525
1526 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1527 {
1528 PixelChannel channel = GetPixelChannelChannel(image,j);
1529 PixelTrait traits = GetPixelChannelTraits(image,channel);
1530 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1531 channel);
1532 if (((traits & UpdatePixelTrait) == 0) ||
1533 ((reconstruct_traits & UpdatePixelTrait) == 0))
1534 continue;
1535 if (channel_similarity[j] > similarity[j])
1536 similarity[j]=channel_similarity[j];
1537 }
1538 if (channel_similarity[CompositePixelChannel] > similarity[CompositePixelChannel])
1539 similarity[CompositePixelChannel]=
1540 channel_similarity[CompositePixelChannel];
1541 }
1542 }
1543 reconstruct_view=DestroyCacheView(reconstruct_view);
1544 image_view=DestroyCacheView(image_view);
1545 return(status);
1546}
1547
1548static MagickBooleanType GetPDCSimilarity(const Image *image,
1549 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
1550{
1551 CacheView
1552 *image_view,
1553 *reconstruct_view;
1554
1555 double
1556 fuzz;
1557
1558 MagickBooleanType
1559 status = MagickTrue;
1560
1561 size_t
1562 columns,
1563 rows;
1564
1565 ssize_t
1566 y;
1567
1568 /*
1569 Compute the pixel difference count similarity.
1570 */
1571 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
1572 (void) memset(similarity,0,(MaxPixelChannels+1)*sizeof(*similarity));
1573 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1574 image_view=AcquireVirtualCacheView(image,exception);
1575 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1576#if defined(MAGICKCORE_OPENMP_SUPPORT)
1577 #pragma omp parallel for schedule(static) shared(similarity,status) \
1578 magick_number_threads(image,image,rows,1)
1579#endif
1580 for (y=0; y < (ssize_t) rows; y++)
1581 {
1582 const Quantum
1583 *magick_restrict p,
1584 *magick_restrict q;
1585
1586 double
1587 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1588
1589 ssize_t
1590 x;
1591
1592 if (status == MagickFalse)
1593 continue;
1594 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1595 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1596 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
1597 {
1598 status=MagickFalse;
1599 continue;
1600 }
1601 for (x=0; x < (ssize_t) columns; x++)
1602 {
1603 double
1604 Da,
1605 Sa;
1606
1607 size_t
1608 count = 0;
1609
1610 ssize_t
1611 i;
1612
1613 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1614 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1615 {
1616 p+=(ptrdiff_t) GetPixelChannels(image);
1617 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1618 continue;
1619 }
1620 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1621 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1622 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1623 {
1624 double
1625 error;
1626
1627 PixelChannel channel = GetPixelChannelChannel(image,i);
1628 PixelTrait traits = GetPixelChannelTraits(image,channel);
1629 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1630 channel);
1631 if (((traits & UpdatePixelTrait) == 0) ||
1632 ((reconstruct_traits & UpdatePixelTrait) == 0))
1633 continue;
1634 if (channel == AlphaPixelChannel)
1635 error=(double) p[i]-(double) GetPixelChannel(reconstruct_image,
1636 channel,q);
1637 else
1638 error=Sa*(double) p[i]-Da*(double) GetPixelChannel(reconstruct_image,
1639 channel,q);
1640 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
1641 {
1642 channel_similarity[i]++;
1643 count++;
1644 }
1645 }
1646 if (count != 0)
1647 channel_similarity[CompositePixelChannel]++;
1648 p+=(ptrdiff_t) GetPixelChannels(image);
1649 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1650 }
1651#if defined(MAGICKCORE_OPENMP_SUPPORT)
1652 #pragma omp critical (MagickCore_GetPDCSimilarity)
1653#endif
1654 {
1655 ssize_t
1656 j;
1657
1658 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1659 {
1660 PixelChannel channel = GetPixelChannelChannel(image,j);
1661 PixelTrait traits = GetPixelChannelTraits(image,channel);
1662 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1663 channel);
1664 if (((traits & UpdatePixelTrait) == 0) ||
1665 ((reconstruct_traits & UpdatePixelTrait) == 0))
1666 continue;
1667 similarity[j]+=channel_similarity[j];
1668 }
1669 similarity[CompositePixelChannel]+=
1670 channel_similarity[CompositePixelChannel];
1671 }
1672 }
1673 reconstruct_view=DestroyCacheView(reconstruct_view);
1674 image_view=DestroyCacheView(image_view);
1675 return(status);
1676}
1677
1678static Image *GetEdgeCorrelationSurface(const Image *image,
1679 ExceptionInfo *exception)
1680{
1681 Image
1682 *surface;
1683
1684 KernelInfo
1685 *kernel;
1686
1687 /*
1688 Build a spatial-domain edge/correlation surface using a LoG convolution.
1689 */
1690 kernel=AcquireKernelInfo("LoG:0,1",exception);
1691 if (kernel == (KernelInfo *) NULL)
1692 return((Image *) NULL);
1693 surface=MorphologyImage(image,ConvolveMorphology,1,kernel,exception);
1694 kernel=DestroyKernelInfo(kernel);
1695 return(surface);
1696}
1697
1698static MagickBooleanType GetPHASESimilarity(const Image *image,
1699 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
1700{
1701 CacheView
1702 *edge_reconstruct_view,
1703 *edge_view,
1704 *image_view,
1705 *reconstruct_view;
1706
1707 double
1708 area = 0,
1709 correlation[MaxPixelChannels+1] = { 0.0 },
1710 image_sum[MaxPixelChannels+1] = { 0.0 },
1711 image_sum_squared[MaxPixelChannels+1] = { 0.0 },
1712 reconstruct_sum[MaxPixelChannels+1] = { 0.0 },
1713 reconstruct_sum_squared[MaxPixelChannels+1] = { 0.0 };
1714
1715 Image
1716 *edge_image,
1717 *edge_reconstruct;
1718
1719 MagickBooleanType
1720 status = MagickTrue;
1721
1722 size_t
1723 columns = 0,
1724 rows = 0;
1725
1726 ssize_t
1727 channels = 0,
1728 j,
1729 y;
1730
1731 /*
1732 Compute a Pearson-correlation similarity between two Laplacian-of-
1733 Gaussian edge surfaces.
1734 */
1735 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1736 edge_image=GetEdgeCorrelationSurface(image,exception);
1737 edge_reconstruct=GetEdgeCorrelationSurface(reconstruct_image,exception);
1738 if ((edge_image == (Image *) NULL) || (edge_reconstruct == (Image *) NULL))
1739 {
1740 if (edge_image != (Image *) NULL)
1741 edge_image=DestroyImage(edge_image);
1742 if (edge_reconstruct != (Image *) NULL)
1743 edge_reconstruct=DestroyImage(edge_reconstruct);
1744 return(MagickFalse);
1745 }
1746 image_view=AcquireVirtualCacheView(image,exception);
1747 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1748 edge_view=AcquireVirtualCacheView(edge_image,exception);
1749 edge_reconstruct_view=AcquireVirtualCacheView(edge_reconstruct,exception);
1750#if defined(MAGICKCORE_OPENMP_SUPPORT)
1751 #pragma omp parallel for schedule(static) shared(status) \
1752 magick_number_threads(edge_image,edge_reconstruct,rows,1)
1753#endif
1754 for (y=0; y < (ssize_t) rows; y++)
1755 {
1756 const Quantum
1757 *magick_restrict p,
1758 *magick_restrict pm,
1759 *magick_restrict q,
1760 *magick_restrict qm;
1761
1762 double
1763 channel_area = 0,
1764 channel_correlation[MaxPixelChannels+1] = { 0.0 },
1765 channel_image_sum[MaxPixelChannels+1] = { 0.0 },
1766 channel_image_sum_squared[MaxPixelChannels+1] = { 0.0 },
1767 channel_reconstruct_sum[MaxPixelChannels+1] = { 0.0 },
1768 channel_reconstruct_sum_squared[MaxPixelChannels+1] = { 0.0 };
1769
1770 ssize_t
1771 i,
1772 x;
1773
1774 if (status == MagickFalse)
1775 continue;
1776 pm=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1777 qm=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1778 p=GetCacheViewVirtualPixels(edge_view,0,y,columns,1,exception);
1779 q=GetCacheViewVirtualPixels(edge_reconstruct_view,0,y,columns,1,exception);
1780 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL) ||
1781 (pm == (const Quantum *) NULL) || (qm == (const Quantum *) NULL))
1782 {
1783 status=MagickFalse;
1784 continue;
1785 }
1786 for (x=0; x < (ssize_t) columns; x++)
1787 {
1788 if ((GetPixelReadMask(image,pm) <= (QuantumRange/2)) ||
1789 (GetPixelReadMask(reconstruct_image,qm) <= (QuantumRange/2)))
1790 {
1791 p+=(ptrdiff_t) GetPixelChannels(edge_image);
1792 q+=(ptrdiff_t) GetPixelChannels(edge_reconstruct);
1793 pm+=(ptrdiff_t) GetPixelChannels(image);
1794 qm+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1795 continue;
1796 }
1797 for (i=0; i < (ssize_t) GetPixelChannels(edge_image); i++)
1798 {
1799 double
1800 alpha,
1801 beta;
1802
1803 PixelChannel
1804 channel;
1805
1806 PixelTrait
1807 reconstruct_traits,
1808 traits;
1809
1810 ssize_t
1811 offset;
1812
1813 channel=GetPixelChannelChannel(edge_image,i);
1814 traits=GetPixelChannelTraits(edge_image,channel);
1815 reconstruct_traits=GetPixelChannelTraits(edge_reconstruct,channel);
1816 if (((traits & UpdatePixelTrait) == 0) ||
1817 ((reconstruct_traits & UpdatePixelTrait) == 0))
1818 continue;
1819 offset=GetPixelChannelOffset(edge_reconstruct,channel);
1820 if (offset < 0)
1821 continue;
1822 alpha=QuantumScale*(double) p[i];
1823 beta=QuantumScale*(double) q[offset];
1824 channel_image_sum[i]+=alpha;
1825 channel_image_sum_squared[i]+=alpha*alpha;
1826 channel_reconstruct_sum[i]+=beta;
1827 channel_reconstruct_sum_squared[i]+=beta*beta;
1828 channel_correlation[i]+=alpha*beta;
1829 }
1830 channel_area++;
1831 p+=(ptrdiff_t) GetPixelChannels(edge_image);
1832 q+=(ptrdiff_t) GetPixelChannels(edge_reconstruct);
1833 pm+=(ptrdiff_t) GetPixelChannels(image);
1834 qm+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1835 }
1836#if defined(MAGICKCORE_OPENMP_SUPPORT)
1837 #pragma omp critical (MagickCore_GetPHASESimilarity)
1838#endif
1839 {
1840 area+=channel_area;
1841 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1842 {
1843 correlation[i]+=channel_correlation[i];
1844 image_sum[i]+=channel_image_sum[i];
1845 image_sum_squared[i]+=channel_image_sum_squared[i];
1846 reconstruct_sum[i]+=channel_reconstruct_sum[i];
1847 reconstruct_sum_squared[i]+=channel_reconstruct_sum_squared[i];
1848 }
1849 }
1850 }
1851 edge_reconstruct_view=DestroyCacheView(edge_reconstruct_view);
1852 edge_view=DestroyCacheView(edge_view);
1853 reconstruct_view=DestroyCacheView(reconstruct_view);
1854 image_view=DestroyCacheView(image_view);
1855 edge_image=DestroyImage(edge_image);
1856 edge_reconstruct=DestroyImage(edge_reconstruct);
1857 if (status == MagickFalse)
1858 return(MagickFalse);
1859 if (area < 1.0)
1860 {
1861 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
1862 "InsufficientImageDataInRaster","`%s'",image->filename);
1863 return(MagickFalse);
1864 }
1865 /*
1866 Reduce to a per-channel Pearson coefficient and average.
1867 */
1868 similarity[CompositePixelChannel]=0.0;
1869 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1870 {
1871 double
1872 denominator,
1873 image_variance,
1874 numerator,
1875 pearson,
1876 reconstruct_variance;
1877
1878 PixelChannel
1879 channel;
1880
1881 PixelTrait
1882 reconstruct_traits,
1883 traits;
1884
1885 channel=GetPixelChannelChannel(image,j);
1886 traits=GetPixelChannelTraits(image,channel);
1887 reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
1888 if (((traits & UpdatePixelTrait) == 0) ||
1889 ((reconstruct_traits & UpdatePixelTrait) == 0))
1890 continue;
1891 numerator=area*correlation[j]-image_sum[j]*reconstruct_sum[j];
1892 image_variance=area*image_sum_squared[j]-image_sum[j]*image_sum[j];
1893 reconstruct_variance=area*reconstruct_sum_squared[j]-reconstruct_sum[j]*
1894 reconstruct_sum[j];
1895 if ((image_variance < MagickEpsilon) &&
1896 (reconstruct_variance < MagickEpsilon))
1897 pearson=(fabs(image_sum[j]-reconstruct_sum[j]) < MagickEpsilon) ?
1898 1.0 : 0.0;
1899 else
1900 {
1901 denominator=sqrt(image_variance)*sqrt(reconstruct_variance);
1902 pearson=denominator < MagickEpsilon ? 0.0 : numerator/denominator;
1903 }
1904 if (pearson < -1.0)
1905 pearson=(-1.0);
1906 if (pearson > 1.0)
1907 pearson=1.0;
1908 similarity[j]=pearson;
1909 similarity[CompositePixelChannel]+=pearson;
1910 channels++;
1911 }
1912 if (channels != 0)
1913 similarity[CompositePixelChannel]/=(double) channels;
1914 return(MagickTrue);
1915}
1916
1917static MagickBooleanType GetPHASHSimilarity(const Image *image,
1918 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
1919{
1920 ChannelPerceptualHash
1921 *channel_phash,
1922 *reconstruct_phash;
1923
1924 const char
1925 *artifact;
1926
1927 ssize_t
1928 channels = 0,
1929 i;
1930
1931 /*
1932 Compute the perceptual hash similarity.
1933 */
1934 channel_phash=GetImagePerceptualHash(image,exception);
1935 if (channel_phash == (ChannelPerceptualHash *) NULL)
1936 return(MagickFalse);
1937 reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
1938 if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
1939 {
1940 channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1941 channel_phash);
1942 return(MagickFalse);
1943 }
1944 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1945 {
1946 double
1947 difference = 0.0;
1948
1949 ssize_t
1950 j;
1951
1952 PixelChannel channel = GetPixelChannelChannel(image,i);
1953 PixelTrait traits = GetPixelChannelTraits(image,channel);
1954 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1955 channel);
1956 if (((traits & UpdatePixelTrait) == 0) ||
1957 ((reconstruct_traits & UpdatePixelTrait) == 0))
1958 continue;
1959 for (j=0; j < (ssize_t) channel_phash[0].number_colorspaces; j++)
1960 {
1961 double
1962 alpha,
1963 beta;
1964
1965 ssize_t
1966 k;
1967
1968 for (k=0; k < MaximumNumberOfPerceptualHashes; k++)
1969 {
1970 double
1971 error;
1972
1973 alpha=channel_phash[i].phash[j][k];
1974 beta=reconstruct_phash[i].phash[j][k];
1975 error=beta-alpha;
1976 if (IsNaN(error) != 0)
1977 error=0.0;
1978 difference+=error*error;
1979 }
1980 }
1981 similarity[i]+=difference;
1982 similarity[CompositePixelChannel]+=difference;
1983 channels++;
1984 }
1985 if (channels != 0)
1986 similarity[CompositePixelChannel]/=(double) channels;
1987 artifact=GetImageArtifact(image,"phash:normalize");
1988 if (IsStringTrue(artifact) != MagickFalse)
1989 {
1990 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1991 {
1992 PixelChannel channel = GetPixelChannelChannel(image,i);
1993 PixelTrait traits = GetPixelChannelTraits(image,channel);
1994 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1995 channel);
1996 if (((traits & UpdatePixelTrait) == 0) ||
1997 ((reconstruct_traits & UpdatePixelTrait) == 0))
1998 continue;
1999 similarity[i]=sqrt(similarity[i]/(double)
2000 channel_phash[0].number_colorspaces);
2001 }
2002 similarity[CompositePixelChannel]=sqrt(similarity[CompositePixelChannel]/
2003 (double) channel_phash[0].number_colorspaces);
2004 }
2005 /*
2006 Free resources.
2007 */
2008 reconstruct_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
2009 reconstruct_phash);
2010 channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(channel_phash);
2011 return(MagickTrue);
2012}
2013
2014static MagickBooleanType GetPSNRSimilarity(const Image *image,
2015 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
2016{
2017 MagickBooleanType
2018 status = MagickTrue;
2019
2020 ssize_t
2021 i;
2022
2023 /*
2024 Compute the peak signal-to-noise ratio similarity.
2025 */
2026 status=GetMSESimilarity(image,reconstruct_image,similarity,exception);
2027 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2028 {
2029 PixelChannel channel = GetPixelChannelChannel(image,i);
2030 PixelTrait traits = GetPixelChannelTraits(image,channel);
2031 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2032 channel);
2033 if (((traits & UpdatePixelTrait) == 0) ||
2034 ((reconstruct_traits & UpdatePixelTrait) == 0))
2035 continue;
2036 similarity[i]=10.0*MagickSafeLog10(MagickSafeReciprocal(
2037 similarity[i]))/MagickSafePSNRRecipicol(10.0);
2038 }
2039 similarity[CompositePixelChannel]=10.0*MagickSafeLog10(
2040 MagickSafeReciprocal(similarity[CompositePixelChannel]))/
2041 MagickSafePSNRRecipicol(10.0);
2042 return(status);
2043}
2044
2045static MagickBooleanType GetRMSESimilarity(const Image *image,
2046 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
2047{
2048#define RMSESquareRoot(x) sqrt((x) < 0.0 ? 0.0 : (x))
2049
2050 MagickBooleanType
2051 status = MagickTrue;
2052
2053 ssize_t
2054 i;
2055
2056 /*
2057 Compute the root mean-squared error similarity.
2058 */
2059 status=GetMSESimilarity(image,reconstruct_image,similarity,exception);
2060 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2061 {
2062 PixelChannel channel = GetPixelChannelChannel(image,i);
2063 PixelTrait traits = GetPixelChannelTraits(image,channel);
2064 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2065 channel);
2066 if (((traits & UpdatePixelTrait) == 0) ||
2067 ((reconstruct_traits & UpdatePixelTrait) == 0))
2068 continue;
2069 similarity[i]=RMSESquareRoot(similarity[i]);
2070 }
2071 similarity[CompositePixelChannel]=RMSESquareRoot(
2072 similarity[CompositePixelChannel]);
2073 return(status);
2074}
2075
2076static MagickBooleanType GetSSIMSimularity(const Image *image,
2077 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
2078{
2079#define SSIMRadius 5.0
2080#define SSIMSigma 1.5
2081#define SSIMK1 0.01
2082#define SSIMK2 0.03
2083#define SSIML 1.0
2084
2085 CacheView
2086 *image_view,
2087 *reconstruct_view;
2088
2089 char
2090 geometry[MagickPathExtent];
2091
2092 const char
2093 *artifact;
2094
2095 double
2096 area = 0.0,
2097 c1,
2098 c2,
2099 radius,
2100 sigma;
2101
2102 KernelInfo
2103 *kernel_info;
2104
2105 MagickBooleanType
2106 status = MagickTrue;
2107
2108 size_t
2109 columns,
2110 rows;
2111
2112 ssize_t
2113 channels = 0,
2114 l,
2115 y;
2116
2117 /*
2118 Compute the structual similarity index similarity.
2119 */
2120 radius=SSIMRadius;
2121 artifact=GetImageArtifact(image,"compare:ssim-radius");
2122 if (artifact != (const char *) NULL)
2123 radius=StringToDouble(artifact,(char **) NULL);
2124 sigma=SSIMSigma;
2125 artifact=GetImageArtifact(image,"compare:ssim-sigma");
2126 if (artifact != (const char *) NULL)
2127 sigma=StringToDouble(artifact,(char **) NULL);
2128 (void) FormatLocaleString(geometry,MagickPathExtent,"gaussian:%.17gx%.17g",
2129 radius,sigma);
2130 kernel_info=AcquireKernelInfo(geometry,exception);
2131 if (kernel_info == (KernelInfo *) NULL)
2132 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2133 image->filename);
2134 c1=pow(SSIMK1*SSIML,2.0);
2135 artifact=GetImageArtifact(image,"compare:ssim-k1");
2136 if (artifact != (const char *) NULL)
2137 c1=pow(StringToDouble(artifact,(char **) NULL)*SSIML,2.0);
2138 c2=pow(SSIMK2*SSIML,2.0);
2139 artifact=GetImageArtifact(image,"compare:ssim-k2");
2140 if (artifact != (const char *) NULL)
2141 c2=pow(StringToDouble(artifact,(char **) NULL)*SSIML,2.0);
2142 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
2143 image_view=AcquireVirtualCacheView(image,exception);
2144 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2145#if defined(MAGICKCORE_OPENMP_SUPPORT)
2146 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
2147 magick_number_threads(image,reconstruct_image,rows,1)
2148#endif
2149 for (y=0; y < (ssize_t) rows; y++)
2150 {
2151 const Quantum
2152 *magick_restrict p,
2153 *magick_restrict q;
2154
2155 double
2156 channel_area = 0.0,
2157 channel_similarity[MaxPixelChannels+1] = { 0.0 };
2158
2159 ssize_t
2160 i,
2161 x;
2162
2163 if (status == MagickFalse)
2164 continue;
2165 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
2166 ((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
2167 kernel_info->height,exception);
2168 q=GetCacheViewVirtualPixels(reconstruct_view,-((ssize_t) kernel_info->width/
2169 2L),y-((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
2170 kernel_info->height,exception);
2171 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
2172 {
2173 status=MagickFalse;
2174 continue;
2175 }
2176 for (x=0; x < (ssize_t) columns; x++)
2177 {
2178 const Quantum
2179 *magick_restrict reconstruct,
2180 *magick_restrict test;
2181
2182 double
2183 x_pixel_mu[MaxPixelChannels+1] = { 0.0 },
2184 x_pixel_sigma_squared[MaxPixelChannels+1] = { 0.0 },
2185 xy_sigma[MaxPixelChannels+1] = { 0.0 },
2186 y_pixel_mu[MaxPixelChannels+1] = { 0.0 },
2187 y_pixel_sigma_squared[MaxPixelChannels+1] = { 0.0 };
2188
2189 MagickRealType
2190 *k;
2191
2192 ssize_t
2193 v;
2194
2195 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
2196 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
2197 {
2198 p+=(ptrdiff_t) GetPixelChannels(image);
2199 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2200 continue;
2201 }
2202 k=kernel_info->values;
2203 test=p;
2204 reconstruct=q;
2205 for (v=0; v < (ssize_t) kernel_info->height; v++)
2206 {
2207 ssize_t
2208 u;
2209
2210 for (u=0; u < (ssize_t) kernel_info->width; u++)
2211 {
2212 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2213 {
2214 double
2215 x_pixel,
2216 y_pixel;
2217
2218 PixelChannel channel = GetPixelChannelChannel(image,i);
2219 PixelTrait traits = GetPixelChannelTraits(image,channel);
2220 PixelTrait reconstruct_traits = GetPixelChannelTraits(
2221 reconstruct_image,channel);
2222 if (((traits & UpdatePixelTrait) == 0) ||
2223 ((reconstruct_traits & UpdatePixelTrait) == 0))
2224 continue;
2225 x_pixel=QuantumScale*(double) test[i];
2226 x_pixel_mu[i]+=(*k)*x_pixel;
2227 x_pixel_sigma_squared[i]+=(*k)*x_pixel*x_pixel;
2228 y_pixel=QuantumScale*(double)
2229 GetPixelChannel(reconstruct_image,channel,reconstruct);
2230 y_pixel_mu[i]+=(*k)*y_pixel;
2231 y_pixel_sigma_squared[i]+=(*k)*y_pixel*y_pixel;
2232 xy_sigma[i]+=(*k)*x_pixel*y_pixel;
2233 }
2234 k++;
2235 test+=(ptrdiff_t) GetPixelChannels(image);
2236 reconstruct+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2237 }
2238 test+=(ptrdiff_t) ((double) GetPixelChannels(image)*(double) columns);
2239 reconstruct+=(ptrdiff_t) ((double) GetPixelChannels(reconstruct_image)*
2240 (double) columns);
2241 }
2242 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2243 {
2244 double
2245 ssim,
2246 x_pixel_mu_squared,
2247 x_pixel_sigmas_squared,
2248 xy_mu,
2249 xy_sigmas,
2250 y_pixel_mu_squared,
2251 y_pixel_sigmas_squared;
2252
2253 PixelChannel channel = GetPixelChannelChannel(image,i);
2254 PixelTrait traits = GetPixelChannelTraits(image,channel);
2255 PixelTrait reconstruct_traits = GetPixelChannelTraits(
2256 reconstruct_image,channel);
2257 if (((traits & UpdatePixelTrait) == 0) ||
2258 ((reconstruct_traits & UpdatePixelTrait) == 0))
2259 continue;
2260 x_pixel_mu_squared=x_pixel_mu[i]*x_pixel_mu[i];
2261 y_pixel_mu_squared=y_pixel_mu[i]*y_pixel_mu[i];
2262 xy_mu=x_pixel_mu[i]*y_pixel_mu[i];
2263 xy_sigmas=xy_sigma[i]-xy_mu;
2264 x_pixel_sigmas_squared=x_pixel_sigma_squared[i]-x_pixel_mu_squared;
2265 y_pixel_sigmas_squared=y_pixel_sigma_squared[i]-y_pixel_mu_squared;
2266 ssim=((2.0*xy_mu+c1)*(2.0*xy_sigmas+c2))*
2267 MagickSafeReciprocal((x_pixel_mu_squared+y_pixel_mu_squared+c1)*
2268 (x_pixel_sigmas_squared+y_pixel_sigmas_squared+c2));
2269 channel_similarity[i]+=ssim;
2270 channel_similarity[CompositePixelChannel]+=ssim;
2271 }
2272 p+=(ptrdiff_t) GetPixelChannels(image);
2273 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2274 channel_area++;
2275 }
2276#if defined(MAGICKCORE_OPENMP_SUPPORT)
2277 #pragma omp critical (MagickCore_GetSSIMSimularity)
2278#endif
2279 {
2280 ssize_t
2281 j;
2282
2283 area+=channel_area;
2284 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2285 {
2286 PixelChannel channel = GetPixelChannelChannel(image,j);
2287 PixelTrait traits = GetPixelChannelTraits(image,channel);
2288 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2289 channel);
2290 if (((traits & UpdatePixelTrait) == 0) ||
2291 ((reconstruct_traits & UpdatePixelTrait) == 0))
2292 continue;
2293 similarity[j]+=channel_similarity[j];
2294 }
2295 similarity[CompositePixelChannel]+=
2296 channel_similarity[CompositePixelChannel];
2297 }
2298 }
2299 image_view=DestroyCacheView(image_view);
2300 reconstruct_view=DestroyCacheView(reconstruct_view);
2301 kernel_info=DestroyKernelInfo(kernel_info);
2302 area=MagickSafeReciprocal(area);
2303 for (l=0; l < (ssize_t) GetPixelChannels(image); l++)
2304 {
2305 PixelChannel channel = GetPixelChannelChannel(image,l);
2306 PixelTrait traits = GetPixelChannelTraits(image,channel);
2307 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2308 channel);
2309 if (((traits & UpdatePixelTrait) == 0) ||
2310 ((reconstruct_traits & UpdatePixelTrait) == 0))
2311 continue;
2312 similarity[l]*=area;
2313 channels++;
2314 }
2315 similarity[CompositePixelChannel]*=area;
2316 if (channels != 0)
2317 similarity[CompositePixelChannel]/=(double) channels;
2318 return(status);
2319}
2320
2321static MagickBooleanType GetDSSIMSimilarity(const Image *image,
2322 const Image *reconstruct_image,double *similarity,ExceptionInfo *exception)
2323{
2324 MagickBooleanType
2325 status = MagickTrue;
2326
2327 ssize_t
2328 i;
2329
2330 /*
2331 Compute the structual dissimilarity index similarity.
2332 */
2333 status=GetSSIMSimularity(image,reconstruct_image,similarity,exception);
2334 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2335 {
2336 PixelChannel channel = GetPixelChannelChannel(image,i);
2337 PixelTrait traits = GetPixelChannelTraits(image,channel);
2338 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2339 channel);
2340 if (((traits & UpdatePixelTrait) == 0) ||
2341 ((reconstruct_traits & UpdatePixelTrait) == 0))
2342 continue;
2343 similarity[i]=(1.0-similarity[i])/2.0;
2344 }
2345 similarity[CompositePixelChannel]=(1.0-similarity[CompositePixelChannel])/2.0;
2346 return(status);
2347}
2348
2349MagickExport MagickBooleanType GetImageDistortion(Image *image,
2350 const Image *reconstruct_image,const MetricType metric,double *distortion,
2351 ExceptionInfo *exception)
2352{
2353#define CompareMetricNotSupportedException "metric not supported"
2354
2355 double
2356 *channel_similarity;
2357
2358 MagickBooleanType
2359 status = MagickTrue;
2360
2361 size_t
2362 length;
2363
2364 assert(image != (Image *) NULL);
2365 assert(image->signature == MagickCoreSignature);
2366 assert(reconstruct_image != (const Image *) NULL);
2367 assert(reconstruct_image->signature == MagickCoreSignature);
2368 assert(distortion != (double *) NULL);
2369 if (IsEventLogging() != MagickFalse)
2370 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2371 /*
2372 Get image distortion.
2373 */
2374 *distortion=0.0;
2375 length=MaxPixelChannels+1UL;
2376 channel_similarity=(double *) AcquireQuantumMemory(length,
2377 sizeof(*channel_similarity));
2378 if (channel_similarity == (double *) NULL)
2379 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2380 (void) memset(channel_similarity,0,length*sizeof(*channel_similarity));
2381 switch (metric)
2382 {
2383 case AbsoluteErrorMetric:
2384 {
2385 status=GetAESimilarity(image,reconstruct_image,channel_similarity,
2386 exception);
2387 break;
2388 }
2389 case DotProductCorrelationErrorMetric:
2390 {
2391 status=GetDPCSimilarity(image,reconstruct_image,channel_similarity,
2392 exception);
2393 break;
2394 }
2395 case FuzzErrorMetric:
2396 {
2397 status=GetFUZZSimilarity(image,reconstruct_image,channel_similarity,
2398 exception);
2399 break;
2400 }
2401 case MeanAbsoluteErrorMetric:
2402 {
2403 status=GetMAESimilarity(image,reconstruct_image,channel_similarity,
2404 exception);
2405 break;
2406 }
2407 case MeanErrorPerPixelErrorMetric:
2408 {
2409 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2410 exception);
2411 break;
2412 }
2413 case MeanSquaredErrorMetric:
2414 {
2415 status=GetMSESimilarity(image,reconstruct_image,channel_similarity,
2416 exception);
2417 break;
2418 }
2419 case NormalizedCrossCorrelationErrorMetric:
2420 {
2421 status=GetNCCSimilarity(image,reconstruct_image,channel_similarity,
2422 exception);
2423 break;
2424 }
2425 case PeakAbsoluteErrorMetric:
2426 {
2427 status=GetPASimilarity(image,reconstruct_image,channel_similarity,
2428 exception);
2429 break;
2430 }
2431 case PeakSignalToNoiseRatioErrorMetric:
2432 {
2433 status=GetPSNRSimilarity(image,reconstruct_image,channel_similarity,
2434 exception);
2435 break;
2436 }
2437 case PerceptualHashErrorMetric:
2438 {
2439 status=GetPHASHSimilarity(image,reconstruct_image,channel_similarity,
2440 exception);
2441 break;
2442 }
2443 case PhaseCorrelationErrorMetric:
2444 {
2445 status=GetPHASESimilarity(image,reconstruct_image,channel_similarity,
2446 exception);
2447 break;
2448 }
2449 case PixelDifferenceCountErrorMetric:
2450 {
2451 status=GetPDCSimilarity(image,reconstruct_image,channel_similarity,
2452 exception);
2453 break;
2454 }
2455 case RootMeanSquaredErrorMetric:
2456 case UndefinedErrorMetric:
2457 default:
2458 {
2459 status=GetRMSESimilarity(image,reconstruct_image,channel_similarity,
2460 exception);
2461 break;
2462 }
2463 case StructuralDissimilarityErrorMetric:
2464 {
2465 status=GetDSSIMSimilarity(image,reconstruct_image,channel_similarity,
2466 exception);
2467 break;
2468 }
2469 case StructuralSimilarityErrorMetric:
2470 {
2471 status=GetSSIMSimularity(image,reconstruct_image,channel_similarity,
2472 exception);
2473 break;
2474 }
2475 }
2476 *distortion=channel_similarity[CompositePixelChannel];
2477 switch (metric)
2478 {
2479 case DotProductCorrelationErrorMetric:
2480 case NormalizedCrossCorrelationErrorMetric:
2481 case PhaseCorrelationErrorMetric:
2482 case StructuralSimilarityErrorMetric:
2483 {
2484 *distortion=(1.0-(*distortion))/2.0;
2485 break;
2486 }
2487 default: break;
2488 }
2489 channel_similarity=(double *) RelinquishMagickMemory(channel_similarity);
2490 if ((IsNaN(*distortion) != 0) || (fabs(*distortion) < MagickEpsilon))
2491 *distortion=0.0;
2492 (void) FormatImageProperty(image,"distortion","%.*g",GetMagickPrecision(),
2493 *distortion);
2494 return(status);
2495}
2496␌
2497/*
2498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2499% %
2500% %
2501% %
2502% G e t I m a g e D i s t o r t i o n s %
2503% %
2504% %
2505% %
2506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2507%
2508% GetImageDistortions() compares the pixel channels of an image to a
2509% reconstructed image and returns the specified metric for each channel.
2510%
2511% The format of the GetImageDistortions method is:
2512%
2513% double *GetImageDistortions(const Image *image,
2514% const Image *reconstruct_image,const MetricType metric,
2515% ExceptionInfo *exception)
2516%
2517% A description of each parameter follows:
2518%
2519% o image: the image.
2520%
2521% o reconstruct_image: the reconstruction image.
2522%
2523% o metric: the metric.
2524%
2525% o exception: return any errors or warnings in this structure.
2526%
2527*/
2528MagickExport double *GetImageDistortions(Image *image,
2529 const Image *reconstruct_image,const MetricType metric,
2530 ExceptionInfo *exception)
2531{
2532 double
2533 *distortion,
2534 *channel_similarity;
2535
2536 MagickBooleanType
2537 status = MagickTrue;
2538
2539 size_t
2540 length;
2541
2542 ssize_t
2543 i;
2544
2545 assert(image != (Image *) NULL);
2546 assert(image->signature == MagickCoreSignature);
2547 assert(reconstruct_image != (const Image *) NULL);
2548 assert(reconstruct_image->signature == MagickCoreSignature);
2549 if (IsEventLogging() != MagickFalse)
2550 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2551 /*
2552 Get image distortion.
2553 */
2554 length=MaxPixelChannels+1UL;
2555 channel_similarity=(double *) AcquireQuantumMemory(length,
2556 sizeof(*channel_similarity));
2557 if (channel_similarity == (double *) NULL)
2558 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2559 (void) memset(channel_similarity,0,length*sizeof(*channel_similarity));
2560 switch (metric)
2561 {
2562 case AbsoluteErrorMetric:
2563 {
2564 status=GetAESimilarity(image,reconstruct_image,channel_similarity,
2565 exception);
2566 break;
2567 }
2568 case DotProductCorrelationErrorMetric:
2569 {
2570 status=GetDPCSimilarity(image,reconstruct_image,channel_similarity,
2571 exception);
2572 break;
2573 }
2574 case FuzzErrorMetric:
2575 {
2576 status=GetFUZZSimilarity(image,reconstruct_image,channel_similarity,
2577 exception);
2578 break;
2579 }
2580 case MeanAbsoluteErrorMetric:
2581 {
2582 status=GetMAESimilarity(image,reconstruct_image,channel_similarity,
2583 exception);
2584 break;
2585 }
2586 case MeanErrorPerPixelErrorMetric:
2587 {
2588 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2589 exception);
2590 break;
2591 }
2592 case MeanSquaredErrorMetric:
2593 {
2594 status=GetMSESimilarity(image,reconstruct_image,channel_similarity,
2595 exception);
2596 break;
2597 }
2598 case NormalizedCrossCorrelationErrorMetric:
2599 {
2600 status=GetNCCSimilarity(image,reconstruct_image,channel_similarity,
2601 exception);
2602 break;
2603 }
2604 case PeakAbsoluteErrorMetric:
2605 {
2606 status=GetPASimilarity(image,reconstruct_image,channel_similarity,
2607 exception);
2608 break;
2609 }
2610 case PeakSignalToNoiseRatioErrorMetric:
2611 {
2612 status=GetPSNRSimilarity(image,reconstruct_image,channel_similarity,
2613 exception);
2614 break;
2615 }
2616 case PerceptualHashErrorMetric:
2617 {
2618 status=GetPHASHSimilarity(image,reconstruct_image,channel_similarity,
2619 exception);
2620 break;
2621 }
2622 case PhaseCorrelationErrorMetric:
2623 {
2624 status=GetPHASESimilarity(image,reconstruct_image,channel_similarity,
2625 exception);
2626 break;
2627 }
2628 case PixelDifferenceCountErrorMetric:
2629 {
2630 status=GetPDCSimilarity(image,reconstruct_image,channel_similarity,
2631 exception);
2632 break;
2633 }
2634 case RootMeanSquaredErrorMetric:
2635 case UndefinedErrorMetric:
2636 default:
2637 {
2638 status=GetRMSESimilarity(image,reconstruct_image,channel_similarity,
2639 exception);
2640 break;
2641 }
2642 case StructuralDissimilarityErrorMetric:
2643 {
2644 status=GetDSSIMSimilarity(image,reconstruct_image,channel_similarity,
2645 exception);
2646 break;
2647 }
2648 case StructuralSimilarityErrorMetric:
2649 {
2650 status=GetSSIMSimularity(image,reconstruct_image,channel_similarity,
2651 exception);
2652 break;
2653 }
2654 }
2655 if (status == MagickFalse)
2656 {
2657 channel_similarity=(double *) RelinquishMagickMemory(channel_similarity);
2658 return((double *) NULL);
2659 }
2660 distortion=channel_similarity;
2661 switch (metric)
2662 {
2663 case DotProductCorrelationErrorMetric:
2664 case NormalizedCrossCorrelationErrorMetric:
2665 case PhaseCorrelationErrorMetric:
2666 case StructuralSimilarityErrorMetric:
2667 {
2668 for (i=0; i <= MaxPixelChannels; i++)
2669 distortion[i]=(1.0-distortion[i])/2.0;
2670 break;
2671 }
2672 default: break;
2673 }
2674 for (i=0; i <= MaxPixelChannels; i++)
2675 if (fabs(distortion[i]) < MagickEpsilon)
2676 distortion[i]=0.0;
2677 (void) FormatImageProperty(image,"distortion","%.*g",GetMagickPrecision(),
2678 distortion[CompositePixelChannel]);
2679 return(distortion);
2680}
2681␌
2682/*
2683%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2684% %
2685% %
2686% %
2687% I s I m a g e s E q u a l %
2688% %
2689% %
2690% %
2691%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2692%
2693% IsImagesEqual() compare the pixels of two images and returns immediately
2694% if any pixel is not identical.
2695%
2696% The format of the IsImagesEqual method is:
2697%
2698% MagickBooleanType IsImagesEqual(const Image *image,
2699% const Image *reconstruct_image,ExceptionInfo *exception)
2700%
2701% A description of each parameter follows.
2702%
2703% o image: the image.
2704%
2705% o reconstruct_image: the reconstruction image.
2706%
2707% o exception: return any errors or warnings in this structure.
2708%
2709*/
2710MagickExport MagickBooleanType IsImagesEqual(const Image *image,
2711 const Image *reconstruct_image,ExceptionInfo *exception)
2712{
2713 CacheView
2714 *image_view,
2715 *reconstruct_view;
2716
2717 size_t
2718 columns,
2719 rows;
2720
2721 ssize_t
2722 y;
2723
2724 assert(image != (Image *) NULL);
2725 assert(image->signature == MagickCoreSignature);
2726 assert(reconstruct_image != (const Image *) NULL);
2727 assert(reconstruct_image->signature == MagickCoreSignature);
2728 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
2729 image_view=AcquireVirtualCacheView(image,exception);
2730 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2731 for (y=0; y < (ssize_t) rows; y++)
2732 {
2733 const Quantum
2734 *magick_restrict p,
2735 *magick_restrict q;
2736
2737 ssize_t
2738 x;
2739
2740 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
2741 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
2742 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
2743 break;
2744 for (x=0; x < (ssize_t) columns; x++)
2745 {
2746 ssize_t
2747 i;
2748
2749 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2750 {
2751 double
2752 distance;
2753
2754 PixelChannel channel = GetPixelChannelChannel(image,i);
2755 PixelTrait traits = GetPixelChannelTraits(image,channel);
2756 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2757 channel);
2758 if (((traits & UpdatePixelTrait) == 0) ||
2759 ((reconstruct_traits & UpdatePixelTrait) == 0))
2760 continue;
2761 distance=fabs((double) p[i]-(double) GetPixelChannel(reconstruct_image,
2762 channel,q));
2763 if (distance >= MagickEpsilon)
2764 break;
2765 }
2766 if (i < (ssize_t) GetPixelChannels(image))
2767 break;
2768 p+=(ptrdiff_t) GetPixelChannels(image);
2769 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2770 }
2771 if (x < (ssize_t) columns)
2772 break;
2773 }
2774 reconstruct_view=DestroyCacheView(reconstruct_view);
2775 image_view=DestroyCacheView(image_view);
2776 return(y < (ssize_t) rows ? MagickFalse : MagickTrue);
2777}
2778␌
2779/*
2780%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2781% %
2782% %
2783% %
2784% S e t I m a g e C o l o r M e t r i c %
2785% %
2786% %
2787% %
2788%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2789%
2790% SetImageColorMetric() measures the difference between colors at each pixel
2791% location of two images. A value other than 0 means the colors match
2792% exactly. Otherwise an error measure is computed by summing over all
2793% pixels in an image the distance squared in RGB space between each image
2794% pixel and its corresponding pixel in the reconstruction image. The error
2795% measure is assigned to these image members:
2796%
2797% o mean_error_per_pixel: The mean error for any single pixel in
2798% the image.
2799%
2800% o normalized_mean_error: The normalized mean quantization error for
2801% any single pixel in the image. This distance measure is normalized to
2802% a range between 0 and 1. It is independent of the range of red, green,
2803% and blue values in the image.
2804%
2805% o normalized_maximum_error: The normalized maximum quantization
2806% error for any single pixel in the image. This distance measure is
2807% normalized to a range between 0 and 1. It is independent of the range
2808% of red, green, and blue values in your image.
2809%
2810% A small normalized mean square error, accessed as
2811% image->normalized_mean_error, suggests the images are very similar in
2812% spatial layout and color.
2813%
2814% The format of the SetImageColorMetric method is:
2815%
2816% MagickBooleanType SetImageColorMetric(Image *image,
2817% const Image *reconstruct_image,ExceptionInfo *exception)
2818%
2819% A description of each parameter follows.
2820%
2821% o image: the image.
2822%
2823% o reconstruct_image: the reconstruction image.
2824%
2825% o exception: return any errors or warnings in this structure.
2826%
2827*/
2828MagickExport MagickBooleanType SetImageColorMetric(Image *image,
2829 const Image *reconstruct_image,ExceptionInfo *exception)
2830{
2831 double
2832 channel_similarity[MaxPixelChannels+1] = { 0.0 };
2833
2834 MagickBooleanType
2835 status;
2836
2837 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2838 exception);
2839 if (status == MagickFalse)
2840 return(MagickFalse);
2841 status=fabs(image->error.mean_error_per_pixel) < MagickEpsilon ?
2842 MagickTrue : MagickFalse;
2843 return(status);
2844}
2845␌
2846/*
2847%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2848% %
2849% %
2850% %
2851% S i m i l a r i t y I m a g e %
2852% %
2853% %
2854% %
2855%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2856%
2857% SimilarityImage() compares the reconstruction of the image and returns the
2858% best match offset. In addition, it returns a similarity image such that an
2859% exact match location is completely white and if none of the pixels match,
2860% black, otherwise some gray level in-between.
2861%
2862% Contributed by Fred Weinhaus.
2863%
2864% The format of the SimilarityImageImage method is:
2865%
2866% Image *SimilarityImage(const Image *image,const Image *reconstruct,
2867% const MetricType metric,const double similarity_threshold,
2868% RectangleInfo *offset,double *similarity,ExceptionInfo *exception)
2869%
2870% A description of each parameter follows:
2871%
2872% o image: the image.
2873%
2874% o reconstruct: find an area of the image that closely resembles this image.
2875%
2876% o metric: the metric.
2877%
2878% o similarity_threshold: minimum similarity for (sub)image match.
2879%
2880% o offset: the best match offset of the reconstruction image within the
2881% image.
2882%
2883% o similarity: the computed similarity between the images.
2884%
2885% o exception: return any errors or warnings in this structure.
2886%
2887*/
2888
2889#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
2890static Image *SIMCrossCorrelationImage(const Image *alpha_image,
2891 const Image *beta_image,ExceptionInfo *exception)
2892{
2893 Image
2894 *alpha_fft = (Image *) NULL,
2895 *beta_fft = (Image *) NULL,
2896 *complex_conjugate = (Image *) NULL,
2897 *complex_multiplication = (Image *) NULL,
2898 *cross_correlation = (Image *) NULL,
2899 *temp_image = (Image *) NULL;
2900
2901 /*
2902 Take the FFT of beta (reconstruction) image.
2903 */
2904 temp_image=CloneImage(beta_image,0,0,MagickTrue,exception);
2905 if (temp_image == (Image *) NULL)
2906 return((Image *) NULL);
2907 (void) SetImageArtifact(temp_image,"fourier:normalize","inverse");
2908 beta_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2909 temp_image=DestroyImageList(temp_image);
2910 if (beta_fft == (Image *) NULL)
2911 return((Image *) NULL);
2912 /*
2913 Take the complex conjugate of beta_fft.
2914 */
2915 complex_conjugate=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
2916 beta_fft=DestroyImageList(beta_fft);
2917 if (complex_conjugate == (Image *) NULL)
2918 return((Image *) NULL);
2919 /*
2920 Take the FFT of the alpha (test) image.
2921 */
2922 temp_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
2923 if (temp_image == (Image *) NULL)
2924 {
2925 complex_conjugate=DestroyImageList(complex_conjugate);
2926 return((Image *) NULL);
2927 }
2928 (void) SetImageArtifact(temp_image,"fourier:normalize","inverse");
2929 alpha_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2930 temp_image=DestroyImageList(temp_image);
2931 if (alpha_fft == (Image *) NULL)
2932 {
2933 complex_conjugate=DestroyImageList(complex_conjugate);
2934 return((Image *) NULL);
2935 }
2936 /*
2937 Do complex multiplication.
2938 */
2939 DisableCompositeClampUnlessSpecified(complex_conjugate);
2940 DisableCompositeClampUnlessSpecified(complex_conjugate->next);
2941 AppendImageToList(&complex_conjugate,alpha_fft);
2942 complex_multiplication=ComplexImages(complex_conjugate,
2943 MultiplyComplexOperator,exception);
2944 complex_conjugate=DestroyImageList(complex_conjugate);
2945 if (complex_multiplication == (Image *) NULL)
2946 return((Image *) NULL);
2947 /*
2948 Do the IFT and return the cross-correlation result.
2949 */
2950 cross_correlation=InverseFourierTransformImage(complex_multiplication,
2951 complex_multiplication->next,MagickFalse,exception);
2952 complex_multiplication=DestroyImageList(complex_multiplication);
2953 return(cross_correlation);
2954}
2955
2956static Image *SIMDerivativeImage(const Image *image,const char *kernel,
2957 ExceptionInfo *exception)
2958{
2959 Image
2960 *derivative_image;
2961
2962 KernelInfo
2963 *kernel_info;
2964
2965 kernel_info=AcquireKernelInfo(kernel,exception);
2966 if (kernel_info == (KernelInfo *) NULL)
2967 return((Image *) NULL);
2968 derivative_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
2969 exception);
2970 kernel_info=DestroyKernelInfo(kernel_info);
2971 return(derivative_image);
2972}
2973
2974static Image *SIMDivideImage(const Image *numerator_image,
2975 const Image *denominator_image,ExceptionInfo *exception)
2976{
2977 CacheView
2978 *denominator_view,
2979 *numerator_view;
2980
2981 Image
2982 *divide_image;
2983
2984 MagickBooleanType
2985 status = MagickTrue;
2986
2987 ssize_t
2988 y;
2989
2990 /*
2991 Divide one image into another.
2992 */
2993 divide_image=CloneImage(numerator_image,0,0,MagickTrue,exception);
2994 if (divide_image == (Image *) NULL)
2995 return(divide_image);
2996 numerator_view=AcquireAuthenticCacheView(divide_image,exception);
2997 denominator_view=AcquireVirtualCacheView(denominator_image,exception);
2998#if defined(MAGICKCORE_OPENMP_SUPPORT)
2999 #pragma omp parallel for schedule(static) shared(status) \
3000 magick_number_threads(denominator_image,divide_image,divide_image->rows,1)
3001#endif
3002 for (y=0; y < (ssize_t) divide_image->rows; y++)
3003 {
3004 const Quantum
3005 *magick_restrict p;
3006
3007 Quantum
3008 *magick_restrict q;
3009
3010 ssize_t
3011 x;
3012
3013 if (status == MagickFalse)
3014 continue;
3015 p=GetCacheViewVirtualPixels(denominator_view,0,y,
3016 divide_image->columns,1,exception);
3017 q=GetCacheViewAuthenticPixels(numerator_view,0,y,divide_image->columns,1,
3018 exception);
3019 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3020 {
3021 status=MagickFalse;
3022 continue;
3023 }
3024 for (x=0; x < (ssize_t) divide_image->columns; x++)
3025 {
3026 ssize_t
3027 i;
3028
3029 for (i=0; i < (ssize_t) GetPixelChannels(divide_image); i++)
3030 {
3031 PixelChannel channel = GetPixelChannelChannel(divide_image,i);
3032 PixelTrait traits = GetPixelChannelTraits(divide_image,channel);
3033 PixelTrait denominator_traits = GetPixelChannelTraits(denominator_image,
3034 channel);
3035 if (((traits & UpdatePixelTrait) == 0) ||
3036 ((denominator_traits & UpdatePixelTrait) == 0))
3037 continue;
3038 q[i]=(Quantum) ((double) q[i]*MagickSafeReciprocal(QuantumScale*
3039 (double) GetPixelChannel(denominator_image,channel,p)));
3040 }
3041 p+=(ptrdiff_t) GetPixelChannels(denominator_image);
3042 q+=(ptrdiff_t) GetPixelChannels(divide_image);
3043 }
3044 if (SyncCacheViewAuthenticPixels(numerator_view,exception) == MagickFalse)
3045 status=MagickFalse;
3046 }
3047 denominator_view=DestroyCacheView(denominator_view);
3048 numerator_view=DestroyCacheView(numerator_view);
3049 if (status == MagickFalse)
3050 divide_image=DestroyImage(divide_image);
3051 return(divide_image);
3052}
3053
3054static Image *SIMDivideByMagnitude(Image *image,Image *magnitude_image,
3055 const Image *source_image,ExceptionInfo *exception)
3056{
3057 Image
3058 *divide_image,
3059 *result_image;
3060
3061 RectangleInfo
3062 geometry;
3063
3064 divide_image=SIMDivideImage(image,magnitude_image,exception);
3065 if (divide_image == (Image *) NULL)
3066 return((Image *) NULL);
3067 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
3068 &divide_image->background_color);
3069 SetGeometry(source_image,&geometry);
3070 geometry.width=MagickMax(source_image->columns,divide_image->columns);
3071 geometry.height=MagickMax(source_image->rows,divide_image->rows);
3072 result_image=ExtentImage(divide_image,&geometry,exception);
3073 divide_image=DestroyImage(divide_image);
3074 return(result_image);
3075}
3076
3077static MagickBooleanType SIMFilterImageNaNs(Image *image,
3078 ExceptionInfo *exception)
3079{
3080 CacheView
3081 *image_view;
3082
3083 MagickBooleanType
3084 status = MagickTrue;
3085
3086 ssize_t
3087 y;
3088
3089 /*
3090 Square each pixel in the image.
3091 */
3092 image_view=AcquireAuthenticCacheView(image,exception);
3093#if defined(MAGICKCORE_OPENMP_SUPPORT)
3094 #pragma omp parallel for schedule(static) shared(status) \
3095 magick_number_threads(image,image,image->rows,1)
3096#endif
3097 for (y=0; y < (ssize_t) image->rows; y++)
3098 {
3099 Quantum
3100 *magick_restrict q;
3101
3102 ssize_t
3103 x;
3104
3105 if (status == MagickFalse)
3106 continue;
3107 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3108 if (q == (Quantum *) NULL)
3109 {
3110 status=MagickFalse;
3111 continue;
3112 }
3113 for (x=0; x < (ssize_t) image->columns; x++)
3114 {
3115 ssize_t
3116 i;
3117
3118 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3119 {
3120 PixelChannel channel = GetPixelChannelChannel(image,i);
3121 PixelTrait traits = GetPixelChannelTraits(image,channel);
3122 if ((traits & UpdatePixelTrait) == 0)
3123 continue;
3124 if (IsNaN((double) q[i]) != 0)
3125 q[i]=(Quantum) 0;
3126 }
3127 q+=(ptrdiff_t) GetPixelChannels(image);
3128 }
3129 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3130 status=MagickFalse;
3131 }
3132 image_view=DestroyCacheView(image_view);
3133 return(status);
3134}
3135
3136static Image *SIMSquareImage(const Image *image,ExceptionInfo *exception)
3137{
3138 CacheView
3139 *image_view;
3140
3141 Image
3142 *square_image;
3143
3144 MagickBooleanType
3145 status = MagickTrue;
3146
3147 ssize_t
3148 y;
3149
3150 /*
3151 Square each pixel in the image.
3152 */
3153 square_image=CloneImage(image,0,0,MagickTrue,exception);
3154 if (square_image == (Image *) NULL)
3155 return(square_image);
3156 image_view=AcquireAuthenticCacheView(square_image,exception);
3157#if defined(MAGICKCORE_OPENMP_SUPPORT)
3158 #pragma omp parallel for schedule(static) shared(status) \
3159 magick_number_threads(square_image,square_image,square_image->rows,1)
3160#endif
3161 for (y=0; y < (ssize_t) square_image->rows; y++)
3162 {
3163 Quantum
3164 *magick_restrict q;
3165
3166 ssize_t
3167 x;
3168
3169 if (status == MagickFalse)
3170 continue;
3171 q=GetCacheViewAuthenticPixels(image_view,0,y,square_image->columns,1,
3172 exception);
3173 if (q == (Quantum *) NULL)
3174 {
3175 status=MagickFalse;
3176 continue;
3177 }
3178 for (x=0; x < (ssize_t) square_image->columns; x++)
3179 {
3180 ssize_t
3181 i;
3182
3183 for (i=0; i < (ssize_t) GetPixelChannels(square_image); i++)
3184 {
3185 PixelChannel channel = GetPixelChannelChannel(square_image,i);
3186 PixelTrait traits = GetPixelChannelTraits(square_image,channel);
3187 if ((traits & UpdatePixelTrait) == 0)
3188 continue;
3189 q[i]=(Quantum) (QuantumScale*(double) q[i]*(double) q[i]);
3190 }
3191 q+=(ptrdiff_t) GetPixelChannels(square_image);
3192 }
3193 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3194 status=MagickFalse;
3195 }
3196 image_view=DestroyCacheView(image_view);
3197 if (status == MagickFalse)
3198 square_image=DestroyImage(square_image);
3199 return(square_image);
3200}
3201
3202static Image *SIMMagnitudeImage(Image *alpha_image,Image *beta_image,
3203 ExceptionInfo *exception)
3204{
3205 Image
3206 *magnitude_image,
3207 *xsq_image,
3208 *ysq_image;
3209
3210 MagickBooleanType
3211 status = MagickTrue;
3212
3213 (void) SetImageArtifact(alpha_image,"compose:clamp","False");
3214 xsq_image=SIMSquareImage(alpha_image,exception);
3215 if (xsq_image == (Image *) NULL)
3216 return((Image *) NULL);
3217 (void) SetImageArtifact(beta_image,"compose:clamp","False");
3218 ysq_image=SIMSquareImage(beta_image,exception);
3219 if (ysq_image == (Image *) NULL)
3220 {
3221 xsq_image=DestroyImage(xsq_image);
3222 return((Image *) NULL);
3223 }
3224 status=CompositeImage(xsq_image,ysq_image,PlusCompositeOp,MagickTrue,0,0,
3225 exception);
3226 magnitude_image=xsq_image;
3227 ysq_image=DestroyImage(ysq_image);
3228 if (status == MagickFalse)
3229 {
3230 magnitude_image=DestroyImage(magnitude_image);
3231 return((Image *) NULL);
3232 }
3233 status=EvaluateImage(magnitude_image,PowEvaluateOperator,0.5,exception);
3234 if (status == MagickFalse)
3235 {
3236 magnitude_image=DestroyImage(magnitude_image);
3237 return (Image *) NULL;
3238 }
3239 return(magnitude_image);
3240}
3241
3242static MagickBooleanType SIMMaximaImage(const Image *image,double *maxima,
3243 RectangleInfo *offset,ExceptionInfo *exception)
3244{
3245 typedef struct
3246 {
3247 double
3248 maxima;
3249
3250 ssize_t
3251 x,
3252 y;
3253 } MaximaInfo;
3254
3255 CacheView
3256 *image_view;
3257
3258 const Quantum
3259 *magick_restrict q;
3260
3261 MagickBooleanType
3262 status = MagickTrue;
3263
3264 MaximaInfo
3265 initial_maxima,
3266 maxima_info = { -MagickMaximumValue, 0, 0 };
3267
3268 ssize_t
3269 y;
3270
3271 /*
3272 Identify the maxima value in the image and its location.
3273 */
3274 image_view=AcquireVirtualCacheView(image,exception);
3275 q=GetCacheViewVirtualPixels(image_view,maxima_info.x,maxima_info.y,1,1,
3276 exception);
3277 if (q != (const Quantum *) NULL)
3278 maxima_info.maxima=IsNaN((double) q[0]) != 0 ? 0.0 : (double) q[0];
3279 initial_maxima=maxima_info;
3280#if defined(MAGICKCORE_OPENMP_SUPPORT)
3281 #pragma omp parallel for schedule(static) shared(maxima_info,status) \
3282 magick_number_threads(image,image,image->rows,1)
3283#endif
3284 for (y=0; y < (ssize_t) image->rows; y++)
3285 {
3286 const Quantum
3287 *magick_restrict p;
3288
3289 MaximaInfo
3290 channel_maxima;
3291
3292 ssize_t
3293 x;
3294
3295 if (status == MagickFalse)
3296 continue;
3297 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3298 if (p == (const Quantum *) NULL)
3299 {
3300 status=MagickFalse;
3301 continue;
3302 }
3303 channel_maxima=initial_maxima;
3304 for (x=0; x < (ssize_t) image->columns; x++)
3305 {
3306 ssize_t
3307 i;
3308
3309 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3310 {
3311 double
3312 pixel;
3313
3314 PixelChannel channel = GetPixelChannelChannel(image,i);
3315 PixelTrait traits = GetPixelChannelTraits(image,channel);
3316 if ((traits & UpdatePixelTrait) == 0)
3317 continue;
3318 pixel=(double) p[i];
3319 if (IsNaN(pixel) != 0)
3320 pixel=0.0;
3321 if (pixel > channel_maxima.maxima)
3322 {
3323 channel_maxima.maxima=(double) p[i];
3324 channel_maxima.x=x;
3325 channel_maxima.y=y;
3326 }
3327 }
3328 p+=(ptrdiff_t) GetPixelChannels(image);
3329 }
3330#if defined(MAGICKCORE_OPENMP_SUPPORT)
3331 #pragma omp critical (MagickCore_SIMMaximaImage)
3332#endif
3333 if (channel_maxima.maxima > maxima_info.maxima)
3334 maxima_info=channel_maxima;
3335 }
3336 image_view=DestroyCacheView(image_view);
3337 *maxima=maxima_info.maxima;
3338 offset->x=maxima_info.x;
3339 offset->y=maxima_info.y;
3340 return(status);
3341}
3342
3343static MagickBooleanType SIMMinimaImage(const Image *image,double *minima,
3344 RectangleInfo *offset,ExceptionInfo *exception)
3345{
3346 typedef struct
3347 {
3348 double
3349 minima;
3350
3351 ssize_t
3352 x,
3353 y;
3354 } MinimaInfo;
3355
3356 CacheView
3357 *image_view;
3358
3359 const Quantum
3360 *magick_restrict q;
3361
3362 MagickBooleanType
3363 status = MagickTrue;
3364
3365 MinimaInfo
3366 initial_minima,
3367 minima_info = { MagickMaximumValue, 0, 0 };
3368
3369 ssize_t
3370 y;
3371
3372 /*
3373 Identify the minima value in the image and its location.
3374 */
3375 image_view=AcquireVirtualCacheView(image,exception);
3376 q=GetCacheViewVirtualPixels(image_view,minima_info.x,minima_info.y,1,1,
3377 exception);
3378 if (q != (const Quantum *) NULL)
3379 minima_info.minima=IsNaN((double) q[0]) != 0 ? 0.0 : (double) q[0];
3380 initial_minima=minima_info;
3381#if defined(MAGICKCORE_OPENMP_SUPPORT)
3382 #pragma omp parallel for schedule(static) shared(minima_info,status) \
3383 magick_number_threads(image,image,image->rows,1)
3384#endif
3385 for (y=0; y < (ssize_t) image->rows; y++)
3386 {
3387 const Quantum
3388 *magick_restrict p;
3389
3390 MinimaInfo
3391 channel_minima;
3392
3393 ssize_t
3394 x;
3395
3396 if (status == MagickFalse)
3397 continue;
3398 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3399 if (p == (const Quantum *) NULL)
3400 {
3401 status=MagickFalse;
3402 continue;
3403 }
3404 channel_minima=initial_minima;
3405 for (x=0; x < (ssize_t) image->columns; x++)
3406 {
3407 ssize_t
3408 i;
3409
3410 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3411 {
3412 double
3413 pixel;
3414
3415 PixelChannel channel = GetPixelChannelChannel(image,i);
3416 PixelTrait traits = GetPixelChannelTraits(image,channel);
3417 if ((traits & UpdatePixelTrait) == 0)
3418 continue;
3419 pixel=(double) p[i];
3420 if (IsNaN(pixel) != 0)
3421 pixel=0.0;
3422 if (pixel < channel_minima.minima)
3423 {
3424 channel_minima.minima=pixel;
3425 channel_minima.x=x;
3426 channel_minima.y=y;
3427 }
3428 }
3429 p+=(ptrdiff_t) GetPixelChannels(image);
3430 }
3431#if defined(MAGICKCORE_OPENMP_SUPPORT)
3432 #pragma omp critical (MagickCore_SIMMinimaImage)
3433#endif
3434 if (channel_minima.minima < minima_info.minima)
3435 minima_info=channel_minima;
3436 }
3437 image_view=DestroyCacheView(image_view);
3438 *minima=minima_info.minima;
3439 offset->x=minima_info.x;
3440 offset->y=minima_info.y;
3441 return(status);
3442}
3443
3444static MagickBooleanType SIMMultiplyImage(Image *image,const double factor,
3445 const ChannelStatistics *channel_statistics,ExceptionInfo *exception)
3446{
3447 CacheView
3448 *image_view;
3449
3450 MagickBooleanType
3451 status = MagickTrue;
3452
3453 ssize_t
3454 y;
3455
3456 /*
3457 Multiply each pixel by a factor.
3458 */
3459 image_view=AcquireAuthenticCacheView(image,exception);
3460#if defined(MAGICKCORE_OPENMP_SUPPORT)
3461 #pragma omp parallel for schedule(static) shared(status) \
3462 magick_number_threads(image,image,image->rows,1)
3463#endif
3464 for (y=0; y < (ssize_t) image->rows; y++)
3465 {
3466 Quantum
3467 *magick_restrict q;
3468
3469 ssize_t
3470 x;
3471
3472 if (status == MagickFalse)
3473 continue;
3474 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3475 if (q == (Quantum *) NULL)
3476 {
3477 status=MagickFalse;
3478 continue;
3479 }
3480 for (x=0; x < (ssize_t) image->columns; x++)
3481 {
3482 ssize_t
3483 i;
3484
3485 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3486 {
3487 PixelChannel channel = GetPixelChannelChannel(image,i);
3488 PixelTrait traits = GetPixelChannelTraits(image,channel);
3489 if ((traits & UpdatePixelTrait) == 0)
3490 continue;
3491 if (channel_statistics != (const ChannelStatistics *) NULL)
3492 q[i]=(Quantum) (factor*(double) q[i]*QuantumScale*
3493 channel_statistics[channel].standard_deviation);
3494 else
3495 q[i]=(Quantum) (factor*(double) q[i]);
3496 }
3497 q+=(ptrdiff_t) GetPixelChannels(image);
3498 }
3499 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3500 status=MagickFalse;
3501 }
3502 image_view=DestroyCacheView(image_view);
3503 return(status);
3504}
3505
3506static Image *SIMPhaseCorrelationImage(const Image *target_image,
3507 const Image *reconstruct_image,const Image *magnitude_image,
3508 ExceptionInfo *exception)
3509{
3510 Image
3511 *target_fft = (Image *) NULL,
3512 *reconstruct_fft = (Image *) NULL,
3513 *complex_multiplication = (Image *) NULL,
3514 *cross_correlation = (Image *) NULL;
3515
3516 /*
3517 Take the FFT of the reconstruction image.
3518 */
3519 reconstruct_fft=CloneImage(reconstruct_image,0,0,MagickTrue,exception);
3520 if (reconstruct_fft == NULL)
3521 return((Image *) NULL);
3522 (void) SetImageArtifact(reconstruct_fft,"fourier:normalize","inverse");
3523 reconstruct_fft=ForwardFourierTransformImage(reconstruct_fft,MagickFalse,
3524 exception);
3525 if (reconstruct_fft == NULL)
3526 return((Image *) NULL);
3527 /*
3528 Take the FFT of the target image.
3529 */
3530 target_fft=CloneImage(target_image,0,0,MagickTrue,exception);
3531 if (target_fft == (Image *) NULL)
3532 {
3533 reconstruct_fft=DestroyImageList(reconstruct_fft);
3534 return((Image *) NULL);
3535 }
3536 (void) SetImageArtifact(target_fft,"fourier:normalize","inverse");
3537 target_fft=ForwardFourierTransformImage(target_fft,MagickFalse,exception);
3538 if (target_fft == (Image *) NULL)
3539 {
3540 reconstruct_fft=DestroyImageList(reconstruct_fft);
3541 return((Image *) NULL);
3542 }
3543 /*
3544 Take the complex conjugate of the reconstruction FFT.
3545 */
3546 reconstruct_fft=ComplexImages(reconstruct_fft,ConjugateComplexOperator,
3547 exception);
3548 if (reconstruct_fft == (Image *) NULL)
3549 {
3550 target_fft=DestroyImageList(target_fft);
3551 return((Image *) NULL);
3552 }
3553 /*
3554 Do complex multiplication.
3555 */
3556 AppendImageToList(&reconstruct_fft,target_fft);
3557 DisableCompositeClampUnlessSpecified(reconstruct_fft);
3558 DisableCompositeClampUnlessSpecified(reconstruct_fft->next);
3559 complex_multiplication=ComplexImages(reconstruct_fft,MultiplyComplexOperator,
3560 exception);
3561 reconstruct_fft=DestroyImageList(reconstruct_fft);
3562 if (complex_multiplication == (Image *) NULL)
3563 return((Image *) NULL);
3564 if (complex_multiplication->next != (Image *) NULL)
3565 {
3566 /*
3567 Normalize the cross-power spectrum by the product magnitude.
3568 */
3569 DisableCompositeClampUnlessSpecified(complex_multiplication);
3570 DisableCompositeClampUnlessSpecified(complex_multiplication->next);
3571 (void) CompositeImage(complex_multiplication,magnitude_image,
3572 DivideSrcCompositeOp,MagickTrue,0,0,exception);
3573 (void) CompositeImage(complex_multiplication->next,magnitude_image,
3574 DivideSrcCompositeOp,MagickTrue,0,0,exception);
3575 }
3576 /*
3577 Do the IFT and return the phase-correlation result.
3578 */
3579 (void) SetImageArtifact(complex_multiplication,"fourier:normalize","inverse");
3580 cross_correlation=InverseFourierTransformImage(complex_multiplication,
3581 complex_multiplication->next,MagickFalse,exception);
3582 complex_multiplication=DestroyImageList(complex_multiplication);
3583 return(cross_correlation);
3584}
3585
3586static MagickBooleanType SIMSetImageMean(Image *image,
3587 const ChannelStatistics *channel_statistics,ExceptionInfo *exception)
3588{
3589 CacheView
3590 *image_view;
3591
3592 MagickBooleanType
3593 status = MagickTrue;
3594
3595 ssize_t
3596 y;
3597
3598 /*
3599 Set image mean.
3600 */
3601 image_view=AcquireAuthenticCacheView(image,exception);
3602#if defined(MAGICKCORE_OPENMP_SUPPORT)
3603 #pragma omp parallel for schedule(static) shared(status) \
3604 magick_number_threads(image,image,image->rows,1)
3605#endif
3606 for (y=0; y < (ssize_t) image->rows; y++)
3607 {
3608 Quantum
3609 *magick_restrict q;
3610
3611 ssize_t
3612 x;
3613
3614 if (status == MagickFalse)
3615 continue;
3616 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3617 if (q == (Quantum *) NULL)
3618 {
3619 status=MagickFalse;
3620 continue;
3621 }
3622 for (x=0; x < (ssize_t) image->columns; x++)
3623 {
3624 ssize_t
3625 i;
3626
3627 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3628 {
3629 PixelChannel channel = GetPixelChannelChannel(image,i);
3630 PixelTrait traits = GetPixelChannelTraits(image,channel);
3631 if ((traits & UpdatePixelTrait) == 0)
3632 continue;
3633 q[i]=(Quantum) channel_statistics[channel].mean;
3634 }
3635 q+=(ptrdiff_t) GetPixelChannels(image);
3636 }
3637 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3638 status=MagickFalse;
3639 }
3640 image_view=DestroyCacheView(image_view);
3641 return(status);
3642}
3643
3644static Image *SIMSubtractImageMean(const Image *alpha_image,
3645 const Image *beta_image,const ChannelStatistics *channel_statistics,
3646 ExceptionInfo *exception)
3647{
3648 CacheView
3649 *beta_view,
3650 *image_view;
3651
3652 Image
3653 *subtract_image;
3654
3655 MagickBooleanType
3656 status = MagickTrue;
3657
3658 ssize_t
3659 y;
3660
3661 /*
3662 Subtract the image mean and pad.
3663 */
3664 subtract_image=CloneImage(beta_image,alpha_image->columns,alpha_image->rows,
3665 MagickTrue,exception);
3666 if (subtract_image == (Image *) NULL)
3667 return(subtract_image);
3668 image_view=AcquireAuthenticCacheView(subtract_image,exception);
3669 beta_view=AcquireVirtualCacheView(beta_image,exception);
3670#if defined(MAGICKCORE_OPENMP_SUPPORT)
3671 #pragma omp parallel for schedule(static) shared(status) \
3672 magick_number_threads(beta_image,subtract_image,subtract_image->rows,1)
3673#endif
3674 for (y=0; y < (ssize_t) subtract_image->rows; y++)
3675 {
3676 const Quantum
3677 *magick_restrict p;
3678
3679 Quantum
3680 *magick_restrict q;
3681
3682 ssize_t
3683 x;
3684
3685 if (status == MagickFalse)
3686 continue;
3687 p=GetCacheViewVirtualPixels(beta_view,0,y,subtract_image->columns,1,
3688 exception);
3689 q=GetCacheViewAuthenticPixels(image_view,0,y,subtract_image->columns,1,
3690 exception);
3691 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3692 {
3693 status=MagickFalse;
3694 continue;
3695 }
3696 for (x=0; x < (ssize_t) subtract_image->columns; x++)
3697 {
3698 ssize_t
3699 i;
3700
3701 for (i=0; i < (ssize_t) GetPixelChannels(subtract_image); i++)
3702 {
3703 PixelChannel channel = GetPixelChannelChannel(subtract_image,i);
3704 PixelTrait traits = GetPixelChannelTraits(subtract_image,channel);
3705 PixelTrait beta_traits = GetPixelChannelTraits(beta_image,channel);
3706 if (((traits & UpdatePixelTrait) == 0) ||
3707 ((beta_traits & UpdatePixelTrait) == 0))
3708 continue;
3709 if ((x >= (ssize_t) beta_image->columns) ||
3710 (y >= (ssize_t) beta_image->rows))
3711 q[i]=(Quantum) 0;
3712 else
3713 q[i]=(Quantum) ((double) GetPixelChannel(beta_image,channel,p)-
3714 channel_statistics[channel].mean);
3715 }
3716 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3717 q+=(ptrdiff_t) GetPixelChannels(subtract_image);
3718 }
3719 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3720 status=MagickFalse;
3721 }
3722 beta_view=DestroyCacheView(beta_view);
3723 image_view=DestroyCacheView(image_view);
3724 if (status == MagickFalse)
3725 subtract_image=DestroyImage(subtract_image);
3726 return(subtract_image);
3727}
3728
3729static Image *SIMUnityImage(const Image *alpha_image,const Image *beta_image,
3730 ExceptionInfo *exception)
3731{
3732 CacheView
3733 *image_view;
3734
3735 Image
3736 *unity_image;
3737
3738 MagickBooleanType
3739 status = MagickTrue;
3740
3741 ssize_t
3742 y;
3743
3744 /*
3745 Create a padded unity image.
3746 */
3747 unity_image=CloneImage(alpha_image,alpha_image->columns,alpha_image->rows,
3748 MagickTrue,exception);
3749 if (unity_image == (Image *) NULL)
3750 return(unity_image);
3751 if (SetImageStorageClass(unity_image,DirectClass,exception) == MagickFalse)
3752 return(DestroyImage(unity_image));
3753 image_view=AcquireAuthenticCacheView(unity_image,exception);
3754#if defined(MAGICKCORE_OPENMP_SUPPORT)
3755 #pragma omp parallel for schedule(static) shared(status) \
3756 magick_number_threads(unity_image,unity_image,unity_image->rows,1)
3757#endif
3758 for (y=0; y < (ssize_t) unity_image->rows; y++)
3759 {
3760 Quantum
3761 *magick_restrict q;
3762
3763 ssize_t
3764 x;
3765
3766 if (status == MagickFalse)
3767 continue;
3768 q=GetCacheViewAuthenticPixels(image_view,0,y,unity_image->columns,1,
3769 exception);
3770 if (q == (Quantum *) NULL)
3771 {
3772 status=MagickFalse;
3773 continue;
3774 }
3775 for (x=0; x < (ssize_t) unity_image->columns; x++)
3776 {
3777 ssize_t
3778 i;
3779
3780 for (i=0; i < (ssize_t) GetPixelChannels(unity_image); i++)
3781 {
3782 PixelChannel channel = GetPixelChannelChannel(unity_image,i);
3783 PixelTrait traits = GetPixelChannelTraits(unity_image,channel);
3784 if ((traits & UpdatePixelTrait) == 0)
3785 continue;
3786 if ((x >= (ssize_t) beta_image->columns) ||
3787 (y >= (ssize_t) beta_image->rows))
3788 q[i]=(Quantum) 0;
3789 else
3790 q[i]=QuantumRange;
3791 }
3792 q+=(ptrdiff_t) GetPixelChannels(unity_image);
3793 }
3794 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3795 status=MagickFalse;
3796 }
3797 image_view=DestroyCacheView(image_view);
3798 if (status == MagickFalse)
3799 unity_image=DestroyImage(unity_image);
3800 return(unity_image);
3801}
3802
3803static Image *SIMVarianceImage(Image *alpha_image,const Image *beta_image,
3804 ExceptionInfo *exception)
3805{
3806 CacheView
3807 *beta_view,
3808 *image_view;
3809
3810 Image
3811 *variance_image;
3812
3813 MagickBooleanType
3814 status = MagickTrue;
3815
3816 ssize_t
3817 y;
3818
3819 /*
3820 Compute the variance of the two images.
3821 */
3822 variance_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
3823 if (variance_image == (Image *) NULL)
3824 return(variance_image);
3825 image_view=AcquireAuthenticCacheView(variance_image,exception);
3826 beta_view=AcquireVirtualCacheView(beta_image,exception);
3827#if defined(MAGICKCORE_OPENMP_SUPPORT)
3828 #pragma omp parallel for schedule(static) shared(status) \
3829 magick_number_threads(beta_image,variance_image,variance_image->rows,1)
3830#endif
3831 for (y=0; y < (ssize_t) variance_image->rows; y++)
3832 {
3833 const Quantum
3834 *magick_restrict p;
3835
3836 Quantum
3837 *magick_restrict q;
3838
3839 ssize_t
3840 x;
3841
3842 if (status == MagickFalse)
3843 continue;
3844 p=GetCacheViewVirtualPixels(beta_view,0,y,variance_image->columns,1,
3845 exception);
3846 q=GetCacheViewAuthenticPixels(image_view,0,y,variance_image->columns,1,
3847 exception);
3848 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3849 {
3850 status=MagickFalse;
3851 continue;
3852 }
3853 for (x=0; x < (ssize_t) variance_image->columns; x++)
3854 {
3855 ssize_t
3856 i;
3857
3858 for (i=0; i < (ssize_t) GetPixelChannels(variance_image); i++)
3859 {
3860 double
3861 error;
3862
3863 PixelChannel channel = GetPixelChannelChannel(variance_image,i);
3864 PixelTrait traits = GetPixelChannelTraits(variance_image,channel);
3865 PixelTrait beta_traits = GetPixelChannelTraits(beta_image,channel);
3866 if (((traits & UpdatePixelTrait) == 0) ||
3867 ((beta_traits & UpdatePixelTrait) == 0))
3868 continue;
3869 error=(double) q[i]-(double) GetPixelChannel(beta_image,channel,p);
3870 q[i]=(Quantum) ((double) ClampToQuantum((double) QuantumRange*
3871 (sqrt(fabs(QuantumScale*error))/sqrt((double) QuantumRange))));
3872 }
3873 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3874 q+=(ptrdiff_t) GetPixelChannels(variance_image);
3875 }
3876 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3877 status=MagickFalse;
3878 }
3879 beta_view=DestroyCacheView(beta_view);
3880 image_view=DestroyCacheView(image_view);
3881 if (status == MagickFalse)
3882 variance_image=DestroyImage(variance_image);
3883 return(variance_image);
3884}
3885
3886static Image *DPCSimilarityImage(const Image *image,const Image *reconstruct,
3887 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
3888{
3889#define ThrowDPCSimilarityException() \
3890{ \
3891 if (dot_product_image != (Image *) NULL) \
3892 dot_product_image=DestroyImage(dot_product_image); \
3893 if (magnitude_image != (Image *) NULL) \
3894 magnitude_image=DestroyImage(magnitude_image); \
3895 if (reconstruct_image != (Image *) NULL) \
3896 reconstruct_image=DestroyImage(reconstruct_image); \
3897 if (rx_image != (Image *) NULL) \
3898 rx_image=DestroyImage(rx_image); \
3899 if (ry_image != (Image *) NULL) \
3900 ry_image=DestroyImage(ry_image); \
3901 if (target_image != (Image *) NULL) \
3902 target_image=DestroyImage(target_image); \
3903 if (threshold_image != (Image *) NULL) \
3904 threshold_image=DestroyImage(threshold_image); \
3905 if (trx_image != (Image *) NULL) \
3906 trx_image=DestroyImage(trx_image); \
3907 if (try_image != (Image *) NULL) \
3908 try_image=DestroyImage(try_image); \
3909 if (tx_image != (Image *) NULL) \
3910 tx_image=DestroyImage(tx_image); \
3911 if (ty_image != (Image *) NULL) \
3912 ty_image=DestroyImage(ty_image); \
3913 return((Image *) NULL); \
3914}
3915
3916 double
3917 edge_factor = 0.0,
3918 maxima = 0.0,
3919 mean = 0.0,
3920 standard_deviation = 0.0;
3921
3922 Image
3923 *dot_product_image = (Image *) NULL,
3924 *magnitude_image = (Image *) NULL,
3925 *reconstruct_image = (Image *) NULL,
3926 *rx_image = (Image *) NULL,
3927 *ry_image = (Image *) NULL,
3928 *trx_image = (Image *) NULL,
3929 *target_image = (Image *) NULL,
3930 *threshold_image = (Image *) NULL,
3931 *try_image = (Image *) NULL,
3932 *tx_image = (Image *) NULL,
3933 *ty_image = (Image *) NULL;
3934
3935 MagickBooleanType
3936 status = MagickTrue;
3937
3938 RectangleInfo
3939 geometry;
3940
3941 /*
3942 Dot product correlation-based image similarity using FFT local statistics.
3943 */
3944 target_image=CloneImage(image,0,0,MagickTrue,exception);
3945 if (target_image == (Image *) NULL)
3946 return((Image *) NULL);
3947 /*
3948 Compute the cross correlation of the test and reconstruct magnitudes.
3949 */
3950 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3951 if (reconstruct_image == (Image *) NULL)
3952 ThrowDPCSimilarityException();
3953 /*
3954 Compute X and Y derivatives of reference image.
3955 */
3956 (void) SetImageVirtualPixelMethod(reconstruct_image,EdgeVirtualPixelMethod,
3957 exception);
3958 rx_image=SIMDerivativeImage(reconstruct_image,"Sobel",exception);
3959 if (rx_image == (Image *) NULL)
3960 ThrowDPCSimilarityException();
3961 ry_image=SIMDerivativeImage(reconstruct_image,"Sobel:90",exception);
3962 reconstruct_image=DestroyImage(reconstruct_image);
3963 if (ry_image == (Image *) NULL)
3964 ThrowDPCSimilarityException();
3965 /*
3966 Compute magnitude of derivatives.
3967 */
3968 magnitude_image=SIMMagnitudeImage(rx_image,ry_image,exception);
3969 if (magnitude_image == (Image *) NULL)
3970 ThrowDPCSimilarityException();
3971 /*
3972 Compute an edge normalization correction.
3973 */
3974 threshold_image=CloneImage(magnitude_image,0,0,MagickTrue,exception);
3975 if (threshold_image == (Image *) NULL)
3976 ThrowDPCSimilarityException();
3977 status=BilevelImage(threshold_image,0.0,exception);
3978 if (status == MagickFalse)
3979 ThrowDPCSimilarityException();
3980 status=GetImageMean(threshold_image,&mean,&standard_deviation,exception);
3981 threshold_image=DestroyImage(threshold_image);
3982 if (status == MagickFalse)
3983 ThrowDPCSimilarityException();
3984 edge_factor=MagickSafeReciprocal(QuantumScale*mean*(double)
3985 reconstruct->columns*(double) reconstruct->rows)+QuantumScale;
3986 /*
3987 Divide X and Y derivitives of reference image by magnitude.
3988 */
3989 trx_image=SIMDivideByMagnitude(rx_image,magnitude_image,image,exception);
3990 rx_image=DestroyImage(rx_image);
3991 if (trx_image == (Image *) NULL)
3992 ThrowDPCSimilarityException();
3993 rx_image=trx_image;
3994 trx_image=(Image *) NULL;
3995 try_image=SIMDivideByMagnitude(ry_image,magnitude_image,image,exception);
3996 magnitude_image=DestroyImage(magnitude_image);
3997 ry_image=DestroyImage(ry_image);
3998 if (try_image == (Image *) NULL)
3999 ThrowDPCSimilarityException();
4000 ry_image=try_image;
4001 try_image=(Image *) NULL;
4002 /*
4003 Compute X and Y derivatives of image.
4004 */
4005 (void) SetImageVirtualPixelMethod(target_image,EdgeVirtualPixelMethod,
4006 exception);
4007 tx_image=SIMDerivativeImage(target_image,"Sobel",exception);
4008 if (tx_image == (Image *) NULL)
4009 ThrowDPCSimilarityException();
4010 ty_image=SIMDerivativeImage(target_image,"Sobel:90",exception);
4011 target_image=DestroyImage(target_image);
4012 if (ty_image == (Image *) NULL)
4013 ThrowDPCSimilarityException();
4014 /*
4015 Compute magnitude of derivatives.
4016 */
4017 magnitude_image=SIMMagnitudeImage(tx_image,ty_image,exception);
4018 if (magnitude_image == (Image *) NULL)
4019 ThrowDPCSimilarityException();
4020 /*
4021 Divide Lx and Ly by magnitude.
4022 */
4023 trx_image=SIMDivideByMagnitude(tx_image,magnitude_image,image,exception);
4024 tx_image=DestroyImage(tx_image);
4025 if (trx_image == (Image *) NULL)
4026 ThrowDPCSimilarityException();
4027 tx_image=trx_image;
4028 trx_image=(Image *) NULL;
4029 try_image=SIMDivideByMagnitude(ty_image,magnitude_image,image,exception);
4030 ty_image=DestroyImage(ty_image);
4031 magnitude_image=DestroyImage(magnitude_image);
4032 if (try_image == (Image *) NULL)
4033 ThrowDPCSimilarityException();
4034 ty_image=try_image;
4035 try_image=(Image *) NULL;
4036 /*
4037 Compute the cross correlation of the test and reference images.
4038 */
4039 trx_image=SIMCrossCorrelationImage(tx_image,rx_image,exception);
4040 rx_image=DestroyImage(rx_image);
4041 tx_image=DestroyImage(tx_image);
4042 if (trx_image == (Image *) NULL)
4043 ThrowDPCSimilarityException();
4044 try_image=SIMCrossCorrelationImage(ty_image,ry_image,exception);
4045 ry_image=DestroyImage(ry_image);
4046 ty_image=DestroyImage(ty_image);
4047 if (try_image == (Image *) NULL)
4048 ThrowDPCSimilarityException();
4049 /*
4050 Evaluate dot product correlation image.
4051 */
4052 (void) SetImageArtifact(try_image,"compose:clamp","false");
4053 status=CompositeImage(trx_image,try_image,PlusCompositeOp,MagickTrue,0,0,
4054 exception);
4055 try_image=DestroyImage(try_image);
4056 if (status == MagickFalse)
4057 ThrowDPCSimilarityException();
4058 status=SIMMultiplyImage(trx_image,edge_factor,
4059 (const ChannelStatistics *) NULL,exception);
4060 if (status == MagickFalse)
4061 ThrowDPCSimilarityException();
4062 /*
4063 Crop results.
4064 */
4065 SetGeometry(image,&geometry);
4066 geometry.width=image->columns;
4067 geometry.height=image->rows;
4068 (void) ResetImagePage(trx_image,"0x0+0+0");
4069 dot_product_image=CropImage(trx_image,&geometry,exception);
4070 trx_image=DestroyImage(trx_image);
4071 if (dot_product_image == (Image *) NULL)
4072 ThrowDPCSimilarityException();
4073 (void) ResetImagePage(dot_product_image,"0x0+0+0");
4074 /*
4075 Identify the maxima value in the image and its location.
4076 */
4077 status=GrayscaleImage(dot_product_image,AveragePixelIntensityMethod,
4078 exception);
4079 if (status == MagickFalse)
4080 ThrowDPCSimilarityException();
4081 dot_product_image->depth=32;
4082 dot_product_image->colorspace=GRAYColorspace;
4083 dot_product_image->alpha_trait=UndefinedPixelTrait;
4084 status=SIMFilterImageNaNs(dot_product_image,exception);
4085 if (status == MagickFalse)
4086 ThrowDPCSimilarityException();
4087 status=SIMMaximaImage(dot_product_image,&maxima,offset,exception);
4088 if (status == MagickFalse)
4089 ThrowDPCSimilarityException();
4090 if ((QuantumScale*maxima) > 1.0)
4091 {
4092 status=SIMMultiplyImage(dot_product_image,1.0/(QuantumScale*maxima),
4093 (const ChannelStatistics *) NULL,exception);
4094 maxima=(double) QuantumRange;
4095 }
4096 *similarity_metric=QuantumScale*maxima;
4097 return(dot_product_image);
4098}
4099
4100static Image *MSESimilarityImage(const Image *image,const Image *reconstruct,
4101 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
4102{
4103#define ThrowMSESimilarityException() \
4104{ \
4105 if (alpha_image != (Image *) NULL) \
4106 alpha_image=DestroyImage(alpha_image); \
4107 if (beta_image != (Image *) NULL) \
4108 beta_image=DestroyImage(beta_image); \
4109 if (channel_statistics != (ChannelStatistics *) NULL) \
4110 channel_statistics=(ChannelStatistics *) \
4111 RelinquishMagickMemory(channel_statistics); \
4112 if (mean_image != (Image *) NULL) \
4113 mean_image=DestroyImage(mean_image); \
4114 if (mse_image != (Image *) NULL) \
4115 mse_image=DestroyImage(mse_image); \
4116 if (reconstruct_image != (Image *) NULL) \
4117 reconstruct_image=DestroyImage(reconstruct_image); \
4118 if (sum_image != (Image *) NULL) \
4119 sum_image=DestroyImage(sum_image); \
4120 if (alpha_image != (Image *) NULL) \
4121 alpha_image=DestroyImage(alpha_image); \
4122 return((Image *) NULL); \
4123}
4124
4125 ChannelStatistics
4126 *channel_statistics = (ChannelStatistics *) NULL;
4127
4128 double
4129 minima = 0.0;
4130
4131 Image
4132 *alpha_image = (Image *) NULL,
4133 *beta_image = (Image *) NULL,
4134 *mean_image = (Image *) NULL,
4135 *mse_image = (Image *) NULL,
4136 *reconstruct_image = (Image *) NULL,
4137 *sum_image = (Image *) NULL,
4138 *target_image = (Image *) NULL;
4139
4140 MagickBooleanType
4141 status = MagickTrue;
4142
4143 RectangleInfo
4144 geometry;
4145
4146 /*
4147 MSE correlation-based image similarity using FFT local statistics.
4148 */
4149 target_image=SIMSquareImage(image,exception);
4150 if (target_image == (Image *) NULL)
4151 ThrowMSESimilarityException();
4152 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
4153 if (reconstruct_image == (Image *) NULL)
4154 ThrowMSESimilarityException();
4155 /*
4156 Create (U * test)/# pixels.
4157 */
4158 alpha_image=SIMCrossCorrelationImage(target_image,reconstruct_image,
4159 exception);
4160 target_image=DestroyImage(target_image);
4161 if (alpha_image == (Image *) NULL)
4162 ThrowMSESimilarityException();
4163 status=SIMMultiplyImage(alpha_image,1.0/(double) reconstruct->columns/(double)
4164 reconstruct->rows,(const ChannelStatistics *) NULL,exception);
4165 if (status == MagickFalse)
4166 ThrowMSESimilarityException();
4167 /*
4168 Create 2*(test * reconstruction)# pixels.
4169 */
4170 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
4171 MagickTrue,0,0,exception);
4172 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
4173 if (beta_image == (Image *) NULL)
4174 {
4175 reconstruct_image=DestroyImage(reconstruct_image);
4176 ThrowMSESimilarityException();
4177 }
4178 status=SIMMultiplyImage(beta_image,-2.0/(double) reconstruct->columns/(double)
4179 reconstruct->rows,(const ChannelStatistics *) NULL,exception);
4180 reconstruct_image=DestroyImage(reconstruct_image);
4181 if (status == MagickFalse)
4182 ThrowMSESimilarityException();
4183 /*
4184 Mean of reconstruction squared.
4185 */
4186 sum_image=SIMSquareImage(reconstruct,exception);
4187 if (sum_image == (Image *) NULL)
4188 ThrowMSESimilarityException();
4189 channel_statistics=GetImageStatistics(sum_image,exception);
4190 if (channel_statistics == (ChannelStatistics *) NULL)
4191 ThrowMSESimilarityException();
4192 status=SetImageStorageClass(sum_image,DirectClass,exception);
4193 if (status == MagickFalse)
4194 ThrowMSESimilarityException();
4195 status=SIMSetImageMean(sum_image,channel_statistics,exception);
4196 channel_statistics=(ChannelStatistics *)
4197 RelinquishMagickMemory(channel_statistics);
4198 if (status == MagickFalse)
4199 ThrowMSESimilarityException();
4200 /*
4201 Create mean image.
4202 */
4203 AppendImageToList(&sum_image,alpha_image);
4204 AppendImageToList(&sum_image,beta_image);
4205 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
4206 if (mean_image == (Image *) NULL)
4207 ThrowMSESimilarityException();
4208 sum_image=DestroyImage(sum_image);
4209 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
4210 if (status == MagickFalse)
4211 ThrowMSESimilarityException();
4212 /*
4213 Crop to difference of reconstruction and test images.
4214 */
4215 SetGeometry(image,&geometry);
4216 geometry.width=image->columns;
4217 geometry.height=image->rows;
4218 (void) ResetImagePage(mean_image,"0x0+0+0");
4219 mse_image=CropImage(mean_image,&geometry,exception);
4220 mean_image=DestroyImage(mean_image);
4221 if (mse_image == (Image *) NULL)
4222 ThrowMSESimilarityException();
4223 /*
4224 Identify the minima value in the correlation image and its location.
4225 */
4226 (void) ResetImagePage(mse_image,"0x0+0+0");
4227 (void) ClampImage(mse_image,exception);
4228 mse_image->depth=32;
4229 mse_image->colorspace=GRAYColorspace;
4230 mse_image->alpha_trait=UndefinedPixelTrait;
4231 status=SIMMinimaImage(mse_image,&minima,offset,exception);
4232 if (status == MagickFalse)
4233 ThrowMSESimilarityException();
4234 status=NegateImage(mse_image,MagickFalse,exception);
4235 if (status == MagickFalse)
4236 ThrowMSESimilarityException();
4237 alpha_image=DestroyImage(alpha_image);
4238 beta_image=DestroyImage(beta_image);
4239 if ((QuantumScale*minima) < (double) FLT_EPSILON)
4240 minima=0.0;
4241 *similarity_metric=QuantumScale*minima;
4242 return(mse_image);
4243}
4244
4245static Image *NCCSimilarityImage(const Image *image,const Image *reconstruct,
4246 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
4247{
4248#define ThrowNCCSimilarityException() \
4249{ \
4250 if (alpha_image != (Image *) NULL) \
4251 alpha_image=DestroyImage(alpha_image); \
4252 if (beta_image != (Image *) NULL) \
4253 beta_image=DestroyImage(beta_image); \
4254 if (channel_statistics != (ChannelStatistics *) NULL) \
4255 channel_statistics=(ChannelStatistics *) \
4256 RelinquishMagickMemory(channel_statistics); \
4257 if (correlation_image != (Image *) NULL) \
4258 correlation_image=DestroyImage(correlation_image); \
4259 if (divide_image != (Image *) NULL) \
4260 divide_image=DestroyImage(divide_image); \
4261 if (ncc_image != (Image *) NULL) \
4262 ncc_image=DestroyImage(ncc_image); \
4263 if (normalize_image != (Image *) NULL) \
4264 normalize_image=DestroyImage(normalize_image); \
4265 if (reconstruct_image != (Image *) NULL) \
4266 reconstruct_image=DestroyImage(reconstruct_image); \
4267 if (target_image != (Image *) NULL) \
4268 target_image=DestroyImage(target_image); \
4269 if (variance_image != (Image *) NULL) \
4270 variance_image=DestroyImage(variance_image); \
4271 return((Image *) NULL); \
4272}
4273
4274 ChannelStatistics
4275 *channel_statistics = (ChannelStatistics *) NULL;
4276
4277 double
4278 maxima = 0.0;
4279
4280 Image
4281 *alpha_image = (Image *) NULL,
4282 *beta_image = (Image *) NULL,
4283 *correlation_image = (Image *) NULL,
4284 *divide_image = (Image *) NULL,
4285 *ncc_image = (Image *) NULL,
4286 *normalize_image = (Image *) NULL,
4287 *reconstruct_image = (Image *) NULL,
4288 *target_image = (Image *) NULL,
4289 *variance_image = (Image *) NULL;
4290
4291 MagickBooleanType
4292 status = MagickTrue;
4293
4294 RectangleInfo
4295 geometry;
4296
4297 /*
4298 NCC correlation-based image similarity with FFT local statistics.
4299 */
4300 target_image=SIMSquareImage(image,exception);
4301 if (target_image == (Image *) NULL)
4302 ThrowNCCSimilarityException();
4303 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
4304 if (reconstruct_image == (Image *) NULL)
4305 ThrowNCCSimilarityException();
4306 /*
4307 Compute the cross correlation of the test and reconstruction images.
4308 */
4309 alpha_image=SIMCrossCorrelationImage(target_image,reconstruct_image,
4310 exception);
4311 target_image=DestroyImage(target_image);
4312 if (alpha_image == (Image *) NULL)
4313 ThrowNCCSimilarityException();
4314 status=SIMMultiplyImage(alpha_image,(double) QuantumRange*
4315 (double) reconstruct->columns*(double) reconstruct->rows,
4316 (const ChannelStatistics *) NULL,exception);
4317 if (status == MagickFalse)
4318 ThrowNCCSimilarityException();
4319 /*
4320 Compute the cross correlation of the source and reconstruction images.
4321 */
4322 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
4323 reconstruct_image=DestroyImage(reconstruct_image);
4324 if (beta_image == (Image *) NULL)
4325 ThrowNCCSimilarityException();
4326 target_image=SIMSquareImage(beta_image,exception);
4327 beta_image=DestroyImage(beta_image);
4328 if (target_image == (Image *) NULL)
4329 ThrowNCCSimilarityException();
4330 status=SIMMultiplyImage(target_image,(double) QuantumRange,
4331 (const ChannelStatistics *) NULL,exception);
4332 if (status == MagickFalse)
4333 ThrowNCCSimilarityException();
4334 /*
4335 Compute the variance of the two images.
4336 */
4337 variance_image=SIMVarianceImage(alpha_image,target_image,exception);
4338 target_image=DestroyImage(target_image);
4339 alpha_image=DestroyImage(alpha_image);
4340 if (variance_image == (Image *) NULL)
4341 ThrowNCCSimilarityException();
4342 /*
4343 Subtract the image mean.
4344 */
4345 channel_statistics=GetImageStatistics(reconstruct,exception);
4346 if (channel_statistics == (ChannelStatistics *) NULL)
4347 ThrowNCCSimilarityException();
4348 status=SIMMultiplyImage(variance_image,1.0,channel_statistics,exception);
4349 if (status == MagickFalse)
4350 ThrowNCCSimilarityException();
4351 normalize_image=SIMSubtractImageMean(image,reconstruct,channel_statistics,
4352 exception);
4353 channel_statistics=(ChannelStatistics *)
4354 RelinquishMagickMemory(channel_statistics);
4355 if (normalize_image == (Image *) NULL)
4356 ThrowNCCSimilarityException();
4357 correlation_image=SIMCrossCorrelationImage(image,normalize_image,exception);
4358 normalize_image=DestroyImage(normalize_image);
4359 if (correlation_image == (Image *) NULL)
4360 ThrowNCCSimilarityException();
4361 /*
4362 Divide the two images.
4363 */
4364 divide_image=SIMDivideImage(correlation_image,variance_image,exception);
4365 correlation_image=DestroyImage(correlation_image);
4366 variance_image=DestroyImage(variance_image);
4367 if (divide_image == (Image *) NULL)
4368 ThrowNCCSimilarityException();
4369 /*
4370 Crop padding.
4371 */
4372 SetGeometry(image,&geometry);
4373 geometry.width=image->columns;
4374 geometry.height=image->rows;
4375 (void) ResetImagePage(divide_image,"0x0+0+0");
4376 ncc_image=CropImage(divide_image,&geometry,exception);
4377 divide_image=DestroyImage(divide_image);
4378 if (ncc_image == (Image *) NULL)
4379 ThrowNCCSimilarityException();
4380 /*
4381 Identify the maxima value in the image and its location.
4382 */
4383 (void) ResetImagePage(ncc_image,"0x0+0+0");
4384 status=GrayscaleImage(ncc_image,AveragePixelIntensityMethod,exception);
4385 if (status == MagickFalse)
4386 ThrowNCCSimilarityException();
4387 ncc_image->depth=32;
4388 ncc_image->colorspace=GRAYColorspace;
4389 ncc_image->alpha_trait=UndefinedPixelTrait;
4390 status=SIMMaximaImage(ncc_image,&maxima,offset,exception);
4391 if (status == MagickFalse)
4392 ThrowNCCSimilarityException();
4393 if ((QuantumScale*maxima) > 1.0)
4394 {
4395 status=SIMMultiplyImage(ncc_image,1.0/(QuantumScale*maxima),
4396 (const ChannelStatistics *) NULL,exception);
4397 maxima=(double) QuantumRange;
4398 }
4399 *similarity_metric=QuantumScale*maxima;
4400 return(ncc_image);
4401}
4402
4403static Image *PhaseSimilarityImage(const Image *image,const Image *reconstruct,
4404 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
4405{
4406#define ThrowPhaseSimilarityException() \
4407{ \
4408 if (phase_image != (Image *) NULL) \
4409 phase_image=DestroyImage(phase_image); \
4410 if (gamma_image != (Image *) NULL) \
4411 gamma_image=DestroyImage(gamma_image); \
4412 if (test_magnitude != (Image *) NULL) \
4413 test_magnitude=DestroyImage(test_magnitude); \
4414 if (magnitude_image != (Image *) NULL) \
4415 magnitude_image=DestroyImage(magnitude_image); \
4416 if (reconstruct_magnitude != (Image *) NULL) \
4417 reconstruct_magnitude=DestroyImage(reconstruct_magnitude); \
4418 if (correlation_image != (Image *) NULL) \
4419 correlation_image=DestroyImage(correlation_image); \
4420 if (fft_images != (Image *) NULL) \
4421 fft_images=DestroyImageList(fft_images); \
4422 if (reconstruct_image != (Image *) NULL) \
4423 reconstruct_image=DestroyImage(reconstruct_image); \
4424 if (target_image != (Image *) NULL) \
4425 target_image=DestroyImage(target_image); \
4426 return((Image *) NULL); \
4427}
4428
4429 double
4430 maxima = 0.0;
4431
4432 Image
4433 *correlation_image = (Image *) NULL,
4434 *fft_images = (Image *) NULL,
4435 *gamma_image = (Image *) NULL,
4436 *magnitude_image = (Image *) NULL,
4437 *phase_image = (Image *) NULL,
4438 *reconstruct_image = (Image *) NULL,
4439 *reconstruct_magnitude = (Image *) NULL,
4440 *target_image = (Image *) NULL,
4441 *test_magnitude = (Image *) NULL;
4442
4443 MagickBooleanType
4444 status = MagickTrue;
4445
4446 RectangleInfo
4447 geometry;
4448
4449 /*
4450 Phase correlation-based image similarity using FFT local statistics.
4451 */
4452 target_image=CloneImage(image,0,0,MagickTrue,exception);
4453 if (target_image == (Image *) NULL)
4454 ThrowPhaseSimilarityException();
4455 (void) ResetImagePage(target_image,"0x0+0+0");
4456 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
4457 &target_image->background_color);
4458 status=SetImageExtent(target_image,2*CastDoubleToSizeT(ceil((double)
4459 image->columns/2.0)),2*CastDoubleToSizeT(ceil((double) image->rows/2.0)),
4460 exception);
4461 if (status == MagickFalse)
4462 ThrowPhaseSimilarityException();
4463 /*
4464 Compute the cross correlation of the target and reconstruct magnitudes.
4465 */
4466 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
4467 if (reconstruct_image == (Image *) NULL)
4468 ThrowPhaseSimilarityException();
4469 (void) ResetImagePage(reconstruct_image,"0x0+0+0");
4470 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
4471 &reconstruct_image->background_color);
4472 status=SetImageExtent(reconstruct_image,2*CastDoubleToSizeT(ceil((double)
4473 image->columns/2.0)),2*CastDoubleToSizeT(ceil((double) image->rows/2.0)),
4474 exception);
4475 if (status == MagickFalse)
4476 ThrowPhaseSimilarityException();
4477 /*
4478 Evaluate phase coorelation image and divide by the product magnitude.
4479 */
4480 (void) SetImageArtifact(target_image,"fourier:normalize","inverse");
4481 fft_images=ForwardFourierTransformImage(target_image,MagickTrue,exception);
4482 if (fft_images == (Image *) NULL)
4483 ThrowPhaseSimilarityException();
4484 test_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
4485 fft_images=DestroyImageList(fft_images);
4486 if (test_magnitude == (Image *) NULL)
4487 ThrowPhaseSimilarityException();
4488 (void) SetImageArtifact(reconstruct_image,"fourier:normalize","inverse");
4489 fft_images=ForwardFourierTransformImage(reconstruct_image,MagickTrue,
4490 exception);
4491 if (fft_images == (Image *) NULL)
4492 ThrowPhaseSimilarityException();
4493 reconstruct_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
4494 fft_images=DestroyImageList(fft_images);
4495 if (reconstruct_magnitude == (Image *) NULL)
4496 ThrowPhaseSimilarityException();
4497 magnitude_image=CloneImage(reconstruct_magnitude,0,0,MagickTrue,exception);
4498 if (magnitude_image == (Image *) NULL)
4499 ThrowPhaseSimilarityException();
4500 DisableCompositeClampUnlessSpecified(magnitude_image);
4501 (void) CompositeImage(magnitude_image,test_magnitude,MultiplyCompositeOp,
4502 MagickTrue,0,0,exception);
4503 /*
4504 Compute the cross correlation of the target and reconstruction images.
4505 */
4506 correlation_image=SIMPhaseCorrelationImage(target_image,reconstruct_image,
4507 magnitude_image,exception);
4508 target_image=DestroyImage(target_image);
4509 reconstruct_image=DestroyImage(reconstruct_image);
4510 test_magnitude=DestroyImage(test_magnitude);
4511 reconstruct_magnitude=DestroyImage(reconstruct_magnitude);
4512 if (correlation_image == (Image *) NULL)
4513 ThrowPhaseSimilarityException();
4514 gamma_image=CloneImage(correlation_image,0,0,MagickTrue,exception);
4515 correlation_image=DestroyImage(correlation_image);
4516 if (gamma_image == (Image *) NULL)
4517 ThrowPhaseSimilarityException();
4518 /*
4519 Crop padding.
4520 */
4521 SetGeometry(image,&geometry);
4522 geometry.width=image->columns;
4523 geometry.height=image->rows;
4524 (void) ResetImagePage(gamma_image,"0x0+0+0");
4525 phase_image=CropImage(gamma_image,&geometry,exception);
4526 gamma_image=DestroyImage(gamma_image);
4527 if (phase_image == (Image *) NULL)
4528 ThrowPhaseSimilarityException();
4529 (void) ResetImagePage(phase_image,"0x0+0+0");
4530 /*
4531 Identify the maxima value in the correlation image and its location.
4532 */
4533 status=GrayscaleImage(phase_image,AveragePixelIntensityMethod,exception);
4534 if (status == MagickFalse)
4535 ThrowPhaseSimilarityException();
4536 phase_image->depth=32;
4537 phase_image->colorspace=GRAYColorspace;
4538 phase_image->alpha_trait=UndefinedPixelTrait;
4539 status=SIMFilterImageNaNs(phase_image,exception);
4540 if (status == MagickFalse)
4541 ThrowPhaseSimilarityException();
4542 status=SIMMaximaImage(phase_image,&maxima,offset,exception);
4543 if (status == MagickFalse)
4544 ThrowPhaseSimilarityException();
4545 magnitude_image=DestroyImage(magnitude_image);
4546 *similarity_metric=QuantumScale*maxima;
4547 return(phase_image);
4548}
4549
4550static Image *PSNRSimilarityImage(const Image *image,const Image *reconstruct,
4551 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
4552{
4553 Image
4554 *psnr_image = (Image *) NULL;
4555
4556 psnr_image=MSESimilarityImage(image,reconstruct,offset,similarity_metric,
4557 exception);
4558 if (psnr_image == (Image *) NULL)
4559 return(psnr_image);
4560 *similarity_metric=10.0*MagickSafeLog10(MagickSafeReciprocal(
4561 *similarity_metric))/MagickSafePSNRRecipicol(10.0);
4562 return(psnr_image);
4563}
4564
4565static Image *RMSESimilarityImage(const Image *image,const Image *reconstruct,
4566 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
4567{
4568 Image
4569 *rmse_image = (Image *) NULL;
4570
4571 rmse_image=MSESimilarityImage(image,reconstruct,offset,similarity_metric,
4572 exception);
4573 if (rmse_image == (Image *) NULL)
4574 return(rmse_image);
4575 *similarity_metric=sqrt(*similarity_metric);
4576 return(rmse_image);
4577}
4578#endif
4579
4580static double GetSimilarityMetric(const Image *image,
4581 const Image *reconstruct_image,const MetricType metric,
4582 const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
4583{
4584 double
4585 *channel_similarity,
4586 similarity = 0.0;
4587
4588 ExceptionInfo
4589 *sans_exception = AcquireExceptionInfo();
4590
4591 Image
4592 *similarity_image;
4593
4594 MagickBooleanType
4595 status = MagickTrue;
4596
4597 RectangleInfo
4598 geometry;
4599
4600 size_t
4601 length = MaxPixelChannels+1UL;
4602
4603 SetGeometry(reconstruct_image,&geometry);
4604 geometry.x=x_offset;
4605 geometry.y=y_offset;
4606 similarity_image=CropImage(image,&geometry,sans_exception);
4607 sans_exception=DestroyExceptionInfo(sans_exception);
4608 if (similarity_image == (Image *) NULL)
4609 return(NAN);
4610 /*
4611 Get image distortion.
4612 */
4613 channel_similarity=(double *) AcquireQuantumMemory(length,
4614 sizeof(*channel_similarity));
4615 if (channel_similarity == (double *) NULL)
4616 {
4617 similarity_image=DestroyImage(similarity_image);
4618 return(NAN);
4619 }
4620 (void) memset(channel_similarity,0,length*sizeof(*channel_similarity));
4621 switch (metric)
4622 {
4623 case AbsoluteErrorMetric:
4624 {
4625 status=GetAESimilarity(similarity_image,reconstruct_image,
4626 channel_similarity,exception);
4627 break;
4628 }
4629 case DotProductCorrelationErrorMetric:
4630 {
4631 status=GetDPCSimilarity(similarity_image,reconstruct_image,
4632 channel_similarity,exception);
4633 break;
4634 }
4635 case FuzzErrorMetric:
4636 {
4637 status=GetFUZZSimilarity(similarity_image,reconstruct_image,
4638 channel_similarity,exception);
4639 break;
4640 }
4641 case MeanAbsoluteErrorMetric:
4642 {
4643 status=GetMAESimilarity(similarity_image,reconstruct_image,
4644 channel_similarity,exception);
4645 break;
4646 }
4647 case MeanErrorPerPixelErrorMetric:
4648 {
4649 status=GetMEPPSimilarity(similarity_image,reconstruct_image,
4650 channel_similarity,exception);
4651 break;
4652 }
4653 case MeanSquaredErrorMetric:
4654 {
4655 status=GetMSESimilarity(similarity_image,reconstruct_image,
4656 channel_similarity,exception);
4657 break;
4658 }
4659 case NormalizedCrossCorrelationErrorMetric:
4660 {
4661 status=GetNCCSimilarity(similarity_image,reconstruct_image,
4662 channel_similarity,exception);
4663 break;
4664 }
4665 case PeakAbsoluteErrorMetric:
4666 {
4667 status=GetPASimilarity(similarity_image,reconstruct_image,
4668 channel_similarity,exception);
4669 break;
4670 }
4671 case PeakSignalToNoiseRatioErrorMetric:
4672 {
4673 status=GetPSNRSimilarity(similarity_image,reconstruct_image,
4674 channel_similarity,exception);
4675 break;
4676 }
4677 case PerceptualHashErrorMetric:
4678 {
4679 status=GetPHASHSimilarity(similarity_image,reconstruct_image,
4680 channel_similarity,exception);
4681 break;
4682 }
4683 case PhaseCorrelationErrorMetric:
4684 {
4685 status=GetPHASESimilarity(similarity_image,reconstruct_image,
4686 channel_similarity,exception);
4687 break;
4688 }
4689 case PixelDifferenceCountErrorMetric:
4690 {
4691 status=GetPDCSimilarity(similarity_image,reconstruct_image,
4692 channel_similarity,exception);
4693 break;
4694 }
4695 case RootMeanSquaredErrorMetric:
4696 case UndefinedErrorMetric:
4697 default:
4698 {
4699 status=GetRMSESimilarity(similarity_image,reconstruct_image,
4700 channel_similarity,exception);
4701 break;
4702 }
4703 case StructuralDissimilarityErrorMetric:
4704 {
4705 status=GetDSSIMSimilarity(similarity_image,reconstruct_image,
4706 channel_similarity,exception);
4707 break;
4708 }
4709 case StructuralSimilarityErrorMetric:
4710 {
4711 status=GetSSIMSimularity(similarity_image,reconstruct_image,
4712 channel_similarity,exception);
4713 break;
4714 }
4715 }
4716 similarity_image=DestroyImage(similarity_image);
4717 similarity=channel_similarity[CompositePixelChannel];
4718 channel_similarity=(double *) RelinquishMagickMemory(channel_similarity);
4719 if (status == MagickFalse)
4720 return(NAN);
4721 return(similarity);
4722}
4723
4724MagickExport Image *SimilarityImage(const Image *image,const Image *reconstruct,
4725 const MetricType metric,const double similarity_threshold,
4726 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
4727{
4728#define SimilarityImageTag "Similarity/Image"
4729
4730 typedef struct
4731 {
4732 double
4733 similarity;
4734
4735 ssize_t
4736 x,
4737 y;
4738 } SimilarityInfo;
4739
4740 CacheView
4741 *similarity_view;
4742
4743 Image
4744 *similarity_image = (Image *) NULL;
4745
4746 MagickBooleanType
4747 status = MagickTrue,
4748 threshold_trigger = MagickFalse;
4749
4750 MagickOffsetType
4751 progress = 0;
4752
4753 SimilarityInfo
4754 similarity_info = { 0.0, 0, 0 };
4755
4756 size_t
4757 columns,
4758 rows;
4759
4760 ssize_t
4761 y;
4762
4763 assert(image != (const Image *) NULL);
4764 assert(image->signature == MagickCoreSignature);
4765 assert(exception != (ExceptionInfo *) NULL);
4766 assert(exception->signature == MagickCoreSignature);
4767 assert(offset != (RectangleInfo *) NULL);
4768 if (IsEventLogging() != MagickFalse)
4769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4770 SetGeometry(reconstruct,offset);
4771 *similarity_metric=0.0;
4772 offset->x=0;
4773 offset->y=0;
4774#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
4775{
4776 const char *artifact = GetImageArtifact(image,"compare:frequency-domain");
4777 if (artifact == (const char *) NULL)
4778 artifact=GetImageArtifact(image,"compare:accelerate-ncc");
4779 if (((artifact == (const char *) NULL) ||
4780 (IsStringTrue(artifact) != MagickFalse)) &&
4781 ((image->channels & ReadMaskChannel) == 0))
4782 switch (metric)
4783 {
4784 case DotProductCorrelationErrorMetric:
4785 {
4786 similarity_image=DPCSimilarityImage(image,reconstruct,offset,
4787 similarity_metric,exception);
4788 return(similarity_image);
4789 }
4790 case MeanSquaredErrorMetric:
4791 {
4792 similarity_image=MSESimilarityImage(image,reconstruct,offset,
4793 similarity_metric,exception);
4794 return(similarity_image);
4795 }
4796 case NormalizedCrossCorrelationErrorMetric:
4797 {
4798 similarity_image=NCCSimilarityImage(image,reconstruct,offset,
4799 similarity_metric,exception);
4800 return(similarity_image);
4801 }
4802 case PeakSignalToNoiseRatioErrorMetric:
4803 {
4804 similarity_image=PSNRSimilarityImage(image,reconstruct,offset,
4805 similarity_metric,exception);
4806 return(similarity_image);
4807 }
4808 case PhaseCorrelationErrorMetric:
4809 {
4810 similarity_image=PhaseSimilarityImage(image,reconstruct,offset,
4811 similarity_metric,exception);
4812 return(similarity_image);
4813 }
4814 case RootMeanSquaredErrorMetric:
4815 case UndefinedErrorMetric:
4816 {
4817 similarity_image=RMSESimilarityImage(image,reconstruct,offset,
4818 similarity_metric,exception);
4819 return(similarity_image);
4820 }
4821 default:
4822 break;
4823 }
4824}
4825#endif
4826 if ((image->columns < reconstruct->columns) ||
4827 (image->rows < reconstruct->rows))
4828 {
4829 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
4830 "GeometryDoesNotContainImage","`%s'",image->filename);
4831 return((Image *) NULL);
4832 }
4833 SetImageCompareBounds(image,reconstruct,&columns,&rows);
4834 similarity_image=CloneImage(image,columns,rows,MagickTrue,exception);
4835 if (similarity_image == (Image *) NULL)
4836 return((Image *) NULL);
4837 similarity_image->depth=32;
4838 similarity_image->colorspace=GRAYColorspace;
4839 similarity_image->alpha_trait=UndefinedPixelTrait;
4840 status=SetImageStorageClass(similarity_image,DirectClass,exception);
4841 if (status == MagickFalse)
4842 return(DestroyImage(similarity_image));
4843 /*
4844 Measure similarity of reconstruction image against image.
4845 */
4846 similarity_info.similarity=GetSimilarityMetric(image,reconstruct,metric,
4847 similarity_info.x,similarity_info.y,exception);
4848 similarity_view=AcquireAuthenticCacheView(similarity_image,exception);
4849#if defined(MAGICKCORE_OPENMP_SUPPORT)
4850 #pragma omp parallel for schedule(static) shared(similarity_info,status,threshold_trigger) \
4851 magick_number_threads(image,reconstruct,similarity_image->rows,1)
4852#endif
4853 for (y=0; y < (ssize_t) similarity_image->rows; y++)
4854 {
4855 double
4856 similarity;
4857
4858 Quantum
4859 *magick_restrict q;
4860
4861 SimilarityInfo
4862 channel_info = similarity_info;
4863
4864 ssize_t
4865 x;
4866
4867 if (status == MagickFalse)
4868 continue;
4869 if (threshold_trigger != MagickFalse)
4870 continue;
4871 q=QueueCacheViewAuthenticPixels(similarity_view,0,y,
4872 similarity_image->columns,1,exception);
4873 if (q == (Quantum *) NULL)
4874 {
4875 status=MagickFalse;
4876 continue;
4877 }
4878 for (x=0; x < (ssize_t) similarity_image->columns; x++)
4879 {
4880 ssize_t
4881 i;
4882
4883 similarity=GetSimilarityMetric((Image *) image,reconstruct,metric,x,y,
4884 exception);
4885 switch (metric)
4886 {
4887 case DotProductCorrelationErrorMetric:
4888 case NormalizedCrossCorrelationErrorMetric:
4889 case PeakSignalToNoiseRatioErrorMetric:
4890 case PhaseCorrelationErrorMetric:
4891 case StructuralSimilarityErrorMetric:
4892 {
4893 if (similarity <= channel_info.similarity)
4894 break;
4895 channel_info.similarity=similarity;
4896 channel_info.x=x;
4897 channel_info.y=y;
4898 break;
4899 }
4900 default:
4901 {
4902 if (similarity >= channel_info.similarity)
4903 break;
4904 channel_info.similarity=similarity;
4905 channel_info.x=x;
4906 channel_info.y=y;
4907 break;
4908 }
4909 }
4910 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4911 {
4912 PixelChannel channel = GetPixelChannelChannel(image,i);
4913 PixelTrait traits = GetPixelChannelTraits(image,channel);
4914 PixelTrait similarity_traits = GetPixelChannelTraits(similarity_image,
4915 channel);
4916 if (((traits & UpdatePixelTrait) == 0) ||
4917 ((similarity_traits & UpdatePixelTrait) == 0))
4918 continue;
4919 switch (metric)
4920 {
4921 case DotProductCorrelationErrorMetric:
4922 case NormalizedCrossCorrelationErrorMetric:
4923 case PeakSignalToNoiseRatioErrorMetric:
4924 case PhaseCorrelationErrorMetric:
4925 case StructuralSimilarityErrorMetric:
4926 {
4927 SetPixelChannel(similarity_image,channel,ClampToQuantum((double)
4928 QuantumRange*similarity),q);
4929 break;
4930 }
4931 default:
4932 {
4933 SetPixelChannel(similarity_image,channel,ClampToQuantum((double)
4934 QuantumRange*(1.0-similarity)),q);
4935 break;
4936 }
4937 }
4938 }
4939 q+=(ptrdiff_t) GetPixelChannels(similarity_image);
4940 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4941 {
4942 MagickBooleanType
4943 proceed;
4944
4945 progress++;
4946 proceed=SetImageProgress(image,SimilarityImageTag,progress,
4947 (MagickSizeType) image->columns*image->rows);
4948 if (proceed == MagickFalse)
4949 status=MagickFalse;
4950 }
4951 }
4952#if defined(MAGICKCORE_OPENMP_SUPPORT)
4953 #pragma omp critical (MagickCore_GetSimilarityMetric)
4954#endif
4955 switch (metric)
4956 {
4957 case DotProductCorrelationErrorMetric:
4958 case NormalizedCrossCorrelationErrorMetric:
4959 case PeakSignalToNoiseRatioErrorMetric:
4960 case PhaseCorrelationErrorMetric:
4961 case StructuralSimilarityErrorMetric:
4962 {
4963 if (similarity_threshold != DefaultSimilarityThreshold)
4964 if (channel_info.similarity >= similarity_threshold)
4965 threshold_trigger=MagickTrue;
4966 if (channel_info.similarity >= similarity_info.similarity)
4967 similarity_info=channel_info;
4968 break;
4969 }
4970 default:
4971 {
4972 if (similarity_threshold != DefaultSimilarityThreshold)
4973 if (channel_info.similarity < similarity_threshold)
4974 threshold_trigger=MagickTrue;
4975 if (channel_info.similarity < similarity_info.similarity)
4976 similarity_info=channel_info;
4977 break;
4978 }
4979 }
4980 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
4981 status=MagickFalse;
4982 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4983 {
4984 MagickBooleanType
4985 proceed;
4986
4987 progress++;
4988 proceed=SetImageProgress(image,SimilarityImageTag,progress,
4989 (MagickSizeType) image->columns*image->rows);
4990 if (proceed == MagickFalse)
4991 status=MagickFalse;
4992 }
4993 }
4994 similarity_view=DestroyCacheView(similarity_view);
4995 if (status == MagickFalse)
4996 similarity_image=DestroyImage(similarity_image);
4997 *similarity_metric=similarity_info.similarity;
4998 if (fabs(*similarity_metric) < MagickEpsilon)
4999 *similarity_metric=0.0;
5000 offset->x=similarity_info.x;
5001 offset->y=similarity_info.y;
5002 (void) FormatImageProperty((Image *) image,"similarity","%.*g",
5003 GetMagickPrecision(),*similarity_metric);
5004 (void) FormatImageProperty((Image *) image,"similarity.offset.x","%.*g",
5005 GetMagickPrecision(),(double) offset->x);
5006 (void) FormatImageProperty((Image *) image,"similarity.offset.y","%.*g",
5007 GetMagickPrecision(),(double) offset->y);
5008 return(similarity_image);
5009}