]> git.cworth.org Git - apitrace/blobdiff - scripts/snapdiff.py
snapdiff: Compare .bmp images too.
[apitrace] / scripts / snapdiff.py
index 13e5069abc09fd79b2e6aa0a43464a390d15d196..ddc2bf7bcc977b0c498e7c58524da976a5ee8835 100755 (executable)
@@ -65,6 +65,9 @@ class Comparer:
 
         self.diff = ImageChops.difference(self.src_im, self.ref_im)
 
+    def size_mismatch(self):
+        return self.ref_im.size != self.src_im.size
+
     def write_diff(self, diff_image, fuzz = 0.05):
         # make a difference image similar to ImageMagick's compare utility
         mask = ImageEnhance.Brightness(self.diff).enhance(1.0/fuzz)
@@ -78,6 +81,9 @@ class Comparer:
         diff_im.save(diff_image)
 
     def precision(self):
+        if self.size_mismatch():
+            return 0.0
+
         # See also http://effbot.org/zone/pil-comparing-images.htm
         h = self.diff.histogram()
         square_error = 0
@@ -87,8 +93,12 @@ class Comparer:
         bits = -math.log(rel_error)/math.log(2.0)
         return bits
 
-    def ae(self):
+    def ae(self, fuzz = 0.05):
         # Compute absolute error
+
+        if self.size_mismatch():
+            return sys.maxint
+
         # TODO: this is approximate due to the grayscale conversion
         h = self.diff.convert('L').histogram()
         ae = sum(h[int(255 * fuzz) + 1 : 256])
@@ -111,10 +121,11 @@ def surface(html, image):
 
 
 def is_image(path):
-    return \
-        path.endswith('.png') \
-        and not path.endswith('.diff.png') \
-        and not path.endswith('.thumb.png')
+    name = os.path.basename(path)
+    name, ext1 = os.path.splitext(name)
+    name, ext2 = os.path.splitext(name)
+    print name, ext1, ext2
+    return ext1 in ('.png', '.bmp') and ext2 not in ('.diff', '.thumb')
 
 
 def find_images(prefix):
@@ -147,10 +158,14 @@ def main():
         '-f', '--fuzz',
         type="float", dest="fuzz", default=0.05,
         help="fuzz ratio [default: %default]")
+    optparser.add_option(
+        '-a', '--alpha',
+        action="store_true", dest="alpha", default=False,
+        help="take alpha channel in consideration")
     optparser.add_option(
         '--overwrite',
         action="store_true", dest="overwrite", default=False,
-        help="overwrite")
+        help="overwrite images")
 
     (options, args) = optparser.parse_args(sys.argv[1:])
 
@@ -184,7 +199,7 @@ def main():
                or (os.path.getmtime(delta_image) < os.path.getmtime(ref_image) \
                    and os.path.getmtime(delta_image) < os.path.getmtime(src_image)):
 
-                comparer = Comparer(ref_image, src_image)
+                comparer = Comparer(ref_image, src_image, options.alpha)
                 comparer.write_diff(delta_image, fuzz=options.fuzz)
 
             html.write('      <tr>\n')