parameters: ()
docstring: Main program.
docstring_summary: Main program.
docstring_tokens: Main program .
function: def main():
      '''Main program.
      '''
  
      global options
  
      # Parse command line options
      optparser = optparse.OptionParser(
          usage='\n\t%prog [options] -- [glretrace options] <trace>',
          version='%%prog')
      optparser.add_option(
          '-r', '--retrace', metavar='PROGRAM',
          type='string', dest='retrace', default='glretrace',
          help='retrace command [default: %default]')
      optparser.add_option(
          '--ref-driver', metavar='DRIVER',
          type='string', dest='ref_driver', default=None,
          help='force reference driver')
      optparser.add_option(
          '--src-driver', metavar='DRIVER',
          type='string', dest='src_driver', default=None,
          help='force source driver')
      optparser.add_option(
          '--ref-arg', metavar='OPTION',
          type='string', action='append', dest='ref_args', default=[],
          help='pass argument to reference retrace')
      optparser.add_option(
          '--src-arg', metavar='OPTION',
          type='string', action='append', dest='src_args', default=[],
          help='pass argument to source retrace')
      optparser.add_option(
          '--ref-env', metavar='NAME=VALUE',
          type='string', action='append', dest='ref_env', default=[],
          help='add variable to reference environment')
      optparser.add_option(
          '--src-env', metavar='NAME=VALUE',
          type='string', action='append', dest='src_env', default=[],
          help='add variable to source environment')
      optparser.add_option(
          '--diff-prefix', metavar='PATH',
          type='string', dest='diff_prefix', default='.',
          help='prefix for the difference images')
      optparser.add_option(
          '-t', '--threshold', metavar='BITS',
          type="float", dest="threshold", default=12.0,
          help="threshold precision  [default: %default]")
      optparser.add_option(
          '-S', '--snapshot-frequency', metavar='CALLSET',
          type="string", dest="snapshot_frequency", default='draw',
          help="calls to compare [default: %default]")
      optparser.add_option(
          '--diff-state',
          action='store_true', dest='diff_state', default=False,
          help='diff state between failing calls')
      optparser.add_option(
          '-o', '--output', metavar='FILE',
          type="string", dest="output",
          help="output file [default: stdout]")
  
      (options, args) = optparser.parse_args(sys.argv[1:])
      ref_env = parse_env(optparser, options.ref_env)
      src_env = parse_env(optparser, options.src_env)
      if not args:
          optparser.error("incorrect number of arguments")
      
      if options.ref_driver:
          options.ref_args.insert(0, '--driver=' + options.ref_driver)
      if options.src_driver:
          options.src_args.insert(0, '--driver=' + options.src_driver)
  
      refRetracer = Retracer(options.retrace, options.ref_args + args, ref_env)
      srcRetracer = Retracer(options.retrace, options.src_args + args, src_env)
  
      if options.output:
          output = open(options.output, 'wt')
      else:
          output = sys.stdout
  
      highligher = AutoHighlighter(output)
  
      highligher.write('call\tprecision\n')
  
      last_bad = -1
      last_good = 0
      refRun = refRetracer.snapshot(options.snapshot_frequency)
      try:
          srcRun = srcRetracer.snapshot(options.snapshot_frequency)
          try:
              while True:
                  # Get the reference image
                  refImage, refCallNo = refRun.nextSnapshot()
                  if refImage is None:
                      break
  
                  # Get the source image
                  srcImage, srcCallNo = srcRun.nextSnapshot()
                  if srcImage is None:
                      break
  
                  assert refCallNo == srcCallNo
                  callNo = refCallNo
  
                  # Compare the two images
                  if isinstance(refImage, Image.Image) and isinstance(srcImage, Image.Image):
                      # Using PIL
                      numpyImages = False
                      comparer = Comparer(refImage, srcImage)
                      precision = comparer.precision()
                  else:
                      # Using numpy (for floating point images)
                      # TODO: drop PIL when numpy path becomes general enough
                      import numpy
                      assert not isinstance(refImage, Image.Image)
                      assert not isinstance(srcImage, Image.Image)
                      numpyImages = True
                      assert refImage.shape == srcImage.shape
                      diffImage = numpy.square(srcImage - refImage)
  
                      height, width, channels = diffImage.shape
                      square_error = numpy.sum(diffImage)
                      square_error += numpy.finfo(numpy.float32).eps
                      rel_error = square_error / float(height*width*channels)
                      bits = -math.log(rel_error)/math.log(2.0)
                      precision = bits
  
                  mismatch = precision < options.threshold
  
                  if mismatch:
                      highligher.color(highligher.red)
                      highligher.bold()
                  highligher.write('%u\t%f\n' % (callNo, precision))
                  if mismatch:
                      highligher.normal()
  
                  if mismatch:
                      if options.diff_prefix:
                          prefix = os.path.join(options.diff_prefix, '%010u' % callNo)
                          prefix_dir = os.path.dirname(prefix)
                          if not os.path.isdir(prefix_dir):
                              os.makedirs(prefix_dir)
                          if numpyImages:
                              dumpNumpyImage(output, refImage, prefix + '.ref.png')
                              dumpNumpyImage(output, srcImage, prefix + '.src.png')
                          else:
                              refImage.save(prefix + '.ref.png')
                              srcImage.save(prefix + '.src.png')
                              comparer.write_diff(prefix + '.diff.png')
                      if last_bad < last_good and options.diff_state:
                          srcRetracer.diff_state(last_good, callNo, output)
                      last_bad = callNo
                  else:
                      last_good = callNo
  
                  highligher.flush()
          finally:
              srcRun.terminate()
      finally:
          refRun.terminate()
function_tokens: def main ( ) : global options # Parse command line options optparser = optparse . OptionParser ( usage = '\n\t%prog [options] -- [glretrace options] <trace>' , version = '%%prog' ) optparser . add_option ( '-r' , '--retrace' , metavar = 'PROGRAM' , type = 'string' , dest = 'retrace' , default = 'glretrace' , help = 'retrace command [default: %default]' ) optparser . add_option ( '--ref-driver' , metavar = 'DRIVER' , type = 'string' , dest = 'ref_driver' , default = None , help = 'force reference driver' ) optparser . add_option ( '--src-driver' , metavar = 'DRIVER' , type = 'string' , dest = 'src_driver' , default = None , help = 'force source driver' ) optparser . add_option ( '--ref-arg' , metavar = 'OPTION' , type = 'string' , action = 'append' , dest = 'ref_args' , default = [ ] , help = 'pass argument to reference retrace' ) optparser . add_option ( '--src-arg' , metavar = 'OPTION' , type = 'string' , action = 'append' , dest = 'src_args' , default = [ ] , help = 'pass argument to source retrace' ) optparser . add_option ( '--ref-env' , metavar = 'NAME=VALUE' , type = 'string' , action = 'append' , dest = 'ref_env' , default = [ ] , help = 'add variable to reference environment' ) optparser . add_option ( '--src-env' , metavar = 'NAME=VALUE' , type = 'string' , action = 'append' , dest = 'src_env' , default = [ ] , help = 'add variable to source environment' ) optparser . add_option ( '--diff-prefix' , metavar = 'PATH' , type = 'string' , dest = 'diff_prefix' , default = '.' , help = 'prefix for the difference images' ) optparser . add_option ( '-t' , '--threshold' , metavar = 'BITS' , type = "float" , dest = "threshold" , default = 12.0 , help = "threshold precision  [default: %default]" ) optparser . add_option ( '-S' , '--snapshot-frequency' , metavar = 'CALLSET' , type = "string" , dest = "snapshot_frequency" , default = 'draw' , help = "calls to compare [default: %default]" ) optparser . add_option ( '--diff-state' , action = 'store_true' , dest = 'diff_state' , default = False , help = 'diff state between failing calls' ) optparser . add_option ( '-o' , '--output' , metavar = 'FILE' , type = "string" , dest = "output" , help = "output file [default: stdout]" ) ( options , args ) = optparser . parse_args ( sys . argv [ 1 : ] ) ref_env = parse_env ( optparser , options . ref_env ) src_env = parse_env ( optparser , options . src_env ) if not args : optparser . error ( "incorrect number of arguments" ) if options . ref_driver : options . ref_args . insert ( 0 , '--driver=' + options . ref_driver ) if options . src_driver : options . src_args . insert ( 0 , '--driver=' + options . src_driver ) refRetracer = Retracer ( options . retrace , options . ref_args + args , ref_env ) srcRetracer = Retracer ( options . retrace , options . src_args + args , src_env ) if options . output : output = open ( options . output , 'wt' ) else : output = sys . stdout highligher = AutoHighlighter ( output ) highligher . write ( 'call\tprecision\n' ) last_bad = - 1 last_good = 0 refRun = refRetracer . snapshot ( options . snapshot_frequency ) try : srcRun = srcRetracer . snapshot ( options . snapshot_frequency ) try : while True : # Get the reference image refImage , refCallNo = refRun . nextSnapshot ( ) if refImage is None : break # Get the source image srcImage , srcCallNo = srcRun . nextSnapshot ( ) if srcImage is None : break assert refCallNo == srcCallNo callNo = refCallNo # Compare the two images if isinstance ( refImage , Image . Image ) and isinstance ( srcImage , Image . Image ) : # Using PIL numpyImages = False comparer = Comparer ( refImage , srcImage ) precision = comparer . precision ( ) else : # Using numpy (for floating point images) # TODO: drop PIL when numpy path becomes general enough import numpy assert not isinstance ( refImage , Image . Image ) assert not isinstance ( srcImage , Image . Image ) numpyImages = True assert refImage . shape == srcImage . shape diffImage = numpy . square ( srcImage - refImage ) height , width , channels = diffImage . shape square_error = numpy . sum ( diffImage ) square_error += numpy . finfo ( numpy . float32 ) . eps rel_error = square_error / float ( height * width * channels ) bits = - math . log ( rel_error ) / math . log ( 2.0 ) precision = bits mismatch = precision < options . threshold if mismatch : highligher . color ( highligher . red ) highligher . bold ( ) highligher . write ( '%u\t%f\n' % ( callNo , precision ) ) if mismatch : highligher . normal ( ) if mismatch : if options . diff_prefix : prefix = os . path . join ( options . diff_prefix , '%010u' % callNo ) prefix_dir = os . path . dirname ( prefix ) if not os . path . isdir ( prefix_dir ) : os . makedirs ( prefix_dir ) if numpyImages : dumpNumpyImage ( output , refImage , prefix + '.ref.png' ) dumpNumpyImage ( output , srcImage , prefix + '.src.png' ) else : refImage . save ( prefix + '.ref.png' ) srcImage . save ( prefix + '.src.png' ) comparer . write_diff ( prefix + '.diff.png' ) if last_bad < last_good and options . diff_state : srcRetracer . diff_state ( last_good , callNo , output ) last_bad = callNo else : last_good = callNo highligher . flush ( ) finally : srcRun . terminate ( ) finally : refRun . terminate ( )