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
24import matplotlib.pylab
25import matplotlib.pyplot
26
27NAME = os.path.basename(__file__).split(".")[0]
28THRESHOLD_MAX_RMS_DIFF = 0.03
29
30
31def main():
32    """Test that the reported sizes and formats for image capture work.
33    """
34
35    with its.device.ItsSession() as cam:
36        props = cam.get_camera_properties()
37        its.caps.skip_unless(its.caps.compute_target_exposure(props) and
38                             its.caps.per_frame_control(props))
39
40        # Use a manual request with a linear tonemap so that the YUV and JPEG
41        # should look the same (once converted by the its.image module).
42        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
43        req = its.objects.manual_capture_request(s, e, 0.0, True, props)
44
45        rgbs = []
46
47        for size in its.objects.get_available_output_sizes("yuv", props):
48            out_surface = {"width": size[0], "height": size[1], "format": "yuv"}
49            cap = cam.do_capture(req, out_surface)
50            assert cap["format"] == "yuv"
51            assert cap["width"] == size[0]
52            assert cap["height"] == size[1]
53            print "Captured YUV %dx%d" % (cap["width"], cap["height"]),
54            img = its.image.convert_capture_to_rgb_image(cap)
55            its.image.write_image(img, "%s_yuv_w%d_h%d.jpg"%(
56                    NAME, size[0], size[1]))
57            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
58            rgb = its.image.compute_image_means(tile)
59            print "rgb =", rgb
60            rgbs.append(rgb)
61
62        for size in its.objects.get_available_output_sizes("jpg", props):
63            out_surface = {"width": size[0], "height": size[1], "format": "jpg"}
64            cap = cam.do_capture(req, out_surface)
65            assert cap["format"] == "jpeg"
66            assert cap["width"] == size[0]
67            assert cap["height"] == size[1]
68            img = its.image.decompress_jpeg_to_rgb_image(cap["data"])
69            its.image.write_image(img, "%s_jpg_w%d_h%d.jpg"%(
70                    NAME, size[0], size[1]))
71            assert img.shape[0] == size[1]
72            assert img.shape[1] == size[0]
73            assert img.shape[2] == 3
74            print "Captured JPEG %dx%d" % (cap["width"], cap["height"]),
75            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
76            rgb = its.image.compute_image_means(tile)
77            print "rgb =", rgb
78            rgbs.append(rgb)
79
80        # Plot means vs format
81        matplotlib.pylab.title(NAME)
82        matplotlib.pylab.plot(range(len(rgbs)), [r[0] for r in rgbs], "-ro")
83        matplotlib.pylab.plot(range(len(rgbs)), [g[1] for g in rgbs], "-go")
84        matplotlib.pylab.plot(range(len(rgbs)), [b[2] for b in rgbs], "-bo")
85        matplotlib.pylab.ylim([0, 1])
86        matplotlib.pylab.xlabel("format number")
87        matplotlib.pylab.ylabel("RGB avg [0, 1]")
88        matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
89
90        max_diff = 0
91        rgb0 = rgbs[0]
92        for rgb1 in rgbs[1:]:
93            rms_diff = math.sqrt(
94                    sum([pow(rgb0[i] - rgb1[i], 2.0) for i in range(3)]) / 3.0)
95            max_diff = max(max_diff, rms_diff)
96        print "Max RMS difference:", max_diff
97        msg = "Max RMS difference: %.4f, spec: %.3f" % (max_diff,
98                                                        THRESHOLD_MAX_RMS_DIFF)
99        assert max_diff < THRESHOLD_MAX_RMS_DIFF, msg
100
101if __name__ == "__main__":
102    main()
103
104