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 math
16import os.path
17import its.caps
18import its.device
19import its.image
20import its.objects
21import its.target
22
23NAME = os.path.basename(__file__).split(".")[0]
24THRESHOLD_MAX_RMS_DIFF = 0.035
25
26
27def main():
28    """Test capturing a single frame as both RAW10 and YUV outputs."""
29
30    with its.device.ItsSession() as cam:
31        props = cam.get_camera_properties()
32        its.caps.skip_unless(its.caps.compute_target_exposure(props) and
33                             its.caps.raw10(props) and
34                             its.caps.per_frame_control(props) and
35                             not its.caps.mono_camera(props))
36
37        # Use a manual request with a linear tonemap so that the YUV and RAW
38        # should look the same (once converted by the its.image module).
39        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
40        req = its.objects.manual_capture_request(s, e, 0.0, True, props)
41
42        mode = req["android.shading.mode"]
43        print "shading mode:", mode
44
45        max_raw10_size = its.objects.get_available_output_sizes("raw10",
46                                                                props)[0]
47        w, h = its.objects.get_available_output_sizes(
48                "yuv", props, (1920, 1080), max_raw10_size)[0]
49        out_surfaces = [{"format": "raw10"},
50                        {"format": "yuv", "width": w, "height": h}]
51        cap_raw, cap_yuv = cam.do_capture(req, out_surfaces)
52
53        img = its.image.convert_capture_to_rgb_image(cap_yuv)
54        its.image.write_image(img, "%s_shading=%d_yuv.jpg" % (NAME, mode), True)
55        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
56        rgb0 = its.image.compute_image_means(tile)
57
58        # Raw shots are 1/2 x 1/2 smaller after conversion to RGB, but tile
59        # cropping is relative.
60        img = its.image.convert_capture_to_rgb_image(cap_raw, props=props)
61        its.image.write_image(img, "%s_shading=%d_raw.jpg" % (NAME, mode), True)
62        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
63        rgb1 = its.image.compute_image_means(tile)
64
65        rms_diff = math.sqrt(
66                sum([pow(rgb0[i] - rgb1[i], 2.0) for i in range(3)]) / 3.0)
67        msg = "RMS difference: %.4f, spec: %.3f" % (rms_diff,
68                                                    THRESHOLD_MAX_RMS_DIFF)
69        print msg
70        assert rms_diff < THRESHOLD_MAX_RMS_DIFF, msg
71
72if __name__ == "__main__":
73    main()
74
75