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 JPEG and YUV images are similar.""" 15 16 17import logging 18import os.path 19from mobly import test_runner 20 21import its_base_test 22import camera_properties_utils 23import capture_request_utils 24import image_processing_utils 25import its_session_utils 26 27_MAX_IMG_SIZE = (1920, 1080) 28_NAME = os.path.splitext(os.path.basename(__file__))[0] 29_PATCH_H = 0.1 # center 10% 30_PATCH_W = 0.1 31_PATCH_X = 0.5 - _PATCH_W/2 32_PATCH_Y = 0.5 - _PATCH_H/2 33_THRESHOLD_MAX_RMS_DIFF = 0.01 34 35 36def compute_means_and_save(cap, img_name, log_path): 37 """Compute the RGB means of a capture. 38 39 Args: 40 cap: 'YUV' or 'JPEG' capture 41 img_name: text for saved image name 42 log_path: path for saved image location 43 Returns: 44 RGB means 45 """ 46 img = image_processing_utils.convert_capture_to_rgb_image(cap, True) 47 image_processing_utils.write_image( 48 img, f'{os.path.join(log_path, _NAME)}_{img_name}.jpg') 49 patch = image_processing_utils.get_image_patch( 50 img, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H) 51 rgb_means = image_processing_utils.compute_image_means(patch) 52 logging.debug('%s RGB means: %s', img_name, rgb_means) 53 return rgb_means 54 55 56class YuvPlusJpegTest(its_base_test.ItsBaseTest): 57 """Test capturing a single frame as both YUV and JPEG outputs.""" 58 59 def test_yuv_plus_jpeg(self): 60 logging.debug('Starting %s', _NAME) 61 with its_session_utils.ItsSession( 62 device_id=self.dut.serial, 63 camera_id=self.camera_id, 64 hidden_physical_id=self.hidden_physical_id) as cam: 65 props = cam.get_camera_properties() 66 props = cam.override_with_hidden_physical_camera_props(props) 67 log_path = self.log_path 68 69 # Check SKIP conditions 70 camera_properties_utils.skip_unless( 71 camera_properties_utils.linear_tonemap(props)) 72 73 # Load chart for scene 74 its_session_utils.load_scene( 75 cam, props, self.scene, self.tablet, 76 its_session_utils.CHART_DISTANCE_NO_SCALING) 77 78 # Create requests 79 max_jpeg_size = capture_request_utils.get_available_output_sizes( 80 'jpeg', props)[0] 81 if capture_request_utils.is_common_aspect_ratio(max_jpeg_size): 82 w, h = capture_request_utils.get_available_output_sizes( 83 'yuv', props, _MAX_IMG_SIZE, max_jpeg_size)[0] 84 else: 85 w, h = capture_request_utils.get_available_output_sizes( 86 'yuv', props, max_size=_MAX_IMG_SIZE)[0] 87 logging.debug('YUV size: (%d, %d)', w, h) 88 logging.debug('JPEG size: %s', max_jpeg_size) 89 fmt_yuv = {'format': 'yuv', 'width': w, 'height': h} 90 fmt_jpg = {'format': 'jpeg'} 91 if camera_properties_utils.stream_use_case(props): 92 fmt_yuv['useCase'] = camera_properties_utils.USE_CASE_STILL_CAPTURE 93 fmt_jpg['useCase'] = camera_properties_utils.USE_CASE_STILL_CAPTURE 94 95 # Use an auto_capture_request with linear tonemap so that the YUV and JPEG 96 # should look the same (once converted by the image_processing_utils). 97 # Do not use AF to match manual capture request. 98 req = capture_request_utils.auto_capture_request( 99 linear_tonemap=True, props=props, do_af=False) 100 101 cam.do_3a() 102 cap_yuv, cap_jpg = cam.do_capture(req, [fmt_yuv, fmt_jpg]) 103 rgb_means_yuv = compute_means_and_save(cap_yuv, 'yuv', log_path) 104 rgb_means_jpg = compute_means_and_save(cap_jpg, 'jpg', log_path) 105 106 rms_diff = image_processing_utils.compute_image_rms_difference_1d( 107 rgb_means_yuv, rgb_means_jpg) 108 msg = f'RMS diff: {rms_diff:.4f}' 109 logging.debug('%s', msg) 110 if rms_diff >= _THRESHOLD_MAX_RMS_DIFF: 111 raise AssertionError(msg + f', spec: {_THRESHOLD_MAX_RMS_DIFF}') 112 113 # Log rms-diff, so that it can be written to the report log. 114 print(f'test_yuv_plus_jpeg_rms_diff:{rms_diff:.4f}') 115 116if __name__ == '__main__': 117 test_runner.main() 118