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 os.path
16import its.caps
17import its.device
18import its.image
19import its.objects
20import its.target
21
22NAME = os.path.basename(__file__).split('.')[0]
23GRADIENT_DELTA = 0.1
24Y_RELATIVE_DELTA_FLASH = 0.1  # 10%
25Y_RELATIVE_DELTA_TORCH = 0.05  # 5%
26
27
28def main():
29    """Test that the android.flash.mode parameter is applied."""
30
31    with its.device.ItsSession() as cam:
32        props = cam.get_camera_properties()
33        its.caps.skip_unless(its.caps.compute_target_exposure(props) and
34                             its.caps.flash(props))
35        sync_latency = its.caps.sync_latency(props)
36
37        flash_modes_reported = []
38        flash_states_reported = []
39        means = []
40        grads = []
41
42        # Manually set the exposure to be a little on the dark side, so that
43        # it should be obvious whether the flash fired or not, and use a
44        # linear tonemap.
45        debug = its.caps.debug_mode()
46        largest_yuv = its.objects.get_largest_yuv_format(props)
47        if debug:
48            fmt = largest_yuv
49        else:
50            match_ar = (largest_yuv['width'], largest_yuv['height'])
51            fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar)
52
53        e, s = its.target.get_target_exposure_combos(cam)['midExposureTime']
54        e /= 2
55        req = its.objects.manual_capture_request(s, e, 0.0, True, props)
56
57        for f in [0, 1, 2]:
58            req['android.flash.mode'] = f
59            cap = its.device.do_capture_with_latency(
60                    cam, req, sync_latency, fmt)
61            flash_modes_reported.append(cap['metadata']['android.flash.mode'])
62            flash_states_reported.append(cap['metadata']['android.flash.state'])
63            y, _, _ = its.image.convert_capture_to_planes(cap, props)
64            its.image.write_image(y, '%s_%d.jpg' % (NAME, f))
65            tile = its.image.get_image_patch(y, 0.375, 0.375, 0.25, 0.25)
66            its.image.write_image(tile, '%s_%d_tile.jpg' % (NAME, f))
67            means.append(its.image.compute_image_means(tile)[0])
68            grads.append(its.image.compute_image_max_gradients(tile)[0])
69
70        assert flash_modes_reported == [0, 1, 2]
71        assert flash_states_reported[0] not in [3, 4]
72        assert flash_states_reported[1] in [3, 4]
73        assert flash_states_reported[2] in [3, 4]
74
75        print 'Brightnesses:', means
76        print 'Max gradients: ', grads
77        assert (grads[1]-grads[0] > GRADIENT_DELTA or
78                (means[1]-means[0]) / means[0] > Y_RELATIVE_DELTA_FLASH)
79        assert (grads[2]-grads[0] > GRADIENT_DELTA or
80                (means[2]-means[0]) / means[0] > Y_RELATIVE_DELTA_TORCH)
81
82if __name__ == '__main__':
83    main()
84
85