Blender  V2.93
cryptomatte_test.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2021 by Blender Foundation.
17  */
18 #include "testing/testing.h"
19 
20 #include "BKE_cryptomatte.h"
21 #include "BKE_cryptomatte.hh"
22 #include "BKE_image.h"
23 
24 #include "RE_pipeline.h"
25 
26 #include "MEM_guardedalloc.h"
27 
29 
30 TEST(cryptomatte, meta_data_key)
31 {
32  ASSERT_EQ("cryptomatte/c7dbf5e/key",
33  BKE_cryptomatte_meta_data_key("ViewLayer.CryptoMaterial", "key"));
34  ASSERT_EQ("cryptomatte/b990b65/𝓴𝓮𝔂",
35  BKE_cryptomatte_meta_data_key("𝖚𝖓𝖎𝖈𝖔𝖉𝖊.CryptoMaterial", "𝓴𝓮𝔂"));
36 }
37 
38 TEST(cryptomatte, extract_layer_name)
39 {
40  ASSERT_EQ("ViewLayer.CryptoMaterial",
41  BKE_cryptomatte_extract_layer_name("ViewLayer.CryptoMaterial00"));
42  ASSERT_EQ("𝖚𝖓𝖎𝖈𝖔𝖉𝖊", BKE_cryptomatte_extract_layer_name("𝖚𝖓𝖎𝖈𝖔𝖉𝖊13"));
43  ASSERT_EQ("NoTrailingSampleNumber",
44  BKE_cryptomatte_extract_layer_name("NoTrailingSampleNumber"));
45  ASSERT_EQ("W1thM1dd13Numb3rs", BKE_cryptomatte_extract_layer_name("W1thM1dd13Numb3rs09"));
46  ASSERT_EQ("", BKE_cryptomatte_extract_layer_name("0123"));
47  ASSERT_EQ("", BKE_cryptomatte_extract_layer_name(""));
48 }
49 
50 TEST(cryptomatte, layer)
51 {
53  ASSERT_EQ("{}", layer.manifest());
54 
55  layer.add_hash("Object", 123);
56  ASSERT_EQ("{\"Object\":\"0000007b\"}", layer.manifest());
57 
58  layer.add_hash("Object2", 123245678);
59  ASSERT_EQ("{\"Object\":\"0000007b\",\"Object2\":\"0758946e\"}", layer.manifest());
60 }
61 
62 TEST(cryptomatte, layer_quoted)
63 {
65  layer.add_hash("\"Object\"", 123);
66  ASSERT_EQ("{\"\\\"Object\\\"\":\"0000007b\"}", layer.manifest());
67 }
68 
69 static void test_cryptomatte_manifest(std::string expected, std::string manifest)
70 {
71  EXPECT_EQ(expected,
73 }
74 
75 TEST(cryptomatte, layer_from_manifest)
76 {
77  test_cryptomatte_manifest("{}", "{}");
78  test_cryptomatte_manifest(R"({"Object":"12345678"})", R"({"Object": "12345678"})");
79  test_cryptomatte_manifest(R"({"Object":"12345678","Object2":"87654321"})",
80  R"({"Object":"12345678","Object2":"87654321"})");
81  test_cryptomatte_manifest(R"({"Object":"12345678","Object2":"87654321"})",
82  R"( { "Object" : "12345678" , "Object2" : "87654321" } )");
83  test_cryptomatte_manifest(R"({"Object\"01\"":"12345678"})", R"({"Object\"01\"": "12345678"})");
85  R"({"Object\"01\"":"12345678","Object":"12345678","Object2":"87654321"})",
86  R"({"Object\"01\"":"12345678","Object":"12345678", "Object2":"87654321"})");
87 }
88 
89 TEST(cryptomatte, extract_layer_hash_from_metadata_key)
90 {
91  EXPECT_EQ("eb4c67b",
93  "cryptomatte/eb4c67b/conversion"));
94  EXPECT_EQ("qwerty",
96  "cryptomatte/qwerty/name"));
97  /* Check if undefined behaviors are handled. */
98  EXPECT_EQ("",
100  "cryptomatte/name"));
101  EXPECT_EQ("",
103  "cryptomatte/"));
104 }
105 
107  const char *propname,
108  char *propvalue,
109  int UNUSED(len))
110 {
111  blender::StringRefNull prop_name(propname);
112  if (!prop_name.startswith("cryptomatte/")) {
113  return;
114  }
115 
116  if (prop_name == "cryptomatte/87f095e/name") {
117  EXPECT_STREQ("viewlayername.layer1", propvalue);
118  }
119  else if (prop_name == "cryptomatte/87f095e/hash") {
120  EXPECT_STREQ("MurmurHash3_32", propvalue);
121  }
122  else if (prop_name == "cryptomatte/87f095e/conversion") {
123  EXPECT_STREQ("uint32_to_float32", propvalue);
124  }
125  else if (prop_name == "cryptomatte/87f095e/manifest") {
126  EXPECT_STREQ(R"({"Object":"12345678"})", propvalue);
127  }
128 
129  else if (prop_name == "cryptomatte/c42daa7/name") {
130  EXPECT_STREQ("viewlayername.layer2", propvalue);
131  }
132  else if (prop_name == "cryptomatte/c42daa7/hash") {
133  EXPECT_STREQ("MurmurHash3_32", propvalue);
134  }
135  else if (prop_name == "cryptomatte/c42daa7/conversion") {
136  EXPECT_STREQ("uint32_to_float32", propvalue);
137  }
138  else if (prop_name == "cryptomatte/c42daa7/manifest") {
139  EXPECT_STREQ(R"({"Object2":"87654321"})", propvalue);
140  }
141 
142  else {
143  EXPECT_EQ("Unhandled", std::string(propname) + ": " + propvalue);
144  }
145 }
146 
147 TEST(cryptomatte, session_from_stamp_data)
148 {
149  /* Create CryptomatteSession from stamp data. */
150  RenderResult *render_result = static_cast<RenderResult *>(
151  MEM_callocN(sizeof(RenderResult), __func__));
152  BKE_render_result_stamp_data(render_result, "cryptomatte/qwerty/name", "layer1");
154  render_result, "cryptomatte/qwerty/manifest", R"({"Object":"12345678"})");
155  BKE_render_result_stamp_data(render_result, "cryptomatte/uiop/name", "layer2");
157  render_result, "cryptomatte/uiop/manifest", R"({"Object2":"87654321"})");
159  EXPECT_NE(session.get(), nullptr);
160  RE_FreeRenderResult(render_result);
161 
162  /* Create StampData from CryptomatteSession. */
163  ViewLayer view_layer;
164  BLI_strncpy(view_layer.name, "viewlayername", sizeof(view_layer.name));
165  RenderResult *render_result2 = static_cast<RenderResult *>(
166  MEM_callocN(sizeof(RenderResult), __func__));
167  BKE_cryptomatte_store_metadata(session.get(), render_result2, &view_layer);
168 
169  /* Validate StampData. */
171  nullptr, render_result2->stamp_data, validate_cryptomatte_session_from_stamp_data, false);
172 
173  RE_FreeRenderResult(render_result2);
174 }
175 
179 TEST(cryptomatte, parsing_malformed_manifests)
180 {
181  /* Manifest from multilayer.exr in the cryptomatte git-repository. */
183  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"})",
184  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)");
185 }
186 } // namespace blender::bke::cryptomatte::tests
struct CryptomatteSession * BKE_cryptomatte_init_from_render_result(const struct RenderResult *render_result)
Definition: cryptomatte.cc:148
void BKE_cryptomatte_store_metadata(const struct CryptomatteSession *session, struct RenderResult *render_result, const ViewLayer *view_layer)
Definition: cryptomatte.cc:325
void BKE_render_result_stamp_data(struct RenderResult *rr, const char *key, const char *value)
Definition: image.c:2727
void BKE_stamp_info_callback(void *data, struct StampData *stamp_data, StampCallback callback, bool noskip)
Definition: image.c:2686
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
#define UNUSED(x)
Read Guarded memory(de)allocation.
constexpr bool startswith(StringRef prefix) const
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
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:496
std::string BKE_cryptomatte_meta_data_key(const StringRef layer_name, const StringRefNull key_name)
Definition: cryptomatte.cc:487
std::unique_ptr< CryptomatteSession, CryptomatteSessionDeleter > CryptomatteSessionPtr
void RE_FreeRenderResult(RenderResult *rr)
Definition: pipeline.c:265
struct StampData * stamp_data
Definition: RE_pipeline.h:157
char name[64]
void add_hash(blender::StringRef name, CryptomatteHash cryptomatte_hash)
Definition: cryptomatte.cc:572
static std::unique_ptr< CryptomatteLayer > read_from_manifest(blender::StringRefNull manifest)
Definition: cryptomatte.cc:553
static blender::StringRef extract_layer_hash(blender::StringRefNull key)
Definition: cryptomatte.cc:593
uint len