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 26import target_exposure_utils 27 28MAX_IMG_SIZE = (1920, 1080) 29NAME = os.path.splitext(os.path.basename(__file__))[0] 30PATCH_H = 0.1 # center 10% 31PATCH_W = 0.1 32PATCH_X = 0.5 - PATCH_W/2 33PATCH_Y = 0.5 - PATCH_H/2 34THRESHOLD_MAX_RMS_DIFF = 0.01 35 36 37def compute_means_and_save(cap, img_name, log_path): 38 """Compute the RGB means of a capture. 39 40 Args: 41 cap: 'YUV' or 'JPEG' capture 42 img_name: text for saved image name 43 log_path: path for saved image location 44 Returns: 45 RGB means 46 """ 47 img = image_processing_utils.convert_capture_to_rgb_image(cap, True) 48 image_processing_utils.write_image( 49 img, '%s_%s.jpg' % (os.path.join(log_path, NAME), img_name)) 50 patch = image_processing_utils.get_image_patch( 51 img, PATCH_X, PATCH_Y, PATCH_W, PATCH_H) 52 rgb_means = image_processing_utils.compute_image_means(patch) 53 logging.debug('%s rbg_means: %s', img_name, rgb_means) 54 return rgb_means 55 56 57class YuvPlusJpegTest(its_base_test.ItsBaseTest): 58 """Test capturing a single frame as both YUV and JPEG outputs.""" 59 60 def test_yuv_plus_jpeg(self): 61 logging.debug('Starting %s', NAME) 62 with its_session_utils.ItsSession( 63 device_id=self.dut.serial, 64 camera_id=self.camera_id, 65 hidden_physical_id=self.hidden_physical_id) as cam: 66 props = cam.get_camera_properties() 67 props = cam.override_with_hidden_physical_camera_props(props) 68 log_path = self.log_path 69 70 # check SKIP conditions 71 camera_properties_utils.skip_unless( 72 camera_properties_utils.compute_target_exposure(props)) 73 74 # Load chart for scene 75 its_session_utils.load_scene( 76 cam, props, self.scene, self.tablet, self.chart_distance) 77 78 # Create requests 79 max_jpeg_size = capture_request_utils.get_available_output_sizes( 80 'jpeg', props)[0] 81 w, h = capture_request_utils.get_available_output_sizes( 82 'yuv', props, MAX_IMG_SIZE, max_jpeg_size)[0] 83 fmt_yuv = {'format': 'yuv', 'width': w, 'height': h} 84 fmt_jpg = {'format': 'jpeg'} 85 86 # Use a manual request with a linear tonemap so that the YUV and JPEG 87 # should look the same (once converted by the image_processing_utils). 88 e, s = target_exposure_utils.get_target_exposure_combos( 89 log_path, cam)['midExposureTime'] 90 req = capture_request_utils.manual_capture_request(s, e, 0.0, True, props) 91 92 cap_yuv, cap_jpg = cam.do_capture(req, [fmt_yuv, fmt_jpg]) 93 rgb_means_yuv = compute_means_and_save(cap_yuv, 'yuv', log_path) 94 rgb_means_jpg = compute_means_and_save(cap_jpg, 'jpg', log_path) 95 96 rms_diff = image_processing_utils.compute_image_rms_difference( 97 rgb_means_yuv, rgb_means_jpg) 98 msg = f'RMS diff: {rms_diff:.4f}' 99 logging.debug('%s', msg) 100 if rms_diff >= THRESHOLD_MAX_RMS_DIFF: 101 raise AssertionError(msg + f', spec: {THRESHOLD_MAX_RMS_DIFF}') 102 103if __name__ == '__main__': 104 test_runner.main() 105