objects.py revision 87df78b48064a9bd31083968476e10242807985f
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 os
16import os.path
17import sys
18import re
19import json
20import tempfile
21import time
22import unittest
23import subprocess
24
25def int_to_rational(i):
26    """Function to convert Python integers to Camera2 rationals.
27
28    Args:
29        i: Python integer or list of integers.
30
31    Returns:
32        Python dictionary or list of dictionary representing the given int(s)
33        as rationals with denominator=1.
34    """
35    if isinstance(i, list):
36        return [{"numerator":val, "denominator":1} for val in i]
37    else:
38        return {"numerator":i, "denominator":1}
39
40def capture_request(obj):
41    """Function to wrap an object inside a captureRequest object.
42
43    Args:
44        obj: The Python dictionary object to wrap.
45
46    Returns:
47        The dictionary: {"captureRequest": obj}
48    """
49    return {"captureRequest": obj}
50
51def capture_request_list(obj_list):
52    """Function to wrap an object list inside a captureRequestList object.
53
54    Args:
55        obj_list: The list of Python dictionary objects to wrap.
56
57    Returns:
58        The dictionary: {"captureRequestList": obj_list}
59    """
60    return {"captureRequestList": obj_list}
61
62def manual_capture_request(sensitivity, exp_time_ms):
63    """Return a capture request with everything set to manual.
64
65    Uses identity/unit color correction, and the default tonemap curve.
66
67    Args:
68        sensitivity: The sensitivity value to populate the request with.
69        exp_time_ms: The exposure time, in milliseconds, to populate the
70            request with.
71
72    Returns:
73        The default manual capture request, ready to be passed to the
74        its.device.do_capture function.
75    """
76    return capture_request( {
77        "android.control.mode": 0,
78        "android.control.aeMode": 0,
79        "android.control.awbMode": 0,
80        "android.control.afMode": 0,
81        "android.control.effectMode": 0,
82        "android.sensor.frameDuration": 0,
83        "android.sensor.sensitivity": sensitivity,
84        "android.sensor.exposureTime": exp_time_ms*1000*1000,
85        "android.colorCorrection.mode": 0,
86        "android.colorCorrection.transform":
87                int_to_rational([1,0,0, 0,1,0, 0,0,1]),
88        "android.colorCorrection.gains": [1,1,1,1],
89        "android.tonemap.mode": 1,
90        })
91
92def auto_capture_request():
93    """Return a capture request with everything set to auto.
94    """
95    return capture_request( {
96        "android.control.mode": 1,
97        "android.control.aeMode": 1,
98        "android.control.awbMode": 1,
99        "android.control.afMode": 1,
100        "android.colorCorrection.mode": 1,
101        "android.tonemap.mode": 1,
102        })
103
104class __UnitTest(unittest.TestCase):
105    """Run a suite of unit tests on this module.
106    """
107
108    # TODO: Add more unit tests.
109
110    def test_int_to_rational(self):
111        """Unit test for int_to_rational.
112        """
113        self.assertEqual(int_to_rational(10),
114                         {"numerator":10,"denominator":1})
115        self.assertEqual(int_to_rational([1,2]),
116                         [{"numerator":1,"denominator":1},
117                          {"numerator":2,"denominator":1}])
118
119if __name__ == '__main__':
120    unittest.main()
121
122