[Bf-blender-cvs] [ba668c0d063] temp-cryptomatte-manifest-parser: Cleanup: Moved conversion to float to CryptomatteHash.

Jeroen Bakker noreply at git.blender.org
Fri Feb 26 12:42:04 CET 2021


Commit: ba668c0d0634fc5adb49f456cb57d31f8a69e5fe
Author: Jeroen Bakker
Date:   Fri Feb 26 12:40:51 2021 +0100
Branches: temp-cryptomatte-manifest-parser
https://developer.blender.org/rBba668c0d0634fc5adb49f456cb57d31f8a69e5fe

Cleanup: Moved conversion to float to CryptomatteHash.

===================================================================

M	source/blender/blenkernel/BKE_cryptomatte.hh
M	source/blender/blenkernel/intern/cryptomatte.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_cryptomatte.hh b/source/blender/blenkernel/BKE_cryptomatte.hh
index cdcda484876..c1da0339359 100644
--- a/source/blender/blenkernel/BKE_cryptomatte.hh
+++ b/source/blender/blenkernel/BKE_cryptomatte.hh
@@ -68,6 +68,7 @@ struct CryptomatteHash {
   static CryptomatteHash from_hex_encoded(blender::StringRef hex_encoded);
 
   std::string hex_encoded() const;
+  float float_encoded() const;
 };
 
 struct CryptomatteLayer {
diff --git a/source/blender/blenkernel/intern/cryptomatte.cc b/source/blender/blenkernel/intern/cryptomatte.cc
index 4c1ea64a5a6..c6971267c44 100644
--- a/source/blender/blenkernel/intern/cryptomatte.cc
+++ b/source/blender/blenkernel/intern/cryptomatte.cc
@@ -124,29 +124,9 @@ uint32_t BKE_cryptomatte_asset_hash(CryptomatteSession *session, const Object *o
   return session->assets.add_ID(asset_object->id);
 }
 
-/* Convert a cryptomatte hash to a float.
- *
- * Cryptomatte hashes are stored in float textures and images. The conversion is taken from the
- * cryptomatte specification. See Floating point conversion section in
- * https://github.com/Psyop/Cryptomatte/blob/master/specification/cryptomatte_specification.pdf.
- *
- * The conversion uses as many 32 bit floating point values as possible to minimize hash
- * collisions. Unfortunately not all 32 bits can be used as NaN and Inf can be problematic.
- *
- * Note that this conversion assumes to be running on a L-endian system. */
 float BKE_cryptomatte_hash_to_float(uint32_t cryptomatte_hash)
 {
-  uint32_t mantissa = cryptomatte_hash & ((1 << 23) - 1);
-  uint32_t exponent = (cryptomatte_hash >> 23) & ((1 << 8) - 1);
-  exponent = MAX2(exponent, (uint32_t)1);
-  exponent = MIN2(exponent, (uint32_t)254);
-  exponent = exponent << 23;
-  uint32_t sign = (cryptomatte_hash >> 31);
-  sign = sign << 31;
-  uint32_t float_bits = sign | exponent | mantissa;
-  float f;
-  memcpy(&f, &float_bits, sizeof(uint32_t));
-  return f;
+  return blender::bke::cryptomatte::CryptomatteHash(cryptomatte_hash).float_encoded();
 }
 
 char *BKE_cryptomatte_entries_to_matte_id(NodeCryptomatte *node_storage)
@@ -459,6 +439,31 @@ std::string CryptomatteHash::hex_encoded() const
   return encoded.str();
 }
 
+/* Convert a cryptomatte hash to a float.
+ *
+ * Cryptomatte hashes are stored in float textures and images. The conversion is taken from the
+ * cryptomatte specification. See Floating point conversion section in
+ * https://github.com/Psyop/Cryptomatte/blob/master/specification/cryptomatte_specification.pdf.
+ *
+ * The conversion uses as many 32 bit floating point values as possible to minimize hash
+ * collisions. Unfortunately not all 32 bits can be used as NaN and Inf can be problematic.
+ *
+ * Note that this conversion assumes to be running on a L-endian system. */
+float CryptomatteHash::float_encoded() const
+{
+  uint32_t mantissa = hash & ((1 << 23) - 1);
+  uint32_t exponent = (hash >> 23) & ((1 << 8) - 1);
+  exponent = MAX2(exponent, (uint32_t)1);
+  exponent = MIN2(exponent, (uint32_t)254);
+  exponent = exponent << 23;
+  uint32_t sign = (hash >> 31);
+  sign = sign << 31;
+  uint32_t float_bits = sign | exponent | mantissa;
+  float f;
+  memcpy(&f, &float_bits, sizeof(uint32_t));
+  return f;
+}
+
 std::unique_ptr<CryptomatteLayer> CryptomatteLayer::read_from_manifest(
     blender::StringRefNull manifest)
 {



More information about the Bf-blender-cvs mailing list