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