]> git.cworth.org Git - apitrace/blob - scripts/snapdiff.py
Use call number instead of frame number for snapshot filenames.
[apitrace] / scripts / snapdiff.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2008-2009 VMware, Inc.
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 ##########################################################################/
26
27
28 import sys
29 import os.path
30 import subprocess
31 import optparse
32
33 import Image
34
35
36 thumb_size = 320, 320
37
38
39 def compare(im, ref):
40     import ImageMath
41     # See http://www.pythonware.com/library/pil/handbook/imagemath.htm
42     mask = ImageMath.eval("min(abs(a - b), 1)", a=im, b=ref)
43     gray = ref.convert('L')
44     # TODO
45
46
47 def surface(html, image):
48     if False:
49         html.write('        <td><a href="%s"><img src="%s"/></a></td>\n' % (image, image))
50     else:
51         name, ext = os.path.splitext(image)
52         thumb = name + '_thumb' + ext
53         if not os.path.exists(thumb) and os.path.exists(image):
54             im = Image.open(image)
55             im.thumbnail(thumb_size)
56             im.save(thumb)
57         html.write('        <td><a href="%s"><img src="%s"/></a></td>\n' % (image, thumb))
58
59
60 def main():
61     optparser = optparse.OptionParser(
62         usage="\n\t%prog [options] [file] ...",
63         version="%%prog")
64     optparser.add_option(
65         '-o', '--output', metavar='FILE',
66         type="string", dest="output",
67         help="output filename [stdout]")
68     optparser.add_option(
69         '--start', metavar='FRAME',
70         type="int", dest="start", default=0,
71         help="start frame [default: %default]")
72     optparser.add_option(
73         '--stop', metavar='FRAME',
74         type="int", dest="stop", default=0xffffffff,
75         help="stop frame [default: %default]")
76     optparser.add_option(
77         '-f', '--fuzz',
78         type="string", dest="fuzz", default='5%',
79         help="fuzz [default: %default]")
80
81     (options, args) = optparser.parse_args(sys.argv[1:])
82
83     if len(args) != 2:
84         optparser.error('incorrect number of arguments')
85
86     ref_prefix = args[0]
87     src_prefix = args[1]
88
89     if options.output:
90         html = open(options.output, 'wt')
91     else:
92         html = sys.stdout
93     html.write('<html>\n')
94     html.write('  <body>\n')
95     html.write('    <table border="1">\n')
96     html.write('      <tr><th>ref</th><th>src</th><th>&Delta;</th></tr>\n')
97     frame_no = options.start
98     while frame_no <= options.stop:
99         ref_image = "%s%010u.png" % (ref_prefix, frame_no)
100         src_image = "%s%010u.png" % (src_prefix, frame_no)
101         delta_image = "%s%010u_diff.png" % (src_prefix, frame_no)
102         if os.path.exists(ref_image) and os.path.exists(src_image):
103             html.write('      <tr>\n')
104             subprocess.call(["compare", '-metric', 'AE', '-fuzz', options.fuzz, ref_image, src_image, delta_image])
105             surface(html, ref_image)
106             surface(html, src_image)
107             surface(html, delta_image)
108             html.write('      </tr>\n')
109             html.flush()
110         frame_no += 1
111     html.write('    </table>\n')
112     html.write('  </body>\n')
113     html.write('</html>\n')
114
115
116 if __name__ == '__main__':
117     main()