Skip to content

Commit

Permalink
Merge pull request #8 from MrMabulous/improve_logging
Browse files Browse the repository at this point in the history
Improve loggig.
  • Loading branch information
MrMabulous authored Sep 2, 2020
2 parents 8e7e188 + 8ca6e75 commit ef2abe0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
25 changes: 16 additions & 9 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ readEXR(const char fileName[],
height = dw.max.y - dw.min.y + 1;
const ChannelList channels = header.channels();
const bool hasAlpha = channels.findChannel("A") != nullptr;
bool readAlpha = hasAlpha && readAlphaIfPresent;
readAlpha = hasAlpha && readAlphaIfPresent;

const int stride = readAlpha ? 4 : 3;

Expand Down Expand Up @@ -290,7 +290,7 @@ int main( int argc, char *argv[], char *envp[] ) {

Parser p(expression);
if(p.isValid()) {
cout << p.getRoot()->toString() << "\n";
cout << p.getRoot()->toString("") << "\n";
vector<string> inputFilePaths;
string outputFilePath;
std::function<void(const Parser::Node* node)> collectFunc;
Expand Down Expand Up @@ -467,16 +467,21 @@ int main( int argc, char *argv[], char *envp[] ) {
}
}
else if (leftResult.type == CalcResult::ARRAY && rightResult.type == CalcResult::ARRAY) {
res.type = CalcResult::ARRAY;
res.array.resizeErase(leftResult.array.height(), leftResult.array.width());
if (leftResult.array.width() != rightResult.array.width() || leftResult.array.height() != rightResult.array.height()) {
cout << "error: resolution mismatch.\n";
if (leftResult.hasAlpha != rightResult.hasAlpha) {
cout << "error: in " << node->toString(patch) << " \n";
cout << "Alpha mismatch.\n";
cout << "Some inputs have Alpha channels, others do not. Consider using -rgb argument to ignore alpha channels altogether.\n";
assert(false);
}
if (leftResult.hasAlpha != rightResult.hasAlpha) {
cout << "error: some inputs have Alpha channels, others do not. Consider using -rgb argument.\n";
if (leftResult.array.width() != rightResult.array.width() || leftResult.array.height() != rightResult.array.height()) {
const int stride = leftResult.hasAlpha ? 4 : 3;
cout << "error: in " << node->toString(patch) << " \n";
cout << "resolution mismatch. Left is " << leftResult.array.width()/stride << "x" << leftResult.array.height()
<< " and right is " << rightResult.array.width()/stride << "x" << rightResult.array.height() << "\n";
assert(false);
}
res.type = CalcResult::ARRAY;
res.array.resizeErase(leftResult.array.height(), leftResult.array.width());
res.hasAlpha = leftResult.hasAlpha;
CALCRESULT(leftResult.array[y][x], node->type, rightResult.array[y][x]);
}
Expand Down Expand Up @@ -531,12 +536,13 @@ int main( int argc, char *argv[], char *envp[] ) {
writeEXR(targetFileName.c_str(), res.array[0], res.array.width() / stride, res.array.height(), res.hasAlpha, compression);
});
if(verify) {
cout << "verifying written images...";
cout << "verifying written images...\n";
Array2D<float> pixels;
int width, height;
bool verificationSuccessful = true;
for(int i=0; i<outputFilePaths.size(); i++) {
string pathString = outputFilePaths[i];
cout << pathString << " \r";
filesystem::path filePath(pathString);
if (!filesystem::exists(filePath)) {
cout << "error: " << filePath.string() << " has not been written.\n";
Expand All @@ -556,6 +562,7 @@ int main( int argc, char *argv[], char *envp[] ) {
}
}
}
cout << "\n";
if(verificationSuccessful) {
cout << "verification succeeded, " << outputFilePaths.size() <<" files have been written.\n";
} else {
Expand Down
39 changes: 26 additions & 13 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,44 @@

using namespace std;

string Parser::Node::toString() const {
string Parser::Node::toString(const string& patch) const {
switch(type) {
case Node::INVALID:
return "INVALID";
case Node::INPUTFILEPATH:
return path;
case Node::OUTPUTFILEPATH:
return path;
if(!patch.empty()) {
string res = path;
size_t wildCardLength = 1;
size_t wildCardPos = res.find('#');
if(wildCardPos == string::npos) {
wildCardLength = std::count(res.begin(), res.end(), '?');
wildCardPos = res.find('?');
}
if(wildCardPos != string::npos) {
res.replace(wildCardPos, wildCardLength, patch);
}
return res;
} else {
return path;
}
case Node::CONSTANT:
return to_string(constant);
case Node::ADD:
return string("(") + (left?left->toString():"null") + " + " +
(right?right->toString():"null") + ")";
return string("(") + (left?left->toString(patch):"null") + " + " +
(right?right->toString(patch):"null") + ")";
case Node::SUB:
return string("(") + (left?left->toString():"null") + " - " +
(right?right->toString():"null") + ")";
return string("(") + (left?left->toString(patch):"null") + " - " +
(right?right->toString(patch):"null") + ")";
case Node::MULT:
return string("(") + (left?left->toString():"null") + " * " +
(right?right->toString():"null") + ")";
return string("(") + (left?left->toString(patch):"null") + " * " +
(right?right->toString(patch):"null") + ")";
case Node::DIV:
return string("(") + (left?left->toString():"null") + " / " +
(right?right->toString():"null") + ")";
return string("(") + (left?left->toString(patch):"null") + " / " +
(right?right->toString(patch):"null") + ")";
case Node::ASSIGN:
return (left?left->toString():"null") + " = " +
(right?right->toString():"null");
return (left?left->toString(patch):"null") + " = " +
(right?right->toString(patch):"null");
default:
return string("NOT IMPLEMENTED:") + to_string(type) + "this:" + to_string(size_t(this));
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Parser {
enum NodeType { INVALID, INPUTFILEPATH, OUTPUTFILEPATH, CONSTANT, ADD, SUB, MULT, DIV, ASSIGN };
Node() : type(INVALID), path(""), constant(0.0f), left(nullptr), right(nullptr) {}
~Node() { if (left) delete left; if (right) delete right; }
std::string toString() const;
std::string toString(const std::string& patch = "") const;
void evaluate(std::function<void(const Parser::Node* node)>& lambda) const;
NodeType type;
std::string path;
Expand Down

0 comments on commit ef2abe0

Please sign in to comment.