Blender  V3.3
cryptomatte_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Blender Foundation. */
3 #include "testing/testing.h"
4 
5 #include "BKE_cryptomatte.h"
6 #include "BKE_cryptomatte.hh"
7 #include "BKE_image.h"
8 
9 #include "RE_pipeline.h"
10 
11 #include "MEM_guardedalloc.h"
12 
14 
15 TEST(cryptomatte, meta_data_key)
16 {
17  ASSERT_EQ("cryptomatte/c7dbf5e/key",
18  BKE_cryptomatte_meta_data_key("ViewLayer.CryptoMaterial", "key"));
19  ASSERT_EQ("cryptomatte/b990b65/𝓴𝓮𝔂",
20  BKE_cryptomatte_meta_data_key("𝖚𝖓𝖎𝖈𝖔𝖉𝖊.CryptoMaterial", "𝓴𝓮𝔂"));
21 }
22 
23 TEST(cryptomatte, extract_layer_name)
24 {
25  ASSERT_EQ("ViewLayer.CryptoMaterial",
26  BKE_cryptomatte_extract_layer_name("ViewLayer.CryptoMaterial00"));
27  ASSERT_EQ("𝖚𝖓𝖎𝖈𝖔𝖉𝖊", BKE_cryptomatte_extract_layer_name("𝖚𝖓𝖎𝖈𝖔𝖉𝖊13"));
28  ASSERT_EQ("NoTrailingSampleNumber",
29  BKE_cryptomatte_extract_layer_name("NoTrailingSampleNumber"));
30  ASSERT_EQ("W1thM1dd13Numb3rs", BKE_cryptomatte_extract_layer_name("W1thM1dd13Numb3rs09"));
31  ASSERT_EQ("", BKE_cryptomatte_extract_layer_name("0123"));
32  ASSERT_EQ("", BKE_cryptomatte_extract_layer_name(""));
33 }
34 
35 TEST(cryptomatte, layer)
36 {
38  ASSERT_EQ("{}", layer.manifest());
39 
40  layer.add_hash("Object", 123);
41  ASSERT_EQ("{\"Object\":\"0000007b\"}", layer.manifest());
42 
43  layer.add_hash("Object2", 123245678);
44  ASSERT_EQ("{\"Object\":\"0000007b\",\"Object2\":\"0758946e\"}", layer.manifest());
45 }
46 
47 TEST(cryptomatte, layer_quoted)
48 {
50  layer.add_hash("\"Object\"", 123);
51  ASSERT_EQ("{\"\\\"Object\\\"\":\"0000007b\"}", layer.manifest());
52 }
53 
54 static void test_cryptomatte_manifest(std::string expected, std::string manifest)
55 {
56  EXPECT_EQ(expected,
58 }
59 
60 TEST(cryptomatte, layer_from_manifest)
61 {
62  test_cryptomatte_manifest("{}", "{}");
63  test_cryptomatte_manifest(R"({"Object":"12345678"})", R"({"Object": "12345678"})");
64  test_cryptomatte_manifest(R"({"Object":"12345678","Object2":"87654321"})",
65  R"({"Object":"12345678","Object2":"87654321"})");
66  test_cryptomatte_manifest(R"({"Object":"12345678","Object2":"87654321"})",
67  R"( { "Object" : "12345678" , "Object2" : "87654321" } )");
68  test_cryptomatte_manifest(R"({"Object\"01\"":"12345678"})", R"({"Object\"01\"": "12345678"})");
70  R"({"Object\"01\"":"12345678","Object":"12345678","Object2":"87654321"})",
71  R"({"Object\"01\"":"12345678","Object":"12345678", "Object2":"87654321"})");
72 }
73 
74 TEST(cryptomatte, extract_layer_hash_from_metadata_key)
75 {
76  EXPECT_EQ("eb4c67b",
78  "cryptomatte/eb4c67b/conversion"));
79  EXPECT_EQ("qwerty",
81  "cryptomatte/qwerty/name"));
82  /* Check if undefined behaviors are handled. */
83  EXPECT_EQ("",
85  "cryptomatte/name"));
86  EXPECT_EQ("",
88  "cryptomatte/"));
89 }
90 
92  const char *propname,
93  char *propvalue,
94  int UNUSED(len))
95 {
96  blender::StringRefNull prop_name(propname);
97  if (!prop_name.startswith("cryptomatte/")) {
98  return;
99  }
100 
101  if (prop_name == "cryptomatte/87f095e/name") {
102  EXPECT_STREQ("viewlayername.layer1", propvalue);
103  }
104  else if (prop_name == "cryptomatte/87f095e/hash") {
105  EXPECT_STREQ("MurmurHash3_32", propvalue);
106  }
107  else if (prop_name == "cryptomatte/87f095e/conversion") {
108  EXPECT_STREQ("uint32_to_float32", propvalue);
109  }
110  else if (prop_name == "cryptomatte/87f095e/manifest") {
111  EXPECT_STREQ(R"({"Object":"12345678"})", propvalue);
112  }
113 
114  else if (prop_name == "cryptomatte/c42daa7/name") {
115  EXPECT_STREQ("viewlayername.layer2", propvalue);
116  }
117  else if (prop_name == "cryptomatte/c42daa7/hash") {
118  EXPECT_STREQ("MurmurHash3_32", propvalue);
119  }
120  else if (prop_name == "cryptomatte/c42daa7/conversion") {
121  EXPECT_STREQ("uint32_to_float32", propvalue);
122  }
123  else if (prop_name == "cryptomatte/c42daa7/manifest") {
124  EXPECT_STREQ(R"({"Object2":"87654321"})", propvalue);
125  }
126 
127  else {
128  EXPECT_EQ("Unhandled", std::string(propname) + ": " + propvalue);
129  }
130 }
131 
132 TEST(cryptomatte, session_from_stamp_data)
133 {
134  /* Create CryptomatteSession from stamp data. */
135  RenderResult *render_result = static_cast<RenderResult *>(
136  MEM_callocN(sizeof(RenderResult), __func__));
137  BKE_render_result_stamp_data(render_result, "cryptomatte/qwerty/name", "layer1");
139  render_result, "cryptomatte/qwerty/manifest", R"({"Object":"12345678"})");
140  BKE_render_result_stamp_data(render_result, "cryptomatte/uiop/name", "layer2");
142  render_result, "cryptomatte/uiop/manifest", R"({"Object2":"87654321"})");
144  EXPECT_NE(session.get(), nullptr);
145  RE_FreeRenderResult(render_result);
146 
147  /* Create StampData from CryptomatteSession. */
148  ViewLayer view_layer;
149  BLI_strncpy(view_layer.name, "viewlayername", sizeof(view_layer.name));
150  RenderResult *render_result2 = static_cast<RenderResult *>(
151  MEM_callocN(sizeof(RenderResult), __func__));
152  BKE_cryptomatte_store_metadata(session.get(), render_result2, &view_layer);
153 
154  /* Validate StampData. */
156  nullptr, render_result2->stamp_data, validate_cryptomatte_session_from_stamp_data, false);
157 
158  RE_FreeRenderResult(render_result2);
159 }
160 
164 TEST(cryptomatte, parsing_malformed_manifests)
165 {
166  /* Manifest from `multilayer.exr` in the cryptomatte git-repository. */
168  R"({"/obj/instance1:instances:0":"0d54c6cc","/obj/instance1:instances:1":"293d9340","/obj/instance1:instances:110":"ccb9e1f2","/obj/instance1:instances:111":"f8dd3a48","/obj/instance1:instances:112":"a99e07a8","/obj/instance1:instances:113":"e75599a4","/obj/instance1:instances:114":"794200f3","/obj/instance1:instances:115":"2a3a1728","/obj/instance1:instances:116":"478544a1","/obj/instance1:instances:117":"b2bd969a","/obj/instance1:instances:10":"3a0c8681","/obj/instance1:instances:11":"01e5970d","/obj/box:polygons:1":"9d416418","/obj/instance1:instances:100":"2dcd2966","/obj/instance1:instances:101":"9331cd82","/obj/instance1:instances:102":"df50fccb","/obj/instance1:instances:103":"97f8590d","/obj/instance1:instances:104":"bbcd220d","/obj/instance1:instances:105":"4ae06139","/obj/instance1:instances:106":"8873d5ea","/obj/instance1:instances:107":"39d8af8d","/obj/instance1:instances:108":"bb11bd4e","/obj/instance1:instances:109":"a32bba35"})",
169  R"({"\/obj\/box:polygons:1":"9d416418","\/obj\/instance1:instances:0":"0d54c6cc","\/obj\/instance1:instances:1":"293d9340","\/obj\/instance1:instances:10":"3a0c8681","\/obj\/instance1:instances:100":"2dcd2966","\/obj\/instance1:instances:101":"9331cd82","\/obj\/instance1:instances:102":"df50fccb","\/obj\/instance1:instances:103":"97f8590d","\/obj\/instance1:instances:104":"bbcd220d","\/obj\/instance1:instances:105":"4ae06139","\/obj\/instance1:instances:106":"8873d5ea","\/obj\/instance1:instances:107":"39d8af8d","\/obj\/instance1:instances:108":"bb11bd4e","\/obj\/instance1:instances:109":"a32bba35","\/obj\/instance1:instances:11":"01e5970d","\/obj\/instance1:instances:110":"ccb9e1f2","\/obj\/instance1:instances:111":"f8dd3a48","\/obj\/instance1:instances:112":"a99e07a8","\/obj\/instance1:instances:113":"e75599a4","\/obj\/instance1:instances:114":"794200f3","\/obj\/instance1:instances:115":"2a3a1728","\/obj\/instance1:instances:116":"478544a1","\/obj\/instance1:instances:117":"b2bd969a","\/obj\/instance1:instance)");
170 }
171 } // namespace blender::bke::cryptomatte::tests
struct CryptomatteSession * BKE_cryptomatte_init_from_render_result(const struct RenderResult *render_result)
Definition: cryptomatte.cc:132
void BKE_cryptomatte_store_metadata(const struct CryptomatteSession *session, struct RenderResult *render_result, const ViewLayer *view_layer)
Definition: cryptomatte.cc:308
void BKE_render_result_stamp_data(struct RenderResult *rr, const char *key, const char *value)
void BKE_stamp_info_callback(void *data, struct StampData *stamp_data, StampCallback callback, bool noskip)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define UNUSED(x)
Read Guarded memory(de)allocation.
constexpr bool startswith(StringRef prefix) const
int len
Definition: draw_manager.c:108
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
static void test_cryptomatte_manifest(std::string expected, std::string manifest)
static void validate_cryptomatte_session_from_stamp_data(void *UNUSED(data), const char *propname, char *propvalue, int UNUSED(len))
TEST(cryptomatte, meta_data_key)
StringRef BKE_cryptomatte_extract_layer_name(const StringRef render_pass_name)
Definition: cryptomatte.cc:475
std::string BKE_cryptomatte_meta_data_key(const StringRef layer_name, const StringRefNull key_name)
Definition: cryptomatte.cc:470
std::unique_ptr< CryptomatteSession, CryptomatteSessionDeleter > CryptomatteSessionPtr
void RE_FreeRenderResult(RenderResult *rr)
Definition: pipeline.c:233
struct StampData * stamp_data
Definition: RE_pipeline.h:141
char name[64]
void add_hash(blender::StringRef name, CryptomatteHash cryptomatte_hash)
Definition: cryptomatte.cc:541
static std::unique_ptr< CryptomatteLayer > read_from_manifest(blender::StringRefNull manifest)
Definition: cryptomatte.cc:522
static blender::StringRef extract_layer_hash(blender::StringRefNull key)
Definition: cryptomatte.cc:562