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 RAW streams are not croppable."""
15
16
17import logging
18import os.path
19
20from mobly import test_runner
21import numpy as np
22
23import its_base_test
24import camera_properties_utils
25import capture_request_utils
26import image_processing_utils
27import its_session_utils
28import target_exposure_utils
29
30CROP_FULL_ERROR_THRESHOLD = 3  # pixels
31CROP_REGION_ERROR_THRESHOLD = 0.01  # reltol
32DIFF_THRESH = 0.05  # reltol
33NAME = os.path.splitext(os.path.basename(__file__))[0]
34
35
36class CropRegionRawTest(its_base_test.ItsBaseTest):
37  """Test that RAW streams are not croppable."""
38
39  def test_crop_region_raw(self):
40    with its_session_utils.ItsSession(
41        device_id=self.dut.serial,
42        camera_id=self.camera_id,
43        hidden_physical_id=self.hidden_physical_id) as cam:
44      props = cam.get_camera_properties()
45      props = cam.override_with_hidden_physical_camera_props(props)
46      log_path = self.log_path
47
48      # Check SKIP conditions
49      camera_properties_utils.skip_unless(
50          camera_properties_utils.compute_target_exposure(props) and
51          camera_properties_utils.raw16(props) and
52          camera_properties_utils.per_frame_control(props) and
53          not camera_properties_utils.mono_camera(props))
54
55      # Load chart for scene
56      its_session_utils.load_scene(
57          cam, props, self.scene, self.tablet, self.chart_distance)
58
59      # Calculate the active sensor region for a full (non-cropped) image.
60      a = props['android.sensor.info.activeArraySize']
61      ax, ay = a['left'], a['top']
62      aw, ah = a['right'] - a['left'], a['bottom'] - a['top']
63      logging.debug('Active sensor region: (%d,%d %dx%d)', ax, ay, aw, ah)
64
65      full_region = {
66          'left': 0,
67          'top': 0,
68          'right': aw,
69          'bottom': ah
70      }
71
72      # Calculate a center crop region.
73      zoom = min(3.0, camera_properties_utils.get_max_digital_zoom(props))
74      assert zoom >= 1, 'zoom: %.2f' % zoom
75      crop_w = aw // zoom
76      crop_h = ah // zoom
77
78      crop_region = {
79          'left': aw // 2 - crop_w // 2,
80          'top': ah // 2 - crop_h // 2,
81          'right': aw // 2 + crop_w // 2,
82          'bottom': ah // 2 + crop_h // 2
83      }
84
85      # Capture without a crop region.
86      # Use a manual request with a linear tonemap so that the YUV and RAW
87      # should look the same (once converted by image_processing_utils).
88      e, s = target_exposure_utils.get_target_exposure_combos(log_path, cam)[
89          'minSensitivity']
90      req = capture_request_utils.manual_capture_request(s, e, 0.0, True, props)
91      cap1_raw, cap1_yuv = cam.do_capture(req, cam.CAP_RAW_YUV)
92
93      # Capture with a crop region.
94      req['android.scaler.cropRegion'] = crop_region
95      cap2_raw, cap2_yuv = cam.do_capture(req, cam.CAP_RAW_YUV)
96
97      # Check the metadata related to crop regions.
98      # When both YUV and RAW are requested, the crop region that's
99      # applied to YUV should be reported.
100      # Note that the crop region returned by the cropped captures doesn't
101      # need to perfectly match the one that was requested.
102      imgs = {}
103      for s, cap, cr_expected, err_delta in [
104          ('yuv_full', cap1_yuv, full_region, CROP_FULL_ERROR_THRESHOLD),
105          ('raw_full', cap1_raw, full_region, CROP_FULL_ERROR_THRESHOLD),
106          ('yuv_crop', cap2_yuv, crop_region, CROP_REGION_ERROR_THRESHOLD),
107          ('raw_crop', cap2_raw, crop_region, CROP_REGION_ERROR_THRESHOLD)]:
108
109        # Convert the capture to RGB and dump to a file.
110        img = image_processing_utils.convert_capture_to_rgb_image(cap,
111                                                                  props=props)
112        image_processing_utils.write_image(
113            img, '%s_%s.jpg' % (os.path.join(log_path, NAME), s))
114        imgs[s] = img
115
116        # Get the crop region that is reported in the capture result.
117        cr_reported = cap['metadata']['android.scaler.cropRegion']
118        x, y = cr_reported['left'], cr_reported['top']
119        w = cr_reported['right'] - cr_reported['left']
120        h = cr_reported['bottom'] - cr_reported['top']
121        logging.debug('Crop reported on %s: (%d,%d %dx%d)', s, x, y, w, h)
122
123        # Test that the reported crop region is the same as the expected
124        # one, for a non-cropped capture, and is close to the expected one,
125        # for a cropped capture.
126        ex = CROP_FULL_ERROR_THRESHOLD
127        ey = CROP_FULL_ERROR_THRESHOLD
128        if np.isclose(err_delta, CROP_REGION_ERROR_THRESHOLD, rtol=0.01):
129          ex = aw * err_delta
130          ey = ah * err_delta
131        logging.debug('error X, Y: %.2f, %.2f', ex, ey)
132        e_msg = 'expected: %s, reported: %s, ex: %.2f, ex: %.2f' % (
133            str(cr_expected), str(cr_reported), ex, ey)
134        assert (
135            (abs(cr_expected['left'] - cr_reported['left']) <= ex) and
136            (abs(cr_expected['right'] - cr_reported['right']) <= ex) and
137            (abs(cr_expected['top'] - cr_reported['top']) <= ey) and
138            (abs(cr_expected['bottom'] - cr_reported['bottom']) <= ey)), e_msg
139
140      # Also check the image content; 3 of the 4 shots should match.
141      # Note that all the shots are RGB below; the variable names correspond
142      # to what was captured.
143
144      # Shrink the YUV images 2x2 -> 1 to account for the size reduction that
145      # the raw images went through in the RGB conversion.
146      imgs2 = {}
147      for s, img in imgs.items():
148        h, w, _ = img.shape
149        if s in ['yuv_full', 'yuv_crop']:
150          img = img.reshape(h//2, 2, w//2, 2, 3).mean(3).mean(1)
151          img = img.reshape(h//2, w//2, 3)
152        imgs2[s] = img
153
154      # Strip any border pixels from the raw shots (since the raw images may
155      # be larger than the YUV images). Assume a symmetric padded border.
156      xpad = (imgs2['raw_full'].shape[1] - imgs2['yuv_full'].shape[1]) // 2
157      ypad = (imgs2['raw_full'].shape[0] - imgs2['yuv_full'].shape[0]) // 2
158      wyuv = imgs2['yuv_full'].shape[1]
159      hyuv = imgs2['yuv_full'].shape[0]
160      imgs2['raw_full'] = imgs2['raw_full'][ypad:ypad+hyuv:,
161                                            xpad:xpad+wyuv:,
162                                            ::]
163      imgs2['raw_crop'] = imgs2['raw_crop'][ypad:ypad+hyuv:,
164                                            xpad:xpad+wyuv:,
165                                            ::]
166      logging.debug('Stripping padding before comparison: %dx%d', xpad, ypad)
167
168      for s, img in imgs2.items():
169        image_processing_utils.write_image(
170            img, '%s_comp_%s.jpg' % (os.path.join(log_path, NAME), s))
171
172      # Compute diffs between images of the same type.
173      # The raw_crop and raw_full shots should be identical (since the crop
174      # doesn't apply to raw images), and the yuv_crop and yuv_full shots
175      # should be different.
176      diff_yuv = np.fabs((imgs2['yuv_full'] - imgs2['yuv_crop'])).mean()
177      diff_raw = np.fabs((imgs2['raw_full'] - imgs2['raw_crop'])).mean()
178      logging.debug('YUV diff (crop vs. non-crop): %.3f', diff_yuv)
179      logging.debug('RAW diff (crop vs. non-crop): %.3f', diff_raw)
180
181      assert diff_yuv > DIFF_THRESH, 'diff_yuv: %.3f, THRESH: %.2f' % (
182          diff_yuv, DIFF_THRESH)
183      assert diff_raw < DIFF_THRESH, 'diff_raw: %.3f, THRESH: %.2f' % (
184          diff_raw, DIFF_THRESH)
185
186if __name__ == '__main__':
187  test_runner.main()
188
189