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        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
45        e /= 2
46
47        # Test 1: that the tonemap curves have the expected effect. Take two
48        # shots, with n in [0,1], where each has a linear tonemap, with the
49        # n=1 shot having a steeper gradient. The gradient for each R,G,B
50        # channel increases (i.e.) R[n=1] should be brighter than R[n=0],
51        # and G[n=1] should be brighter than G[n=0] by a larger margin, etc.
52        rgb_means = []
53
54        for n in [0,1]:
55            req = its.objects.manual_capture_request(s,e)
56            req["android.tonemap.mode"] = 0
57            req["android.tonemap.curveRed"] = (
58                    sum([[i/LM1, min(1.0,(1+0.5*n)*i/LM1)] for i in range(L)], []))
59            req["android.tonemap.curveGreen"] = (
60                    sum([[i/LM1, min(1.0,(1+1.0*n)*i/LM1)] for i in range(L)], []))
61            req["android.tonemap.curveBlue"] = (
62                    sum([[i/LM1, min(1.0,(1+1.5*n)*i/LM1)] for i in range(L)], []))
63            cap = cam.do_capture(req)
64            img = its.image.convert_capture_to_rgb_image(cap)
65            its.image.write_image(
66                    img, "%s_n=%d.jpg" %(NAME, n))
67            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
68            rgb_means.append(its.image.compute_image_means(tile))
69
70        rgb_ratios = [rgb_means[1][i] / rgb_means[0][i] for i in xrange(3)]
71        print "Test 1: RGB ratios:", rgb_ratios
72        assert(rgb_ratios[0] + THRESHOLD_RATIO_MIN_DIFF < rgb_ratios[1])
73        assert(rgb_ratios[1] + THRESHOLD_RATIO_MIN_DIFF < rgb_ratios[2])
74
75
76        # Test 2: that the length of the tonemap curve (i.e. number of control
77        # points) doesn't affect the output.
78        rgb_means = []
79
80        for size in [32,64]:
81            m = float(size-1)
82            curve = sum([[i/m, i/m] for i in range(size)], [])
83            req = its.objects.manual_capture_request(s,e)
84            req["android.tonemap.mode"] = 0
85            req["android.tonemap.curveRed"] = curve
86            req["android.tonemap.curveGreen"] = curve
87            req["android.tonemap.curveBlue"] = curve
88            cap = cam.do_capture(req)
89            img = its.image.convert_capture_to_rgb_image(cap)
90            its.image.write_image(
91                    img, "%s_size=%02d.jpg" %(NAME, size))
92            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
93            rgb_means.append(its.image.compute_image_means(tile))
94
95        rgb_diffs = [rgb_means[1][i] - rgb_means[0][i] for i in xrange(3)]
96        print "Test 2: RGB diffs:", rgb_diffs
97        assert(abs(rgb_diffs[0]) < THRESHOLD_DIFF_MAX_DIFF)
98        assert(abs(rgb_diffs[1]) < THRESHOLD_DIFF_MAX_DIFF)
99        assert(abs(rgb_diffs[2]) < THRESHOLD_DIFF_MAX_DIFF)
100
101if __name__ == '__main__':
102    main()
103
104