blob: fa634fcf49c9a444d891f2dee0436cb542f4a9ba [file] [log] [blame]
José Fonsecaf87d8c62011-02-15 12:19:11 +00001#!/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
28import sys
29import os.path
30import subprocess
31import optparse
32
33import Image
34
35
36thumb_size = 320, 320
37
38
39def 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
47def surface(html, image):
José Fonseca0c881f72011-05-15 11:50:01 +010048 if True:
José Fonsecaf87d8c62011-02-15 12:19:11 +000049 name, ext = os.path.splitext(image)
José Fonseca0c881f72011-05-15 11:50:01 +010050 thumb = name + '.thumb' + ext
51 if os.path.exists(image) \
52 and (not os.path.exists(thumb) \
53 or os.path.getmtime(thumb) < os.path.getmtime(image)):
José Fonsecaf87d8c62011-02-15 12:19:11 +000054 im = Image.open(image)
55 im.thumbnail(thumb_size)
56 im.save(thumb)
José Fonseca0c881f72011-05-15 11:50:01 +010057 else:
58 thumb = image
59 html.write(' <td><a href="%s"><img src="%s"/></a></td>\n' % (image, thumb))
60
61
62def is_image(path):
63 return \
64 path.endswith('.png') \
65 and not path.endswith('.diff.png') \
66 and not path.endswith('.thumb.png')
67
68
69def find_images(prefix):
70 prefix = os.path.abspath(prefix)
71 if os.path.isdir(prefix):
72 prefix_dir = prefix
73 else:
74 prefix_dir = os.path.dirname(prefix)
75
76 images = []
77 for dirname, dirnames, filenames in os.walk(prefix_dir, followlinks=True):
78 for filename in filenames:
79 filepath = os.path.join(dirname, filename)
80 if filepath.startswith(prefix) and is_image(filepath):
81 images.append(filepath[len(prefix):])
82
83 return images
José Fonsecaf87d8c62011-02-15 12:19:11 +000084
85
86def main():
87 optparser = optparse.OptionParser(
José Fonseca0c881f72011-05-15 11:50:01 +010088 usage="\n\t%prog [options] <ref_prefix> <src_prefix>",
José Fonsecaf87d8c62011-02-15 12:19:11 +000089 version="%%prog")
90 optparser.add_option(
91 '-o', '--output', metavar='FILE',
92 type="string", dest="output",
93 help="output filename [stdout]")
94 optparser.add_option(
José Fonsecaf87d8c62011-02-15 12:19:11 +000095 '-f', '--fuzz',
96 type="string", dest="fuzz", default='5%',
97 help="fuzz [default: %default]")
98
99 (options, args) = optparser.parse_args(sys.argv[1:])
100
101 if len(args) != 2:
102 optparser.error('incorrect number of arguments')
103
José Fonseca2f6720e2011-04-13 15:57:15 +0100104 ref_prefix = args[0]
105 src_prefix = args[1]
José Fonsecaf87d8c62011-02-15 12:19:11 +0000106
José Fonseca0c881f72011-05-15 11:50:01 +0100107 ref_images = find_images(ref_prefix)
108 src_images = find_images(src_prefix)
109 images = list(set(ref_images).intersection(set(src_images)))
110 images.sort()
111
José Fonsecaf87d8c62011-02-15 12:19:11 +0000112 if options.output:
113 html = open(options.output, 'wt')
114 else:
115 html = sys.stdout
116 html.write('<html>\n')
117 html.write(' <body>\n')
118 html.write(' <table border="1">\n')
José Fonseca0c881f72011-05-15 11:50:01 +0100119 html.write(' <tr><th>%s</th><th>%s</th><th>&Delta;</th></tr>\n' % (ref_prefix, src_prefix))
120 for image in images:
121 ref_image = ref_prefix + image
122 src_image = src_prefix + image
123 root, ext = os.path.splitext(src_image)
124 delta_image = "%s.diff.png" % (root, )
José Fonsecaf87d8c62011-02-15 12:19:11 +0000125 if os.path.exists(ref_image) and os.path.exists(src_image):
José Fonseca0c881f72011-05-15 11:50:01 +0100126 if not os.path.exists(delta_image) \
127 or (os.path.getmtime(delta_image) < os.path.getmtime(ref_image) \
128 and os.path.getmtime(delta_image) < os.path.getmtime(src_image)):
129 subprocess.call(["compare", '-metric', 'AE', '-fuzz', options.fuzz, ref_image, src_image, delta_image])
130
José Fonsecaf87d8c62011-02-15 12:19:11 +0000131 html.write(' <tr>\n')
José Fonsecaf87d8c62011-02-15 12:19:11 +0000132 surface(html, ref_image)
133 surface(html, src_image)
134 surface(html, delta_image)
135 html.write(' </tr>\n')
136 html.flush()
José Fonsecaf87d8c62011-02-15 12:19:11 +0000137 html.write(' </table>\n')
138 html.write(' </body>\n')
139 html.write('</html>\n')
140
141
142if __name__ == '__main__':
143 main()