1# Copyright 2015 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.caps
16import its.device
17import its.objects
18import matplotlib
19import numpy
20import os
21import os.path
22import pylab
23
24def main():
25    """Test that the android.shading.mode param is applied.
26
27    Switching shading modes and checks that the lens shading maps are
28    modified as expected.
29    """
30    NAME = os.path.basename(__file__).split(".")[0]
31
32    NUM_SHADING_MODE_SWITCH_LOOPS = 3
33    THRESHOLD_DIFF_RATIO = 0.15
34
35    with its.device.ItsSession() as cam:
36        props = cam.get_camera_properties()
37
38        its.caps.skip_unless(its.caps.per_frame_control(props) and
39                             its.caps.lsc_map(props) and
40                             its.caps.lsc_off(props))
41
42        assert(props.has_key("android.lens.info.shadingMapSize") and
43               props["android.lens.info.shadingMapSize"] != None)
44
45        # lsc_off devices should always support OFF(0), FAST(1), and HQ(2)
46        assert(props.has_key("android.shading.availableModes") and
47               set(props["android.shading.availableModes"]) == set([0, 1, 2]))
48
49        num_map_gains = props["android.lens.info.shadingMapSize"]["width"] * \
50                        props["android.lens.info.shadingMapSize"]["height"] * 4
51
52        # Test 1: Switching shading modes several times and verify:
53        #   1. Lens shading maps with mode OFF are all 1.0
54        #   2. Lens shading maps with mode FAST are similar after switching
55        #      shading modes.
56        #   3. Lens shading maps with mode HIGH_QUALITY are similar after
57        #      switching shading modes.
58        cam.do_3a();
59
60        # Get the reference lens shading maps for OFF, FAST, and HIGH_QUALITY
61        # in different sessions.
62        # reference_maps[mode]
63        reference_maps = [[] for mode in range(3)]
64        reference_maps[0] = [1.0] * num_map_gains
65        for mode in range(1, 3):
66            req = its.objects.auto_capture_request();
67            req["android.statistics.lensShadingMapMode"] = 1
68            req["android.shading.mode"] = mode
69            reference_maps[mode] = cam.do_capture(req)["metadata"] \
70                    ["android.statistics.lensShadingMap"]
71
72        # Get the lens shading maps while switching modes in one session.
73        reqs = []
74        for i in range(NUM_SHADING_MODE_SWITCH_LOOPS):
75            for mode in range(3):
76                req = its.objects.auto_capture_request();
77                req["android.statistics.lensShadingMapMode"] = 1
78                req["android.shading.mode"] = mode
79                reqs.append(req);
80
81        caps = cam.do_capture(reqs)
82
83        # shading_maps[mode][loop]
84        shading_maps = [[[] for loop in range(NUM_SHADING_MODE_SWITCH_LOOPS)]
85                for mode in range(3)]
86
87        # Get the shading maps out of capture results
88        for i in range(len(caps)):
89            shading_maps[i % 3][i / 3] = \
90                    caps[i]["metadata"]["android.statistics.lensShadingMap"]
91
92        # Draw the maps
93        for mode in range(3):
94            for i in range(NUM_SHADING_MODE_SWITCH_LOOPS):
95                pylab.clf()
96                pylab.plot(range(num_map_gains), shading_maps[mode][i], 'r')
97                pylab.plot(range(num_map_gains), reference_maps[mode], 'g')
98                pylab.xlim([0, num_map_gains])
99                pylab.ylim([0.9, 4.0])
100                matplotlib.pyplot.savefig("%s_ls_maps_mode_%d_loop_%d.png" %
101                                          (NAME, mode, i))
102
103        print "Verifying lens shading maps with mode OFF are all 1.0"
104        for i in range(NUM_SHADING_MODE_SWITCH_LOOPS):
105            assert(numpy.allclose(shading_maps[0][i], reference_maps[0]))
106
107        for mode in range(1, 3):
108            print "Verifying lens shading maps with mode", mode, "are similar"
109            for i in range(NUM_SHADING_MODE_SWITCH_LOOPS):
110                assert(numpy.allclose(shading_maps[mode][i],
111                                      reference_maps[mode],
112                                      THRESHOLD_DIFF_RATIO))
113
114if __name__ == '__main__':
115    main()
116