Coverage Report

Created: 2025-10-10 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/alembic/lib/Alembic/Ogawa/IGroup.cpp
Line
Count
Source
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
572
    {
49
572
        numChildren = 0;
50
572
        pos = 0;
51
572
        streams = iStreams;
52
572
    }
53
54
572
    ~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
572
    mData(new IGroup::PrivateData(iStreams))
69
572
{
70
    // all done, we have no children, or our streams aren't good
71
572
    if (iPos == EMPTY_GROUP || !mData->streams || !mData->streams->isValid())
72
1
    {
73
1
        return;
74
1
    }
75
76
571
    mData->pos = iPos;
77
571
    mData->streams->read(iThreadIndex, iPos, 8, &mData->numChildren);
78
79
    // make sure we don't have a maliciously bad number of children
80
571
    if ( mData->numChildren > (mData->streams->getSize() / 8) ||
81
513
         mData->numChildren == 0 )
82
64
    {
83
64
        mData->numChildren = 0;
84
64
        return;
85
64
    }
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
507
    if (!iLight || mData->numChildren < 9)
93
500
    {
94
500
        mData->childVec.resize(mData->numChildren);
95
500
        mData->streams->read(iThreadIndex, iPos + 8, mData->numChildren * 8,
96
500
                             &(mData->childVec.front()));
97
500
    }
98
507
}
99
100
IGroup::~IGroup()
101
560
{
102
103
560
}
104
105
IGroupPtr IGroup::getGroup(Alembic::Util::uint64_t iIndex, bool iLight,
106
                           std::size_t iThreadIndex)
107
328
{
108
328
    IGroupPtr child;
109
110
328
    Alembic::Util::uint64_t childPos = EMPTY_DATA;
111
112
328
    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
328
    else if (isChildGroup(iIndex))
121
328
    {
122
328
        childPos = mData->childVec[iIndex];
123
328
    }
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
328
    if (childPos == EMPTY_GROUP || ((childPos & EMPTY_DATA) == 0 &&
128
327
        childPos > 8 && childPos != mData->pos))
129
328
    {
130
328
        child.reset(new IGroup(mData->streams, childPos, iLight, iThreadIndex));
131
328
    }
132
133
328
    return child;
134
328
}
135
136
IDataPtr IGroup::getData(Alembic::Util::uint64_t iIndex,
137
                         std::size_t iThreadIndex)
138
1.20k
{
139
1.20k
    IDataPtr child;
140
1.20k
    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.20k
    else if (isChildData(iIndex))
156
1.20k
    {
157
1.20k
        child.reset(new IData(mData->streams, mData->childVec[iIndex],
158
1.20k
                              iThreadIndex));
159
1.20k
    }
160
1.20k
    return child;
161
1.20k
}
162
163
Alembic::Util::uint64_t IGroup::getNumChildren() const
164
560
{
165
560
    return mData->numChildren;
166
560
}
167
168
bool IGroup::isChildGroup(Alembic::Util::uint64_t iIndex) const
169
667
{
170
667
    return (iIndex < mData->childVec.size() &&
171
667
            (mData->childVec[iIndex] & EMPTY_DATA) == 0);
172
667
}
173
174
bool IGroup::isChildData(Alembic::Util::uint64_t iIndex) const
175
2.66k
{
176
2.66k
    return (iIndex < mData->childVec.size() &&
177
2.66k
            (mData->childVec[iIndex] & EMPTY_DATA) != 0);
178
2.66k
}
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.52k
{
194
1.52k
    return mData->numChildren != 0 && mData->childVec.empty();
195
1.52k
}
196
197
} // End namespace ALEMBIC_VERSION_NS
198
} // End namespace Ogawa
199
} // End namespace Alembic