2 ##########################################################################
4 # Copyright 2011 Jose Fonseca
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:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
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
25 ##########################################################################/
27 '''Run two retrace instances in parallel, comparing generated snapshots.
39 from snapdiff import Comparer
43 # Null file, to use when we're not interested in subprocesses output
44 if platform.system() == 'Windows':
45 NULL = open('NUL:', 'wt')
47 NULL = open('/dev/null', 'wt')
52 def __init__(self, args, env=None):
60 '-S', options.snapshot_frequency,
62 p = subprocess.Popen(cmd, env=self.env, stdout=subprocess.PIPE, stderr=NULL)
65 def dump_state(self, call_no):
66 '''Get the state dump at the specified call no.'''
72 p = subprocess.Popen(cmd, env=self.env, stdout=subprocess.PIPE, stderr=NULL)
73 state = jsondiff.load(p.stdout)
77 def diff_state(self, ref_call_no, src_call_no):
78 '''Compare the state between two calls.'''
80 ref_state = self.dump_state(ref_call_no)
81 src_state = self.dump_state(src_call_no)
83 differ = jsondiff.Differ(sys.stdout)
84 differ.visit(ref_state, src_state)
85 sys.stdout.write('\n')
89 '''Read a PNM from the stream, and return the image object, and the comment.'''
91 magic = stream.readline()
94 assert magic.rstrip() == 'P6'
96 line = stream.readline()
97 while line.startswith('#'):
99 line = stream.readline()
100 width, height = map(int, line.strip().split())
101 maximum = int(stream.readline().strip())
102 assert maximum == 255
103 data = stream.read(height * width * 3)
104 image = Image.frombuffer('RGB', (width, height), data, 'raw', 'RGB', 0, 1)
105 return image, comment
108 def parse_env(optparser, entries):
109 '''Translate a list of NAME=VALUE entries into an environment dictionary.'''
111 env = os.environ.copy()
112 for entry in entries:
114 name, var = entry.split('=', 1)
116 optparser.error('invalid environment entry %r' % entry)
127 # Parse command line options
128 optparser = optparse.OptionParser(
129 usage='\n\t%prog [options] -- [glretrace options] <trace>',
131 optparser.add_option(
132 '-r', '--retrace', metavar='PROGRAM',
133 type='string', dest='retrace', default='glretrace',
134 help='retrace command [default: %default]')
135 optparser.add_option(
136 '--ref-env', metavar='NAME=VALUE',
137 type='string', action='append', dest='ref_env', default=[],
138 help='add variable to reference environment')
139 optparser.add_option(
140 '--src-env', metavar='NAME=VALUE',
141 type='string', action='append', dest='src_env', default=[],
142 help='add variable to source environment')
143 optparser.add_option(
144 '--diff-prefix', metavar='PATH',
145 type='string', dest='diff_prefix', default='.',
146 help='prefix for the difference images')
147 optparser.add_option(
148 '-t', '--threshold', metavar='BITS',
149 type="float", dest="threshold", default=12.0,
150 help="threshold precision [default: %default]")
151 optparser.add_option(
152 '-S', '--snapshot-frequency', metavar='FREQUENCY',
153 type="string", dest="snapshot_frequency", default='draw',
154 help="snapshot frequency: frame, framebuffer, or draw [default: %default]")
156 (options, args) = optparser.parse_args(sys.argv[1:])
157 ref_env = parse_env(optparser, options.ref_env)
158 src_env = parse_env(optparser, options.src_env)
160 optparser.error("incorrect number of arguments")
162 ref_setup = Setup(args, ref_env)
163 src_setup = Setup(args, src_env)
165 sys.stdout.write('call\tprecision\n')
169 ref_proc = ref_setup.retrace()
171 src_proc = src_setup.retrace()
174 # Get the reference image
175 ref_image, ref_comment = read_pnm(ref_proc.stdout)
176 if ref_image is None:
179 # Get the source image
180 src_image, src_comment = read_pnm(src_proc.stdout)
181 if src_image is None:
184 assert ref_comment == src_comment
186 call_no = int(ref_comment.strip())
188 # Compare the two images
189 comparer = Comparer(ref_image, src_image)
190 precision = comparer.precision()
192 sys.stdout.write('%u\t%f\n' % (call_no, precision))
194 if precision < options.threshold:
195 if options.diff_prefix:
196 prefix = os.path.join(options.diff_prefix, '%010u' % call_no)
197 prefix_dir = os.path.dirname(prefix)
198 if not os.path.isdir(prefix_dir):
199 os.makedirs(prefix_dir)
200 ref_image.save(prefix + '.ref.png')
201 src_image.save(prefix + '.src.png')
202 comparer.write_diff(prefix + '.diff.png')
203 if last_bad < last_good:
204 src_setup.diff_state(last_good, call_no)
216 if __name__ == '__main__':