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.device
17import its.objects
18from matplotlib import pylab
19import os.path
20import matplotlib
21import matplotlib.pyplot
22
23def main():
24    """Test that BLC and LSC look reasonable.
25    """
26    NAME = os.path.basename(__file__).split(".")[0]
27
28    r_means_center = []
29    g_means_center = []
30    b_means_center = []
31    r_means_corner = []
32    g_means_corner = []
33    b_means_corner = []
34
35    with its.device.ItsSession() as cam:
36        props = cam.get_camera_properties()
37        expt_range = props['android.sensor.info.exposureTimeRange']
38
39        # Get AE+AWB lock first, so the auto values in the capture result are
40        # populated properly.
41        r = [[0,0,1,1,1]]
42        ae_sen,ae_exp,awb_gains,awb_transform,_ \
43                = cam.do_3a(r,r,r,do_af=False,get_results=True)
44        print "AE:", ae_sen, ae_exp / 1000000.0
45        print "AWB:", awb_gains, awb_transform
46
47        # Set analog gain (sensitivity) to 800
48        ae_exp = ae_exp * ae_sen / 800
49        ae_sen = 800
50
51        # Capture range of exposures from 1/100x to 4x of AE estimate.
52        exposures = [ae_exp*x/100.0 for x in [1]+range(10,401,40)]
53        exposures = [e for e in exposures
54                     if e >= expt_range[0] and e <= expt_range[1]]
55
56        # Convert the transform back to rational.
57        awb_transform_rat = its.objects.float_to_rational(awb_transform)
58
59        # Linear tonemap
60        tmap = sum([[i/63.0,i/63.0] for i in range(64)], [])
61
62        reqs = []
63        for e in exposures:
64            req = its.objects.manual_capture_request(ae_sen,e)
65            req["android.tonemap.mode"] = 0
66            req["android.tonemap.curve"] = {
67                "red": tmap, "green": tmap, "blue": tmap}
68            req["android.colorCorrection.transform"] = awb_transform_rat
69            req["android.colorCorrection.gains"] = awb_gains
70            reqs.append(req)
71
72        caps = cam.do_capture(reqs)
73        for i,cap in enumerate(caps):
74            img = its.image.convert_capture_to_rgb_image(cap)
75            its.image.write_image(img, "%s_i=%d.jpg"%(NAME, i))
76
77            tile_center = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
78            rgb_means = its.image.compute_image_means(tile_center)
79            r_means_center.append(rgb_means[0])
80            g_means_center.append(rgb_means[1])
81            b_means_center.append(rgb_means[2])
82
83            tile_corner = its.image.get_image_patch(img, 0.0, 0.0, 0.1, 0.1)
84            rgb_means = its.image.compute_image_means(tile_corner)
85            r_means_corner.append(rgb_means[0])
86            g_means_corner.append(rgb_means[1])
87            b_means_corner.append(rgb_means[2])
88
89    fig = matplotlib.pyplot.figure()
90    pylab.plot(exposures, r_means_center, 'r')
91    pylab.plot(exposures, g_means_center, 'g')
92    pylab.plot(exposures, b_means_center, 'b')
93    pylab.ylim([0,1])
94    matplotlib.pyplot.savefig("%s_plot_means_center.png" % (NAME))
95
96    fig = matplotlib.pyplot.figure()
97    pylab.plot(exposures, r_means_corner, 'r')
98    pylab.plot(exposures, g_means_corner, 'g')
99    pylab.plot(exposures, b_means_corner, 'b')
100    pylab.ylim([0,1])
101    matplotlib.pyplot.savefig("%s_plot_means_corner.png" % (NAME))
102
103if __name__ == '__main__':
104    main()
105
106