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 if zoom < 1: 75 raise AssertionError(f'zoom: {zoom:.2f}') 76 crop_w = aw // zoom 77 crop_h = ah // zoom 78 79 crop_region = { 80 'left': aw // 2 - crop_w // 2, 81 'top': ah // 2 - crop_h // 2, 82 'right': aw // 2 + crop_w // 2, 83 'bottom': ah // 2 + crop_h // 2 84 } 85 86 # Capture without a crop region. 87 # Use a manual request with a linear tonemap so that the YUV and RAW 88 # should look the same (once converted by image_processing_utils). 89 e, s = target_exposure_utils.get_target_exposure_combos(log_path, cam)[ 90 'minSensitivity'] 91 req = capture_request_utils.manual_capture_request(s, e, 0.0, True, props) 92 cap1_raw, cap1_yuv = cam.do_capture(req, cam.CAP_RAW_YUV) 93 94 # Capture with a crop region. 95 req['android.scaler.cropRegion'] = crop_region 96 cap2_raw, cap2_yuv = cam.do_capture(req, cam.CAP_RAW_YUV) 97 98 # Check the metadata related to crop regions. 99 # When both YUV and RAW are requested, the crop region that's 100 # applied to YUV should be reported. 101 # Note that the crop region returned by the cropped captures doesn't 102 # need to perfectly match the one that was requested. 103 imgs = {} 104 for s, cap, cr_expected, err_delta in [ 105 ('yuv_full', cap1_yuv, full_region, CROP_FULL_ERROR_THRESHOLD), 106 ('raw_full', cap1_raw, full_region, CROP_FULL_ERROR_THRESHOLD), 107 ('yuv_crop', cap2_yuv, crop_region, CROP_REGION_ERROR_THRESHOLD), 108 ('raw_crop', cap2_raw, crop_region, CROP_REGION_ERROR_THRESHOLD)]: 109 110 # Convert the capture to RGB and dump to a file. 111 img = image_processing_utils.convert_capture_to_rgb_image(cap, 112 props=props) 113 image_processing_utils.write_image( 114 img, '%s_%s.jpg' % (os.path.join(log_path, NAME), s)) 115 imgs[s] = img 116 117 # Get the crop region that is reported in the capture result. 118 cr_reported = cap['metadata']['android.scaler.cropRegion'] 119 x, y = cr_reported['left'], cr_reported['top'] 120 w = cr_reported['right'] - cr_reported['left'] 121 h = cr_reported['bottom'] - cr_reported['top'] 122 logging.debug('Crop reported on %s: (%d,%d %dx%d)', s, x, y, w, h) 123 124 # Test that the reported crop region is the same as the expected 125 # one, for a non-cropped capture, and is close to the expected one, 126 # for a cropped capture. 127 ex = CROP_FULL_ERROR_THRESHOLD 128 ey = CROP_FULL_ERROR_THRESHOLD 129 if np.isclose(err_delta, CROP_REGION_ERROR_THRESHOLD, rtol=0.01): 130 ex = aw * err_delta 131 ey = ah * err_delta 132 logging.debug('error X, Y: %.2f, %.2f', ex, ey) 133 if not ( 134 (abs(cr_expected['left'] - cr_reported['left']) <= ex) and 135 (abs(cr_expected['right'] - cr_reported['right']) <= ex) and 136 (abs(cr_expected['top'] - cr_reported['top']) <= ey) and 137 (abs(cr_expected['bottom'] - cr_reported['bottom']) <= ey)): 138 raise AssertionError(f'expected: {cr_expected}, reported: ' 139 f'{cr_reported}, ex: {ex:.2f}, ey: {ey:.2f}') 140 141 # Also check the image content; 3 of the 4 shots should match. 142 # Note that all the shots are RGB below; the variable names correspond 143 # to what was captured. 144 145 # Shrink the YUV images 2x2 -> 1 to account for the size reduction that 146 # the raw images went through in the RGB conversion. 147 imgs2 = {} 148 for s, img in imgs.items(): 149 h, w, _ = img.shape 150 if s in ['yuv_full', 'yuv_crop']: 151 img = img.reshape(h//2, 2, w//2, 2, 3).mean(3).mean(1) 152 img = img.reshape(h//2, w//2, 3) 153 imgs2[s] = img 154 155 # Strip any border pixels from the raw shots (since the raw images may 156 # be larger than the YUV images). Assume a symmetric padded border. 157 xpad = (imgs2['raw_full'].shape[1] - imgs2['yuv_full'].shape[1]) // 2 158 ypad = (imgs2['raw_full'].shape[0] - imgs2['yuv_full'].shape[0]) // 2 159 wyuv = imgs2['yuv_full'].shape[1] 160 hyuv = imgs2['yuv_full'].shape[0] 161 imgs2['raw_full'] = imgs2['raw_full'][ypad:ypad+hyuv:, 162 xpad:xpad+wyuv:, 163 ::] 164 imgs2['raw_crop'] = imgs2['raw_crop'][ypad:ypad+hyuv:, 165 xpad:xpad+wyuv:, 166 ::] 167 logging.debug('Stripping padding before comparison: %dx%d', xpad, ypad) 168 169 for s, img in imgs2.items(): 170 image_processing_utils.write_image( 171 img, '%s_comp_%s.jpg' % (os.path.join(log_path, NAME), s)) 172 173 # Compute diffs between images of the same type. 174 # The raw_crop and raw_full shots should be identical (since the crop 175 # doesn't apply to raw images), and the yuv_crop and yuv_full shots 176 # should be different. 177 diff_yuv = np.fabs((imgs2['yuv_full'] - imgs2['yuv_crop'])).mean() 178 diff_raw = np.fabs((imgs2['raw_full'] - imgs2['raw_crop'])).mean() 179 logging.debug('YUV diff (crop vs. non-crop): %.3f', diff_yuv) 180 logging.debug('RAW diff (crop vs. non-crop): %.3f', diff_raw) 181 182 if diff_yuv <= DIFF_THRESH: 183 raise AssertionError('YUV diff too small! ' 184 f'diff_yuv: {diff_yuv:.3f}, THRESH: {DIFF_THRESH}') 185 if diff_raw >= DIFF_THRESH: 186 raise AssertionError('RAW diff too big! ' 187 f'diff_raw: {diff_raw:.3f}, THRESH: {DIFF_THRESH}') 188 189if __name__ == '__main__': 190 test_runner.main() 191 192