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