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 15 import os 16 import os.path 17 import sys 18 import re 19 import json 20 import tempfile 21 import time 22 import unittest 23 import subprocess 24 25 def 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 40 def 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 51 def 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 62 def 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 92 def 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 104 class __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 119 if __name__ == '__main__': 120 unittest.main() 121 122