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 its.image
16import its.caps
17import its.device
18import its.objects
19import os.path
20import math
21
22def main():
23    """Capture auto and manual shots that should look the same.
24
25    Manual shots taken with just manual WB, and also with manual WB+tonemap.
26
27    In all cases, the general color/look of the shots should be the same,
28    however there can be variations in brightness/contrast due to different
29    "auto" ISP blocks that may be disabled in the manual flows.
30    """
31    NAME = os.path.basename(__file__).split(".")[0]
32
33    with its.device.ItsSession() as cam:
34        props = cam.get_camera_properties()
35        its.caps.skip_unless(its.caps.manual_sensor(props) and
36                             its.caps.manual_post_proc(props) and
37                             its.caps.per_frame_control(props))
38
39        # Converge 3A and get the estimates.
40        debug = its.caps.debug_mode()
41        largest_yuv = its.objects.get_largest_yuv_format(props)
42        if debug:
43            fmt = largest_yuv
44        else:
45            match_ar = (largest_yuv['width'], largest_yuv['height'])
46            fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar)
47        sens, exp, gains, xform, focus = cam.do_3a(get_results=True)
48        xform_rat = its.objects.float_to_rational(xform)
49        print "AE sensitivity %d, exposure %dms" % (sens, exp/1000000.0)
50        print "AWB gains", gains
51        print "AWB transform", xform
52        print "AF distance", focus
53
54        # Auto capture.
55        req = its.objects.auto_capture_request()
56        cap_auto = cam.do_capture(req, fmt)
57        img_auto = its.image.convert_capture_to_rgb_image(cap_auto)
58        its.image.write_image(img_auto, "%s_auto.jpg" % (NAME))
59        xform_a = its.objects.rational_to_float(
60                cap_auto["metadata"]["android.colorCorrection.transform"])
61        gains_a = cap_auto["metadata"]["android.colorCorrection.gains"]
62        print "Auto gains:", gains_a
63        print "Auto transform:", xform_a
64
65        # Manual capture 1: WB
66        req = its.objects.manual_capture_request(sens, exp, focus)
67        req["android.colorCorrection.transform"] = xform_rat
68        req["android.colorCorrection.gains"] = gains
69        cap_man1 = cam.do_capture(req, fmt)
70        img_man1 = its.image.convert_capture_to_rgb_image(cap_man1)
71        its.image.write_image(img_man1, "%s_manual_wb.jpg" % (NAME))
72        xform_m1 = its.objects.rational_to_float(
73                cap_man1["metadata"]["android.colorCorrection.transform"])
74        gains_m1 = cap_man1["metadata"]["android.colorCorrection.gains"]
75        print "Manual wb gains:", gains_m1
76        print "Manual wb transform:", xform_m1
77
78        # Manual capture 2: WB + tonemap
79        gamma = sum([[i/63.0,math.pow(i/63.0,1/2.2)] for i in xrange(64)],[])
80        req["android.tonemap.mode"] = 0
81        req["android.tonemap.curveRed"] = gamma
82        req["android.tonemap.curveGreen"] = gamma
83        req["android.tonemap.curveBlue"] = gamma
84        cap_man2 = cam.do_capture(req, fmt)
85        img_man2 = its.image.convert_capture_to_rgb_image(cap_man2)
86        its.image.write_image(img_man2, "%s_manual_wb_tm.jpg" % (NAME))
87        xform_m2 = its.objects.rational_to_float(
88                cap_man2["metadata"]["android.colorCorrection.transform"])
89        gains_m2 = cap_man2["metadata"]["android.colorCorrection.gains"]
90        print "Manual wb+tm gains:", gains_m2
91        print "Manual wb+tm transform:", xform_m2
92
93        # Check that the WB gains and transform reported in each capture
94        # result match with the original AWB estimate from do_3a.
95        for g,x in [(gains_a,xform_a),(gains_m1,xform_m1),(gains_m2,xform_m2)]:
96            assert(all([abs(xform[i] - x[i]) < 0.05 for i in range(9)]))
97            assert(all([abs(gains[i] - g[i]) < 0.05 for i in range(4)]))
98
99if __name__ == '__main__':
100    main()
101
102