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
22import numpy
23
24def main():
25    """Test that when the black level is locked, it doesn't change.
26
27    Shoot with the camera covered (i.e.) dark/black. The test varies the
28    sensitivity parameter and checks if the black level changes.
29    """
30    NAME = os.path.basename(__file__).split(".")[0]
31
32    NUM_STEPS = 5
33
34    req = {
35        "android.blackLevel.lock": True,
36        "android.control.mode": 0,
37        "android.control.aeMode": 0,
38        "android.control.awbMode": 0,
39        "android.control.afMode": 0,
40        "android.sensor.frameDuration": 0,
41        "android.sensor.exposureTime": 10*1000*1000
42        }
43
44    # The most frequent pixel value in each image; assume this is the black
45    # level, since the images are all dark (shot with the lens covered).
46    modes = []
47
48    with its.device.ItsSession() as cam:
49        props = cam.get_camera_properties()
50        sens_range = props['android.sensor.info.sensitivityRange']
51        sensitivities = range(sens_range[0],
52                              sens_range[1]+1,
53                              int((sens_range[1] - sens_range[0]) / NUM_STEPS))
54        for si, s in enumerate(sensitivities):
55            req["android.sensor.sensitivity"] = s
56            cap = cam.do_capture(req)
57            yimg,_,_ = its.image.convert_capture_to_planes(cap)
58            hist,_ = numpy.histogram(yimg*255, 256, (0,256))
59            modes.append(numpy.argmax(hist))
60
61            # Add this histogram to a plot; solid for shots without BL
62            # lock, dashes for shots with BL lock
63            pylab.plot(range(16), hist.tolist()[:16])
64
65    pylab.xlabel("Luma DN, showing [0:16] out of full [0:256] range")
66    pylab.ylabel("Pixel count")
67    pylab.title("Histograms for different sensitivities")
68    matplotlib.pyplot.savefig("%s_plot_histograms.png" % (NAME))
69
70    # Check that the black levels are all the same.
71    print "Black levels:", modes
72    assert(all([modes[i] == modes[0] for i in range(len(modes))]))
73
74if __name__ == '__main__':
75    main()
76
77