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 settings latch on the right frame.
31
32    Takes a bunch of shots using back-to-back requests, varying the capture
33    request parameters between shots. Checks that the images that come back
34    have the expected properties.
35    """
36
37    with its.device.ItsSession() as cam:
38        props = cam.get_camera_properties()
39        its.caps.skip_unless(its.caps.full_or_better(props))
40
41        _, fmt = its.objects.get_fastest_manual_capture_settings(props)
42        e, s = its.target.get_target_exposure_combos(cam)['midExposureTime']
43        e /= 2.0
44
45        r_means = []
46        g_means = []
47        b_means = []
48
49        reqs = [
50                its.objects.manual_capture_request(s, e, 0.0, True, props),
51                its.objects.manual_capture_request(s, e, 0.0, True, props),
52                its.objects.manual_capture_request(s*2, e, 0.0, True, props),
53                its.objects.manual_capture_request(s*2, e, 0.0, True, props),
54                its.objects.manual_capture_request(s, e, 0.0, True, props),
55                its.objects.manual_capture_request(s, e, 0.0, True, props),
56                its.objects.manual_capture_request(s, e*2, 0.0, True, props),
57                its.objects.manual_capture_request(s, e, 0.0, True, props),
58                its.objects.manual_capture_request(s*2, e, 0.0, True, props),
59                its.objects.manual_capture_request(s, e, 0.0, True, props),
60                its.objects.manual_capture_request(s, e*2, 0.0, True, props),
61                its.objects.manual_capture_request(s, e, 0.0, True, props),
62                its.objects.manual_capture_request(s, e*2, 0.0, True, props),
63                its.objects.manual_capture_request(s, e*2, 0.0, True, props),
64                ]
65
66        caps = cam.do_capture(reqs, fmt)
67        for i, cap in enumerate(caps):
68            img = its.image.convert_capture_to_rgb_image(cap)
69            its.image.write_image(img, '%s_i=%02d.jpg' % (NAME, i))
70            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
71            rgb_means = its.image.compute_image_means(tile)
72            r_means.append(rgb_means[0])
73            g_means.append(rgb_means[1])
74            b_means.append(rgb_means[2])
75
76        # Draw a plot.
77        idxs = range(len(r_means))
78        pylab.plot(idxs, r_means, '-ro')
79        pylab.plot(idxs, g_means, '-go')
80        pylab.plot(idxs, b_means, '-bo')
81        pylab.ylim([0, 1])
82        pylab.title(NAME)
83        pylab.xlabel('capture')
84        pylab.ylabel('RGB means')
85        matplotlib.pyplot.savefig('%s_plot_means.png' % (NAME))
86
87        g_avg = sum(g_means) / len(g_means)
88        g_ratios = [g / g_avg for g in g_means]
89        g_hilo = [g > 1.0 for g in g_ratios]
90        assert(g_hilo == [False, False, True, True, False, False, True,
91                          False, True, False, True, False, True, True])
92
93if __name__ == '__main__':
94    main()
95
96