]> git.cworth.org Git - apitrace/blobdiff - image.cpp
Compare screenshots.
[apitrace] / image.cpp
index ce37c190c097cff6a624d4870e9dca5a58adfad3..9fd1dc8096992e78c651609c1e58d68d68200d19 100644 (file)
--- a/image.cpp
+++ b/image.cpp
@@ -26,6 +26,7 @@
 
 #include <png.h>
 
+#include <math.h>
 #include <stdint.h>
 
 #include <fstream>
@@ -273,4 +274,37 @@ no_fp:
 }
 
 
+double Image::compare(Image &ref)
+{
+    if (width != ref.width ||
+        height != ref.height) {
+        return 0.0;
+    }
+
+    const unsigned char *pSrc = start();
+    const unsigned char *pRef = ref.start();
+
+    unsigned long long error = 0;
+    for (unsigned y = 0; y < height; ++y) {
+        for (unsigned  x = 0; x < width*4; ++x) {
+            int delta = pSrc[x] - pRef[x];
+            error += delta*delta;
+        }
+
+        pSrc += stride();
+        pRef += ref.stride();
+    }
+
+    double numerator = error*2 + 1;
+    double denominator = height*width*4ULL*255ULL*255ULL*2;
+    double quotient = numerator/denominator;
+
+    // Precision in bits
+    double precision = -log(quotient)/log(2.0);
+
+    return precision;
+}
+
+
+
 } /* namespace Image */