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