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