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 its.image
16import its.caps
17import its.device
18import its.objects
19import its.target
20import os.path
21import math
22
23def main():
24    """Test that the reported sizes and formats for image capture work.
25    """
26    NAME = os.path.basename(__file__).split(".")[0]
27
28    THRESHOLD_MAX_RMS_DIFF = 0.03
29
30    with its.device.ItsSession() as cam:
31        props = cam.get_camera_properties()
32        its.caps.skip_unless(its.caps.compute_target_exposure(props) and
33                             its.caps.per_frame_control(props))
34
35        # Use a manual request with a linear tonemap so that the YUV and JPEG
36        # should look the same (once converted by the its.image module).
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        rgbs = []
41
42        for size in its.objects.get_available_output_sizes("yuv", props):
43            out_surface = {"width":size[0], "height":size[1], "format":"yuv"}
44            cap = cam.do_capture(req, out_surface)
45            assert(cap["format"] == "yuv")
46            assert(cap["width"] == size[0])
47            assert(cap["height"] == size[1])
48            print "Captured YUV %dx%d" % (cap["width"], cap["height"])
49            img = its.image.convert_capture_to_rgb_image(cap)
50            its.image.write_image(img, "%s_yuv_w%d_h%d.jpg"%(
51                    NAME,size[0],size[1]))
52            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
53            rgb = its.image.compute_image_means(tile)
54            rgbs.append(rgb)
55
56        for size in its.objects.get_available_output_sizes("jpg", props):
57            out_surface = {"width":size[0], "height":size[1], "format":"jpg"}
58            cap = cam.do_capture(req, out_surface)
59            assert(cap["format"] == "jpeg")
60            assert(cap["width"] == size[0])
61            assert(cap["height"] == size[1])
62            img = its.image.decompress_jpeg_to_rgb_image(cap["data"])
63            its.image.write_image(img, "%s_jpg_w%d_h%d.jpg"%(
64                    NAME,size[0], size[1]))
65            assert(img.shape[0] == size[1])
66            assert(img.shape[1] == size[0])
67            assert(img.shape[2] == 3)
68            print "Captured JPEG %dx%d" % (cap["width"], cap["height"])
69            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
70            rgb = its.image.compute_image_means(tile)
71            rgbs.append(rgb)
72
73        max_diff = 0
74        rgb0 = rgbs[0]
75        for rgb1 in rgbs[1:]:
76            rms_diff = math.sqrt(
77                    sum([pow(rgb0[i] - rgb1[i], 2.0) for i in range(3)]) / 3.0)
78            max_diff = max(max_diff, rms_diff)
79        print "Max RMS difference:", max_diff
80        assert(rms_diff < THRESHOLD_MAX_RMS_DIFF)
81
82if __name__ == '__main__':
83    main()
84
85