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.curveRed"] = tmap
67            req["android.tonemap.curveGreen"] = tmap
68            req["android.tonemap.curveBlue"] = tmap
69            req["android.colorCorrection.transform"] = awb_transform_rat
70            req["android.colorCorrection.gains"] = awb_gains
71            reqs.append(req)
72
73        caps = cam.do_capture(reqs)
74        for i,cap in enumerate(caps):
75            img = its.image.convert_capture_to_rgb_image(cap)
76            its.image.write_image(img, "%s_i=%d.jpg"%(NAME, i))
77
78            tile_center = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
79            rgb_means = its.image.compute_image_means(tile_center)
80            r_means_center.append(rgb_means[0])
81            g_means_center.append(rgb_means[1])
82            b_means_center.append(rgb_means[2])
83
84            tile_corner = its.image.get_image_patch(img, 0.0, 0.0, 0.1, 0.1)
85            rgb_means = its.image.compute_image_means(tile_corner)
86            r_means_corner.append(rgb_means[0])
87            g_means_corner.append(rgb_means[1])
88            b_means_corner.append(rgb_means[2])
89
90    fig = matplotlib.pyplot.figure()
91    pylab.plot(exposures, r_means_center, 'r')
92    pylab.plot(exposures, g_means_center, 'g')
93    pylab.plot(exposures, b_means_center, 'b')
94    pylab.ylim([0,1])
95    matplotlib.pyplot.savefig("%s_plot_means_center.png" % (NAME))
96
97    fig = matplotlib.pyplot.figure()
98    pylab.plot(exposures, r_means_corner, 'r')
99    pylab.plot(exposures, g_means_corner, 'g')
100    pylab.plot(exposures, b_means_corner, 'b')
101    pylab.ylim([0,1])
102    matplotlib.pyplot.savefig("%s_plot_means_corner.png" % (NAME))
103
104if __name__ == '__main__':
105    main()
106
107