[Bf-blender-cvs] [acd8c8d7a32] temp_cryptomatte: Cryptomatte: Improved robustness of matte string parsing

Stefan Werner noreply at git.blender.org
Mon Nov 20 23:15:00 CET 2017


Commit: acd8c8d7a327a756f0a33209ee74f8ff3488bd33
Author: Stefan Werner
Date:   Mon Nov 20 22:46:44 2017 +0100
Branches: temp_cryptomatte
https://developer.blender.org/rBacd8c8d7a327a756f0a33209ee74f8ff3488bd33

Cryptomatte: Improved robustness of matte string parsing

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

M	source/blender/compositor/nodes/COM_CryptomatteNode.cpp
M	source/blender/nodes/composite/nodes/node_composite_cryptomatte.c

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

diff --git a/source/blender/compositor/nodes/COM_CryptomatteNode.cpp b/source/blender/compositor/nodes/COM_CryptomatteNode.cpp
index 8ac31e20d54..43d45b599be 100644
--- a/source/blender/compositor/nodes/COM_CryptomatteNode.cpp
+++ b/source/blender/compositor/nodes/COM_CryptomatteNode.cpp
@@ -190,21 +190,27 @@ void CryptomatteNode::convertToOperations(NodeConverter &converter, const Compos
 	if (cryptoMatteSettings) {
 		std::string input = cryptoMatteSettings->matte_id;
 		if (!input.empty()) {
-			// split the string by commas, ignoring white space
+			/* Split the string by commas, ignoring white space. */
 			std::istringstream ss(input);
 			while (ss.good())
 			{
 				std::string token;
 				getline(ss, token, ',');
-				size_t first = token.find_first_not_of(' ');
-				size_t last = token.find_last_not_of(' ');
-				token = token.substr(first, (last - first + 1));
-				if (*token.begin() == '<' && *(--token.end()) == '>')
-					operation->addObjectIndex(atof(token.substr(1, token.length() - 2).c_str()));
-				else {
-					uint32_t hash = 0;
-					MurmurHash3_x86_32(token.c_str(), token.length(), 0, &hash);
-					operation->addObjectIndex(hash_to_float(hash));
+				/* Ignore empty tokens. */
+				if (token.length() > 0) {
+					size_t first = token.find_first_not_of(' ');
+					size_t last = token.find_last_not_of(' ');
+					if (first == std::string::npos || last == std::string::npos) {
+						break;
+					}
+					token = token.substr(first, (last - first + 1));
+					if (*token.begin() == '<' && *(--token.end()) == '>')
+						operation->addObjectIndex(atof(token.substr(1, token.length() - 2).c_str()));
+					else {
+						uint32_t hash = 0;
+						MurmurHash3_x86_32(token.c_str(), token.length(), 0, &hash);
+						operation->addObjectIndex(hash_to_float(hash));
+					}
 				}
 			}
 		}
diff --git a/source/blender/nodes/composite/nodes/node_composite_cryptomatte.c b/source/blender/nodes/composite/nodes/node_composite_cryptomatte.c
index 49149a2bc4f..3ff078ad6df 100644
--- a/source/blender/nodes/composite/nodes/node_composite_cryptomatte.c
+++ b/source/blender/nodes/composite/nodes/node_composite_cryptomatte.c
@@ -251,7 +251,7 @@ static void cryptomatte_remove(NodeCryptomatte*n, float f)
 		return;
 	}
 
-	/* This will be the new srting without the removed key. */
+	/* This will be the new string without the removed key. */
 	DynStr *new_matte = BLI_dynstr_new();
 	if (!new_matte) {
 		return;
@@ -265,23 +265,28 @@ static void cryptomatte_remove(NodeCryptomatte*n, float f)
 	size_t start = 0;
 	const size_t end = strlen(n->matte_id);
 	size_t token_len = 0;
+	bool is_first = true;
 	while (start < end) {
 		bool skip = false;
-		/* Ignore leading whitespace. */
-		while(start < end && n->matte_id[start] == ' ') {
+		/* Ignore leading whitespace or commas. */
+		while(start < end &&
+		      (n->matte_id[start] == ' ') || (n->matte_id[start] == ',')) {
 			++start;
 		}
 
 		/* Find the next seprator. */
-		char* token_end = strchr(n->matte_id+start, ',');
+		char* token_end = strchr(n->matte_id+start+1, ',');
 		if (token_end == NULL || token_end == n->matte_id+start) {
 			token_end = n->matte_id+end;
 		}
 		/* Be aware that token_len still contains any trailing white space. */
 		token_len = token_end - (n->matte_id + start);
 
+		if (token_len == 1) {
+			skip = true;
+		}
 		/* If this has a leading bracket, assume a raw floating point number and look for the closing bracket. */
-		if (n->matte_id[start] == '<') {
+		else if (n->matte_id[start] == '<') {
 			if(strncmp(n->matte_id+start, number, strlen(number)) == 0) {
 				/* This number is already there, so skip it. */
 				skip = true;
@@ -301,7 +306,13 @@ static void cryptomatte_remove(NodeCryptomatte*n, float f)
 			}
 		}
 		if (!skip) {
-			BLI_dynstr_nappend(new_matte, n->matte_id+start, token_len+1);
+			if (is_first) {
+				is_first = false;
+			}
+			else {
+				BLI_dynstr_append(new_matte, ", ");
+			}
+			BLI_dynstr_nappend(new_matte, n->matte_id+start, token_len);
 		}
 		start += token_len+1;
 	}



More information about the Bf-blender-cvs mailing list