Cleanup: comment line length (editors)
[blender.git] / source / blender / compositor / nodes / COM_CryptomatteNode.cpp
1 /*
2  * Copyright 2018, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor:
19  *              Lukas Stockner
20  *              Stefan Werner
21  */
22
23 #include "COM_CryptomatteNode.h"
24 #include "COM_CryptomatteOperation.h"
25 #include "COM_SetAlphaOperation.h"
26 #include "COM_ConvertOperation.h"
27 #include "BLI_string.h"
28 #include "BLI_hash_mm3.h"
29 #include "BLI_assert.h"
30 #include <iterator>
31
32 CryptomatteNode::CryptomatteNode(bNode *editorNode) : Node(editorNode)
33 {
34         /* pass */
35 }
36
37 /* This is taken from the Cryptomatte specification 1.0. */
38 static inline float hash_to_float(uint32_t hash)
39 {
40         uint32_t mantissa = hash & (( 1 << 23) - 1);
41         uint32_t exponent = (hash >> 23) & ((1 << 8) - 1);
42         exponent = max(exponent, (uint32_t) 1);
43         exponent = min(exponent, (uint32_t) 254);
44         exponent = exponent << 23;
45         uint32_t sign = (hash >> 31);
46         sign = sign << 31;
47         uint32_t float_bits = sign | exponent | mantissa;
48         float f;
49         /* Bit casting relies on equal size for both types. */
50         BLI_STATIC_ASSERT(sizeof(float) == sizeof(uint32_t), "float and uint32_t are not the same size")
51         ::memcpy(&f, &float_bits, sizeof(float));
52         return f;
53 }
54
55 void CryptomatteNode::convertToOperations(NodeConverter &converter, const CompositorContext &/*context*/) const
56 {
57         NodeInput *inputSocketImage = this->getInputSocket(0);
58         NodeOutput *outputSocketImage = this->getOutputSocket(0);
59         NodeOutput *outputSocketMatte = this->getOutputSocket(1);
60         NodeOutput *outputSocketPick = this->getOutputSocket(2);
61
62         bNode *node = this->getbNode();
63         NodeCryptomatte *cryptoMatteSettings = (NodeCryptomatte *)node->storage;
64
65         CryptomatteOperation *operation = new CryptomatteOperation(getNumberOfInputSockets() - 1);
66         if (cryptoMatteSettings) {
67                 if (cryptoMatteSettings->matte_id) {
68                         /* Split the string by commas, ignoring white space. */
69                         std::string input = cryptoMatteSettings->matte_id;
70                         std::istringstream ss(input);
71                         while (ss.good()) {
72                                 std::string token;
73                                 getline(ss, token, ',');
74                                 /* Ignore empty tokens. */
75                                 if (token.length() > 0) {
76                                         size_t first = token.find_first_not_of(' ');
77                                         size_t last = token.find_last_not_of(' ');
78                                         if (first == std::string::npos || last == std::string::npos) {
79                                                 break;
80                                         }
81                                         token = token.substr(first, (last - first + 1));
82                                         if (*token.begin() == '<' && *(--token.end()) == '>') {
83                                                 operation->addObjectIndex(atof(token.substr(1, token.length() - 2).c_str()));
84                                         }
85                                         else {
86                                                 uint32_t hash = BLI_hash_mm3((const unsigned char *)token.c_str(), token.length(), 0);
87                                                 operation->addObjectIndex(hash_to_float(hash));
88                                         }
89                                 }
90                         }
91                 }
92         }
93
94         converter.addOperation(operation);
95
96         for (int i = 0; i < getNumberOfInputSockets() - 1; ++i) {
97                 converter.mapInputSocket(this->getInputSocket(i + 1), operation->getInputSocket(i));
98         }
99
100         SeparateChannelOperation *separateOperation = new SeparateChannelOperation;
101         separateOperation->setChannel(3);
102         converter.addOperation(separateOperation);
103
104         SetAlphaOperation *operationAlpha = new SetAlphaOperation();
105         converter.addOperation(operationAlpha);
106
107         converter.addLink(operation->getOutputSocket(0), separateOperation->getInputSocket(0));
108         converter.addLink(separateOperation->getOutputSocket(0), operationAlpha->getInputSocket(1));
109
110         SetAlphaOperation *clearAlphaOperation = new SetAlphaOperation();
111         converter.addOperation(clearAlphaOperation);
112         converter.addInputValue(clearAlphaOperation->getInputSocket(1), 1.0f);
113
114         converter.addLink(operation->getOutputSocket(0), clearAlphaOperation->getInputSocket(0));
115
116         converter.mapInputSocket(inputSocketImage, operationAlpha->getInputSocket(0));
117         converter.mapOutputSocket(outputSocketMatte, separateOperation->getOutputSocket(0));
118         converter.mapOutputSocket(outputSocketImage, operationAlpha->getOutputSocket(0));
119         converter.mapOutputSocket(outputSocketPick, clearAlphaOperation->getOutputSocket(0));
120
121 }