1# Copyright 2019 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
16
17import its.caps
18import its.device
19import its.image
20import its.objects
21import numpy as np
22
23NAME = os.path.basename(__file__).split('.')[0]
24RGB_FULL_SCALE = 255.0
25RGB_SAT_MIN = 253.0
26RGB_SAT_TOL = 1.0
27
28
29def main():
30    """Test that channels saturate evenly."""
31
32    with its.device.ItsSession() as cam:
33        props = cam.get_camera_properties()
34        its.caps.skip_unless(its.caps.manual_sensor(props))
35        sync_latency = its.caps.sync_latency(props)
36
37        debug = its.caps.debug_mode()
38        largest_yuv = its.objects.get_largest_yuv_format(props)
39        if debug:
40            fmt = largest_yuv
41        else:
42            match_ar = (largest_yuv['width'], largest_yuv['height'])
43            fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar)
44
45        exp = props['android.sensor.info.exposureTimeRange'][1]
46        iso = props['android.sensor.info.sensitivityRange'][1]
47
48        # Take shot with very high ISO and exposure time. Expect saturation
49        req = its.objects.manual_capture_request(iso, exp)
50        cap = its.device.do_capture_with_latency(cam, req, sync_latency, fmt)
51        img = its.image.convert_capture_to_rgb_image(cap)
52        its.image.write_image(img, '%s.jpg' % NAME)
53        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
54        white_means = its.image.compute_image_means(tile)
55        r = white_means[0] * RGB_FULL_SCALE
56        g = white_means[1] * RGB_FULL_SCALE
57        b = white_means[2] * RGB_FULL_SCALE
58        print ' Saturated pixels r, g, b:', white_means
59        r_exp = cap['metadata']['android.sensor.exposureTime']
60        r_iso = cap['metadata']['android.sensor.sensitivity']
61        print ' Saturated shot write values: iso = %d, exp = %.2fms' % (
62                iso, exp/1000000.0)
63        print ' Saturated shot read values: iso = %d, exp = %.2fms\n' % (
64                r_iso, r_exp/1000000.0)
65
66        # assert saturation
67        assert min(r, g, b) > RGB_SAT_MIN, (
68                'r: %.1f, g: %.1f, b: %.1f, MIN: %.f' % (r, g, b, RGB_SAT_MIN))
69        # assert channels saturate evenly
70        assert np.isclose(min(r, g, b), max(r, g, b), atol=RGB_SAT_TOL), (
71                'ch_sat not EQ!  r: %.1f, g: %.1f, b: %.1f, TOL: %.f' % (
72                        r, g, b, RGB_SAT_TOL))
73
74
75if __name__ == '__main__':
76    main()
77
78