1# Copyright 2015 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.cv2image
19import its.device
20import its.image
21import its.objects
22import its.target
23
24import numpy
25
26NAME = os.path.basename(__file__).split(".")[0]
27NUM_SAMPLES = 4
28THRESH_REL_SHARPNESS_DIFF = 0.1
29
30
31def test_edge_mode(cam, edge_mode, sensitivity, exp, fd, out_surface, chart):
32    """Return sharpness of the output image and the capture result metadata.
33
34       Processes a capture request with a given edge mode, sensitivity, exposure
35       time, focus distance, output surface parameter.
36
37    Args:
38        cam: An open device session.
39        edge_mode: Edge mode for the request as defined in android.edge.mode
40        sensitivity: Sensitivity for the request as defined in
41            android.sensor.sensitivity
42        exp: Exposure time for the request as defined in
43            android.sensor.exposureTime.
44        fd: Focus distance for the request as defined in
45            android.lens.focusDistance
46        out_surface: Specifications of the output image format and size.
47        chart: object that contains chart information
48
49    Returns:
50        Object containing reported edge mode and the sharpness of the output
51        image, keyed by the following strings:
52            "edge_mode"
53            "sharpness"
54    """
55
56    req = its.objects.manual_capture_request(sensitivity, exp)
57    req["android.lens.focusDistance"] = fd
58    req["android.edge.mode"] = edge_mode
59
60    sharpness_list = []
61    for n in range(NUM_SAMPLES):
62        cap = cam.do_capture(req, out_surface, repeat_request=req)
63        y, _, _ = its.image.convert_capture_to_planes(cap)
64        chart.img = its.image.normalize_img(its.image.get_image_patch(
65                y, chart.xnorm, chart.ynorm, chart.wnorm, chart.hnorm))
66        if n == 0:
67            its.image.write_image(
68                    chart.img, "%s_edge=%d.jpg" % (NAME, edge_mode))
69            res_edge_mode = cap["metadata"]["android.edge.mode"]
70        sharpness_list.append(its.image.compute_image_sharpness(chart.img))
71
72    ret = {}
73    ret["edge_mode"] = res_edge_mode
74    ret["sharpness"] = numpy.mean(sharpness_list)
75
76    return ret
77
78
79def main():
80    """Test that the android.edge.mode param is applied correctly.
81
82    Capture non-reprocess images for each edge mode and calculate their
83    sharpness as a baseline.
84    """
85
86    with its.device.ItsSession() as cam:
87        props = cam.get_camera_properties()
88
89        its.caps.skip_unless(its.caps.read_3a(props) and
90                             its.caps.per_frame_control(props) and
91                             its.caps.edge_mode(props, 0))
92
93    # initialize chart class and locate chart in scene
94    chart = its.cv2image.Chart()
95
96    with its.device.ItsSession() as cam:
97        mono_camera = its.caps.mono_camera(props)
98        test_fmt = "yuv"
99        size = its.objects.get_available_output_sizes(test_fmt, props)[0]
100        out_surface = {"width": size[0], "height": size[1], "format": test_fmt}
101
102        # Get proper sensitivity, exposure time, and focus distance.
103        s, e, _, _, fd = cam.do_3a(get_results=True, mono_camera=mono_camera)
104
105        # Get the sharpness for each edge mode for regular requests
106        sharpness_regular = []
107        edge_mode_reported_regular = []
108        for edge_mode in range(4):
109            # Skip unavailable modes
110            if not its.caps.edge_mode(props, edge_mode):
111                edge_mode_reported_regular.append(edge_mode)
112                sharpness_regular.append(0)
113                continue
114            ret = test_edge_mode(cam, edge_mode, s, e, fd, out_surface, chart)
115            edge_mode_reported_regular.append(ret["edge_mode"])
116            sharpness_regular.append(ret["sharpness"])
117
118        print "Reported edge modes:", edge_mode_reported_regular
119        print "Sharpness with EE mode [0,1,2,3]:", sharpness_regular
120
121        print "Verify HQ(2) is sharper than OFF(0)"
122        assert sharpness_regular[2] > sharpness_regular[0]
123
124        print "Verify OFF(0) is not sharper than FAST(1)"
125        msg = "FAST: %.3f, OFF: %.3f, TOL: %.2f" % (
126                sharpness_regular[1], sharpness_regular[0],
127                THRESH_REL_SHARPNESS_DIFF)
128        assert (sharpness_regular[1] >
129                sharpness_regular[0] * (1.0 - THRESH_REL_SHARPNESS_DIFF)), msg
130
131        # Verify FAST(1) is not sharper than HQ(2)
132        msg = "HQ: %.3f, FAST: %.3f, TOL: %.2f" % (
133                sharpness_regular[2], sharpness_regular[1],
134                THRESH_REL_SHARPNESS_DIFF)
135        assert (sharpness_regular[2] >
136                sharpness_regular[1] * (1.0 - THRESH_REL_SHARPNESS_DIFF)), msg
137
138if __name__ == "__main__":
139    main()
140