Coverage Report

Created: 2025-05-14 06:54

/src/alembic/lib/Alembic/Ogawa/IGroup.cpp
Line
Count
Source (jump to first uncovered line)
1
//-*****************************************************************************
2
//
3
// Copyright (c) 2013,
4
//  Sony Pictures Imageworks Inc. and
5
//  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6
//
7
// All rights reserved.
8
//
9
// Redistribution and use in source and binary forms, with or without
10
// modification, are permitted provided that the following conditions are
11
// met:
12
// *       Redistributions of source code must retain the above copyright
13
// notice, this list of conditions and the following disclaimer.
14
// *       Redistributions in binary form must reproduce the above
15
// copyright notice, this list of conditions and the following disclaimer
16
// in the documentation and/or other materials provided with the
17
// distribution.
18
// *       Neither the name of Industrial Light & Magic nor the names of
19
// its contributors may be used to endorse or promote products derived
20
// from this software without specific prior written permission.
21
//
22
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
//
34
//-*****************************************************************************
35
36
#include <Alembic/Ogawa/IGroup.h>
37
#include <Alembic/Ogawa/IArchive.h>
38
#include <Alembic/Ogawa/IStreams.h>
39
40
namespace Alembic {
41
namespace Ogawa {
42
namespace ALEMBIC_VERSION_NS {
43
44
class IGroup::PrivateData
45
{
46
public:
47
    PrivateData(IStreamsPtr iStreams)
48
487
    {
49
487
        numChildren = 0;
50
487
        pos = 0;
51
487
        streams = iStreams;
52
487
    }
53
54
487
    ~PrivateData() {}
55
56
    IStreamsPtr streams;
57
58
    std::vector<Alembic::Util::uint64_t> childVec;
59
60
    Alembic::Util::uint64_t numChildren;
61
    Alembic::Util::uint64_t pos;
62
};
63
64
IGroup::IGroup(IStreamsPtr iStreams,
65
               Alembic::Util::uint64_t iPos,
66
               bool iLight,
67
               std::size_t iThreadIndex) :
68
487
    mData(new IGroup::PrivateData(iStreams))
69
487
{
70
    // all done, we have no children, or our streams aren't good
71
487
    if (iPos == EMPTY_GROUP || !mData->streams || !mData->streams->isValid())
72
2
    {
73
2
        return;
74
2
    }
75
76
485
    mData->pos = iPos;
77
485
    mData->streams->read(iThreadIndex, iPos, 8, &mData->numChildren);
78
79
    // make sure we don't have a maliciously bad number of children
80
485
    if ( mData->numChildren > (mData->streams->getSize() / 8) ||
81
485
         mData->numChildren == 0 )
82
61
    {
83
61
        mData->numChildren = 0;
84
61
        return;
85
61
    }
86
87
    // 0 should NOT have been written, this groups should have been the
88
    // special EMPTY_GROUP instead
89
90
    // read all our child indices, unless we are light and have more than 8
91
    // children
92
424
    if (!iLight || mData->numChildren < 9)
93
409
    {
94
409
        mData->childVec.resize(mData->numChildren);
95
409
        mData->streams->read(iThreadIndex, iPos + 8, mData->numChildren * 8,
96
409
                             &(mData->childVec.front()));
97
409
    }
98
424
}
99
100
IGroup::~IGroup()
101
458
{
102
103
458
}
104
105
IGroupPtr IGroup::getGroup(Alembic::Util::uint64_t iIndex, bool iLight,
106
                           std::size_t iThreadIndex)
107
261
{
108
261
    IGroupPtr child;
109
110
261
    Alembic::Util::uint64_t childPos = EMPTY_DATA;
111
112
261
    if (isLight())
113
0
    {
114
0
        if (iIndex < mData->numChildren)
115
0
        {
116
0
            mData->streams->read(iThreadIndex, mData->pos + 8 * iIndex + 8, 8,
117
0
                                 &childPos);
118
0
        }
119
0
    }
120
261
    else if (isChildGroup(iIndex))
121
261
    {
122
261
        childPos = mData->childVec[iIndex];
123
261
    }
124
125
    // sanity check that we have a valid group, either an empty one
126
    // or a non data that has a decent value
127
261
    if (childPos == EMPTY_GROUP || ((childPos & EMPTY_DATA) == 0 &&
128
259
        childPos > 8 && childPos != mData->pos))
129
261
    {
130
261
        child.reset(new IGroup(mData->streams, childPos, iLight, iThreadIndex));
131
261
    }
132
133
261
    return child;
134
261
}
135
136
IDataPtr IGroup::getData(Alembic::Util::uint64_t iIndex,
137
                         std::size_t iThreadIndex)
138
1.06k
{
139
1.06k
    IDataPtr child;
140
1.06k
    if (isLight())
141
0
    {
142
0
        if (iIndex < mData->numChildren)
143
0
        {
144
0
            Alembic::Util::uint64_t childPos = 0;
145
0
            mData->streams->read(iThreadIndex, mData->pos + 8 * iIndex + 8, 8,
146
0
                                 &childPos);
147
148
            // top bit should be set for data
149
0
            if ((childPos & EMPTY_DATA) != 0)
150
0
            {
151
0
                child.reset(new IData(mData->streams, childPos, iThreadIndex));
152
0
            }
153
0
        }
154
0
    }
155
1.06k
    else if (isChildData(iIndex))
156
1.06k
    {
157
1.06k
        child.reset(new IData(mData->streams, mData->childVec[iIndex],
158
1.06k
                              iThreadIndex));
159
1.06k
    }
160
1.06k
    return child;
161
1.06k
}
162
163
Alembic::Util::uint64_t IGroup::getNumChildren() const
164
458
{
165
458
    return mData->numChildren;
166
458
}
167
168
bool IGroup::isChildGroup(Alembic::Util::uint64_t iIndex) const
169
551
{
170
551
    return (iIndex < mData->childVec.size() &&
171
551
            (mData->childVec[iIndex] & EMPTY_DATA) == 0);
172
551
}
173
174
bool IGroup::isChildData(Alembic::Util::uint64_t iIndex) const
175
2.33k
{
176
2.33k
    return (iIndex < mData->childVec.size() &&
177
2.33k
            (mData->childVec[iIndex] & EMPTY_DATA) != 0);
178
2.33k
}
179
180
bool IGroup::isEmptyChildGroup(Alembic::Util::uint64_t iIndex) const
181
0
{
182
0
    return (iIndex < mData->childVec.size() &&
183
0
            mData->childVec[iIndex] == EMPTY_GROUP);
184
0
}
185
186
bool IGroup::isEmptyChildData(Alembic::Util::uint64_t iIndex) const
187
0
{
188
0
    return (iIndex < mData->childVec.size() &&
189
0
        mData->childVec[iIndex] == EMPTY_DATA);
190
0
}
191
192
bool IGroup::isLight() const
193
1.32k
{
194
1.32k
    return mData->numChildren != 0 && mData->childVec.empty();
195
1.32k
}
196
197
} // End namespace ALEMBIC_VERSION_NS
198
} // End namespace Ogawa
199
} // End namespace Alembic