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 15import os.path 16 17import its.caps 18import its.device 19import its.image 20import its.objects 21import matplotlib 22from matplotlib import pylab 23import numpy as np 24 25NAME = os.path.basename(__file__).split('.')[0] 26LOCKED = 3 27LUMA_LOCKED_TOL = 0.05 28THRESH_CONVERGE_FOR_EV = 8 # AE must converge within this num 29YUV_FULL_SCALE = 255.0 30YUV_SATURATION_MIN = 253.0 31YUV_SATURATION_TOL = 1.0 32 33 34def main(): 35 """Tests that EV compensation is applied.""" 36 37 with its.device.ItsSession() as cam: 38 props = cam.get_camera_properties() 39 its.caps.skip_unless(its.caps.ev_compensation(props) and 40 its.caps.ae_lock(props)) 41 42 debug = its.caps.debug_mode() 43 mono_camera = its.caps.mono_camera(props) 44 largest_yuv = its.objects.get_largest_yuv_format(props) 45 if debug: 46 fmt = largest_yuv 47 else: 48 match_ar = (largest_yuv['width'], largest_yuv['height']) 49 fmt = its.objects.get_smallest_yuv_format(props, match_ar=match_ar) 50 51 ev_per_step = its.objects.rational_to_float( 52 props['android.control.aeCompensationStep']) 53 steps_per_ev = int(1.0 / ev_per_step) 54 evs = range(-2 * steps_per_ev, 2 * steps_per_ev + 1, steps_per_ev) 55 lumas = [] 56 reds = [] 57 greens = [] 58 blues = [] 59 60 # Converge 3A, and lock AE once converged. skip AF trigger as 61 # dark/bright scene could make AF convergence fail and this test 62 # doesn't care the image sharpness. 63 cam.do_3a(ev_comp=0, lock_ae=True, do_af=False, mono_camera=mono_camera) 64 65 for ev in evs: 66 # Capture a single shot with the same EV comp and locked AE. 67 req = its.objects.auto_capture_request() 68 req['android.control.aeExposureCompensation'] = ev 69 req['android.control.aeLock'] = True 70 caps = cam.do_capture([req]*THRESH_CONVERGE_FOR_EV, fmt) 71 luma_locked = [] 72 for i, cap in enumerate(caps): 73 if cap['metadata']['android.control.aeState'] == LOCKED: 74 y = its.image.convert_capture_to_planes(cap)[0] 75 tile = its.image.get_image_patch(y, 0.45, 0.45, 0.1, 0.1) 76 luma = its.image.compute_image_means(tile)[0] 77 luma_locked.append(luma) 78 if i == THRESH_CONVERGE_FOR_EV-1: 79 lumas.append(luma) 80 rgb = its.image.convert_capture_to_rgb_image(cap) 81 rgb_tile = its.image.get_image_patch(rgb, 82 0.45, 0.45, 83 0.1, 0.1) 84 rgb_means = its.image.compute_image_means(rgb_tile) 85 reds.append(rgb_means[0]) 86 greens.append(rgb_means[1]) 87 blues.append(rgb_means[2]) 88 print 'lumas in AE locked captures: ', luma_locked 89 assert np.isclose(min(luma_locked), max(luma_locked), 90 rtol=LUMA_LOCKED_TOL) 91 assert caps[THRESH_CONVERGE_FOR_EV-1]['metadata']['android.control.aeState'] == LOCKED 92 93 pylab.plot(evs, lumas, '-ro') 94 pylab.xlabel('EV Compensation') 95 pylab.ylabel('Mean Luma (Normalized)') 96 matplotlib.pyplot.savefig('%s_plot_means.png' % (NAME)) 97 98 # Trim extra saturated images 99 while lumas and lumas[-1] >= YUV_SATURATION_MIN/YUV_FULL_SCALE: 100 if (np.isclose(reds[-1], greens[-1], 101 YUV_SATURATION_TOL/YUV_FULL_SCALE) and 102 np.isclose(blues[-1], greens[-1], 103 YUV_SATURATION_TOL/YUV_FULL_SCALE)): 104 lumas.pop(-1) 105 reds.pop(-1) 106 greens.pop(-1) 107 blues.pop(-1) 108 print 'Removed saturated image.' 109 else: 110 break 111 # Only allow positive EVs to give saturated image 112 assert len(lumas) > 2 113 luma_diffs = np.diff(lumas) 114 min_luma_diffs = min(luma_diffs) 115 print 'Min of the luma value difference between adjacent ev comp: ', 116 print min_luma_diffs 117 # All luma brightness should be increasing with increasing ev comp. 118 assert min_luma_diffs > 0 119 120if __name__ == '__main__': 121 main() 122