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
19from matplotlib import 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        debug = its.caps.debug_mode()
39        largest_yuv = its.objects.get_largest_yuv_format(props)
40        if debug:
41            fmt = largest_yuv
42        else:
43            match_ar = (largest_yuv['width'], largest_yuv['height'])
44            fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar)
45
46        expt_range = props['android.sensor.info.exposureTimeRange']
47        sens_range = props['android.sensor.info.sensitivityRange']
48
49        # Take a shot with very low ISO and exposure time. Expect it to
50        # be black.
51        print "Black shot: sens = %d, exp time = %.4fms" % (
52                sens_range[0], expt_range[0]/1000000.0)
53        req = its.objects.manual_capture_request(sens_range[0], expt_range[0])
54        cap = cam.do_capture(req, fmt)
55        img = its.image.convert_capture_to_rgb_image(cap)
56        its.image.write_image(img, "%s_black.jpg" % (NAME))
57        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
58        black_means = its.image.compute_image_means(tile)
59        r_means.append(black_means[0])
60        g_means.append(black_means[1])
61        b_means.append(black_means[2])
62        print "Dark pixel means:", black_means
63
64        # Take a shot with very high ISO and exposure time. Expect it to
65        # be white.
66        print "White shot: sens = %d, exp time = %.2fms" % (
67                sens_range[1], expt_range[1]/1000000.0)
68        req = its.objects.manual_capture_request(sens_range[1], expt_range[1])
69        cap = cam.do_capture(req, fmt)
70        img = its.image.convert_capture_to_rgb_image(cap)
71        its.image.write_image(img, "%s_white.jpg" % (NAME))
72        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
73        white_means = its.image.compute_image_means(tile)
74        r_means.append(white_means[0])
75        g_means.append(white_means[1])
76        b_means.append(white_means[2])
77        print "Bright pixel means:", white_means
78
79        # Draw a plot.
80        pylab.plot([0,1], r_means, 'r')
81        pylab.plot([0,1], g_means, 'g')
82        pylab.plot([0,1], b_means, 'b')
83        pylab.ylim([0,1])
84        matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
85
86        for val in black_means:
87            assert(val < 0.025)
88        for val in white_means:
89            assert(val > 0.975)
90
91if __name__ == '__main__':
92    main()
93
94