aboutsummaryrefslogtreecommitdiff
path: root/apps/CameraITS/tests/scene1/test_param_tonemap_mode.py
blob: fbd452c527f53fee168dcc44432b726637d11d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright 2013 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import its.image
import its.caps
import its.device
import its.objects
import its.target
import os
import os.path

def main():
    """Test that the android.tonemap.mode param is applied.

    Applies different tonemap curves to each R,G,B channel, and checks
    that the output images are modified as expected.
    """
    NAME = os.path.basename(__file__).split(".")[0]

    THRESHOLD_RATIO_MIN_DIFF = 0.1
    THRESHOLD_DIFF_MAX_DIFF = 0.05

    # The HAL3.2 spec requires that curves up to 64 control points in length
    # must be supported.
    L = 32
    LM1 = float(L-1)

    with its.device.ItsSession() as cam:
        props = cam.get_camera_properties()
        if not its.caps.compute_target_exposure(props):
            print "Test skipped"
            return

        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
        e /= 2

        # Test 1: that the tonemap curves have the expected effect. Take two
        # shots, with n in [0,1], where each has a linear tonemap, with the
        # n=1 shot having a steeper gradient. The gradient for each R,G,B
        # channel increases (i.e.) R[n=1] should be brighter than R[n=0],
        # and G[n=1] should be brighter than G[n=0] by a larger margin, etc.
        rgb_means = []

        for n in [0,1]:
            req = its.objects.manual_capture_request(s,e)
            req["android.tonemap.mode"] = 0
            req["android.tonemap.curveRed"] = (
                    sum([[i/LM1, min(1.0,(1+0.5*n)*i/LM1)] for i in range(L)], []))
            req["android.tonemap.curveGreen"] = (
                    sum([[i/LM1, min(1.0,(1+1.0*n)*i/LM1)] for i in range(L)], []))
            req["android.tonemap.curveBlue"] = (
                    sum([[i/LM1, min(1.0,(1+1.5*n)*i/LM1)] for i in range(L)], []))
            cap = cam.do_capture(req)
            img = its.image.convert_capture_to_rgb_image(cap)
            its.image.write_image(
                    img, "%s_n=%d.jpg" %(NAME, n))
            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
            rgb_means.append(its.image.compute_image_means(tile))

        rgb_ratios = [rgb_means[1][i] / rgb_means[0][i] for i in xrange(3)]
        print "Test 1: RGB ratios:", rgb_ratios
        assert(rgb_ratios[0] + THRESHOLD_RATIO_MIN_DIFF < rgb_ratios[1])
        assert(rgb_ratios[1] + THRESHOLD_RATIO_MIN_DIFF < rgb_ratios[2])


        # Test 2: that the length of the tonemap curve (i.e. number of control
        # points) doesn't affect the output.
        rgb_means = []

        for size in [32,64]:
            m = float(size-1)
            curve = sum([[i/m, i/m] for i in range(size)], [])
            req = its.objects.manual_capture_request(s,e)
            req["android.tonemap.mode"] = 0
            req["android.tonemap.curveRed"] = curve
            req["android.tonemap.curveGreen"] = curve
            req["android.tonemap.curveBlue"] = curve
            cap = cam.do_capture(req)
            img = its.image.convert_capture_to_rgb_image(cap)
            its.image.write_image(
                    img, "%s_size=%02d.jpg" %(NAME, size))
            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
            rgb_means.append(its.image.compute_image_means(tile))

        rgb_diffs = [rgb_means[1][i] - rgb_means[0][i] for i in xrange(3)]
        print "Test 2: RGB diffs:", rgb_diffs
        assert(abs(rgb_diffs[0]) < THRESHOLD_DIFF_MAX_DIFF)
        assert(abs(rgb_diffs[1]) < THRESHOLD_DIFF_MAX_DIFF)
        assert(abs(rgb_diffs[2]) < THRESHOLD_DIFF_MAX_DIFF)

if __name__ == '__main__':
    main()