From 41a73a47a9175facd62fc18b467c94bad2474cda Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Mon, 6 Aug 2018 22:46:06 +0200
Subject: [PATCH] Add macro to check value range bit depth

---
 scripts/check_value_range_depth.ijm | 36 +++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 scripts/check_value_range_depth.ijm

diff --git a/scripts/check_value_range_depth.ijm b/scripts/check_value_range_depth.ijm
new file mode 100644
index 0000000..5ce65aa
--- /dev/null
+++ b/scripts/check_value_range_depth.ijm
@@ -0,0 +1,36 @@
+// macro code to check a 16-bit image for its actual value range bit depth and
+// to test for saturation within that range
+
+// define the value range bit depths to test for:
+possible_depths = newArray(8, 10, 11, 12, 14, 16);
+
+// code below expects array to be sorted, so make sure this is true:
+Array.sort(possible_depths);
+
+getStatistics(_, _, _, max, _, _);
+print("maximum value in image: " + max);
+
+saturated = false;
+actual_depth = 16;
+// traverse the possible depths array backwards:
+for (i = lengthOf(possible_depths) - 1; i > 0; i--) {
+	print("checking bit-depth " + possible_depths[i]);
+	sat = pow(2, possible_depths[i]) - 1;
+	
+	if (max == sat) {
+		saturated = true;
+		actual_depth = possible_depths[i];
+		print("saturated " + actual_depth + "-bit image detected!");
+	} else {
+		next_lower = pow(2, possible_depths[i-1]);
+		print("checking against next lower range limit: " + next_lower);
+		if (max < sat && max >= next_lower) {
+			actual_depth = possible_depths[i];
+			print("non-saturated " + actual_depth + "-bit image detected!");
+		}
+	}	
+}
+
+print("\nRESULT");
+print(" - value range depth = " + actual_depth);
+print(" - saturated = " + saturated);
-- 
GitLab