Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/OpenColorIO/ViewingRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,118 @@ std::ostream & operator<< (std::ostream & os, const ViewingRules & vr)
return os;
}

bool viewingRulesAreEqual(const ConstViewingRulesRcPtr & r1,
size_t r1Idx,
const ConstViewingRulesRcPtr & r2,
size_t r2Idx)
{
// NB: No need to compare the name of the rules, that should be done in the caller.

// Compare color space tokens, handling the case where they may be in a different order.

if (r1->getNumColorSpaces(r1Idx) != r2->getNumColorSpaces(r2Idx))
{
return false;
}

TokensManager r1ColorSpaces;
for (size_t m = 0; m < r1->getNumColorSpaces(r1Idx); m++)
{
r1ColorSpaces.addToken(r1->getColorSpace(r1Idx, m));
}

for (size_t m = 0; m < r2->getNumColorSpaces(r2Idx); m++)
{
if (!r1ColorSpaces.hasToken(r2->getColorSpace(r2Idx, m)))
{
return false;
}
}

// Compare encoding tokens, handling the case where they may be in a different order.

if (r1->getNumEncodings(r1Idx) != r2->getNumEncodings(r2Idx))
{
return false;
}

TokensManager r1Encodings;
for (size_t m = 0; m < r1->getNumEncodings(r1Idx); m++)
{
r1Encodings.addToken(r1->getEncoding(r1Idx, m));
}

for (size_t m = 0; m < r2->getNumEncodings(r2Idx); m++)
{
if(!r1Encodings.hasToken(r2->getEncoding(r2Idx, m)))
{
return false;
}
}

// Compare the custom keys, handling the case where they may be in a different order.

if (r1->getNumCustomKeys(r1Idx) != r2->getNumCustomKeys(r2Idx))
{
return false;
}

CustomKeysContainer r1CustomKeys;
for (size_t m = 0; m < r1->getNumCustomKeys(r1Idx); m++)
{
r1CustomKeys.set(r1->getCustomKeyName(r1Idx, m), r1->getCustomKeyValue(r1Idx, m));
}

for (size_t m = 0; m < r2->getNumCustomKeys(r2Idx); m++)
{
if (!r1CustomKeys.hasKey(r2->getCustomKeyName(r2Idx, m)))
{
return false;
}
else
{
if (Platform::Strcasecmp(r1CustomKeys.getValueForKey(r2->getCustomKeyName(r2Idx, m)),
r2->getCustomKeyValue(r2Idx, m)) != 0)
{
return false;
}
}
}

return true;
}

void copyViewingRule(const ConstViewingRulesRcPtr & src,
size_t srcIdx,
size_t dstIdx,
ViewingRulesRcPtr & rules)
{
try
{
rules->insertRule(dstIdx, src->getName(srcIdx));

for (int j = 0; j < static_cast<int>(src->getNumColorSpaces(srcIdx)); j++)
{
rules->addColorSpace(dstIdx, src->getColorSpace(srcIdx, j));
}

for (int k = 0; k < static_cast<int>(src->getNumEncodings(srcIdx)); k++)
{
rules->addEncoding(dstIdx, src->getEncoding(srcIdx, k));
}

for (int l = 0; l < static_cast<int>(src->getNumCustomKeys(srcIdx)); l++)
{
rules->setCustomKey(dstIdx, src->getCustomKeyName(srcIdx, l), src->getCustomKeyValue(srcIdx, l));
}
}
catch(...)
{
// Don't add it if any errors.
// Continue.
}
}

bool FindRule(ConstViewingRulesRcPtr vr, const std::string & name, size_t & ruleIndex)
{
const auto numrules = vr->getNumEntries();
Expand Down
19 changes: 19 additions & 0 deletions src/OpenColorIO/ViewingRules.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ constexpr char CustomKey[] { "custom" };

bool FindRule(ConstViewingRulesRcPtr vr, const std::string & name, size_t & ruleIndex);

// Compares the rule at r1Idx in r1 against the rule at r2Idx in r2: same colorspaces,
// encodings, and custom keys (order-independent). Does not compare the rules' names,
// callers that care about the name should compare it separately.
bool viewingRulesAreEqual(const ConstViewingRulesRcPtr & r1,
size_t r1Idx,
const ConstViewingRulesRcPtr & r2,
size_t r2Idx);

// Copies the rule at srcIdx in src (name, colorspaces, encodings, and custom keys) into
// rules at dstIdx. Best-effort: if the copy fails for any reason (e.g. rules already has
// a rule named src's name), the exception is swallowed rather than propagated, and rules
// is left as it was before the call. src must not alias rules: if they're the same object
// and dstIdx < srcIdx, inserting shifts srcIdx before it's read, silently copying the
// wrong (freshly-inserted, empty) rule instead of throwing.
void copyViewingRule(const ConstViewingRulesRcPtr & src,
size_t srcIdx,
size_t dstIdx,
ViewingRulesRcPtr & rules);

class ViewingRule;
using ViewingRuleRcPtr = OCIO_SHARED_PTR<ViewingRule>;

Expand Down
114 changes: 1 addition & 113 deletions src/OpenColorIO/apphelpers/mergeconfigs/SectionMerger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include "ParseUtils.h"
#include "Platform.h"
#include "SectionMerger.h"
#include "TokensManager.h"
#include "utils/StringUtils.h"
#include "ViewingRules.h"

namespace OCIO_NAMESPACE
{
Expand Down Expand Up @@ -728,118 +728,6 @@ void FileRulesMerger::handleRemove()
namespace
{

bool viewingRulesAreEqual(const ConstViewingRulesRcPtr & r1,
size_t r1Idx,
const ConstViewingRulesRcPtr & r2,
size_t r2Idx)
{
// NB: No need to compare the name of the rules, that should be done in the caller.

// Compare color space tokens, handling the case where they may be in a different order.

if (r1->getNumColorSpaces(r1Idx) != r2->getNumColorSpaces(r2Idx))
{
return false;
}

TokensManager r1ColorSpaces;
for (size_t m = 0; m < r1->getNumColorSpaces(r1Idx); m++)
{
r1ColorSpaces.addToken(r1->getColorSpace(r1Idx, m));
}

for (size_t m = 0; m < r2->getNumColorSpaces(r2Idx); m++)
{
if (!r1ColorSpaces.hasToken(r2->getColorSpace(r2Idx, m)))
{
return false;
}
}

// Compare encoding tokens, handling the case where they may be in a different order.

if (r1->getNumEncodings(r1Idx) != r2->getNumEncodings(r2Idx))
{
return false;
}

TokensManager r1Encodings;
for (size_t m = 0; m < r1->getNumEncodings(r1Idx); m++)
{
r1Encodings.addToken(r1->getEncoding(r1Idx, m));
}

for (size_t m = 0; m < r2->getNumEncodings(r2Idx); m++)
{
if(!r1Encodings.hasToken(r2->getEncoding(r2Idx, m)))
{
return false;
}
}

// Compare the custom keys, handling the case where they may be in a different order.

if (r1->getNumCustomKeys(r1Idx) != r2->getNumCustomKeys(r2Idx))
{
return false;
}

CustomKeysContainer r1CustomKeys;
for (size_t m = 0; m < r1->getNumCustomKeys(r1Idx); m++)
{
r1CustomKeys.set(r1->getCustomKeyName(r1Idx, m), r1->getCustomKeyValue(r1Idx, m));
}

for (size_t m = 0; m < r2->getNumCustomKeys(r2Idx); m++)
{
if (!r1CustomKeys.hasKey(r2->getCustomKeyName(r2Idx, m)))
{
return false;
}
else
{
if (Platform::Strcasecmp(r1CustomKeys.getValueForKey(r2->getCustomKeyName(r2Idx, m)),
r2->getCustomKeyValue(r2Idx, m)) != 0)
{
return false;
}
}
}

return true;
}

void copyViewingRule(const ConstViewingRulesRcPtr & src,
size_t srcIdx,
size_t dstIdx,
ViewingRulesRcPtr & rules)
{
try
{
rules->insertRule(dstIdx, src->getName(srcIdx));

for (int j = 0; j < static_cast<int>(src->getNumColorSpaces(srcIdx)); j++)
{
rules->addColorSpace(dstIdx, src->getColorSpace(srcIdx, j));
}

for (int k = 0; k < static_cast<int>(src->getNumEncodings(srcIdx)); k++)
{
rules->addEncoding(dstIdx, src->getEncoding(srcIdx, k));
}

for (int l = 0; l < static_cast<int>(src->getNumCustomKeys(srcIdx)); l++)
{
rules->setCustomKey(dstIdx, src->getCustomKeyName(srcIdx, l), src->getCustomKeyValue(srcIdx, l));
}
}
catch(...)
{
// Don't add it if any errors.
// Continue.
}
}

void addUniqueViewingRules(const ConstViewingRulesRcPtr & rules,
ViewingRulesRcPtr & mergedRules)
{
Expand Down
Loading
Loading