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        sens, exp, gains, xform, focus = cam.do_3a(get_results=True)
41        xform_rat = its.objects.float_to_rational(xform)
42        print "AE sensitivity %d, exposure %dms" % (sens, exp/1000000.0)
43        print "AWB gains", gains
44        print "AWB transform", xform
45        print "AF distance", focus
46
47        # Auto capture.
48        req = its.objects.auto_capture_request()
49        cap_auto = cam.do_capture(req)
50        img_auto = its.image.convert_capture_to_rgb_image(cap_auto)
51        its.image.write_image(img_auto, "%s_auto.jpg" % (NAME))
52        xform_a = its.objects.rational_to_float(
53                cap_auto["metadata"]["android.colorCorrection.transform"])
54        gains_a = cap_auto["metadata"]["android.colorCorrection.gains"]
55        print "Auto gains:", gains_a
56        print "Auto transform:", xform_a
57
58        # Manual capture 1: WB
59        req = its.objects.manual_capture_request(sens, exp)
60        req["android.colorCorrection.transform"] = xform_rat
61        req["android.colorCorrection.gains"] = gains
62        cap_man1 = cam.do_capture(req)
63        img_man1 = its.image.convert_capture_to_rgb_image(cap_man1)
64        its.image.write_image(img_man1, "%s_manual_wb.jpg" % (NAME))
65        xform_m1 = its.objects.rational_to_float(
66                cap_man1["metadata"]["android.colorCorrection.transform"])
67        gains_m1 = cap_man1["metadata"]["android.colorCorrection.gains"]
68        print "Manual wb gains:", gains_m1
69        print "Manual wb transform:", xform_m1
70
71        # Manual capture 2: WB + tonemap
72        gamma = sum([[i/63.0,math.pow(i/63.0,1/2.2)] for i in xrange(64)],[])
73        req["android.tonemap.mode"] = 0
74        req["android.tonemap.curveRed"] = gamma
75        req["android.tonemap.curveGreen"] = gamma
76        req["android.tonemap.curveBlue"] = gamma
77        cap_man2 = cam.do_capture(req)
78        img_man2 = its.image.convert_capture_to_rgb_image(cap_man2)
79        its.image.write_image(img_man2, "%s_manual_wb_tm.jpg" % (NAME))
80        xform_m2 = its.objects.rational_to_float(
81                cap_man2["metadata"]["android.colorCorrection.transform"])
82        gains_m2 = cap_man2["metadata"]["android.colorCorrection.gains"]
83        print "Manual wb+tm gains:", gains_m2
84        print "Manual wb+tm transform:", xform_m2
85
86        # Check that the WB gains and transform reported in each capture
87        # result match with the original AWB estimate from do_3a.
88        for g,x in [(gains_a,xform_a),(gains_m1,xform_m1),(gains_m2,xform_m2)]:
89            assert(all([abs(xform[i] - x[i]) < 0.05 for i in range(9)]))
90            assert(all([abs(gains[i] - g[i]) < 0.05 for i in range(4)]))
91
92if __name__ == '__main__':
93    main()
94
95