1# Copyright 2014 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"""Verifies EV compensation is applied.""" 15 16 17import logging 18import math 19import os.path 20import matplotlib 21from matplotlib import pylab 22from mobly import test_runner 23 24import its_base_test 25import camera_properties_utils 26import capture_request_utils 27import image_processing_utils 28import its_session_utils 29 30LINEAR_TONEMAP_CURVE = [0.0, 0.0, 1.0, 1.0] 31LOCKED = 3 32LUMA_DELTA_ATOL = 0.05 33LUMA_DELTA_ATOL_SAT = 0.10 34LUMA_SAT_THRESH = 0.75 # luma value at which ATOL changes from MID to SAT 35NAME = os.path.splitext(os.path.basename(__file__))[0] 36PATCH_H = 0.1 # center 10% 37PATCH_W = 0.1 38PATCH_X = 0.5 - PATCH_W/2 39PATCH_Y = 0.5 - PATCH_H/2 40THRESH_CONVERGE_FOR_EV = 8 # AE must converge within this num auto reqs for EV 41 42 43def create_request_with_ev(ev): 44 req = capture_request_utils.auto_capture_request() 45 req['android.control.aeExposureCompensation'] = ev 46 req['android.control.aeLock'] = True 47 # Use linear tonemap to avoid brightness being impacted by tone curves. 48 req['android.tonemap.mode'] = 0 49 req['android.tonemap.curve'] = {'red': LINEAR_TONEMAP_CURVE, 50 'green': LINEAR_TONEMAP_CURVE, 51 'blue': LINEAR_TONEMAP_CURVE} 52 return req 53 54 55def extract_luma_from_capture(cap): 56 """Extract luma from capture.""" 57 y = image_processing_utils.convert_capture_to_planes(cap)[0] 58 patch = image_processing_utils.get_image_patch( 59 y, PATCH_X, PATCH_Y, PATCH_W, PATCH_H) 60 luma = image_processing_utils.compute_image_means(patch)[0] 61 return luma 62 63 64def create_ev_comp_changes(props): 65 """Create the ev compensation steps and shifts from control params.""" 66 ev_compensation_range = props['android.control.aeCompensationRange'] 67 range_min = ev_compensation_range[0] 68 range_max = ev_compensation_range[1] 69 ev_per_step = capture_request_utils.rational_to_float( 70 props['android.control.aeCompensationStep']) 71 logging.debug('ev_step_size_in_stops: %d', ev_per_step) 72 steps_per_ev = int(round(1.0 / ev_per_step)) 73 ev_steps = range(range_min, range_max + 1, steps_per_ev) 74 ev_shifts = [pow(2, step * ev_per_step) for step in ev_steps] 75 return ev_steps, ev_shifts 76 77 78class EvCompensationAdvancedTest(its_base_test.ItsBaseTest): 79 """Tests that EV compensation is applied.""" 80 81 def test_ev_compensation_advanced(self): 82 logging.debug('Starting %s', NAME) 83 with its_session_utils.ItsSession( 84 device_id=self.dut.serial, 85 camera_id=self.camera_id, 86 hidden_physical_id=self.hidden_physical_id) as cam: 87 props = cam.get_camera_properties() 88 props = cam.override_with_hidden_physical_camera_props(props) 89 log_path = self.log_path 90 91 # check SKIP conditions 92 camera_properties_utils.skip_unless( 93 camera_properties_utils.ev_compensation(props) and 94 camera_properties_utils.manual_sensor(props) and 95 camera_properties_utils.manual_post_proc(props) and 96 camera_properties_utils.per_frame_control(props)) 97 98 # Load chart for scene 99 its_session_utils.load_scene( 100 cam, props, self.scene, self.tablet, self.chart_distance) 101 102 # Create ev compensation changes 103 ev_steps, ev_shifts = create_ev_comp_changes(props) 104 105 # Converge 3A, and lock AE once converged. skip AF trigger as 106 # dark/bright scene could make AF convergence fail and this test 107 # doesn't care the image sharpness. 108 mono_camera = camera_properties_utils.mono_camera(props) 109 cam.do_3a(ev_comp=0, lock_ae=True, do_af=False, mono_camera=mono_camera) 110 111 # Create requests and capture 112 largest_yuv = capture_request_utils.get_largest_yuv_format(props) 113 match_ar = (largest_yuv['width'], largest_yuv['height']) 114 fmt = capture_request_utils.get_smallest_yuv_format( 115 props, match_ar=match_ar) 116 lumas = [] 117 for ev in ev_steps: 118 # Capture a single shot with the same EV comp and locked AE. 119 req = create_request_with_ev(ev) 120 caps = cam.do_capture([req]*THRESH_CONVERGE_FOR_EV, fmt) 121 for cap in caps: 122 if cap['metadata']['android.control.aeState'] == LOCKED: 123 ev_meta = cap['metadata']['android.control.aeExposureCompensation'] 124 if ev_meta != ev: 125 raise AssertionError( 126 f'EV comp capture != request! cap: {ev_meta}, req: {ev}') 127 lumas.append(extract_luma_from_capture(cap)) 128 break 129 if caps[THRESH_CONVERGE_FOR_EV-1]['metadata'][ 130 'android.control.aeState'] != LOCKED: 131 raise AssertionError('AE does not reach locked state in ' 132 f'{THRESH_CONVERGE_FOR_EV} frames.') 133 logging.debug('lumas in AE locked captures: %s', str(lumas)) 134 135 i_mid = len(ev_steps) // 2 136 luma_normal = lumas[i_mid] / ev_shifts[i_mid] 137 expected_lumas = [min(1.0, luma_normal*shift) for shift in ev_shifts] 138 luma_delta_atols = [LUMA_DELTA_ATOL if l < LUMA_SAT_THRESH 139 else LUMA_DELTA_ATOL_SAT for l in expected_lumas] 140 141 # Create plot 142 pylab.figure(NAME) 143 pylab.plot(ev_steps, lumas, '-ro', label='measured', alpha=0.7) 144 pylab.plot(ev_steps, expected_lumas, '-bo', label='expected', alpha=0.7) 145 pylab.title(NAME) 146 pylab.xlabel('EV Compensation') 147 pylab.ylabel('Mean Luma (Normalized)') 148 pylab.legend(loc='lower right', numpoints=1, fancybox=True) 149 matplotlib.pyplot.savefig( 150 '%s_plot_means.png' % os.path.join(log_path, NAME)) 151 152 for i, luma in enumerate(lumas): 153 luma_delta_atol = luma_delta_atols[i] 154 logging.debug('EV step: %3d, luma: %.3f, model: %.3f, ATOL: %.2f', 155 ev_steps[i], luma, expected_lumas[i], luma_delta_atol) 156 if not math.isclose(luma, expected_lumas[i], 157 abs_tol=luma_delta_atol): 158 raise AssertionError('Modeled/measured luma deltas too large! ' 159 f'meas: {lumas[i]}, model: {expected_lumas[i]}, ' 160 f'ATOL: {luma_delta_atol}.') 161 162 163if __name__ == '__main__': 164 test_runner.main() 165