1# Copyright 2014 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import logging 6import os 7import sys 8 9from autotest_lib.client.common_lib import utils 10 11 12class ImageGenerator(object): 13 """A class to generate the calibration images with different sizes. 14 15 It generates the SVG images which are easy to be produced by changing its 16 XML text content. 17 18 """ 19 20 TEMPLATE_WIDTH = 1680 21 TEMPLATE_HEIGHT = 1052 22 TEMPLATE_FILENAME = 'template-%dx%d.svg' % (TEMPLATE_WIDTH, TEMPLATE_HEIGHT) 23 24 25 def __init__(self): 26 """Construct an ImageGenerator. 27 """ 28 module_dir = os.path.dirname(sys.modules[__name__].__file__) 29 template_path = os.path.join(module_dir, 'calibration_images', 30 self.TEMPLATE_FILENAME) 31 self._image_template = open(template_path).read() 32 33 34 def generate_image(self, width, height, filename): 35 """Generate the image with the given width and height. 36 37 @param width: The width of the image. 38 @param height: The height of the image. 39 @param filename: The filename to output, svg file or other format. 40 """ 41 if filename.endswith('.svg'): 42 filename_svg = filename 43 else: 44 filename_svg = filename + '.svg' 45 46 with open(filename_svg, 'w+') as f: 47 logging.debug('Generate the image with size %dx%d to %s', 48 width, height, filename_svg) 49 f.write(self._image_template.format( 50 scale_width=float(width)/self.TEMPLATE_WIDTH, 51 scale_height=float(height)/self.TEMPLATE_HEIGHT)) 52 53 # Convert to different format if needed. 54 if filename_svg != filename: 55 utils.run('convert %s %s' % (filename_svg, filename)) 56 57 58 @staticmethod 59 def get_extrema(image): 60 """Returns a 2-tuple containing minimum and maximum values of the image. 61 62 @param image: the calibration image projected by DUT. 63 @return a tuple of (minimum, maximum) 64 """ 65 w, h = image.size 66 min_value = 255 67 max_value = 0 68 # scan the middle vertical line 69 x = w / 2 70 for i in range(0, h/2): 71 v = image.getpixel((x, i))[0] 72 if v < min_value: 73 min_value = v 74 if v > max_value: 75 max_value = v 76 return (min_value, max_value) 77