Skip to content

Commit

Permalink
handle custom attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Kimball Thurston <kdt3rd@gmail.com>
  • Loading branch information
kdt3rd committed Apr 17, 2024
1 parent 66aceb0 commit 2297c10
Showing 1 changed file with 74 additions and 5 deletions.
79 changes: 74 additions & 5 deletions src/lib/OpenEXR/ImfContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,61 @@ class LegacyStream final : public IStream
exr_read_func_ptr_t _read_fn = nullptr;
}; // class LegacyStream

class MemAttrStream : public OPENEXR_IMF_NAMESPACE::IStream
{
public:
MemAttrStream (const exr_attr_opaquedata_t *opaque)
: IStream ("<mem_attr>"),
_data (static_cast<char*> (opaque->packed_data)),
_sz (static_cast<uint64_t> (opaque->size)),
_pos (0)
{}

~MemAttrStream () override {}

bool isMemoryMapped () const override { return true; }

bool read (char c[/*n*/], int n) override
{
if (_pos >= _sz && n != 0)
throw IEX_NAMESPACE::InputExc ("Unexpected end of file.");

uint64_t n2 = n;
bool retVal = true;

if (_sz - _pos <= n2)
{
n2 = _sz - _pos;
retVal = false;
}

memcpy (c, _data + _pos, n2);
_pos += n2;
return retVal;
}

char* readMemoryMapped (int n) override
{
if (_pos >= _sz)
throw IEX_NAMESPACE::InputExc ("Unexpected end of file.");

if (_pos + n > _sz)
throw IEX_NAMESPACE::InputExc ("Reading past end of file.");

char* retVal = _data + _pos;
_pos += n;
return retVal;
}
uint64_t tellg () override { return _pos; }
void seekg (uint64_t pos) override { _pos = pos; }
void clear () override {}

private:
char* _data;
uint64_t _sz;
uint64_t _pos;
};

} // namespace
////////////////////////////////////////

Expand Down Expand Up @@ -641,11 +696,25 @@ Context::header (int partidx) const
break;

case EXR_ATTR_OPAQUE:
hdr.insert (
cur->name, OpaqueAttribute (
cur->type_name,
cur->opaque->size,
cur->opaque->packed_data));
if (Attribute::knownType (cur->type_name))
{
MemAttrStream mas {cur->opaque};

std::unique_ptr<Attribute> attr;
attr.reset (Attribute::newAttribute (cur->type_name));

attr->readValueFrom (mas, cur->opaque->size, version ());
// TODO: can we avoid a double copy?
hdr.insert (cur->name, *attr);
}
else
{
hdr.insert (
cur->name, OpaqueAttribute (
cur->type_name,
cur->opaque->size,
cur->opaque->packed_data));
}
break;

case EXR_ATTR_UNKNOWN:
Expand Down

0 comments on commit 2297c10

Please sign in to comment.