1# Copyright 2013 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 converted YUV images & device JPEG images look the same.""" 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 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_img_means_and_save(img, fmt_name, log_path): 37 """Extract center patch, compute means, and save image. 38 39 Args: 40 img: image array 41 fmt_name: text to identify image 42 log_path: location to save image 43 44 Returns: 45 means of image patch 46 """ 47 name_with_log_path = os.path.join(log_path, _NAME) 48 image_processing_utils.write_image( 49 img, f'{name_with_log_path}_fmt={fmt_name}.jpg') 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 rgb_means: %s', fmt_name, str(rgb_means)) 54 return rgb_means 55 56 57class JpegTest(its_base_test.ItsBaseTest): 58 """Test that converted YUV images and device JPEG images look the same.""" 59 60 def test_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 sync_latency = camera_properties_utils.sync_latency(props) 71 72 # Check SKIP conditions 73 camera_properties_utils.skip_unless( 74 camera_properties_utils.linear_tonemap(props)) 75 76 # Load chart for scene 77 its_session_utils.load_scene( 78 cam, props, self.scene, self.tablet, 79 its_session_utils.CHART_DISTANCE_NO_SCALING) 80 81 # Initialize common request parameters 82 if camera_properties_utils.compute_target_exposure(props): 83 e, s = target_exposure_utils.get_target_exposure_combos( 84 log_path, cam)['midExposureTime'] 85 req = capture_request_utils.manual_capture_request( 86 s, e, 0.0, True, props) 87 else: 88 cam.do_3a(do_af=False) 89 req = capture_request_utils.auto_capture_request( 90 linear_tonemap=True, props=props, do_af=False) 91 92 # YUV 93 size = capture_request_utils.get_available_output_sizes('yuv', props)[0] 94 out_surface = {'width': size[0], 'height': size[1], 'format': 'yuv'} 95 cap = its_session_utils.do_capture_with_latency( 96 cam, req, sync_latency, out_surface) 97 img = image_processing_utils.convert_capture_to_rgb_image(cap) 98 rgb_means_yuv = compute_img_means_and_save(img, 'yuv', log_path) 99 100 # JPEG 101 size = capture_request_utils.get_available_output_sizes('jpg', props)[0] 102 out_surface = {'width': size[0], 'height': size[1], 'format': 'jpg'} 103 cap = its_session_utils.do_capture_with_latency( 104 cam, req, sync_latency, out_surface) 105 img = image_processing_utils.decompress_jpeg_to_rgb_image(cap['data']) 106 rgb_means_jpg = compute_img_means_and_save(img, 'jpg', log_path) 107 108 # Assert images are similar 109 rms_diff = image_processing_utils.compute_image_rms_difference_1d( 110 rgb_means_yuv, rgb_means_jpg) 111 logging.debug('RMS difference: %.3f', rms_diff) 112 if rms_diff >= _THRESHOLD_MAX_RMS_DIFF: 113 raise AssertionError( 114 f'RMS diff: {rms_diff:.3f}, spec: {_THRESHOLD_MAX_RMS_DIFF}') 115 116if __name__ == '__main__': 117 test_runner.main() 118 119