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