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"""CameraITS test to check test patterns generation."""
15
16import logging
17import os
18
19from mobly import test_runner
20import numpy as np
21
22import its_base_test
23import camera_properties_utils
24import capture_request_utils
25import image_processing_utils
26import its_session_utils
27
28
29NAME = os.path.basename(__file__).split('.')[0]
30CHECKED_PATTERNS = [1, 2, 5]  # [SOLID_COLOR, COLOR_BARS, BLACK]
31COLOR_BAR_ORDER = ['WHITE', 'YELLOW', 'CYAN', 'GREEN', 'MAGENTA', 'RED',
32                   'BLUE', 'BLACK']
33COLOR_CHECKER = {'BLACK': [0, 0, 0], 'RED': [1, 0, 0], 'GREEN': [0, 1, 0],
34                 'BLUE': [0, 0, 1], 'MAGENTA': [1, 0, 1], 'CYAN': [0, 1, 1],
35                 'YELLOW': [1, 1, 0], 'WHITE': [1, 1, 1]}
36CH_TOL = 2E-3  # 1/2 DN in [0:1]
37
38
39def check_solid_color(cap, props):
40  """Checks for solid color test pattern.
41
42  Args:
43    cap: capture element
44    props: capture properties
45
46  Returns:
47    True/False
48  """
49  logging.debug('Checking solid TestPattern...')
50  r, gr, gb, b = image_processing_utils.convert_capture_to_planes(cap, props)
51  r_tile = image_processing_utils.get_image_patch(r, 0.0, 0.0, 1.0, 1.0)
52  gr_tile = image_processing_utils.get_image_patch(gr, 0.0, 0.0, 1.0, 1.0)
53  gb_tile = image_processing_utils.get_image_patch(gb, 0.0, 0.0, 1.0, 1.0)
54  b_tile = image_processing_utils.get_image_patch(b, 0.0, 0.0, 1.0, 1.0)
55  var_max = max(
56      np.amax(r_tile), np.amax(gr_tile), np.amax(gb_tile), np.amax(b_tile))
57  var_min = min(
58      np.amin(r_tile), np.amin(gr_tile), np.amin(gb_tile), np.amin(b_tile))
59  white_level = int(props['android.sensor.info.whiteLevel'])
60  logging.debug('pixel min: %.f, pixel max: %.f', white_level * var_min,
61                white_level * var_max)
62  return np.isclose(var_max, var_min, atol=CH_TOL)
63
64
65def check_color_bars(cap, props, mirror=False):
66  """Checks for color bar test pattern.Compute avg of bars and compare to ideal.
67
68  Args:
69    cap: capture element
70    props: capture properties
71    mirror: boolean; whether to mirror image or not
72
73  Returns:
74    True/False
75
76
77  """
78  logging.debug('Checking color bar TestPattern...')
79  delta = 0.0005
80  num_bars = len(COLOR_BAR_ORDER)
81  color_match = []
82  img = image_processing_utils.convert_capture_to_rgb_image(cap, props=props)
83  if mirror:
84    logging.debug('Image mirrored')
85    img = np.fliplr(img)
86  for i, color in enumerate(COLOR_BAR_ORDER):
87    tile = image_processing_utils.get_image_patch(img,
88                                                  float(i) / num_bars + delta,
89                                                  0.0,
90                                                  1.0 / num_bars - 2 * delta,
91                                                  1.0)
92    color_match.append(
93        np.allclose(
94            image_processing_utils.compute_image_means(tile),
95            COLOR_CHECKER[color],
96            atol=CH_TOL))
97  logging.debug(COLOR_BAR_ORDER)
98  logging.debug(color_match)
99  return all(color_match)
100
101
102def check_pattern(cap, props, pattern):
103  """Checks for pattern correctness.
104
105  Args:
106    cap: capture element
107    props: capture properties
108    pattern (int): valid number for pattern
109
110  Returns:
111    True/False
112  """
113  if pattern == 1 or pattern == 5:  # solid color or black
114    return check_solid_color(cap, props)
115  elif pattern == 2:  # color bars
116    striped = check_color_bars(cap, props, mirror=False)
117    # check mirrored version in case image rotated from sensor orientation
118    if not striped:
119      striped = check_color_bars(cap, props, mirror=True)
120    return striped
121  else:
122    logging.debug('No specific test for TestPattern: %d', pattern)
123    return True
124
125
126def test_test_patterns_impl(cam, props, af_fd, name):
127  """Image sensor test patterns implementation.
128
129  Args:
130    cam: An open device session.
131    props: Properties of cam
132    af_fd: Focus distance
133    name: Path to save the captured image.
134  """
135
136  avail_patterns = props['android.sensor.availableTestPatternModes']
137  logging.debug('avail_patterns: %s', avail_patterns)
138  sens_min, _ = props['android.sensor.info.sensitivityRange']
139  exposure = min(props['android.sensor.info.exposureTimeRange'])
140
141  for pattern in CHECKED_PATTERNS:
142    if pattern in avail_patterns:
143      req = capture_request_utils.manual_capture_request(
144          int(sens_min), exposure)
145      req['android.lens.focusDistance'] = af_fd
146      req['android.sensor.testPatternMode'] = pattern
147      fmt = {'format': 'raw'}
148      cap = cam.do_capture(req, fmt)
149      img = image_processing_utils.convert_capture_to_rgb_image(
150          cap, props=props)
151      # Save pattern
152      image_processing_utils.write_image(img, '%s_%d.jpg' % (name, pattern),
153                                         True)
154
155      # Check pattern for correctness
156      assert check_pattern(cap, props, pattern)
157    else:
158      logging.debug('%d not in android.sensor.availableTestPatternModes.',
159                    (pattern))
160
161
162class TestPatterns(its_base_test.ItsBaseTest):
163  """Test pattern generation test.
164
165    Test: Capture frames for each valid test pattern and check if
166    generated correctly.
167    android.sensor.testPatternMode
168    0: OFF
169    1: SOLID_COLOR
170    2: COLOR_BARS
171    3: COLOR_BARS_FADE_TO_GREY
172    4: PN9
173    5: BLACK (test/system only)
174  """
175
176  def test_test_patterns(self):
177    logging.debug('Starting %s', NAME)
178    with its_session_utils.ItsSession(
179        device_id=self.dut.serial,
180        camera_id=self.camera_id,
181        hidden_physical_id=self.hidden_physical_id) as cam:
182      props = cam.get_camera_properties()
183      props = cam.override_with_hidden_physical_camera_props(props)
184      camera_properties_utils.skip_unless(
185          camera_properties_utils.raw16(props) and
186          camera_properties_utils.manual_sensor(props) and
187          camera_properties_utils.per_frame_control(props))
188
189      # For test pattern, use min_fd
190      focus_distance = props['android.lens.info.minimumFocusDistance']
191      name = os.path.join(self.log_path, NAME)
192      test_test_patterns_impl(cam, props, focus_distance, name)
193
194if __name__ == '__main__':
195  test_runner.main()
196