• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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.device
16import its.caps
17import its.image
18import its.objects
19import its.target
20import os.path
21from matplotlib import pylab
22import matplotlib
23import matplotlib.pyplot
24
25def main():
26    """Capture a set of raw/yuv images with different
27        sensitivity/post Raw sensitivity boost combination
28        and check if the output pixel mean matches request settings
29    """
30    NAME = os.path.basename(__file__).split(".")[0]
31
32    # Each raw image
33    RATIO_THRESHOLD = 0.1
34    # Waive the check if raw pixel value is below this level (signal too small
35    # that small black level error converts to huge error in percentage)
36    RAW_PIXEL_VAL_THRESHOLD = 0.03
37
38    with its.device.ItsSession() as cam:
39        props = cam.get_camera_properties()
40        its.caps.skip_unless(its.caps.raw_output(props) and
41                             its.caps.post_raw_sensitivity_boost(props) and
42                             its.caps.compute_target_exposure(props) and
43                             its.caps.per_frame_control(props) and
44                             not its.caps.mono_camera(props))
45
46        w,h = its.objects.get_available_output_sizes(
47                "yuv", props, (1920, 1080))[0]
48
49        if its.caps.raw16(props):
50            raw_format = 'raw'
51        elif its.caps.raw10(props):
52            raw_format = 'raw10'
53        elif its.caps.raw12(props):
54            raw_format = 'raw12'
55        else: # should not reach here
56            raise its.error.Error('Cannot find available RAW output format')
57
58        out_surfaces = [{"format": raw_format},
59                        {"format": "yuv", "width": w, "height": h}]
60
61        sens_min, sens_max = props['android.sensor.info.sensitivityRange']
62        sens_boost_min, sens_boost_max = \
63                props['android.control.postRawSensitivityBoostRange']
64
65
66        e_target, s_target = \
67                its.target.get_target_exposure_combos(cam)["midSensitivity"]
68
69        reqs = []
70        settings = []
71        s_boost = sens_boost_min
72        while s_boost <= sens_boost_max:
73            s_raw = int(round(s_target * 100.0 / s_boost))
74            if s_raw < sens_min or s_raw > sens_max:
75                break
76            req = its.objects.manual_capture_request(s_raw, e_target)
77            req['android.control.postRawSensitivityBoost'] = s_boost
78            reqs.append(req)
79            settings.append((s_raw, s_boost))
80            if s_boost == sens_boost_max:
81                break
82            s_boost *= 2
83            # Always try to test maximum sensitivity boost value
84            if s_boost > sens_boost_max:
85                s_boost = sens_boost_max
86
87        caps = cam.do_capture(reqs, out_surfaces)
88
89        raw_rgb_means = []
90        yuv_rgb_means = []
91        raw_caps, yuv_caps = caps
92        if not isinstance(raw_caps, list):
93            raw_caps = [raw_caps]
94        if not isinstance(yuv_caps, list):
95            yuv_caps = [yuv_caps]
96        for i in xrange(len(reqs)):
97            (s, s_boost) = settings[i]
98            raw_cap = raw_caps[i]
99            yuv_cap = yuv_caps[i]
100            raw_rgb = its.image.convert_capture_to_rgb_image(raw_cap, props=props)
101            yuv_rgb = its.image.convert_capture_to_rgb_image(yuv_cap)
102            raw_tile = its.image.get_image_patch(raw_rgb, 0.45,0.45,0.1,0.1)
103            yuv_tile = its.image.get_image_patch(yuv_rgb, 0.45,0.45,0.1,0.1)
104            raw_rgb_means.append(its.image.compute_image_means(raw_tile))
105            yuv_rgb_means.append(its.image.compute_image_means(yuv_tile))
106            its.image.write_image(raw_tile,
107                    "%s_raw_s=%04d_boost=%04d.jpg" % (NAME,s,s_boost))
108            its.image.write_image(yuv_tile,
109                    "%s_yuv_s=%04d_boost=%04d.jpg" % (NAME,s,s_boost))
110            print "s=%d, s_boost=%d: raw_means %s, yuv_means %s"%(
111                    s,s_boost,raw_rgb_means[-1], yuv_rgb_means[-1])
112
113        xs = range(len(reqs))
114        pylab.plot(xs, [rgb[0] for rgb in raw_rgb_means], 'r')
115        pylab.plot(xs, [rgb[1] for rgb in raw_rgb_means], 'g')
116        pylab.plot(xs, [rgb[2] for rgb in raw_rgb_means], 'b')
117        pylab.ylim([0,1])
118        matplotlib.pyplot.savefig("%s_raw_plot_means.png" % (NAME))
119        pylab.clf()
120        pylab.plot(xs, [rgb[0] for rgb in yuv_rgb_means], 'r')
121        pylab.plot(xs, [rgb[1] for rgb in yuv_rgb_means], 'g')
122        pylab.plot(xs, [rgb[2] for rgb in yuv_rgb_means], 'b')
123        pylab.ylim([0,1])
124        matplotlib.pyplot.savefig("%s_yuv_plot_means.png" % (NAME))
125
126        rgb_str = ["R", "G", "B"]
127        # Test that raw means is about 2x brighter than next step
128        for step in range(1, len(reqs)):
129            (s_prev, s_boost_prev) = settings[step - 1]
130            (s, s_boost) = settings[step]
131            expect_raw_ratio = s_prev / float(s)
132            raw_thres_min = expect_raw_ratio * (1 - RATIO_THRESHOLD)
133            raw_thres_max = expect_raw_ratio * (1 + RATIO_THRESHOLD)
134            for rgb in range(3):
135                ratio = raw_rgb_means[step - 1][rgb] / raw_rgb_means[step][rgb]
136                print ("Step (%d,%d) %s channel: %f, %f, ratio %f," +
137                       " threshold_min %f, threshold_max %f") % (
138                        step-1, step, rgb_str[rgb],
139                        raw_rgb_means[step - 1][rgb],
140                        raw_rgb_means[step][rgb],
141                        ratio, raw_thres_min, raw_thres_max)
142                if (raw_rgb_means[step][rgb] <= RAW_PIXEL_VAL_THRESHOLD):
143                    continue
144                assert(raw_thres_min < ratio < raw_thres_max)
145
146        # Test that each yuv step is about the same bright as their mean
147        yuv_thres_min = 1 - RATIO_THRESHOLD
148        yuv_thres_max = 1 + RATIO_THRESHOLD
149        for rgb in range(3):
150            vals = [val[rgb] for val in yuv_rgb_means]
151            for step in range(len(reqs)):
152                if (raw_rgb_means[step][rgb] <= RAW_PIXEL_VAL_THRESHOLD):
153                    vals = vals[:step]
154            mean = sum(vals) / len(vals)
155            print "%s channel vals %s mean %f"%(rgb_str[rgb], vals, mean)
156            for step in range(len(vals)):
157                ratio = vals[step] / mean
158                assert(yuv_thres_min < ratio < yuv_thres_max)
159
160if __name__ == '__main__':
161    main()
162