am db3a6c44: Merge "CameraITS: Updates to test_crop_regions_raw" into lmp-sprout-dev

* commit 'db3a6c445106c6dc93cba0f2c170050498de9a41':
  CameraITS: Updates to test_crop_regions_raw
diff --git a/apps/CameraITS/tests/scene1/test_crop_region_raw.py b/apps/CameraITS/tests/scene1/test_crop_region_raw.py
index 9fc52cb..189e987 100644
--- a/apps/CameraITS/tests/scene1/test_crop_region_raw.py
+++ b/apps/CameraITS/tests/scene1/test_crop_region_raw.py
@@ -20,25 +20,6 @@
 import numpy
 import os.path
 
-
-def check_crop_region(expected, reported, active, err_threshold):
-    """Check if the reported region is within the tolerance.
-
-    Args:
-        expected: expected crop region
-        reported: reported crop region
-        active: active resolution
-        err_threshold: error threshold for the active resolution
-    """
-
-    ex = (active["right"] - active["left"]) * err_threshold
-    ey = (active["bottom"] - active["top"]) * err_threshold
-
-    assert ((abs(expected["left"] - reported["left"]) <= ex) and
-            (abs(expected["right"] - reported["right"]) <= ex) and
-            (abs(expected["top"] - reported["top"]) <= ey) and
-            (abs(expected["bottom"] - reported["bottom"]) <= ey))
-
 def main():
     """Test that raw streams are not croppable.
     """
@@ -53,6 +34,7 @@
                              its.caps.raw16(props) and
                              its.caps.per_frame_control(props))
 
+        # Calculate the active sensor region for a full (non-cropped) image.
         a = props['android.sensor.info.activeArraySize']
         ax, ay = a["left"], a["top"]
         aw, ah = a["right"] - a["left"], a["bottom"] - a["top"]
@@ -65,6 +47,19 @@
             "bottom": ah
         }
 
+        # Calculate a center crop region.
+        zoom = min(3.0, its.objects.get_max_digital_zoom(props))
+        assert(zoom >= 1)
+        cropw = aw / zoom
+        croph = ah / zoom
+
+        crop_region = {
+            "left": aw / 2 - cropw / 2,
+            "top": ah / 2 - croph / 2,
+            "right": aw / 2 + cropw / 2,
+            "bottom": ah / 2 + croph / 2
+        }
+
         # Capture without a crop region.
         # Use a manual request with a linear tonemap so that the YUV and RAW
         # should look the same (once converted by the its.image module).
@@ -72,59 +67,57 @@
         req = its.objects.manual_capture_request(s,e, True)
         cap1_raw, cap1_yuv = cam.do_capture(req, cam.CAP_RAW_YUV)
 
-        # Calculate a center crop region.
-        zoom = min(3.0, its.objects.get_max_digital_zoom(props))
-        assert(zoom >= 1)
-        cropw = aw / zoom
-        croph = ah / zoom
-
-        req["android.scaler.cropRegion"] = {
-            "left": aw / 2 - cropw / 2,
-            "top": ah / 2 - croph / 2,
-            "right": aw / 2 + cropw / 2,
-            "bottom": ah / 2 + croph / 2
-        }
-
-        # when both YUV and RAW are requested, the crop region that's
-        # applied to YUV should be reported.
-        crop_region = req["android.scaler.cropRegion"]
-        if crop_region == full_region:
-            crop_region_err_thresh = 0.0
-        else:
-            crop_region_err_thresh = CROP_REGION_ERROR_THRESHOLD
-
+        # Capture with a crop region.
+        req["android.scaler.cropRegion"] = crop_region
         cap2_raw, cap2_yuv = cam.do_capture(req, cam.CAP_RAW_YUV)
 
+        # Check the metadata related to crop regions.
+        # When both YUV and RAW are requested, the crop region that's
+        # applied to YUV should be reported.
+        # Note that the crop region returned by the cropped captures doesn't
+        # need to perfectly match the one that was requested.
         imgs = {}
-        for s, cap, cr, err_delta in [("yuv_full", cap1_yuv, full_region, 0),
-                      ("raw_full", cap1_raw, full_region, 0),
-                      ("yuv_crop", cap2_yuv, crop_region, crop_region_err_thresh),
-                      ("raw_crop", cap2_raw, crop_region, crop_region_err_thresh)]:
+        for s, cap, cr_expected, err_delta in [
+                ("yuv_full",cap1_yuv,full_region,0),
+                ("raw_full",cap1_raw,full_region,0),
+                ("yuv_crop",cap2_yuv,crop_region,CROP_REGION_ERROR_THRESHOLD),
+                ("raw_crop",cap2_raw,crop_region,CROP_REGION_ERROR_THRESHOLD)]:
+
+            # Convert the capture to RGB and dump to a file.
             img = its.image.convert_capture_to_rgb_image(cap, props=props)
             its.image.write_image(img, "%s_%s.jpg" % (NAME, s))
-            r = cap["metadata"]["android.scaler.cropRegion"]
-            x, y = r["left"], r["top"]
-            w, h = r["right"] - r["left"], r["bottom"] - r["top"]
             imgs[s] = img
-            print "Crop on %s: (%d,%d %dx%d)" % (s, x, y, w, h)
-            check_crop_region(cr, r, a, err_delta)
+
+            # Get the crop region that is reported in the capture result.
+            cr_reported = cap["metadata"]["android.scaler.cropRegion"]
+            x, y = cr_reported["left"], cr_reported["top"]
+            w = cr_reported["right"] - cr_reported["left"]
+            h = cr_reported["bottom"] - cr_reported["top"]
+            print "Crop reported on %s: (%d,%d %dx%d)" % (s, x, y, w, h)
+
+            # Test that the reported crop region is the same as the expected
+            # one, for a non-cropped capture, and is close to the expected one,
+            # for a cropped capture.
+            ex = aw * err_delta
+            ey = ah * err_delta
+            assert ((abs(cr_expected["left"] - cr_reported["left"]) <= ex) and
+                    (abs(cr_expected["right"] - cr_reported["right"]) <= ex) and
+                    (abs(cr_expected["top"] - cr_reported["top"]) <= ey) and
+                    (abs(cr_expected["bottom"] - cr_reported["bottom"]) <= ey))
 
         # Also check the image content; 3 of the 4 shots should match.
         # Note that all the shots are RGB below; the variable names correspond
         # to what was captured.
-        # Average the images down 4x4 -> 1 prior to comparison to smooth out
-        # noise.
-        # Shrink the YUV images an additional 2x2 -> 1 to account for the size
-        # reduction that the raw images went through in the RGB conversion.
+
+        # Shrink the YUV images 2x2 -> 1 to account for the size reduction that
+        # the raw images went through in the RGB conversion.
         imgs2 = {}
         for s,img in imgs.iteritems():
             h,w,ch = img.shape
-            m = 4
             if s in ["yuv_full", "yuv_crop"]:
-                m = 8
-            img = img.reshape(h/m,m,w/m,m,3).mean(3).mean(1).reshape(h/m,w/m,3)
+                img = img.reshape(h/2,2,w/2,2,3).mean(3).mean(1)
+                img = img.reshape(h/2,w/2,3)
             imgs2[s] = img
-            print s, img.shape
 
         # Strip any border pixels from the raw shots (since the raw images may
         # be larger than the YUV images). Assume a symmetric padded border.
@@ -139,7 +132,10 @@
         for s,img in imgs2.iteritems():
             its.image.write_image(img, "%s_comp_%s.jpg" % (NAME, s))
 
-        # Compute image diffs.
+        # Compute diffs between images of the same type.
+        # The raw_crop and raw_full shots should be identical (since the crop
+        # doesn't apply to raw images), and the yuv_crop and yuv_full shots
+        # should be different.
         diff_yuv = numpy.fabs((imgs2["yuv_full"] - imgs2["yuv_crop"])).mean()
         diff_raw = numpy.fabs((imgs2["raw_full"] - imgs2["raw_crop"])).mean()
         print "YUV diff (crop vs. non-crop):", diff_yuv