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 os.path
16import its.caps
17import its.device
18import its.image
19import its.objects
20import matplotlib
21from matplotlib import pylab
22
23NAME = os.path.basename(__file__).split('.')[0]
24BAYER_LIST = ['R', 'GR', 'GB', 'B']
25DIFF_THRESH = 0.0012  # absolute variance delta threshold
26FRAC_THRESH = 0.2  # relative variance delta threshold
27NUM_STEPS = 4
28STATS_GRID = 49  # center 2.04% of image for calculations
29
30
31def main():
32    """Verify that the DNG raw model parameters are correct."""
33
34    # Pass if the difference between expected and computed variances is small,
35    # defined as being within an absolute variance delta or relative variance
36    # delta of the expected variance, whichever is larger. This is to allow the
37    # test to pass in the presence of some randomness (since this test is
38    # measuring noise of a small patch) and some imperfect scene conditions
39    # (since ITS doesn't require a perfectly uniformly lit scene).
40
41    with its.device.ItsSession() as cam:
42
43        props = cam.get_camera_properties()
44        its.caps.skip_unless(its.caps.raw(props) and
45                             its.caps.raw16(props) and
46                             its.caps.manual_sensor(props) and
47                             its.caps.read_3a(props) and
48                             its.caps.per_frame_control(props) and
49                             not its.caps.mono_camera(props))
50        debug = its.caps.debug_mode()
51
52        white_level = float(props['android.sensor.info.whiteLevel'])
53        cfa_idxs = its.image.get_canonical_cfa_order(props)
54        aax = props['android.sensor.info.activeArraySize']['left']
55        aay = props['android.sensor.info.activeArraySize']['top']
56        aaw = props['android.sensor.info.activeArraySize']['right']-aax
57        aah = props['android.sensor.info.activeArraySize']['bottom']-aay
58
59        # Expose for the scene with min sensitivity
60        sens_min, sens_max = props['android.sensor.info.sensitivityRange']
61        sens_step = (sens_max - sens_min) / NUM_STEPS
62        s_ae, e_ae, _, _, f_dist = cam.do_3a(get_results=True)
63        s_e_prod = s_ae * e_ae
64        sensitivities = range(sens_min, sens_max, sens_step)
65
66        var_expected = [[], [], [], []]
67        var_measured = [[], [], [], []]
68        x = STATS_GRID/2  # center in H of STATS_GRID
69        y = STATS_GRID/2  # center in W of STATS_GRID
70        for sens in sensitivities:
71
72            # Capture a raw frame with the desired sensitivity
73            exp = int(s_e_prod / float(sens))
74            req = its.objects.manual_capture_request(sens, exp, f_dist)
75            if debug:
76                cap = cam.do_capture(req, cam.CAP_RAW)
77                planes = its.image.convert_capture_to_planes(cap, props)
78            else:
79                cap = cam.do_capture(req, {'format': 'rawStats',
80                                           'gridWidth': aaw/STATS_GRID,
81                                           'gridHeight': aah/STATS_GRID})
82                mean_img, var_img = its.image.unpack_rawstats_capture(cap)
83
84            # Test each raw color channel (R, GR, GB, B)
85            noise_profile = cap['metadata']['android.sensor.noiseProfile']
86            assert len(noise_profile) == len(BAYER_LIST)
87            for i in range(len(BAYER_LIST)):
88                # Get the noise model parameters for this channel of this shot.
89                ch = cfa_idxs[i]
90                s, o = noise_profile[ch]
91
92                # Use a very small patch to ensure gross uniformity (i.e. so
93                # non-uniform lighting or vignetting doesn't affect the variance
94                # calculation)
95                black_level = its.image.get_black_level(i, props,
96                                                        cap['metadata'])
97                level_range = white_level - black_level
98                if debug:
99                    plane = ((planes[i] * white_level - black_level) /
100                             level_range)
101                    tile = its.image.get_image_patch(plane, 0.49, 0.49,
102                                                     0.02, 0.02)
103                    mean_img_ch = tile.mean()
104                    var_measured[i].append(
105                            its.image.compute_image_variances(tile)[0])
106                else:
107                    mean_img_ch = (mean_img[x, y, ch]-black_level)/level_range
108                    var_measured[i].append(var_img[x, y, ch]/level_range**2)
109                var_expected[i].append(s * mean_img_ch + o)
110
111    for i, ch in enumerate(BAYER_LIST):
112        pylab.plot(sensitivities, var_expected[i], 'rgkb'[i],
113                   label=ch+' expected')
114        pylab.plot(sensitivities, var_measured[i], 'rgkb'[i]+'--',
115                   label=ch+' measured')
116    pylab.xlabel('Sensitivity')
117    pylab.ylabel('Center patch variance')
118    pylab.legend(loc=2)
119    matplotlib.pyplot.savefig('%s_plot.png' % NAME)
120
121    # PASS/FAIL check
122    for i, ch in enumerate(BAYER_LIST):
123        diffs = [abs(var_measured[i][j] - var_expected[i][j])
124                 for j in range(len(sensitivities))]
125        print 'Diffs (%s):'%(ch), diffs
126        for j, diff in enumerate(diffs):
127            thresh = max(DIFF_THRESH, FRAC_THRESH*var_expected[i][j])
128            assert diff <= thresh
129
130if __name__ == '__main__':
131    main()
132