Blender V5.3
cryptomatte_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "testing/testing.h"
6
7#include "BKE_cryptomatte.h"
8#include "BKE_cryptomatte.hh"
9#include "BKE_gtest_base.hh"
10#include "BKE_image.hh"
11
12#include "RE_pipeline.h"
13
14#include "MEM_guardedalloc.h"
15
17
19
20TEST_F(CryptomatteTest, meta_data_key)
21{
22 ASSERT_EQ("cryptomatte/c7dbf5e/key",
23 BKE_cryptomatte_meta_data_key("ViewLayer.CryptoMaterial", "key"));
24 ASSERT_EQ("cryptomatte/b990b65/𝓴𝓮𝔂",
25 BKE_cryptomatte_meta_data_key("𝖚𝖓𝖎𝖈𝖔𝖉𝖊.CryptoMaterial", "𝓴𝓮𝔂"));
26}
27
28TEST_F(CryptomatteTest, extract_layer_name)
29{
30 ASSERT_EQ("ViewLayer.CryptoMaterial",
31 BKE_cryptomatte_extract_layer_name("ViewLayer.CryptoMaterial00"));
32 ASSERT_EQ("𝖚𝖓𝖎𝖈𝖔𝖉𝖊", BKE_cryptomatte_extract_layer_name("𝖚𝖓𝖎𝖈𝖔𝖉𝖊13"));
33 ASSERT_EQ("NoTrailingSampleNumber",
34 BKE_cryptomatte_extract_layer_name("NoTrailingSampleNumber"));
35 ASSERT_EQ("W1thM1dd13Numb3rs", BKE_cryptomatte_extract_layer_name("W1thM1dd13Numb3rs09"));
36 ASSERT_EQ("", BKE_cryptomatte_extract_layer_name("0123"));
37 ASSERT_EQ("", BKE_cryptomatte_extract_layer_name(""));
38}
39
41{
43 ASSERT_EQ("{}", layer.manifest());
44
45 layer.add_hash("Object", 123);
46 ASSERT_EQ("{\"Object\":\"0000007b\"}", layer.manifest());
47
48 layer.add_hash("Object2", 123245678);
49 ASSERT_EQ("{\"Object\":\"0000007b\",\"Object2\":\"0758946e\"}", layer.manifest());
50}
51
52TEST_F(CryptomatteTest, layer_quoted)
53{
55 layer.add_hash("\"Object\"", 123);
56 ASSERT_EQ("{\"\\\"Object\\\"\":\"0000007b\"}", layer.manifest());
57}
58
59static void test_cryptomatte_manifest(std::string expected, std::string manifest)
60{
61 EXPECT_EQ(expected,
63}
64
65TEST_F(CryptomatteTest, layer_from_manifest)
66{
67 test_cryptomatte_manifest("{}", "{}");
68 test_cryptomatte_manifest(R"({"Object":"12345678"})", R"({"Object": "12345678"})");
69 test_cryptomatte_manifest(R"({"Object":"12345678","Object2":"87654321"})",
70 R"({"Object":"12345678","Object2":"87654321"})");
71 test_cryptomatte_manifest(R"({"Object":"12345678","Object2":"87654321"})",
72 R"( { "Object" : "12345678" , "Object2" : "87654321" } )");
73 test_cryptomatte_manifest(R"({"Object\"01\"":"12345678"})", R"({"Object\"01\"": "12345678"})");
75 R"({"Object\"01\"":"12345678","Object":"12345678","Object2":"87654321"})",
76 R"({"Object\"01\"":"12345678","Object":"12345678", "Object2":"87654321"})");
77}
78
79TEST_F(CryptomatteTest, extract_layer_hash_from_metadata_key)
80{
81 EXPECT_EQ("eb4c67b",
83 "cryptomatte/eb4c67b/conversion"));
84 EXPECT_EQ("qwerty",
86 "cryptomatte/qwerty/name"));
87 /* Check if undefined behaviors are handled. */
89 "",
93}
94
96 const char *propname,
97 char *propvalue,
98 int /*propvalue_maxncpy*/)
99{
100 StringRefNull prop_name(propname);
101 if (!prop_name.startswith("cryptomatte/")) {
102 return;
103 }
104
105 if (prop_name == "cryptomatte/67da54b/name") {
106 EXPECT_STREQ("layer1", propvalue);
107 }
108 else if (prop_name == "cryptomatte/67da54b/hash") {
109 EXPECT_STREQ("MurmurHash3_32", propvalue);
110 }
111 else if (prop_name == "cryptomatte/67da54b/conversion") {
112 EXPECT_STREQ("uint32_to_float32", propvalue);
113 }
114 else if (prop_name == "cryptomatte/67da54b/manifest") {
115 EXPECT_STREQ(R"({"Object":"12345678"})", propvalue);
116 }
117
118 else if (prop_name == "cryptomatte/79b2306/name") {
119 EXPECT_STREQ("layer2", propvalue);
120 }
121 else if (prop_name == "cryptomatte/79b2306/hash") {
122 EXPECT_STREQ("MurmurHash3_32", propvalue);
123 }
124 else if (prop_name == "cryptomatte/79b2306/conversion") {
125 EXPECT_STREQ("uint32_to_float32", propvalue);
126 }
127 else if (prop_name == "cryptomatte/79b2306/manifest") {
128 EXPECT_STREQ(R"({"Object2":"87654321"})", propvalue);
129 }
130
131 else {
132 EXPECT_EQ("Unhandled", std::string(propname) + ": " + propvalue);
133 }
134}
135
136TEST_F(CryptomatteTest, session_from_stamp_data)
137{
138 /* Create CryptomatteSession from stamp data. */
139 RenderResult *render_result = MEM_new<RenderResult>(__func__);
140 BKE_render_result_stamp_data(render_result, "cryptomatte/qwerty/name", "layer1");
142 render_result, "cryptomatte/qwerty/manifest", R"({"Object":"12345678"})");
143 BKE_render_result_stamp_data(render_result, "cryptomatte/uiop/name", "layer2");
145 render_result, "cryptomatte/uiop/manifest", R"({"Object2":"87654321"})");
147 EXPECT_NE(session.get(), nullptr);
148 RE_FreeRenderResult(render_result);
149
150 /* Create StampData from CryptomatteSession. */
151 RenderResult *render_result2 = MEM_new<RenderResult>(__func__);
152 BKE_cryptomatte_store_metadata(session.get(), render_result2);
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
165TEST_F(CryptomatteTest, parsing_malformed_manifests)
166{
167 /* Manifest from `multilayer.exr` in the cryptomatte git-repository. */
169 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"})",
170 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)");
171}
172} // namespace blender::bke::cryptomatte::tests
Read Blender memory allocation and freeing.
constexpr bool startswith(StringRef prefix) const
#define layer
#define EXPECT_NE(result, expect)
static void test_cryptomatte_manifest(std::string expected, std::string manifest)
static void validate_cryptomatte_session_from_stamp_data(void *, const char *propname, char *propvalue, int)
TEST_F(CryptomatteTest, meta_data_key)
StringRef BKE_cryptomatte_extract_layer_name(StringRef render_pass_name)
std::unique_ptr< CryptomatteSession, CryptomatteSessionDeleter > CryptomatteSessionPtr
std::string BKE_cryptomatte_meta_data_key(StringRef layer_name, StringRefNull key_name)
void BKE_render_result_stamp_data(RenderResult *rr, const char *key, const char *value)
void BKE_cryptomatte_store_metadata(const struct CryptomatteSession *session, struct RenderResult *render_result)
void RE_FreeRenderResult(RenderResult *rr)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BKE_stamp_info_callback(void *data, StampData *stamp_data, StampCallback callback, bool noskip)
struct CryptomatteSession * BKE_cryptomatte_init_from_render_result(const struct RenderResult *render_result)
struct StampData * stamp_data
static std::unique_ptr< CryptomatteLayer > read_from_manifest(StringRefNull manifest)
static StringRef extract_layer_hash(StringRefNull key)