1# Copyright 2023 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 P3 JPEG output is correct."""
15
16
17import logging
18import os
19
20from mobly import test_runner
21
22import its_base_test
23import camera_properties_utils
24import capture_request_utils
25import image_processing_utils
26import its_session_utils
27
28
29_NAME = os.path.splitext(os.path.basename(__file__))[0]
30
31
32def _check_icc(jpeg_img, color_space, fmt_str, icc_path):
33  if not image_processing_utils.jpeg_has_icc_profile(jpeg_img):
34    logging.error('FMT: %s/%s has no icc profile', fmt_str, color_space)
35    return False
36  elif not image_processing_utils.is_jpeg_icc_profile_correct(
37      jpeg_img, color_space, icc_path):
38    logging.error('FMT: %s/%s has incorrect icc profile', fmt_str, color_space)
39    return False
40  return True
41
42
43class DisplayP3Test(its_base_test.ItsBaseTest):
44  """Test Display P3 JPEG capture.
45
46  Note the test does not require a specific target but does perform
47  both automatic and manual captures so it requires a fixed scene
48  where 3A can converge.
49  """
50
51  def test_display_p3(self):
52    logging.debug('Starting %s', _NAME)
53    with its_session_utils.ItsSession(
54        device_id=self.dut.serial,
55        camera_id=self.camera_id,
56        hidden_physical_id=self.hidden_physical_id) as cam:
57
58      # check SKIP conditions
59      camera_properties_utils.skip_unless(cam.is_p3_capture_supported())
60
61      props = cam.get_camera_properties()
62      props = cam.override_with_hidden_physical_camera_props(props)
63
64      # Load chart for scene
65      its_session_utils.load_scene(
66          cam, props, self.scene, self.tablet, self.chart_distance)
67
68      cam.do_3a()
69
70      req = capture_request_utils.auto_capture_request()
71
72      display_p3_color_space = camera_properties_utils.color_space_to_int(
73          'DISPLAY_P3')
74
75      surface_fmt = {'format': 'jpeg'}
76
77      try:
78        p3_fmt = surface_fmt.copy()
79        p3_fmt['colorSpace'] = display_p3_color_space
80
81        p3_cap = cam.do_capture(req, p3_fmt)
82
83        name_with_path = os.path.join(self.log_path, _NAME)
84
85        fmt_str = surface_fmt['format']
86        p3_img_name = f'{name_with_path}_{fmt_str}_cap_display_p3.jpg'
87        p3_img_icc = p3_img_name.strip('.jpg') + '.icc'
88        p3_jpeg_img = image_processing_utils.get_img(p3_cap['data'])
89        p3_jpeg_img.save(p3_img_name)
90
91        if not _check_icc(p3_jpeg_img, 'DISPLAY_P3', fmt_str, p3_img_icc):
92          raise AssertionError('Failure: P3 JPEG does not contain correct '
93                               'icc profile')
94        if not image_processing_utils.p3_img_has_wide_gamut(p3_jpeg_img):
95          raise AssertionError('Failure: P3 JPEG does not contain wide gamut '
96                               'pixels outside the SRGB color space.')
97
98      # pylint: disable=broad-except
99      except Exception as e:
100        raise AssertionError('Failure') from e
101
102if __name__ == '__main__':
103  test_runner.main()
104