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
16
17import its.caps
18import its.device
19import its.image
20import its.objects
21import its.target
22
23import matplotlib
24from matplotlib import pylab
25
26NAME = os.path.basename(__file__).split('.')[0]
27
28
29def main():
30    """Test that the android.sensor.exposureTime parameter is applied."""
31
32    exp_times = []
33    r_means = []
34    g_means = []
35    b_means = []
36
37    with its.device.ItsSession() as cam:
38        props = cam.get_camera_properties()
39        its.caps.skip_unless(its.caps.compute_target_exposure(props))
40        sync_latency = its.caps.sync_latency(props)
41
42        debug = its.caps.debug_mode()
43        largest_yuv = its.objects.get_largest_yuv_format(props)
44        if debug:
45            fmt = largest_yuv
46        else:
47            match_ar = (largest_yuv['width'], largest_yuv['height'])
48            fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar)
49
50        e, s = its.target.get_target_exposure_combos(cam)['midExposureTime']
51        for i, e_mult in enumerate([0.8, 0.9, 1.0, 1.1, 1.2]):
52            req = its.objects.manual_capture_request(
53                    s, e * e_mult, 0.0, True, props)
54            cap = its.device.do_capture_with_latency(
55                    cam, req, sync_latency, fmt)
56            img = its.image.convert_capture_to_rgb_image(cap)
57            its.image.write_image(
58                    img, '%s_frame%d.jpg' % (NAME, i))
59            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
60            rgb_means = its.image.compute_image_means(tile)
61            exp_times.append(e * e_mult)
62            r_means.append(rgb_means[0])
63            g_means.append(rgb_means[1])
64            b_means.append(rgb_means[2])
65
66    # Draw a plot.
67    pylab.plot(exp_times, r_means, '-ro')
68    pylab.plot(exp_times, g_means, '-go')
69    pylab.plot(exp_times, b_means, '-bo')
70    pylab.ylim([0, 1])
71    pylab.title(NAME)
72    pylab.xlabel('Exposure times (ns)')
73    pylab.ylabel('RGB means')
74    plot_name = '%s_plot_means.png' % NAME
75    matplotlib.pyplot.savefig(plot_name)
76
77    # Test for pass/fail: check that each shot is brighter than the previous.
78    for means in [r_means, g_means, b_means]:
79        for i in range(len(means)-1):
80            assert means[i+1] > means[i], 'See %s' % plot_name
81
82if __name__ == '__main__':
83    main()
84
85