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_or_better(props))
37
38        _,fmt = its.objects.get_fastest_manual_capture_settings(props)
39        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
40        e /= 2.0
41
42        r_means = []
43        g_means = []
44        b_means = []
45
46        reqs = [
47            its.objects.manual_capture_request(s,  e,   True, props),
48            its.objects.manual_capture_request(s,  e,   True, props),
49            its.objects.manual_capture_request(s*2,e,   True, props),
50            its.objects.manual_capture_request(s*2,e,   True, props),
51            its.objects.manual_capture_request(s,  e,   True, props),
52            its.objects.manual_capture_request(s,  e,   True, props),
53            its.objects.manual_capture_request(s,  e*2, True, props),
54            its.objects.manual_capture_request(s,  e,   True, props),
55            its.objects.manual_capture_request(s*2,e,   True, props),
56            its.objects.manual_capture_request(s,  e,   True, props),
57            its.objects.manual_capture_request(s,  e*2, True, props),
58            its.objects.manual_capture_request(s,  e,   True, props),
59            its.objects.manual_capture_request(s,  e*2, True, props),
60            its.objects.manual_capture_request(s,  e*2, True, props),
61            ]
62
63        caps = cam.do_capture(reqs, fmt)
64        for i,cap in enumerate(caps):
65            img = its.image.convert_capture_to_rgb_image(cap)
66            its.image.write_image(img, "%s_i=%02d.jpg" % (NAME, i))
67            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
68            rgb_means = its.image.compute_image_means(tile)
69            r_means.append(rgb_means[0])
70            g_means.append(rgb_means[1])
71            b_means.append(rgb_means[2])
72
73        # Draw a plot.
74        idxs = range(len(r_means))
75        pylab.plot(idxs, r_means, 'r')
76        pylab.plot(idxs, g_means, 'g')
77        pylab.plot(idxs, b_means, 'b')
78        pylab.ylim([0,1])
79        matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
80
81        g_avg = sum(g_means) / len(g_means)
82        g_ratios = [g / g_avg for g in g_means]
83        g_hilo = [g>1.0 for g in g_ratios]
84        assert(g_hilo == [False, False, True, True, False, False, True,
85                          False, True, False, True, False, True, True])
86
87if __name__ == '__main__':
88    main()
89
90