AcademySoftwareFoundation / OpenColorIO

A color management framework for visual effects and animation.
https://opencolorio.org
BSD 3-Clause "New" or "Revised" License
1.8k stars 456 forks source link

Add helper methods to ViewingRules class #2050

Open doug-walker opened 2 months ago

doug-walker commented 2 months ago

Here are some helper functions that contain useful functionality to add to the ViewingRules class. The API of the functions below should be altered, as appropriate. As always, unit tests must be added.

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)
{
        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));
        }
}