1#!/usr/bin/env python2 2# Copyright 2014 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import argparse 7import errno 8import logging 9import os 10import re 11import sys 12 13import common # pylint: disable=unused-import 14 15from autotest_lib.client.common_lib import utils 16 17 18class ImageGeneratorError(Exception): 19 """Error in ImageGenerator.""" 20 pass 21 22 23class ImageGenerator(object): 24 """A class to generate the calibration images with different sizes. 25 26 It generates the SVG images which are easy to be produced by changing its 27 XML text content. 28 29 """ 30 31 TEMPLATE_WIDTH = 1680 32 TEMPLATE_HEIGHT = 1052 33 TEMPLATE_FILENAME = 'template-%dx%d.svg' % (TEMPLATE_WIDTH, TEMPLATE_HEIGHT) 34 GS_URL = ('http://commondatastorage.googleapis.com/chromeos-localmirror/' 35 'distfiles/chameleon_calibration_images') 36 37 38 def __init__(self): 39 """Construct an ImageGenerator. 40 """ 41 module_dir = os.path.dirname(sys.modules[__name__].__file__) 42 template_path = os.path.join(module_dir, 'calibration_images', 43 self.TEMPLATE_FILENAME) 44 self._image_template = open(template_path).read() 45 46 47 def generate_image(self, width, height, filename): 48 """Generate the image with the given width and height. 49 50 @param width: The width of the image. 51 @param height: The height of the image. 52 @param filename: The filename to output, svg file or png file. 53 """ 54 if filename.endswith('.svg'): 55 with open(filename, 'w+') as f: 56 logging.debug('Generate the image with size %dx%d to %s', 57 width, height, filename) 58 f.write(self._image_template.format( 59 scale_width=float(width)/self.TEMPLATE_WIDTH, 60 scale_height=float(height)/self.TEMPLATE_HEIGHT)) 61 elif filename.endswith('.png'): 62 pregen_filename = 'image-%dx%d.png' % (width, height) 63 pregen_path = os.path.join(self.GS_URL, pregen_filename) 64 logging.debug('Fetch the image from: %s', pregen_path) 65 if utils.system('wget -q -O %s %s' % (filename, pregen_path), 66 ignore_status=True): 67 raise ImageGeneratorError('Failed to fetch the image: %s' % 68 pregen_filename) 69 else: 70 raise ImageGeneratorError('The image format not supported') 71 72 @staticmethod 73 def get_extrema(image): 74 """Returns a 2-tuple containing minimum and maximum values of the image. 75 76 @param image: the calibration image projected by DUT. 77 @return a tuple of (minimum, maximum) 78 """ 79 w, h = image.size 80 min_value = 255 81 max_value = 0 82 # scan the middle vertical line 83 x = w / 2 84 for i in range(0, h/2): 85 v = image.getpixel((x, i))[0] 86 if v < min_value: 87 min_value = v 88 if v > max_value: 89 max_value = v 90 return (min_value, max_value) 91 92 93if __name__ == '__main__': 94 parser = argparse.ArgumentParser() 95 parser.add_argument('output', type=str, 96 help='filename of the calibration image') 97 args = parser.parse_args() 98 99 matched = re.search(r'image-(\d+)x(\d+).(png|svg)', args.output) 100 if not matched: 101 logging.error('The filename should be like: image-1920x1080.svg') 102 parser.print_help() 103 sys.exit(errno.EINVAL) 104 105 width = int(matched.group(1)) 106 height = int(matched.group(2)) 107 generator = ImageGenerator() 108 generator.generate_image(width, height, args.output) 109