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