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 28NAME = os.path.splitext(os.path.basename(__file__))[0] 29PATCH_H = 0.1 # center 10% 30PATCH_W = 0.1 31PATCH_X = 0.5 - PATCH_W/2 32PATCH_Y = 0.5 - PATCH_H/2 33THRESHOLD_MAX_RMS_DIFF = 0.01 34 35 36def compute_img_means_and_save(img, img_name, log_path): 37 """Extract center patch, compute means, and save image. 38 39 Args: 40 img: image array 41 img_name: text to identify image 42 log_path: location to save image 43 44 Returns: 45 means of image patch 46 """ 47 image_processing_utils.write_image( 48 img, '%s_fmt=%s.jpg' % (os.path.join(log_path, NAME), img_name)) 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, str(rgb_means)) 53 return rgb_means 54 55 56class JpegTest(its_base_test.ItsBaseTest): 57 """Test that converted YUV images and device JPEG images look the same.""" 58 59 def test_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.compute_target_exposure(props)) 72 sync_latency = camera_properties_utils.sync_latency(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 # Initialize common request parameters 79 e, s = target_exposure_utils.get_target_exposure_combos( 80 log_path, cam)['midExposureTime'] 81 req = capture_request_utils.manual_capture_request(s, e, 0.0, True, props) 82 83 # YUV 84 size = capture_request_utils.get_available_output_sizes('yuv', props)[0] 85 out_surface = {'width': size[0], 'height': size[1], 'format': 'yuv'} 86 cap = its_session_utils.do_capture_with_latency( 87 cam, req, sync_latency, out_surface) 88 img = image_processing_utils.convert_capture_to_rgb_image(cap) 89 rgb_means_yuv = compute_img_means_and_save(img, 'yuv', log_path) 90 91 # JPEG 92 size = capture_request_utils.get_available_output_sizes('jpg', props)[0] 93 out_surface = {'width': size[0], 'height': size[1], 'format': 'jpg'} 94 cap = its_session_utils.do_capture_with_latency( 95 cam, req, sync_latency, out_surface) 96 img = image_processing_utils.decompress_jpeg_to_rgb_image(cap['data']) 97 rgb_means_jpg = compute_img_means_and_save(img, 'jpg', log_path) 98 99 # Assert images are similar 100 rms_diff = image_processing_utils.compute_image_rms_difference( 101 rgb_means_yuv, rgb_means_jpg) 102 logging.debug('RMS difference: %.3f', rms_diff) 103 e_msg = 'RMS difference: %.3f, spec: %.2f' % ( 104 rms_diff, THRESHOLD_MAX_RMS_DIFF) 105 assert rms_diff < THRESHOLD_MAX_RMS_DIFF, e_msg 106 107if __name__ == '__main__': 108 test_runner.main() 109 110