1# Copyright 2014 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.caps
17import its.device
18import its.objects
19import os.path
20import pylab
21import matplotlib
22import matplotlib.pyplot
23import numpy
24
25def main():
26    """Tests that EV compensation is applied.
27    """
28    NAME = os.path.basename(__file__).split(".")[0]
29
30    with its.device.ItsSession() as cam:
31        props = cam.get_camera_properties()
32        its.caps.skip_unless(its.caps.ev_compensation(props) and
33                             its.caps.ae_lock(props))
34
35        ev_per_step = its.objects.rational_to_float(
36                props['android.control.aeCompensationStep'])
37        steps_per_ev = int(1.0 / ev_per_step)
38        evs = range(-2 * steps_per_ev, 2 * steps_per_ev + 1, steps_per_ev)
39        lumas = []
40        for ev in evs:
41            # Re-converge 3A, and lock AE once converged. skip AF trigger as
42            # dark/bright scene could make AF convergence fail and this test
43            # doesn't care the image sharpness.
44            cam.do_3a(ev_comp=ev, lock_ae=True, do_af=False)
45
46            # Capture a single shot with the same EV comp and locked AE.
47            req = its.objects.auto_capture_request()
48            req['android.control.aeExposureCompensation'] = ev
49            req["android.control.aeLock"] = True
50            cap = cam.do_capture(req)
51            y = its.image.convert_capture_to_planes(cap)[0]
52            tile = its.image.get_image_patch(y, 0.45,0.45,0.1,0.1)
53            lumas.append(its.image.compute_image_means(tile)[0])
54
55        pylab.plot(evs, lumas, 'r')
56        matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
57
58        # trim trailing 1.0s (for saturated image)
59        while lumas and lumas[-1] == 1.0:
60            lumas.pop(-1)
61        # Only allow positive EVs to give saturated image
62        assert(len(lumas) > 2)
63        luma_diffs = numpy.diff(lumas)
64        min_luma_diffs = min(luma_diffs)
65        print "Min of the luma value difference between adjacent ev comp: ", \
66                min_luma_diffs
67        # All luma brightness should be increasing with increasing ev comp.
68        assert(min_luma_diffs > 0)
69
70if __name__ == '__main__':
71    main()
72