CameraITS: add post RAW sensitivity boost test

Bug: 26625646
Change-Id: Ic005745b64f42252faa4a5d4fee546348c1cb023
diff --git a/apps/CameraITS/pymodules/its/caps.py b/apps/CameraITS/pymodules/its/caps.py
index 95f19d9..71dd366 100644
--- a/apps/CameraITS/pymodules/its/caps.py
+++ b/apps/CameraITS/pymodules/its/caps.py
@@ -144,6 +144,29 @@
     """
     return len(its.objects.get_available_output_sizes("raw12", props)) > 0
 
+def raw_output(props):
+    """Returns whether a device supports any of RAW output format.
+
+    Args:
+        props: Camera properties object.
+
+    Returns:
+        Boolean.
+    """
+    return raw16(props) or raw10(props) or raw12(props)
+
+def post_raw_sensitivity_boost(props):
+    """Returns whether a device supports post RAW sensitivity boost..
+
+    Args:
+        props: Camera properties object.
+
+    Returns:
+        Boolean.
+    """
+    return props.has_key("android.control.postRawSensitivityBoostRange") and \
+            props["android.control.postRawSensitivityBoostRange"] != [100, 100]
+
 def sensor_fusion(props):
     """Returns whether the camera and motion sensor timestamps for the device
     are in the same time domain and can be compared directly.
diff --git a/apps/CameraITS/tests/scene1/test_post_raw_sensitivity_boost.py b/apps/CameraITS/tests/scene1/test_post_raw_sensitivity_boost.py
new file mode 100644
index 0000000..b0689c1
--- /dev/null
+++ b/apps/CameraITS/tests/scene1/test_post_raw_sensitivity_boost.py
@@ -0,0 +1,135 @@
+# Copyright 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import its.device
+import its.caps
+import its.objects
+import its.image
+import os.path
+import pylab
+import matplotlib
+import matplotlib.pyplot
+
+def main():
+    """Capture a set of raw/yuv images with different
+        sensitivity/post Raw sensitivity boost combination
+        and check if the output pixel mean matches request settings
+    """
+    NAME = os.path.basename(__file__).split(".")[0]
+
+    # Each raw image
+    RATIO_THRESHOLD = 0.03
+
+    with its.device.ItsSession() as cam:
+        props = cam.get_camera_properties()
+        its.caps.skip_unless(its.caps.raw_output(props) and
+                             its.caps.post_raw_sensitivity_boost(props) and
+                             its.caps.compute_target_exposure(props) and
+                             its.caps.per_frame_control(props))
+
+        w,h = its.objects.get_available_output_sizes(
+                "yuv", props, (1920, 1080))[0]
+
+        if its.caps.raw16(props):
+            raw_format = 'raw'
+        elif its.caps.raw10(props):
+            raw_format = 'raw10'
+        elif its.caps.raw12(props):
+            raw_format = 'raw12'
+        else # should not reach here
+            raise its.error.Error('Cannot find available RAW output format')
+
+        out_surfaces = [{"format": raw_format},
+                        {"format": "yuv", "width": w, "height": h}]
+
+        sens_min, sens_max = props['android.sensor.info.sensitivityRange']
+        sens_boost_min, sens_boost_max =
+                props['android.control.postRawSensitivityBoostRange']
+
+        e_targer, s_target =
+                its.target.get_target_exposure_combos(cam)["midSensitivity"]
+
+        reqs = []
+        settings = []
+        s_boost = sens_boost_min
+        while s_boost <= sens_boost_min:
+            s_raw = int(round(s_target * 100.0 / s_boost))
+            if s_raw < sens_min or s_raw > sens_max:
+                continue
+            req = its.objects.manual_capture_request(s_raw, e_target)
+            req['android.control.postRawSensitivityBoost'] = s_boost
+            reqs.append(req)
+            settings.append((s_raw, s_boost))
+            s_boost *= 2
+
+        caps = cam.do_capture(reqs, out_surfaces)
+
+        raw_rgb_means = []
+        yuv_rgb_means = []
+        for i,cap in enumerate(caps):
+            (s, s_boost) = settings[i]
+            raw_cap, yuv_cap = cap
+            raw_rgb = its.image.convert_capture_to_rgb_image(raw_cap, props=props)
+            yuv_rgb = its.image.convert_capture_to_rgb_image(yuv_cap)
+            raw_tile = its.image.get_image_patch(raw_rgb, 0.45,0.45,0.1,0.1)
+            yuv_tile = its.image.get_image_patch(yuv_rgb, 0.45,0.45,0.1,0.1)
+            raw_rgb_means.append(its.image.compute_image_means(raw_tile))
+            yuv_rgb_means.append(its.image.compute_image_means(yuv_tile))
+            its.image.write_image(raw_tile,
+                    "%s_raw_s=%04d_boost=%04d.jpg" % (NAME,s,s_boost))
+            its.image.write_image(yuv_tile,
+                    "%s_yuv_s=%04d_boost=%04d.jpg" % (NAME,s,s_boost))
+            print "s=%d, s_boost=%d: raw_means %s, yuv_means %d"%(
+                    s,s_boost,raw_rgb_means[-1], yuv_rgb_means[-1])
+
+        xs = range(len(reqs))
+        pylab.plot(xs, [rgb[0] for rgb in raw_rgb_means], 'r')
+        pylab.plot(xs, [rgb[1] for rgb in raw_rgb_means], 'g')
+        pylab.plot(xs, [rgb[2] for rgb in raw_rgb_means], 'b')
+        pylab.ylim([0,1])
+        matplotlib.pyplot.savefig("%s_raw_plot_means.png" % (NAME))
+        pylab.clf()
+        pylab.plot(xs, [rgb[0] for rgb in yuv_rgb_means], 'r')
+        pylab.plot(xs, [rgb[1] for rgb in yuv_rgb_means], 'g')
+        pylab.plot(xs, [rgb[2] for rgb in yuv_rgb_means], 'b')
+        pylab.ylim([0,1])
+        matplotlib.pyplot.savefig("%s_yuv_plot_means.png" % (NAME))
+
+        rgb_str = ["R", "G", "B"]
+        # Test that raw means is about 2x brighter than next step
+        raw_thres_min = 2 * (1 - RATIO_THRESHOLD)
+        raw_thres_max = 2 * (1 + RATIO_THRESHOLD)
+        for step in range(1, len(reqs)):
+            for rgb in range(3):
+                ratio = raw_rgb_means[step - 1][rgb] / raw_rgb_means[step][rgb]
+                print "Step (%d,%d) %s channel: %f, %f, ratio %f" % (
+                        step-1, step, rgb_str[rgb],
+                        raw_rgb_means[step - 1][rgb],
+                        raw_rgb_means[step][rgb],
+                        ratio)
+                assert(raw_thres_min < ratio < raw_thres_max)
+
+        # Test that each yuv step is about the same bright as their mean
+        yuv_thres_min = 1 - RATIO_THRESHOLD
+        yuv_thres_max = 1 + RATIO_THRESHOLD
+        for rgb in range(3):
+            vals = [val[rgb] for val in yuv_rgb_means]
+            mean = sum(vals) / len(vales)
+            print "%s channel vals %s mean %f"%(rgb_str[rgb], vals, mean)
+            for step in range(len(reqs)):
+                ratio = vals[step] / mean
+                assert(yuv_thres_min < ratio < yuv_thres_max)
+
+if __name__ == '__main__':
+    main()
diff --git a/tests/camera/src/android/hardware/camera2/cts/ExtendedCameraCharacteristicsTest.java b/tests/camera/src/android/hardware/camera2/cts/ExtendedCameraCharacteristicsTest.java
index 1c56e98..3bb573b 100644
--- a/tests/camera/src/android/hardware/camera2/cts/ExtendedCameraCharacteristicsTest.java
+++ b/tests/camera/src/android/hardware/camera2/cts/ExtendedCameraCharacteristicsTest.java
@@ -392,6 +392,18 @@
                 expectKeyAvailable(c, CameraCharacteristics.SENSOR_FORWARD_MATRIX2                          , OPT      ,   RAW                  );
             }
 
+            // Required key if any of RAW format output is supported
+            StreamConfigurationMap config =
+                    c.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
+            assertNotNull(String.format("No stream configuration map found for: ID %s",
+                    mIds[counter]), config);
+            if (config.isOutputSupportedFor(ImageFormat.RAW_SENSOR) ||
+                    config.isOutputSupportedFor(ImageFormat.RAW10)  ||
+                    config.isOutputSupportedFor(ImageFormat.RAW12)  ||
+                    config.isOutputSupportedFor(ImageFormat.RAW_PRIVATE)) {
+                expectKeyAvailable(c,
+                        CameraCharacteristics.CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE, OPT, BC);
+            }
             counter++;
         }
     }