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 matplotlib
22from matplotlib import pylab
23
24NAME = os.path.basename(__file__).split(".")[0]
25
26
27def main():
28    """Test that the device will produce full black+white images."""
29
30    r_means = []
31    g_means = []
32    b_means = []
33
34    with its.device.ItsSession() as cam:
35        props = cam.get_camera_properties()
36        its.caps.skip_unless(its.caps.manual_sensor(props))
37        sync_latency = its.caps.sync_latency(props)
38
39        debug = its.caps.debug_mode()
40        largest_yuv = its.objects.get_largest_yuv_format(props)
41        if debug:
42            fmt = largest_yuv
43        else:
44            match_ar = (largest_yuv["width"], largest_yuv["height"])
45            fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar)
46
47        expt_range = props["android.sensor.info.exposureTimeRange"]
48        sens_range = props["android.sensor.info.sensitivityRange"]
49
50        # Take a shot with very low ISO and exposure time. Expect it to
51        # be black.
52        req = its.objects.manual_capture_request(sens_range[0], expt_range[0])
53        cap = its.device.do_capture_with_latency(cam, req, sync_latency, fmt)
54        img = its.image.convert_capture_to_rgb_image(cap)
55        its.image.write_image(img, "%s_black.jpg" % NAME)
56        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
57        black_means = its.image.compute_image_means(tile)
58        r_means.append(black_means[0])
59        g_means.append(black_means[1])
60        b_means.append(black_means[2])
61        print "Dark pixel means:", black_means
62        r_exp = cap["metadata"]["android.sensor.exposureTime"]
63        r_iso = cap["metadata"]["android.sensor.sensitivity"]
64        print "Black shot write values: sens = %d, exp time = %.4fms" % (
65                sens_range[0], expt_range[0]/1000000.0)
66        print "Black shot read values: sens = %d, exp time = %.4fms\n" % (
67                r_iso, r_exp/1000000.0)
68
69        # Take a shot with very high ISO and exposure time. Expect it to
70        # be white.
71        req = its.objects.manual_capture_request(sens_range[1], expt_range[1])
72        cap = its.device.do_capture_with_latency(cam, req, sync_latency, fmt)
73        img = its.image.convert_capture_to_rgb_image(cap)
74        its.image.write_image(img, "%s_white.jpg" % NAME)
75        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
76        white_means = its.image.compute_image_means(tile)
77        r_means.append(white_means[0])
78        g_means.append(white_means[1])
79        b_means.append(white_means[2])
80        print "Bright pixel means:", white_means
81        r_exp = cap["metadata"]["android.sensor.exposureTime"]
82        r_iso = cap["metadata"]["android.sensor.sensitivity"]
83        print "White shot write values: sens = %d, exp time = %.2fms" % (
84                sens_range[1], expt_range[1]/1000000.0)
85        print "White shot read values: sens = %d, exp time = %.2fms\n" % (
86                r_iso, r_exp/1000000.0)
87
88        # Draw a plot.
89        pylab.title("test_black_white")
90        pylab.plot([0, 1], r_means, "-ro")
91        pylab.plot([0, 1], g_means, "-go")
92        pylab.plot([0, 1], b_means, "-bo")
93        pylab.xlabel("Capture Number")
94        pylab.ylabel("Output Values (Normalized)")
95        pylab.ylim([0, 1])
96        matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
97
98        for black_mean in black_means:
99            assert black_mean < 0.025
100        for white_mean in white_means:
101            assert white_mean > 0.975
102
103if __name__ == "__main__":
104    main()
105
106