1# Copyright 2013 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import its.image
16import its.caps
17import its.device
18import its.objects
19import its.target
20import os.path
21
22def main():
23    """Test that the android.flash.mode parameter is applied.
24    """
25    NAME = os.path.basename(__file__).split(".")[0]
26
27    with its.device.ItsSession() as cam:
28        props = cam.get_camera_properties()
29        its.caps.skip_unless(its.caps.compute_target_exposure(props) and
30                             its.caps.flash(props) and
31                             its.caps.per_frame_control(props))
32
33        flash_modes_reported = []
34        flash_states_reported = []
35        g_means = []
36
37        # Manually set the exposure to be a little on the dark side, so that
38        # it should be obvious whether the flash fired or not, and use a
39        # linear tonemap.
40        debug = its.caps.debug_mode()
41        largest_yuv = its.objects.get_largest_yuv_format(props)
42        if debug:
43            fmt = largest_yuv
44        else:
45            match_ar = (largest_yuv['width'], largest_yuv['height'])
46            fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar)
47
48        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
49        e /= 4
50        req = its.objects.manual_capture_request(s, e, 0.0, True, props)
51
52        for f in [0,1,2]:
53            req["android.flash.mode"] = f
54            cap = cam.do_capture(req, fmt)
55            flash_modes_reported.append(cap["metadata"]["android.flash.mode"])
56            flash_states_reported.append(cap["metadata"]["android.flash.state"])
57            img = its.image.convert_capture_to_rgb_image(cap)
58            its.image.write_image(img, "%s_mode=%d.jpg" % (NAME, f))
59            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
60            rgb = its.image.compute_image_means(tile)
61            g_means.append(rgb[1])
62
63        assert(flash_modes_reported == [0,1,2])
64        assert(flash_states_reported[0] not in [3,4])
65        assert(flash_states_reported[1] in [3,4])
66        assert(flash_states_reported[2] in [3,4])
67
68        print "G brightnesses:", g_means
69        assert(g_means[1] > g_means[0])
70        assert(g_means[2] > g_means[0])
71
72if __name__ == '__main__':
73    main()
74
75