1# Copyright 2014 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.error
20import its.target
21import sys
22import os
23import os.path
24
25# Change this to True, to have the test break at the first failure.
26stop_at_first_failure = False
27
28def main():
29    """Test different combinations of output formats.
30    """
31    NAME = os.path.basename(__file__).split(".")[0]
32
33    with its.device.ItsSession() as cam:
34
35        props = cam.get_camera_properties()
36        its.caps.skip_unless(its.caps.compute_target_exposure(props) and
37                             its.caps.raw16(props))
38
39        successes = []
40        failures = []
41        debug = its.caps.debug_mode()
42
43        # Two different requests: auto, and manual.
44        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
45        req_aut = its.objects.auto_capture_request()
46        req_man = its.objects.manual_capture_request(s, e)
47        reqs = [req_aut, # R0
48                req_man] # R1
49
50        # 10 different combos of output formats; some are single surfaces, and
51        # some are multiple surfaces.
52        wyuv,hyuv = its.objects.get_available_output_sizes("yuv", props)[-1]
53        wjpg,hjpg = its.objects.get_available_output_sizes("jpg", props)[-1]
54        fmt_yuv_prev = {"format":"yuv", "width":wyuv, "height":hyuv}
55        fmt_yuv_full = {"format":"yuv"}
56        fmt_jpg_prev = {"format":"jpeg","width":wjpg, "height":hjpg}
57        fmt_jpg_full = {"format":"jpeg"}
58        fmt_raw_full = {"format":"raw"}
59        fmt_combos =[
60                [fmt_yuv_prev],                             # F0
61                [fmt_yuv_full],                             # F1
62                [fmt_jpg_prev],                             # F2
63                [fmt_jpg_full],                             # F3
64                [fmt_raw_full],                             # F4
65                [fmt_yuv_prev, fmt_jpg_prev],               # F5
66                [fmt_yuv_prev, fmt_jpg_full],               # F6
67                [fmt_yuv_prev, fmt_raw_full],               # F7
68                [fmt_yuv_prev, fmt_jpg_prev, fmt_raw_full], # F8
69                [fmt_yuv_prev, fmt_jpg_full, fmt_raw_full]] # F9
70
71        # Two different burst lengths: single frame, and 3 frames.
72        burst_lens = [1, # B0
73                      3] # B1
74
75        # There are 2x10x2=40 different combinations. Run through them all.
76        n = 0
77        for r,req in enumerate(reqs):
78            for f,fmt_combo in enumerate(fmt_combos):
79                for b,burst_len in enumerate(burst_lens):
80                    try:
81                        caps = cam.do_capture([req]*burst_len, fmt_combo)
82                        successes.append((n,r,f,b))
83                        print "==> Success[%02d]: R%d F%d B%d" % (n,r,f,b)
84
85                        # Dump the captures out to jpegs.
86                        if not isinstance(caps, list):
87                            caps = [caps]
88                        elif isinstance(caps[0], list):
89                            caps = sum(caps, [])
90                        for c,cap in enumerate(caps):
91                            img = its.image.convert_capture_to_rgb_image(cap,
92                                    props=props)
93                            if debug:
94                                its.image.write_image(img,
95                                    "%s_n%02d_r%d_f%d_b%d_c%d.jpg"%(NAME,n,r,f,b,c))
96
97                    except Exception as e:
98                        print e
99                        print "==> Failure[%02d]: R%d F%d B%d" % (n,r,f,b)
100                        failures.append((n,r,f,b))
101                        if stop_at_first_failure:
102                            sys.exit(0)
103                    n += 1
104
105        num_fail = len(failures)
106        num_success = len(successes)
107        num_total = len(reqs)*len(fmt_combos)*len(burst_lens)
108        num_not_run = num_total - num_success - num_fail
109
110        print "\nFailures (%d / %d):" % (num_fail, num_total)
111        for (n,r,f,b) in failures:
112            print "  %02d: R%d F%d B%d" % (n,r,f,b)
113        print "\nSuccesses (%d / %d):" % (num_success, num_total)
114        for (n,r,f,b) in successes:
115            print "  %02d: R%d F%d B%d" % (n,r,f,b)
116        if num_not_run > 0:
117            print "\nNumber of tests not run: %d / %d" % (num_not_run, num_total)
118        print ""
119
120        # The test passes if all the combinations successfully capture.
121        assert(num_fail == 0)
122        assert(num_success == num_total)
123
124if __name__ == '__main__':
125    main()
126
127