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
15import math
16import os.path
17
18import its.caps
19import its.device
20import its.image
21import its.objects
22import its.target
23
24NAME = os.path.basename(__file__).split(".")[0]
25THRESHOLD_MAX_RMS_DIFF = 0.01
26
27
28def main():
29    """Test that converted YUV images and device JPEG images look the same.
30    """
31
32    with its.device.ItsSession() as cam:
33        props = cam.get_camera_properties()
34        its.caps.skip_unless(its.caps.compute_target_exposure(props))
35        sync_latency = its.caps.sync_latency(props)
36
37        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
38        req = its.objects.manual_capture_request(s, e, 0.0, True, props)
39
40        # YUV
41        size = its.objects.get_available_output_sizes("yuv", props)[0]
42        out_surface = {"width": size[0], "height": size[1], "format": "yuv"}
43        cap = its.device.do_capture_with_latency(
44                cam, req, sync_latency, out_surface)
45        img = its.image.convert_capture_to_rgb_image(cap)
46        its.image.write_image(img, "%s_fmt=yuv.jpg" % (NAME))
47        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
48        rgb0 = its.image.compute_image_means(tile)
49
50        # JPEG
51        size = its.objects.get_available_output_sizes("jpg", props)[0]
52        out_surface = {"width": size[0], "height": size[1], "format": "jpg"}
53        cap = its.device.do_capture_with_latency(
54                cam, req, sync_latency, out_surface)
55        img = its.image.decompress_jpeg_to_rgb_image(cap["data"])
56        its.image.write_image(img, "%s_fmt=jpg.jpg" % (NAME))
57        tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
58        rgb1 = its.image.compute_image_means(tile)
59
60        rms_diff = math.sqrt(
61                sum([pow(rgb0[i] - rgb1[i], 2.0) for i in range(3)]) / 3.0)
62        print "RMS difference:", rms_diff
63        msg = "RMS difference: %.4f, spec: %.3f" % (rms_diff,
64                                                    THRESHOLD_MAX_RMS_DIFF)
65        assert rms_diff < THRESHOLD_MAX_RMS_DIFF, msg
66
67if __name__ == '__main__':
68    main()
69
70