1# Copyright 2016 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.path 16 17import its.caps 18import its.device 19import its.image 20import its.objects 21import its.target 22 23import matplotlib 24from matplotlib import pylab 25 26NAME = os.path.basename(__file__).split('.')[0] 27RATIO_THRESHOLD = 0.1 # Each raw image 28# Waive the check if raw pixel value is below this level (signal too small 29# that small black level error converts to huge error in percentage) 30RAW_PIXEL_VAL_THRESHOLD = 0.03 31 32 33def main(): 34 """Check post RAW sensitivity boost. 35 36 Capture a set of raw/yuv images with different 37 sensitivity/post RAW sensitivity boost combination 38 and check if the output pixel mean matches request settings 39 """ 40 41 with its.device.ItsSession() as cam: 42 props = cam.get_camera_properties() 43 its.caps.skip_unless(its.caps.raw_output(props) and 44 its.caps.post_raw_sensitivity_boost(props) and 45 its.caps.compute_target_exposure(props) and 46 its.caps.per_frame_control(props) and 47 not its.caps.mono_camera(props)) 48 49 w, h = its.objects.get_available_output_sizes( 50 'yuv', props, (1920, 1080))[0] 51 52 if its.caps.raw16(props): 53 raw_format = 'raw' 54 elif its.caps.raw10(props): 55 raw_format = 'raw10' 56 elif its.caps.raw12(props): 57 raw_format = 'raw12' 58 else: # should not reach here 59 raise its.error.Error('Cannot find available RAW output format') 60 61 out_surfaces = [{'format': raw_format}, 62 {'format': 'yuv', 'width': w, 'height': h}] 63 64 sens_min, sens_max = props['android.sensor.info.sensitivityRange'] 65 sens_boost_min, sens_boost_max = \ 66 props['android.control.postRawSensitivityBoostRange'] 67 68 e_target, s_target = \ 69 its.target.get_target_exposure_combos(cam)['midSensitivity'] 70 71 reqs = [] 72 settings = [] 73 s_boost = sens_boost_min 74 while s_boost <= sens_boost_max: 75 s_raw = int(round(s_target * 100.0 / s_boost)) 76 if s_raw < sens_min or s_raw > sens_max: 77 break 78 req = its.objects.manual_capture_request(s_raw, e_target) 79 req['android.control.postRawSensitivityBoost'] = s_boost 80 reqs.append(req) 81 settings.append((s_raw, s_boost)) 82 if s_boost == sens_boost_max: 83 break 84 s_boost *= 2 85 # Always try to test maximum sensitivity boost value 86 if s_boost > sens_boost_max: 87 s_boost = sens_boost_max 88 89 caps = cam.do_capture(reqs, out_surfaces) 90 91 raw_rgb_means = [] 92 yuv_rgb_means = [] 93 raw_caps, yuv_caps = caps 94 if not isinstance(raw_caps, list): 95 raw_caps = [raw_caps] 96 if not isinstance(yuv_caps, list): 97 yuv_caps = [yuv_caps] 98 for i in xrange(len(reqs)): 99 (s, s_boost) = settings[i] 100 raw_cap = raw_caps[i] 101 yuv_cap = yuv_caps[i] 102 raw_rgb = its.image.convert_capture_to_rgb_image( 103 raw_cap, props=props) 104 yuv_rgb = its.image.convert_capture_to_rgb_image(yuv_cap) 105 raw_tile = its.image.get_image_patch(raw_rgb, 0.45, 0.45, 0.1, 0.1) 106 yuv_tile = its.image.get_image_patch(yuv_rgb, 0.45, 0.45, 0.1, 0.1) 107 raw_rgb_means.append(its.image.compute_image_means(raw_tile)) 108 yuv_rgb_means.append(its.image.compute_image_means(yuv_tile)) 109 its.image.write_image(raw_tile, '%s_raw_s=%04d_boost=%04d.jpg' % ( 110 NAME, s, s_boost)) 111 its.image.write_image(yuv_tile, '%s_yuv_s=%04d_boost=%04d.jpg' % ( 112 NAME, s, s_boost)) 113 print 's=%d, s_boost=%d: raw_means %s, yuv_means %s'%( 114 s, s_boost, raw_rgb_means[-1], yuv_rgb_means[-1]) 115 116 xs = range(len(reqs)) 117 pylab.plot(xs, [rgb[0] for rgb in raw_rgb_means], '-ro') 118 pylab.plot(xs, [rgb[1] for rgb in raw_rgb_means], '-go') 119 pylab.plot(xs, [rgb[2] for rgb in raw_rgb_means], '-bo') 120 pylab.ylim([0, 1]) 121 name = '%s_raw_plot_means' % NAME 122 pylab.title(name) 123 pylab.xlabel('requests') 124 pylab.ylabel('RGB means') 125 matplotlib.pyplot.savefig('%s.png' % name) 126 pylab.clf() 127 pylab.plot(xs, [rgb[0] for rgb in yuv_rgb_means], '-ro') 128 pylab.plot(xs, [rgb[1] for rgb in yuv_rgb_means], '-go') 129 pylab.plot(xs, [rgb[2] for rgb in yuv_rgb_means], '-bo') 130 pylab.ylim([0, 1]) 131 name = '%s_yuv_plot_means' % NAME 132 pylab.title(name) 133 pylab.xlabel('requests') 134 pylab.ylabel('RGB means') 135 matplotlib.pyplot.savefig('%s.png' % name) 136 137 rgb_str = ['R', 'G', 'B'] 138 # Test that raw means is about 2x brighter than next step 139 for step in range(1, len(reqs)): 140 (s_prev, _) = settings[step - 1] 141 (s, s_boost) = settings[step] 142 expect_raw_ratio = s_prev / float(s) 143 raw_thres_min = expect_raw_ratio * (1 - RATIO_THRESHOLD) 144 raw_thres_max = expect_raw_ratio * (1 + RATIO_THRESHOLD) 145 for rgb in range(3): 146 ratio = raw_rgb_means[step - 1][rgb] / raw_rgb_means[step][rgb] 147 print 'Step (%d,%d) %s channel: %f, %f, ratio %f,' % ( 148 step-1, step, rgb_str[rgb], 149 raw_rgb_means[step - 1][rgb], 150 raw_rgb_means[step][rgb], ratio), 151 print 'threshold_min %f, threshold_max %f' % ( 152 raw_thres_min, raw_thres_max) 153 if raw_rgb_means[step][rgb] <= RAW_PIXEL_VAL_THRESHOLD: 154 continue 155 assert raw_thres_min < ratio < raw_thres_max 156 157 # Test that each yuv step is about the same bright as their mean 158 yuv_thres_min = 1 - RATIO_THRESHOLD 159 yuv_thres_max = 1 + RATIO_THRESHOLD 160 for rgb in range(3): 161 vals = [val[rgb] for val in yuv_rgb_means] 162 for step in range(len(reqs)): 163 if raw_rgb_means[step][rgb] <= RAW_PIXEL_VAL_THRESHOLD: 164 vals = vals[:step] 165 mean = sum(vals) / len(vals) 166 print '%s channel vals %s mean %f'%(rgb_str[rgb], vals, mean) 167 for step in range(len(vals)): 168 ratio = vals[step] / mean 169 assert yuv_thres_min < ratio < yuv_thres_max 170 171if __name__ == '__main__': 172 main() 173