1# Copyright 2013 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 its.target
20import os
21import os.path
22
23def main():
24    """Test that the android.tonemap.mode param is applied.
25
26    Applies different tonemap curves to each R,G,B channel, and checks
27    that the output images are modified as expected.
28    """
29    NAME = os.path.basename(__file__).split(".")[0]
30
31    THRESHOLD_RATIO_MIN_DIFF = 0.1
32    THRESHOLD_DIFF_MAX_DIFF = 0.05
33
34    # The HAL3.2 spec requires that curves up to 64 control points in length
35    # must be supported.
36    L = 32
37    LM1 = float(L-1)
38
39    with its.device.ItsSession() as cam:
40        props = cam.get_camera_properties()
41        its.caps.skip_unless(its.caps.compute_target_exposure(props) and
42                             its.caps.per_frame_control(props))
43
44        debug = its.caps.debug_mode()
45        largest_yuv = its.objects.get_largest_yuv_format(props)
46        if debug:
47            fmt = largest_yuv
48        else:
49            match_ar = (largest_yuv['width'], largest_yuv['height'])
50            fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar)
51
52        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
53        e /= 2
54
55        # Test 1: that the tonemap curves have the expected effect. Take two
56        # shots, with n in [0,1], where each has a linear tonemap, with the
57        # n=1 shot having a steeper gradient. The gradient for each R,G,B
58        # channel increases (i.e.) R[n=1] should be brighter than R[n=0],
59        # and G[n=1] should be brighter than G[n=0] by a larger margin, etc.
60        rgb_means = []
61
62        for n in [0,1]:
63            req = its.objects.manual_capture_request(s,e)
64            req["android.tonemap.mode"] = 0
65            req["android.tonemap.curveRed"] = (
66                    sum([[i/LM1, min(1.0,(1+0.5*n)*i/LM1)] for i in range(L)], []))
67            req["android.tonemap.curveGreen"] = (
68                    sum([[i/LM1, min(1.0,(1+1.0*n)*i/LM1)] for i in range(L)], []))
69            req["android.tonemap.curveBlue"] = (
70                    sum([[i/LM1, min(1.0,(1+1.5*n)*i/LM1)] for i in range(L)], []))
71            cap = cam.do_capture(req, fmt)
72            img = its.image.convert_capture_to_rgb_image(cap)
73            its.image.write_image(
74                    img, "%s_n=%d.jpg" %(NAME, n))
75            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
76            rgb_means.append(its.image.compute_image_means(tile))
77
78        rgb_ratios = [rgb_means[1][i] / rgb_means[0][i] for i in xrange(3)]
79        print "Test 1: RGB ratios:", rgb_ratios
80        assert(rgb_ratios[0] + THRESHOLD_RATIO_MIN_DIFF < rgb_ratios[1])
81        assert(rgb_ratios[1] + THRESHOLD_RATIO_MIN_DIFF < rgb_ratios[2])
82
83
84        # Test 2: that the length of the tonemap curve (i.e. number of control
85        # points) doesn't affect the output.
86        rgb_means = []
87
88        for size in [32,64]:
89            m = float(size-1)
90            curve = sum([[i/m, i/m] for i in range(size)], [])
91            req = its.objects.manual_capture_request(s,e)
92            req["android.tonemap.mode"] = 0
93            req["android.tonemap.curveRed"] = curve
94            req["android.tonemap.curveGreen"] = curve
95            req["android.tonemap.curveBlue"] = curve
96            cap = cam.do_capture(req)
97            img = its.image.convert_capture_to_rgb_image(cap)
98            its.image.write_image(
99                    img, "%s_size=%02d.jpg" %(NAME, size))
100            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
101            rgb_means.append(its.image.compute_image_means(tile))
102
103        rgb_diffs = [rgb_means[1][i] - rgb_means[0][i] for i in xrange(3)]
104        print "Test 2: RGB diffs:", rgb_diffs
105        assert(abs(rgb_diffs[0]) < THRESHOLD_DIFF_MAX_DIFF)
106        assert(abs(rgb_diffs[1]) < THRESHOLD_DIFF_MAX_DIFF)
107        assert(abs(rgb_diffs[2]) < THRESHOLD_DIFF_MAX_DIFF)
108
109if __name__ == '__main__':
110    main()
111
112