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]
27NUM_STEPS = 5
28
29
30def main():
31    """Test that the android.sensor.sensitivity parameter is applied."""
32
33    sensitivities = None
34    r_means = []
35    g_means = []
36    b_means = []
37
38    with its.device.ItsSession() as cam:
39        props = cam.get_camera_properties()
40        its.caps.skip_unless(its.caps.compute_target_exposure(props))
41        sync_latency = its.caps.sync_latency(props)
42
43        debug = its.caps.debug_mode()
44        largest_yuv = its.objects.get_largest_yuv_format(props)
45        if debug:
46            fmt = largest_yuv
47        else:
48            match_ar = (largest_yuv['width'], largest_yuv['height'])
49            fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar)
50
51        expt, _ = its.target.get_target_exposure_combos(cam)['midSensitivity']
52        sens_range = props['android.sensor.info.sensitivityRange']
53        sens_step = (sens_range[1] - sens_range[0]) / float(NUM_STEPS-1)
54        sensitivities = [
55                sens_range[0] + i * sens_step for i in range(NUM_STEPS)]
56
57        for s in sensitivities:
58            req = its.objects.manual_capture_request(s, expt)
59            cap = its.device.do_capture_with_latency(
60                    cam, req, sync_latency, fmt)
61            img = its.image.convert_capture_to_rgb_image(cap)
62            its.image.write_image(img, '%s_iso=%04d.jpg' % (NAME, s))
63            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
64            rgb_means = its.image.compute_image_means(tile)
65            r_means.append(rgb_means[0])
66            g_means.append(rgb_means[1])
67            b_means.append(rgb_means[2])
68
69    # Draw a plot.
70    pylab.plot(sensitivities, r_means, '-ro')
71    pylab.plot(sensitivities, g_means, '-go')
72    pylab.plot(sensitivities, b_means, '-bo')
73    pylab.ylim([0, 1])
74    pylab.title(NAME)
75    pylab.xlabel('Gain (ISO)')
76    pylab.ylabel('RGB means')
77    matplotlib.pyplot.savefig('%s_plot_means.png' % (NAME))
78
79    # Test for pass/fail: check that each shot is brighter than the previous.
80    for means in [r_means, g_means, b_means]:
81        for i in range(len(means)-1):
82            assert means[i+1] > means[i]
83
84if __name__ == '__main__':
85    main()
86
87