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 pylab
20import os.path
21import matplotlib
22import matplotlib.pyplot
23
24def main():
25    """Test that the device will produce full black+white images.
26    """
27    NAME = os.path.basename(__file__).split(".")[0]
28
29    r_means = []
30    g_means = []
31    b_means = []
32
33    with its.device.ItsSession() as cam:
34        props = cam.get_camera_properties()
35        its.caps.skip_unless(its.caps.manual_sensor(props) and
36                             its.caps.per_frame_control(props))
37
38        expt_range = props['android.sensor.info.exposureTimeRange']
39        sens_range = props['android.sensor.info.sensitivityRange']
40
41        # Take a shot with very low ISO and exposure time. Expect it to
42        # be black.
43        print "Black shot: sens = %d, exp time = %.4fms" % (
44                sens_range[0], expt_range[0]/1000000.0)
45        req = its.objects.manual_capture_request(sens_range[0], expt_range[0])
46        cap = cam.do_capture(req)
47        img = its.image.convert_capture_to_rgb_image(cap)
48        its.image.write_image(img, "%s_black.jpg" % (NAME))
49        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
50        black_means = its.image.compute_image_means(tile)
51        r_means.append(black_means[0])
52        g_means.append(black_means[1])
53        b_means.append(black_means[2])
54        print "Dark pixel means:", black_means
55
56        # Take a shot with very high ISO and exposure time. Expect it to
57        # be white.
58        print "White shot: sens = %d, exp time = %.2fms" % (
59                sens_range[1], expt_range[1]/1000000.0)
60        req = its.objects.manual_capture_request(sens_range[1], expt_range[1])
61        cap = cam.do_capture(req)
62        img = its.image.convert_capture_to_rgb_image(cap)
63        its.image.write_image(img, "%s_white.jpg" % (NAME))
64        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
65        white_means = its.image.compute_image_means(tile)
66        r_means.append(white_means[0])
67        g_means.append(white_means[1])
68        b_means.append(white_means[2])
69        print "Bright pixel means:", white_means
70
71        # Draw a plot.
72        pylab.plot([0,1], r_means, 'r')
73        pylab.plot([0,1], g_means, 'g')
74        pylab.plot([0,1], b_means, 'b')
75        pylab.ylim([0,1])
76        matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
77
78        for val in black_means:
79            assert(val < 0.025)
80        for val in white_means:
81            assert(val > 0.975)
82
83if __name__ == '__main__':
84    main()
85
86