1# Copyright (c) 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 ConfigParser
6
7from autotest_lib.client.cros.image_comparison import pdiff_image_comparer
8from autotest_lib.client.cros.image_comparison import publisher
9from autotest_lib.client.cros.image_comparison import rgb_image_comparer
10from autotest_lib.client.cros.image_comparison import verifier
11from autotest_lib.client.cros.video import method_logger
12
13
14class ImageComparisonFactory(object):
15    """
16    Responsible for instantiating objects used in image comparison based tests.
17
18    """
19
20
21    def __init__(self, conf_filepath):
22        """
23        @param conf_filepath: path, full path to the conf file.
24
25        """
26        self.conf_filepath = conf_filepath
27        self._load_configuration()
28
29
30    def _load_configuration(self):
31        """
32        Loads values from configuration file.
33
34        """
35        parser = ConfigParser.SafeConfigParser()
36        parser.read(self.conf_filepath)
37
38        self.pixel_thres = parser.getint('rgb', 'rgb_pixel_threshold')
39        self.pixel_count_thres = parser.getint('all', 'pixel_count_threshold')
40        self.desired_comp_h = parser.getint('all', 'desired_comp_h')
41        self.desired_comp_w = parser.getint('all', 'desired_comp_w')
42
43
44    @method_logger.log
45    def make_rgb_comparer(self):
46        """
47        @returns an RGBImageComparer object initialized with config. values.
48
49        """
50        return rgb_image_comparer.RGBImageComparer(self.pixel_thres)
51
52
53    @method_logger.log
54    def make_pdiff_comparer(self):
55        """
56        @returns a PDiffImageComparer object.
57
58        """
59        return pdiff_image_comparer.PdiffImageComparer()
60
61
62    @method_logger.log
63    def make_image_verifier(self, image_comparer, stop_on_first_failure=False):
64        """
65        @param image_comparer: any object that implements compare(). Currently,
66                               it could RGBImageComparer or
67                               UploadOnFailComparer.
68
69        @param stop_on_first_failure: bool, True if we should stop the test when
70                                      we encounter the first failed comparison.
71                                      False if we should continue the test.
72        @returns a Verifier object initialized with config. values.
73
74        """
75        if self.desired_comp_h == 0 or self.desired_comp_w == 0:
76            box = None
77        else:
78            box = (0, 0, self.desired_comp_w, self.desired_comp_h)
79
80        return verifier.Verifier(image_comparer,
81                                 stop_on_first_failure,
82                                 threshold=self.pixel_count_thres,
83                                 box=box)
84
85
86    @method_logger.log
87    def make_imagediff_publisher(self, results_folder):
88        """
89        @param results_folder: path, where to publish the results to
90        @returns an ImageDIffPublisher object
91
92        """
93        return publisher.ImageDiffPublisher(results_folder)