Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for returning combined channel list of multi-part files #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
33 changes: 26 additions & 7 deletions src/parse_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,44 @@ def read_exr_header(exrpath, maxreadsize=2000):
ord(openxr_version_number[0]),
ord(version_field_attrs[0]), ord(version_field_attrs[1]),
ord(version_field_attrs[2]))
is_multipart = bool(ord(version_field_attrs[0]) & 0b10000)
if is_multipart:
log.info('multi-part bit is set')
log.info("METADATA:")
i = 0
part_count = 0
next_header = False

while i < maxreadsize:

# We'll always have attribute name, attribute type separated by a
# null byte. Then attribute size and attribute value follow
attribute_name, attribute_name_length = read_until_null(exr_file)
attribute_type, _ = read_until_null(exr_file)
attribute_size = int(struct.unpack('i', exr_file.read(4))[0])

# If we're reading only byte it means it's the null byte
# and we've reached the end of the header
# and we've reached the end of the header. In multi-part files
# the headers are done after two null bytes in a row.
if attribute_name_length == 1:
log.debug('reached the end of the header!')
break
if is_multipart is False or next_header is True:
log.debug('reached the end of the header!')
break
else:
part_count += 1
next_header = True
log.debug('end of part {}'.format(part_count))
continue
else:
next_header = False

if not attribute_name in metadata:
attribute_type, _ = read_until_null(exr_file)
attribute_size = int(struct.unpack('i', exr_file.read(4))[0])
if attribute_name not in metadata:
metadata[attribute_name] = {}
elif is_multipart and attribute_name != "channels":
# in multi-part files, skip over all attributes that already exist
# except for the channel list
exr_file.read(attribute_size)
continue

# How many bytes of the attribute value we've read
byte_count = 0
Expand Down Expand Up @@ -182,7 +201,7 @@ def read_exr_header(exrpath, maxreadsize=2000):
'ySampling': y_sampling
}

metadata[attribute_name] = channel_data
metadata[attribute_name].update(channel_data)

elif attribute_type == b'chromaticities':
chromaticities = struct.unpack('f' * 8, exr_file.read(4 * 8))
Expand Down
110 changes: 110 additions & 0 deletions tests/test_exr.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,116 @@ def test_exr_meta_owner():
'owner': 'Copyright 2006 Industrial Light & Magic',
'screenWindowWidth': 1.0,
'lineOrder': 'INCREASING_Y'
}),
# Beachball (multipart)
pytest.param(os.path.join(EXR_IMAGES_DIR_PATH, 'Beachball', 'multipart.0001.exr'), {
'compression': 'ZIPS_COMPRESSION',
'pixelAspectRatio': 1.0,
'displayWindow': {
'xMax': 2047,
'xMin': 0,
'yMax': 1555,
'yMin': 0
},
'dataWindow': {
'xMax': 1530,
'xMin': 654,
'yMax': 1120,
'yMin': 245
},
'channels': {
'A': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'B': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'G': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'R': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'Z': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'disparityL.x': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'disparityL.y': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'disparityR.x': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'disparityR.y': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'forward.u': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'forward.v': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
},
'whitebarmask.mask': {
'pLinear': 0,
'pixel_type': 1,
'reserved': [0, 0, 0],
'xSampling': 1,
'ySampling': 1
}
},
'screenWindowCenter': [0.0, 0.0],
'screenWindowWidth': 1.0,
'lineOrder': 'INCREASING_Y',
'chunkCount': 876,
'name': 'rgba_right',
'type': 'scanlineimage',
'view': 'right',
})
])
def test_exr_meta_all(input_path, expected_metadata):
Expand Down