1# Copyright 2018 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 exposure times on RAW images."""
15
16
17import logging
18import math
19import os.path
20import matplotlib
21from matplotlib import pylab
22from mobly import test_runner
23import numpy as np
24
25import its_base_test
26import camera_properties_utils
27import capture_request_utils
28import image_processing_utils
29import its_session_utils
30
31_BAYER_COLORS = ('R', 'Gr', 'Gb', 'B')
32_BLK_LVL_RTOL = 0.1
33_BURST_LEN = 10  # break captures into burst of BURST_LEN requests
34_EXP_LONG_THRESH = 1E6  # 1ms
35_EXP_MULT_SHORT = pow(2, 1.0/3)  # Test 3 steps per 2x exposure
36_EXP_MULT_LONG = pow(10, 1.0/3)  # Test 3 steps per 10x exposure
37_IMG_DELTA_THRESH = 0.99  # Each shot must be > 0.99*previous
38_IMG_INCREASING_ATOL = 2  # Require images get at lease 2x black level
39_IMG_SAT_RTOL = 0.01  # 1%
40_IMG_STATS_GRID = 9  # find used to find the center 11.11%
41_NAME = os.path.splitext(os.path.basename(__file__))[0]
42_NS_TO_MS_FACTOR = 1.0E-6
43_NUM_ISO_STEPS = 5
44
45
46def create_test_exposure_list(e_min, e_max):
47  """Create the list of exposure values to test."""
48  e_list = []
49  mult = 1.0
50  while e_min*mult < e_max:
51    e_list.append(int(e_min*mult))
52    if e_min*mult < _EXP_LONG_THRESH:
53      mult *= _EXP_MULT_SHORT
54    else:
55      mult *= _EXP_MULT_LONG
56  if e_list[-1] < e_max*_IMG_DELTA_THRESH:
57    e_list.append(int(e_max))
58  return e_list
59
60
61def define_raw_stats_fmt(props):
62  """Define format with active array width and height."""
63  aax = props['android.sensor.info.preCorrectionActiveArraySize']['left']
64  aay = props['android.sensor.info.preCorrectionActiveArraySize']['top']
65  aaw = props['android.sensor.info.preCorrectionActiveArraySize']['right']-aax
66  aah = props['android.sensor.info.preCorrectionActiveArraySize']['bottom']-aay
67  return {'format': 'rawStats',
68          'gridWidth': aaw // _IMG_STATS_GRID,
69          'gridHeight': aah // _IMG_STATS_GRID}
70
71
72def create_plot(exps, means, sens, log_path):
73  """Create plots R, Gr, Gb, B vs exposures.
74
75  Args:
76    exps: array of exposure times in ms
77    means: array of means for RAW captures
78    sens: int value for ISO gain
79    log_path: path to write plot file
80  Returns:
81    None
82  """
83  # means[0] is black level value
84  r = [m[0] for m in means[1:]]
85  gr = [m[1] for m in means[1:]]
86  gb = [m[2] for m in means[1:]]
87  b = [m[3] for m in means[1:]]
88  pylab.figure(f'{_NAME}_{sens}')
89  pylab.plot(exps, r, 'r.-', label='R')
90  pylab.plot(exps, gr, 'g.-', label='Gr')
91  pylab.plot(exps, gb, 'k.-', label='Gb')
92  pylab.plot(exps, b, 'b.-', label='B')
93  pylab.xscale('log')
94  pylab.yscale('log')
95  pylab.title(f'{_NAME} ISO={sens}')
96  pylab.xlabel('Exposure time (ms)')
97  pylab.ylabel('Center patch pixel mean')
98  pylab.legend(loc='lower right', numpoints=1, fancybox=True)
99  matplotlib.pyplot.savefig(
100      f'{os.path.join(log_path, _NAME)}_s={sens}.png')
101  pylab.clf()
102
103
104def assert_increasing_means(means, exps, sens, black_levels, white_level):
105  """Assert that each image increases unless over/undersaturated.
106
107  Args:
108    means: BAYER COLORS means for set of images
109    exps: exposure times in ms
110    sens: ISO gain value
111    black_levels: BAYER COLORS black_level values
112    white_level: full scale value
113  Returns:
114    None
115  """
116  lower_thresh = np.array(black_levels) * (1 + _BLK_LVL_RTOL)
117  logging.debug('Lower threshold for check: %s', lower_thresh)
118  allow_under_saturated = True
119  image_increasing = False
120  for i in range(1, len(means)):
121    prev_mean = means[i-1]
122    mean = means[i]
123
124    if max(mean) > min(black_levels) * _IMG_INCREASING_ATOL:
125      image_increasing = True
126
127    if math.isclose(max(mean), white_level, rel_tol=_IMG_SAT_RTOL):
128      logging.debug('Saturated: white_level %f, max_mean %f',
129                    white_level, max(mean))
130      break
131
132    if allow_under_saturated and min(mean-lower_thresh) < 0:
133      # All channel means are close to black level
134      continue
135    allow_under_saturated = False
136
137    # Check pixel means are increasing (with small tolerance)
138    logging.debug('iso: %d, exp: %.3f, means: %s', sens, exps[i-1], mean)
139    for ch, color in enumerate(_BAYER_COLORS):
140      if mean[ch] <= prev_mean[ch] * _IMG_DELTA_THRESH:
141        e_msg = (f'{color} not increasing with increased exp time! '
142                 f'ISO: {sens}, ')
143        if i == 1:
144          e_msg += f'black_level: {black_levels[ch]}, '
145        else:
146          e_msg += (f'exp[i-1]: {exps[i-2]:.3f}ms, '
147                    f'mean[i-1]: {prev_mean[ch]:.2f}, ')
148        e_msg += (f'exp[i]: {exps[i-1]:.3f}ms, mean[i]: {mean[ch]}, '
149                  f'RTOL: {_IMG_DELTA_THRESH}')
150        raise AssertionError(e_msg)
151
152  # Check image increases
153  if not image_increasing:
154    raise AssertionError('Image does not increase with exposure!')
155
156
157class RawExposureTest(its_base_test.ItsBaseTest):
158  """Capture RAW images with increasing exp time and measure pixel values."""
159
160  def test_raw_exposure(self):
161    logging.debug('Starting %s', _NAME)
162    with its_session_utils.ItsSession(
163        device_id=self.dut.serial,
164        camera_id=self.camera_id,
165        hidden_physical_id=self.hidden_physical_id) as cam:
166      props = cam.get_camera_properties()
167      props = cam.override_with_hidden_physical_camera_props(props)
168      camera_properties_utils.skip_unless(
169          camera_properties_utils.raw16(props) and
170          camera_properties_utils.manual_sensor(props) and
171          camera_properties_utils.per_frame_control(props) and
172          not camera_properties_utils.mono_camera(props))
173      log_path = self.log_path
174
175      # Load chart for scene
176      its_session_utils.load_scene(
177          cam, props, self.scene, self.tablet,
178          its_session_utils.CHART_DISTANCE_NO_SCALING)
179
180      # Create list of exposures
181      e_min, e_max = props['android.sensor.info.exposureTimeRange']
182      logging.debug('exposureTimeRange(ns): %d, %d', e_min, e_max)
183      e_test = create_test_exposure_list(e_min, e_max)
184      e_test_ms = [e*_NS_TO_MS_FACTOR for e in e_test]
185
186      # Capture with rawStats to reduce capture times
187      fmt = define_raw_stats_fmt(props)
188
189      # Create sensitivity range from min to max analog sensitivity
190      sens_min, _ = props['android.sensor.info.sensitivityRange']
191      sens_max = props['android.sensor.maxAnalogSensitivity']
192      sens_step = (sens_max - sens_min) // _NUM_ISO_STEPS
193      white_level = float(props['android.sensor.info.whiteLevel'])
194      black_levels = image_processing_utils.get_black_levels(props)
195
196      # Do captures with exposure list over sensitivity range
197      for s in range(sens_min, sens_max, sens_step):
198        # Break caps into bursts and do captures
199        burst_len = _BURST_LEN
200        caps = []
201        reqs = [capture_request_utils.manual_capture_request(
202            s, e, 0) for e in e_test]
203        # Eliminate burst len==1. Error because returns [[]], not [{}, ...]
204        while len(reqs) % burst_len == 1:
205          burst_len -= 1
206        # Break caps into bursts
207        for i in range(len(reqs) // burst_len):
208          caps += cam.do_capture(reqs[i*burst_len:(i+1)*burst_len], fmt)
209        last_n = len(reqs) % burst_len
210        if last_n:
211          caps += cam.do_capture(reqs[-last_n:], fmt)
212
213        # Extract means for each capture
214        means = []
215        means.append(black_levels)
216        for i, cap in enumerate(caps):
217          mean_image, _ = image_processing_utils.unpack_rawstats_capture(cap)
218          mean = mean_image[_IMG_STATS_GRID // 2, _IMG_STATS_GRID // 2]
219          logging.debug('ISO=%d, exp_time=%.3fms, mean=%s',
220                        s, (e_test[i] * _NS_TO_MS_FACTOR), str(mean))
221          means.append(mean)
222
223        # Create plot
224        create_plot(e_test_ms, means, s, log_path)
225
226        # Each shot mean should be brighter (except under/overexposed scene)
227        assert_increasing_means(means, e_test_ms, s, black_levels, white_level)
228
229if __name__ == '__main__':
230  test_runner.main()
231