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 os.path
16import its.caps
17import its.device
18import its.image
19import its.objects
20import its.target
21import numpy
22
23CROP_FULL_ERROR_THRESHOLD = 3  # pixels
24CROP_REGION_ERROR_THRESHOLD = 0.01  # reltol
25DIFF_THRESH = 0.05  # reltol
26NAME = os.path.basename(__file__).split(".")[0]
27
28
29def main():
30    """Test that raw streams are not croppable."""
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) and
35                             its.caps.raw16(props) and
36                             its.caps.per_frame_control(props) and
37                             not its.caps.mono_camera(props))
38
39        # Calculate the active sensor region for a full (non-cropped) image.
40        a = props["android.sensor.info.activeArraySize"]
41        ax, ay = a["left"], a["top"]
42        aw, ah = a["right"] - a["left"], a["bottom"] - a["top"]
43        print "Active sensor region: (%d,%d %dx%d)" % (ax, ay, aw, ah)
44
45        full_region = {
46            "left": 0,
47            "top": 0,
48            "right": aw,
49            "bottom": ah
50        }
51
52        # Calculate a center crop region.
53        zoom = min(3.0, its.objects.get_max_digital_zoom(props))
54        assert(zoom >= 1)
55        cropw = aw / zoom
56        croph = ah / zoom
57
58        crop_region = {
59            "left": aw / 2 - cropw / 2,
60            "top": ah / 2 - croph / 2,
61            "right": aw / 2 + cropw / 2,
62            "bottom": ah / 2 + croph / 2
63        }
64
65        # Capture without a crop region.
66        # Use a manual request with a linear tonemap so that the YUV and RAW
67        # should look the same (once converted by the its.image module).
68        e, s = its.target.get_target_exposure_combos(cam)["minSensitivity"]
69        req = its.objects.manual_capture_request(s,e, 0.0, True, props)
70        cap1_raw, cap1_yuv = cam.do_capture(req, cam.CAP_RAW_YUV)
71
72        # Capture with a crop region.
73        req["android.scaler.cropRegion"] = crop_region
74        cap2_raw, cap2_yuv = cam.do_capture(req, cam.CAP_RAW_YUV)
75
76        # Check the metadata related to crop regions.
77        # When both YUV and RAW are requested, the crop region that's
78        # applied to YUV should be reported.
79        # Note that the crop region returned by the cropped captures doesn't
80        # need to perfectly match the one that was requested.
81        imgs = {}
82        for s, cap, cr_expected, err_delta in [
83                ("yuv_full", cap1_yuv, full_region, CROP_FULL_ERROR_THRESHOLD),
84                ("raw_full", cap1_raw, full_region, CROP_FULL_ERROR_THRESHOLD),
85                ("yuv_crop", cap2_yuv, crop_region, CROP_REGION_ERROR_THRESHOLD),
86                ("raw_crop", cap2_raw, crop_region, CROP_REGION_ERROR_THRESHOLD)]:
87
88            # Convert the capture to RGB and dump to a file.
89            img = its.image.convert_capture_to_rgb_image(cap, props=props)
90            its.image.write_image(img, "%s_%s.jpg" % (NAME, s))
91            imgs[s] = img
92
93            # Get the crop region that is reported in the capture result.
94            cr_reported = cap["metadata"]["android.scaler.cropRegion"]
95            x, y = cr_reported["left"], cr_reported["top"]
96            w = cr_reported["right"] - cr_reported["left"]
97            h = cr_reported["bottom"] - cr_reported["top"]
98            print "Crop reported on %s: (%d,%d %dx%d)" % (s, x, y, w, h)
99
100            # Test that the reported crop region is the same as the expected
101            # one, for a non-cropped capture, and is close to the expected one,
102            # for a cropped capture.
103            ex = aw * err_delta
104            ey = ah * err_delta
105            assert ((abs(cr_expected["left"] - cr_reported["left"]) <= ex) and
106                    (abs(cr_expected["right"] - cr_reported["right"]) <= ex) and
107                    (abs(cr_expected["top"] - cr_reported["top"]) <= ey) and
108                    (abs(cr_expected["bottom"] - cr_reported["bottom"]) <= ey))
109
110        # Also check the image content; 3 of the 4 shots should match.
111        # Note that all the shots are RGB below; the variable names correspond
112        # to what was captured.
113
114        # Shrink the YUV images 2x2 -> 1 to account for the size reduction that
115        # the raw images went through in the RGB conversion.
116        imgs2 = {}
117        for s,img in imgs.iteritems():
118            h,w,ch = img.shape
119            if s in ["yuv_full", "yuv_crop"]:
120                img = img.reshape(h/2,2,w/2,2,3).mean(3).mean(1)
121                img = img.reshape(h/2,w/2,3)
122            imgs2[s] = img
123
124        # Strip any border pixels from the raw shots (since the raw images may
125        # be larger than the YUV images). Assume a symmetric padded border.
126        xpad = (imgs2["raw_full"].shape[1] - imgs2["yuv_full"].shape[1]) / 2
127        ypad = (imgs2["raw_full"].shape[0] - imgs2["yuv_full"].shape[0]) / 2
128        wyuv = imgs2["yuv_full"].shape[1]
129        hyuv = imgs2["yuv_full"].shape[0]
130        imgs2["raw_full"]=imgs2["raw_full"][ypad:ypad+hyuv:,xpad:xpad+wyuv:,::]
131        imgs2["raw_crop"]=imgs2["raw_crop"][ypad:ypad+hyuv:,xpad:xpad+wyuv:,::]
132        print "Stripping padding before comparison:", xpad, ypad
133
134        for s,img in imgs2.iteritems():
135            its.image.write_image(img, "%s_comp_%s.jpg" % (NAME, s))
136
137        # Compute diffs between images of the same type.
138        # The raw_crop and raw_full shots should be identical (since the crop
139        # doesn't apply to raw images), and the yuv_crop and yuv_full shots
140        # should be different.
141        diff_yuv = numpy.fabs((imgs2["yuv_full"] - imgs2["yuv_crop"])).mean()
142        diff_raw = numpy.fabs((imgs2["raw_full"] - imgs2["raw_crop"])).mean()
143        print "YUV diff (crop vs. non-crop):", diff_yuv
144        print "RAW diff (crop vs. non-crop):", diff_raw
145
146        assert(diff_yuv > DIFF_THRESH)
147        assert(diff_raw < DIFF_THRESH)
148
149if __name__ == '__main__':
150    main()
151
152