pylint work, changed f to fptr, stopped passing id around. #71
This commit is contained in:
parent
277be2fb38
commit
1d4c981797
1 changed files with 128 additions and 188 deletions
316
glymur/jp2box.py
316
glymur/jp2box.py
|
|
@ -62,18 +62,18 @@ class Jp2kBox(object):
|
||||||
msg += " @ ({0}, {1})".format(self.offset, self.length)
|
msg += " @ ({0}, {1})".format(self.offset, self.length)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def _write(self, f):
|
def _write(self, fptr):
|
||||||
"""Must be implemented in a subclass.
|
"""Must be implemented in a subclass.
|
||||||
"""
|
"""
|
||||||
msg = "Not supported for {0} box.".format(self.longname)
|
msg = "Not supported for {0} box.".format(self.longname)
|
||||||
raise NotImplementedError(msg)
|
raise NotImplementedError(msg)
|
||||||
|
|
||||||
def _parse_superbox(self, f):
|
def _parse_superbox(self, fptr):
|
||||||
"""Parse a superbox (box consisting of nothing but other boxes.
|
"""Parse a superbox (box consisting of nothing but other boxes.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
|
|
@ -83,7 +83,7 @@ class Jp2kBox(object):
|
||||||
|
|
||||||
superbox = []
|
superbox = []
|
||||||
|
|
||||||
start = f.tell()
|
start = fptr.tell()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
|
|
@ -91,7 +91,7 @@ class Jp2kBox(object):
|
||||||
if start >= self.offset + self.length:
|
if start >= self.offset + self.length:
|
||||||
break
|
break
|
||||||
|
|
||||||
read_buffer = f.read(8)
|
read_buffer = fptr.read(8)
|
||||||
(L, T) = struct.unpack('>I4s', read_buffer)
|
(L, T) = struct.unpack('>I4s', read_buffer)
|
||||||
if sys.hexversion >= 0x03000000:
|
if sys.hexversion >= 0x03000000:
|
||||||
T = T.decode('utf-8')
|
T = T.decode('utf-8')
|
||||||
|
|
@ -99,11 +99,11 @@ class Jp2kBox(object):
|
||||||
if L == 0:
|
if L == 0:
|
||||||
# The length of the box is presumed to last until the end of
|
# The length of the box is presumed to last until the end of
|
||||||
# the file. Compute the effective length of the box.
|
# the file. Compute the effective length of the box.
|
||||||
num_bytes = self._file_size - f.tell() + 8
|
num_bytes = self._file_size - fptr.tell() + 8
|
||||||
|
|
||||||
elif L == 1:
|
elif L == 1:
|
||||||
# The length of the box is in the XL field, a 64-bit value.
|
# The length of the box is in the XL field, a 64-bit value.
|
||||||
read_buffer = f.read(8)
|
read_buffer = fptr.read(8)
|
||||||
num_bytes, = struct.unpack('>Q', read_buffer)
|
num_bytes, = struct.unpack('>Q', read_buffer)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -111,7 +111,7 @@ class Jp2kBox(object):
|
||||||
|
|
||||||
# Call the proper parser for the given box with ID "T".
|
# Call the proper parser for the given box with ID "T".
|
||||||
try:
|
try:
|
||||||
box = _BOX_WITH_ID[T]._parse(f, T, start, num_bytes)
|
box = _BOX_WITH_ID[T]._parse(fptr, start, num_bytes)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
msg = 'Unrecognized box ({0}) encountered.'.format(T)
|
msg = 'Unrecognized box ({0}) encountered.'.format(T)
|
||||||
warnings.warn(msg)
|
warnings.warn(msg)
|
||||||
|
|
@ -127,14 +127,14 @@ class Jp2kBox(object):
|
||||||
msg = '{0} box has incorrect box length ({1})'
|
msg = '{0} box has incorrect box length ({1})'
|
||||||
msg = msg.format(T, num_bytes)
|
msg = msg.format(T, num_bytes)
|
||||||
warnings.warn(msg)
|
warnings.warn(msg)
|
||||||
elif f.tell() > start + num_bytes:
|
elif fptr.tell() > start + num_bytes:
|
||||||
# The box must be invalid somehow, as the file pointer is
|
# The box must be invalid somehow, as the file pointer is
|
||||||
# positioned past the end of the box.
|
# positioned past the end of the box.
|
||||||
msg = '{0} box may be invalid, the file pointer is positioned '
|
msg = '{0} box may be invalid, the file pointer is positioned '
|
||||||
msg += '{1} bytes past the end of the box.'
|
msg += '{1} bytes past the end of the box.'
|
||||||
msg = msg.format(T, f.tell() - (start + num_bytes))
|
msg = msg.format(T, fptr.tell() - (start + num_bytes))
|
||||||
warnings.warn(msg)
|
warnings.warn(msg)
|
||||||
f.seek(start + num_bytes)
|
fptr.seek(start + num_bytes)
|
||||||
|
|
||||||
start += num_bytes
|
start += num_bytes
|
||||||
|
|
||||||
|
|
@ -210,7 +210,7 @@ class ColourSpecificationBox(Jp2kBox):
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def _write(self, f):
|
def _write(self, fptr):
|
||||||
"""Write an Colour Specification box to file.
|
"""Write an Colour Specification box to file.
|
||||||
"""
|
"""
|
||||||
if self.colorspace is None:
|
if self.colorspace is None:
|
||||||
|
|
@ -218,26 +218,24 @@ class ColourSpecificationBox(Jp2kBox):
|
||||||
msg += "colorspaces is not supported at this time."
|
msg += "colorspaces is not supported at this time."
|
||||||
raise NotImplementedError(msg)
|
raise NotImplementedError(msg)
|
||||||
length = 15 if self.icc_profile is None else 11 + len(self.icc_profile)
|
length = 15 if self.icc_profile is None else 11 + len(self.icc_profile)
|
||||||
f.write(struct.pack('>I', length))
|
fptr.write(struct.pack('>I', length))
|
||||||
f.write('colr'.encode())
|
fptr.write('colr'.encode())
|
||||||
|
|
||||||
read_buffer = struct.pack('>BBBI',
|
read_buffer = struct.pack('>BBBI',
|
||||||
self.method,
|
self.method,
|
||||||
self.precedence,
|
self.precedence,
|
||||||
self.approximation,
|
self.approximation,
|
||||||
self.colorspace)
|
self.colorspace)
|
||||||
f.write(read_buffer)
|
fptr.write(read_buffer)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse JPEG 2000 color specification box.
|
"""Parse JPEG 2000 color specification box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -249,12 +247,11 @@ class ColourSpecificationBox(Jp2kBox):
|
||||||
dictionary of parameter values
|
dictionary of parameter values
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
# Read the brand, minor version.
|
# Read the brand, minor version.
|
||||||
read_buffer = f.read(3)
|
read_buffer = fptr.read(3)
|
||||||
(method, precedence, approximation) = struct.unpack('>BBB', read_buffer)
|
(method, precedence, approximation) = struct.unpack('>BBB', read_buffer)
|
||||||
kwargs['method'] = method
|
kwargs['method'] = method
|
||||||
kwargs['precedence'] = precedence
|
kwargs['precedence'] = precedence
|
||||||
|
|
@ -262,21 +259,21 @@ class ColourSpecificationBox(Jp2kBox):
|
||||||
|
|
||||||
if method == 1:
|
if method == 1:
|
||||||
# enumerated colour space
|
# enumerated colour space
|
||||||
read_buffer = f.read(4)
|
read_buffer = fptr.read(4)
|
||||||
kwargs['colorspace'], = struct.unpack('>I', read_buffer)
|
kwargs['colorspace'], = struct.unpack('>I', read_buffer)
|
||||||
kwargs['icc_profile'] = None
|
kwargs['icc_profile'] = None
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# ICC profile
|
# ICC profile
|
||||||
kwargs['colorspace'] = None
|
kwargs['colorspace'] = None
|
||||||
n = offset + length - f.tell()
|
n = offset + length - fptr.tell()
|
||||||
if n < 128:
|
if n < 128:
|
||||||
msg = "ICC profile header is corrupt, length is "
|
msg = "ICC profile header is corrupt, length is "
|
||||||
msg += "only {0} instead of 128."
|
msg += "only {0} instead of 128."
|
||||||
warnings.warn(msg.format(n), UserWarning)
|
warnings.warn(msg.format(n), UserWarning)
|
||||||
kwargs['icc_profile'] = None
|
kwargs['icc_profile'] = None
|
||||||
else:
|
else:
|
||||||
icc_profile = _ICCProfile(f.read(n))
|
icc_profile = _ICCProfile(fptr.read(n))
|
||||||
kwargs['icc_profile'] = icc_profile.header
|
kwargs['icc_profile'] = icc_profile.header
|
||||||
|
|
||||||
box = ColourSpecificationBox(**kwargs)
|
box = ColourSpecificationBox(**kwargs)
|
||||||
|
|
@ -446,29 +443,27 @@ class ChannelDefinitionBox(Jp2kBox):
|
||||||
msg = msg.format(self.index[j], color_type_string, assn)
|
msg = msg.format(self.index[j], color_type_string, assn)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def _write(self, f):
|
def _write(self, fptr):
|
||||||
"""Write a channel definition box to file.
|
"""Write a channel definition box to file.
|
||||||
"""
|
"""
|
||||||
N = len(self.association)
|
N = len(self.association)
|
||||||
f.write(struct.pack('>I', 8 + 2 + N * 6))
|
fptr.write(struct.pack('>I', 8 + 2 + N * 6))
|
||||||
f.write('cdef'.encode('utf-8'))
|
fptr.write('cdef'.encode('utf-8'))
|
||||||
f.write(struct.pack('>H', N))
|
fptr.write(struct.pack('>H', N))
|
||||||
for j in range(N):
|
for j in range(N):
|
||||||
f.write(struct.pack('>' + 'H' * 3,
|
fptr.write(struct.pack('>' + 'H' * 3,
|
||||||
self.index[j],
|
self.index[j],
|
||||||
self.channel_type[j],
|
self.channel_type[j],
|
||||||
self.association[j]))
|
self.association[j]))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse component definition box.
|
"""Parse component definition box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -479,19 +474,14 @@ class ChannelDefinitionBox(Jp2kBox):
|
||||||
kwargs : dictionary of parameter values
|
kwargs : dictionary of parameter values
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
# Read the number of components.
|
# Read the number of components.
|
||||||
read_buffer = f.read(2)
|
read_buffer = fptr.read(2)
|
||||||
N, = struct.unpack('>H', read_buffer)
|
N, = struct.unpack('>H', read_buffer)
|
||||||
|
|
||||||
index = []
|
read_buffer = fptr.read(N * 6)
|
||||||
chan_type = []
|
|
||||||
association = []
|
|
||||||
|
|
||||||
read_buffer = f.read(N * 6)
|
|
||||||
data = struct.unpack('>' + 'HHH' * N, read_buffer)
|
data = struct.unpack('>' + 'HHH' * N, read_buffer)
|
||||||
index = data[0:N * 6:3]
|
index = data[0:N * 6:3]
|
||||||
channel_type = data[1:N * 6:3]
|
channel_type = data[1:N * 6:3]
|
||||||
|
|
@ -522,7 +512,7 @@ class ComponentMappingBox(Jp2kBox):
|
||||||
Index component from palette
|
Index component from palette
|
||||||
"""
|
"""
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
Jp2kBox.__init__(self, id='', longname='Component Mapping')
|
Jp2kBox.__init__(self, id='cmap', longname='Component Mapping')
|
||||||
self.__dict__.update(**kwargs)
|
self.__dict__.update(**kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -539,15 +529,13 @@ class ComponentMappingBox(Jp2kBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse component mapping box.
|
"""Parse component mapping box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -558,14 +546,13 @@ class ComponentMappingBox(Jp2kBox):
|
||||||
kwargs : dictionary of parameter values
|
kwargs : dictionary of parameter values
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
N = offset + length - f.tell()
|
N = offset + length - fptr.tell()
|
||||||
num_components = int(N/4)
|
num_components = int(N/4)
|
||||||
|
|
||||||
read_buffer = f.read(N)
|
read_buffer = fptr.read(N)
|
||||||
data = struct.unpack('>' + 'HBB' * num_components, read_buffer)
|
data = struct.unpack('>' + 'HBB' * num_components, read_buffer)
|
||||||
|
|
||||||
kwargs['component_index'] = data[0:N:num_components]
|
kwargs['component_index'] = data[0:N:num_components]
|
||||||
|
|
@ -610,15 +597,13 @@ class ContiguousCodestreamBox(Jp2kBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id='jp2c', offset=0, length=0):
|
def _parse(fptr, offset=0, length=0):
|
||||||
"""Parse a codestream box.
|
"""Parse a codestream box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
f : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -629,11 +614,10 @@ class ContiguousCodestreamBox(Jp2kBox):
|
||||||
kwargs : dictionary of parameter values
|
kwargs : dictionary of parameter values
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
kwargs['main_header'] = Codestream(f, header_only=True)
|
kwargs['main_header'] = Codestream(fptr, header_only=True)
|
||||||
|
|
||||||
box = ContiguousCodestreamBox(**kwargs)
|
box = ContiguousCodestreamBox(**kwargs)
|
||||||
return box
|
return box
|
||||||
|
|
@ -679,28 +663,26 @@ class FileTypeBox(Jp2kBox):
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def _write(self, f):
|
def _write(self, fptr):
|
||||||
"""Write a File Type box to file.
|
"""Write a File Type box to file.
|
||||||
"""
|
"""
|
||||||
length = 16 + 4*len(self.compatibility_list)
|
length = 16 + 4*len(self.compatibility_list)
|
||||||
f.write(struct.pack('>I', length))
|
fptr.write(struct.pack('>I', length))
|
||||||
f.write('ftyp'.encode())
|
fptr.write('ftyp'.encode())
|
||||||
f.write(self.brand.encode())
|
fptr.write(self.brand.encode())
|
||||||
f.write(struct.pack('>I', self.minor_version))
|
fptr.write(struct.pack('>I', self.minor_version))
|
||||||
|
|
||||||
for item in self.compatibility_list:
|
for item in self.compatibility_list:
|
||||||
f.write(item.encode())
|
fptr.write(item.encode())
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse JPEG 2000 file type box.
|
"""Parse JPEG 2000 file type box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
f : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -710,12 +692,11 @@ class FileTypeBox(Jp2kBox):
|
||||||
kwargs: dictionary of parameter values
|
kwargs: dictionary of parameter values
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
# Read the brand, minor version.
|
# Read the brand, minor version.
|
||||||
read_buffer = f.read(8)
|
read_buffer = fptr.read(8)
|
||||||
(brand, minor_version) = struct.unpack('>4sI', read_buffer)
|
(brand, minor_version) = struct.unpack('>4sI', read_buffer)
|
||||||
if sys.hexversion < 0x030000:
|
if sys.hexversion < 0x030000:
|
||||||
kwargs['brand'] = brand
|
kwargs['brand'] = brand
|
||||||
|
|
@ -724,9 +705,9 @@ class FileTypeBox(Jp2kBox):
|
||||||
kwargs['minor_version'] = minor_version
|
kwargs['minor_version'] = minor_version
|
||||||
|
|
||||||
# Read the compatibility list. Each entry has 4 bytes.
|
# Read the compatibility list. Each entry has 4 bytes.
|
||||||
current_pos = f.tell()
|
current_pos = fptr.tell()
|
||||||
n = (offset + length - current_pos) / 4
|
n = (offset + length - current_pos) / 4
|
||||||
read_buffer = f.read(int(n) * 4)
|
read_buffer = fptr.read(int(n) * 4)
|
||||||
compatibility_list = []
|
compatibility_list = []
|
||||||
for j in range(int(n)):
|
for j in range(int(n)):
|
||||||
CL, = struct.unpack('>4s', read_buffer[4*j:4*(j+1)])
|
CL, = struct.unpack('>4s', read_buffer[4*j:4*(j+1)])
|
||||||
|
|
@ -805,11 +786,11 @@ class ImageHeaderBox(Jp2kBox):
|
||||||
self.colorspace_unknown)
|
self.colorspace_unknown)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def _write(self, f):
|
def _write(self, fptr):
|
||||||
"""Write an Image Header box to file.
|
"""Write an Image Header box to file.
|
||||||
"""
|
"""
|
||||||
f.write(struct.pack('>I', 22))
|
fptr.write(struct.pack('>I', 22))
|
||||||
f.write('ihdr'.encode())
|
fptr.write('ihdr'.encode())
|
||||||
|
|
||||||
# signedness and bps are stored together in a single byte
|
# signedness and bps are stored together in a single byte
|
||||||
bit_depth_signedness = 0x80 if self.signed else 0x00
|
bit_depth_signedness = 0x80 if self.signed else 0x00
|
||||||
|
|
@ -822,18 +803,16 @@ class ImageHeaderBox(Jp2kBox):
|
||||||
self.compression,
|
self.compression,
|
||||||
1 if self.colorspace_unknown else 0,
|
1 if self.colorspace_unknown else 0,
|
||||||
1 if self.ip_provided else 0)
|
1 if self.ip_provided else 0)
|
||||||
f.write(read_buffer)
|
fptr.write(read_buffer)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse JPEG 2000 image header box.
|
"""Parse JPEG 2000 image header box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -843,12 +822,11 @@ class ImageHeaderBox(Jp2kBox):
|
||||||
kwargs: dictionary of parameter values
|
kwargs: dictionary of parameter values
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
# Read the box information
|
# Read the box information
|
||||||
read_buffer = f.read(14)
|
read_buffer = fptr.read(14)
|
||||||
params = struct.unpack('>IIHBBBB', read_buffer)
|
params = struct.unpack('>IIHBBBB', read_buffer)
|
||||||
height = params[0]
|
height = params[0]
|
||||||
width = params[1]
|
width = params[1]
|
||||||
|
|
@ -880,7 +858,7 @@ class AssociationBox(Jp2kBox):
|
||||||
List of boxes contained in this superbox.
|
List of boxes contained in this superbox.
|
||||||
"""
|
"""
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
Jp2kBox.__init__(self, id='', longname='Association')
|
Jp2kBox.__init__(self, id='asoc', longname='Association')
|
||||||
self.__dict__.update(**kwargs)
|
self.__dict__.update(**kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -894,15 +872,13 @@ class AssociationBox(Jp2kBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse association box.
|
"""Parse association box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -914,15 +890,14 @@ class AssociationBox(Jp2kBox):
|
||||||
4-byte tuple.
|
4-byte tuple.
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
box = AssociationBox(**kwargs)
|
box = AssociationBox(**kwargs)
|
||||||
|
|
||||||
# The JP2 header box is a superbox, so go ahead and parse its child
|
# The Association box is a superbox, so go ahead and parse its child
|
||||||
# boxes.
|
# boxes.
|
||||||
box.box = box._parse_superbox(f)
|
box.box = box._parse_superbox(fptr)
|
||||||
|
|
||||||
return box
|
return box
|
||||||
|
|
||||||
|
|
@ -957,31 +932,29 @@ class JP2HeaderBox(Jp2kBox):
|
||||||
msg += ''.join(strs)
|
msg += ''.join(strs)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def _write(self, f):
|
def _write(self, fptr):
|
||||||
"""Write a JP2 Header box to file.
|
"""Write a JP2 Header box to file.
|
||||||
"""
|
"""
|
||||||
# Write the contained boxes, then come back and write the length.
|
# Write the contained boxes, then come back and write the length.
|
||||||
orig_pos = f.tell()
|
orig_pos = fptr.tell()
|
||||||
f.write(struct.pack('>I', 0))
|
fptr.write(struct.pack('>I', 0))
|
||||||
f.write('jp2h'.encode())
|
fptr.write('jp2h'.encode())
|
||||||
for box in self.box:
|
for box in self.box:
|
||||||
box._write(f)
|
box._write(fptr)
|
||||||
|
|
||||||
end_pos = f.tell()
|
end_pos = fptr.tell()
|
||||||
f.seek(orig_pos)
|
fptr.seek(orig_pos)
|
||||||
f.write(struct.pack('>I', end_pos - orig_pos))
|
fptr.write(struct.pack('>I', end_pos - orig_pos))
|
||||||
f.seek(end_pos)
|
fptr.seek(end_pos)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse JPEG 2000 header box.
|
"""Parse JPEG 2000 header box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -993,7 +966,6 @@ class JP2HeaderBox(Jp2kBox):
|
||||||
4-byte tuple.
|
4-byte tuple.
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
|
|
@ -1001,7 +973,7 @@ class JP2HeaderBox(Jp2kBox):
|
||||||
|
|
||||||
# The JP2 header box is a superbox, so go ahead and parse its child
|
# The JP2 header box is a superbox, so go ahead and parse its child
|
||||||
# boxes.
|
# boxes.
|
||||||
box.box = box._parse_superbox(f)
|
box.box = box._parse_superbox(fptr)
|
||||||
|
|
||||||
return box
|
return box
|
||||||
|
|
||||||
|
|
@ -1034,23 +1006,21 @@ class JPEG2000SignatureBox(Jp2kBox):
|
||||||
msg = msg.format(*self.signature)
|
msg = msg.format(*self.signature)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def _write(self, f):
|
def _write(self, fptr):
|
||||||
"""Write a JPEG 2000 Signature box to file.
|
"""Write a JPEG 2000 Signature box to file.
|
||||||
"""
|
"""
|
||||||
f.write(struct.pack('>I', 12))
|
fptr.write(struct.pack('>I', 12))
|
||||||
f.write(self.id.encode())
|
fptr.write(self.id.encode())
|
||||||
f.write(struct.pack('>BBBB', *self.signature))
|
fptr.write(struct.pack('>BBBB', *self.signature))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse JPEG 2000 signature box.
|
"""Parse JPEG 2000 signature box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1061,11 +1031,10 @@ class JPEG2000SignatureBox(Jp2kBox):
|
||||||
4-byte tuple.
|
4-byte tuple.
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
read_buffer = f.read(4)
|
read_buffer = fptr.read(4)
|
||||||
kwargs['signature'] = struct.unpack('>BBBB', read_buffer)
|
kwargs['signature'] = struct.unpack('>BBBB', read_buffer)
|
||||||
|
|
||||||
box = JPEG2000SignatureBox(**kwargs)
|
box = JPEG2000SignatureBox(**kwargs)
|
||||||
|
|
@ -1089,7 +1058,7 @@ class PaletteBox(Jp2kBox):
|
||||||
Colormap represented as list of 1D arrays, one per color component.
|
Colormap represented as list of 1D arrays, one per color component.
|
||||||
"""
|
"""
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
Jp2kBox.__init__(self, id='', longname='Palette')
|
Jp2kBox.__init__(self, id='pclr', longname='Palette')
|
||||||
self.__dict__.update(**kwargs)
|
self.__dict__.update(**kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -1099,15 +1068,13 @@ class PaletteBox(Jp2kBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse palette box.
|
"""Parse palette box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1118,16 +1085,15 @@ class PaletteBox(Jp2kBox):
|
||||||
kwargs: dictionary of parameter values
|
kwargs: dictionary of parameter values
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
# Get the size of the palette.
|
# Get the size of the palette.
|
||||||
read_buffer = f.read(3)
|
read_buffer = fptr.read(3)
|
||||||
(NE, NC) = struct.unpack('>HB', read_buffer)
|
(NE, NC) = struct.unpack('>HB', read_buffer)
|
||||||
|
|
||||||
# Need to determine bps and signed or not
|
# Need to determine bps and signed or not
|
||||||
read_buffer = f.read(NC)
|
read_buffer = fptr.read(NC)
|
||||||
data = struct.unpack('>' + 'B' * NC, read_buffer)
|
data = struct.unpack('>' + 'B' * NC, read_buffer)
|
||||||
bps = [((x & 0x07f) + 1) for x in data]
|
bps = [((x & 0x07f) + 1) for x in data]
|
||||||
signed = [((x & 0x80) > 1) for x in data]
|
signed = [((x & 0x80) > 1) for x in data]
|
||||||
|
|
@ -1160,7 +1126,7 @@ class PaletteBox(Jp2kBox):
|
||||||
msg = 'Unsupported palette bitdepth (%d).'
|
msg = 'Unsupported palette bitdepth (%d).'
|
||||||
raise IOError(msg)
|
raise IOError(msg)
|
||||||
n = NE * bytes_per_row
|
n = NE * bytes_per_row
|
||||||
read_buffer = f.read(n)
|
read_buffer = fptr.read(n)
|
||||||
|
|
||||||
for j in range(NE):
|
for j in range(NE):
|
||||||
row_buffer = read_buffer[(bytes_per_row * j):(bytes_per_row * (j + 1))]
|
row_buffer = read_buffer[(bytes_per_row * j):(bytes_per_row * (j + 1))]
|
||||||
|
|
@ -1202,7 +1168,7 @@ class ReaderRequirementsBox(Jp2kBox):
|
||||||
feature.
|
feature.
|
||||||
"""
|
"""
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
Jp2kBox.__init__(self, id='', longname='Reader Requirements')
|
Jp2kBox.__init__(self, id='rreq', longname='Reader Requirements')
|
||||||
self.__dict__.update(**kwargs)
|
self.__dict__.update(**kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -1225,15 +1191,13 @@ class ReaderRequirementsBox(Jp2kBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse reader requirements box.
|
"""Parse reader requirements box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1244,11 +1208,10 @@ class ReaderRequirementsBox(Jp2kBox):
|
||||||
ReaderRequirementsBox.
|
ReaderRequirementsBox.
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
read_buffer = f.read(1)
|
read_buffer = fptr.read(1)
|
||||||
mask_length, = struct.unpack('>B', read_buffer)
|
mask_length, = struct.unpack('>B', read_buffer)
|
||||||
if mask_length == 1:
|
if mask_length == 1:
|
||||||
mask_format = 'B'
|
mask_format = 'B'
|
||||||
|
|
@ -1263,31 +1226,31 @@ class ReaderRequirementsBox(Jp2kBox):
|
||||||
|
|
||||||
# Fully Understands Aspect Mask
|
# Fully Understands Aspect Mask
|
||||||
# Decodes Completely Mask
|
# Decodes Completely Mask
|
||||||
read_buffer = f.read(2 * mask_length)
|
read_buffer = fptr.read(2 * mask_length)
|
||||||
data = struct.unpack('>' + mask_format * 2, read_buffer)
|
data = struct.unpack('>' + mask_format * 2, read_buffer)
|
||||||
kwargs['FUAM'] = data[0]
|
kwargs['FUAM'] = data[0]
|
||||||
kwargs['DCM'] = data[1]
|
kwargs['DCM'] = data[1]
|
||||||
|
|
||||||
read_buffer = f.read(2)
|
read_buffer = fptr.read(2)
|
||||||
num_standard_flags, = struct.unpack('>H', read_buffer)
|
num_standard_flags, = struct.unpack('>H', read_buffer)
|
||||||
|
|
||||||
# Read in standard flags and standard masks. Each standard flag should
|
# Read in standard flags and standard masks. Each standard flag should
|
||||||
# be two bytes, but the standard mask flag is as long as specified by
|
# be two bytes, but the standard mask flag is as long as specified by
|
||||||
# the mask length.
|
# the mask length.
|
||||||
read_buffer = f.read(num_standard_flags * (2 + mask_length))
|
read_buffer = fptr.read(num_standard_flags * (2 + mask_length))
|
||||||
data = struct.unpack('>' + ('H' + mask_format) * num_standard_flags,
|
data = struct.unpack('>' + ('H' + mask_format) * num_standard_flags,
|
||||||
read_buffer)
|
read_buffer)
|
||||||
kwargs['standard_flag'] = data[0:num_standard_flags * 2:2]
|
kwargs['standard_flag'] = data[0:num_standard_flags * 2:2]
|
||||||
kwargs['standard_mask'] = data[1:num_standard_flags * 2:2]
|
kwargs['standard_mask'] = data[1:num_standard_flags * 2:2]
|
||||||
|
|
||||||
# Vendor features
|
# Vendor features
|
||||||
read_buffer = f.read(2)
|
read_buffer = fptr.read(2)
|
||||||
num_vendor_features, = struct.unpack('>H', read_buffer)
|
num_vendor_features, = struct.unpack('>H', read_buffer)
|
||||||
|
|
||||||
# Each vendor feature consists of a 16-byte UUID plus a mask whose
|
# Each vendor feature consists of a 16-byte UUID plus a mask whose
|
||||||
# length is specified by, you guessed it, "mask_length".
|
# length is specified by, you guessed it, "mask_length".
|
||||||
entry_length = 16 + mask_length
|
entry_length = 16 + mask_length
|
||||||
read_buffer = f.read(num_vendor_features * entry_length)
|
read_buffer = fptr.read(num_vendor_features * entry_length)
|
||||||
vendor_feature = []
|
vendor_feature = []
|
||||||
vendor_mask = []
|
vendor_mask = []
|
||||||
for j in range(num_vendor_features):
|
for j in range(num_vendor_features):
|
||||||
|
|
@ -1321,7 +1284,7 @@ class ResolutionBox(Jp2kBox):
|
||||||
List of boxes contained in this superbox.
|
List of boxes contained in this superbox.
|
||||||
"""
|
"""
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
Jp2kBox.__init__(self, id='', longname='Resolution')
|
Jp2kBox.__init__(self, id='res ', longname='Resolution')
|
||||||
self.__dict__.update(**kwargs)
|
self.__dict__.update(**kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -1335,15 +1298,13 @@ class ResolutionBox(Jp2kBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse Resolution box.
|
"""Parse Resolution box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1355,7 +1316,6 @@ class ResolutionBox(Jp2kBox):
|
||||||
4-byte tuple.
|
4-byte tuple.
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
|
|
@ -1363,7 +1323,7 @@ class ResolutionBox(Jp2kBox):
|
||||||
|
|
||||||
# The JP2 header box is a superbox, so go ahead and parse its child
|
# The JP2 header box is a superbox, so go ahead and parse its child
|
||||||
# boxes.
|
# boxes.
|
||||||
box.box = box._parse_superbox(f)
|
box.box = box._parse_superbox(fptr)
|
||||||
|
|
||||||
return box
|
return box
|
||||||
|
|
||||||
|
|
@ -1385,7 +1345,7 @@ class CaptureResolutionBox(ResolutionBox):
|
||||||
Vertical, horizontal resolution.
|
Vertical, horizontal resolution.
|
||||||
"""
|
"""
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
ResolutionBox.__init__(self, id='', longname='Capture Resolution')
|
ResolutionBox.__init__(self, id='resc', longname='Capture Resolution')
|
||||||
self.__dict__.update(**kwargs)
|
self.__dict__.update(**kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -1395,15 +1355,13 @@ class CaptureResolutionBox(ResolutionBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse Resolution box.
|
"""Parse Resolution box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1415,14 +1373,13 @@ class CaptureResolutionBox(ResolutionBox):
|
||||||
4-byte tuple.
|
4-byte tuple.
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
read_buffer = f.read(10)
|
read_buffer = fptr.read(10)
|
||||||
(RN1, RD1, RN2, RD2, RE1, RE2) = struct.unpack('>HHHHBB', read_buffer)
|
(rn1, rd1, rn2, rd2, re1, re2) = struct.unpack('>HHHHBB', read_buffer)
|
||||||
kwargs['VR'] = RN1 / RD1 * math.pow(10, RE1)
|
kwargs['VR'] = rn1 / rd1 * math.pow(10, re1)
|
||||||
kwargs['HR'] = RN2 / RD2 * math.pow(10, RE2)
|
kwargs['HR'] = rn2 / rd2 * math.pow(10, re2)
|
||||||
|
|
||||||
box = CaptureResolutionBox(**kwargs)
|
box = CaptureResolutionBox(**kwargs)
|
||||||
|
|
||||||
|
|
@ -1446,7 +1403,7 @@ class DisplayResolutionBox(ResolutionBox):
|
||||||
Vertical, horizontal resolution.
|
Vertical, horizontal resolution.
|
||||||
"""
|
"""
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
ResolutionBox.__init__(self, id='', longname='Display Resolution')
|
ResolutionBox.__init__(self, id='resd', longname='Display Resolution')
|
||||||
self.__dict__.update(**kwargs)
|
self.__dict__.update(**kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -1456,15 +1413,13 @@ class DisplayResolutionBox(ResolutionBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse Resolution box.
|
"""Parse Resolution box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1476,14 +1431,13 @@ class DisplayResolutionBox(ResolutionBox):
|
||||||
4-byte tuple.
|
4-byte tuple.
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
read_buffer = f.read(10)
|
read_buffer = fptr.read(10)
|
||||||
(RN1, RD1, RN2, RD2, RE1, RE2) = struct.unpack('>HHHHBB', read_buffer)
|
(rn1, rd1, rn2, rd2, re1, re2) = struct.unpack('>HHHHBB', read_buffer)
|
||||||
kwargs['VR'] = RN1 / RD1 * math.pow(10, RE1)
|
kwargs['VR'] = rn1 / rd1 * math.pow(10, re1)
|
||||||
kwargs['HR'] = RN2 / RD2 * math.pow(10, RE2)
|
kwargs['HR'] = rn2 / rd2 * math.pow(10, re2)
|
||||||
|
|
||||||
box = DisplayResolutionBox(**kwargs)
|
box = DisplayResolutionBox(**kwargs)
|
||||||
|
|
||||||
|
|
@ -1507,7 +1461,7 @@ class LabelBox(Jp2kBox):
|
||||||
Label
|
Label
|
||||||
"""
|
"""
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
Jp2kBox.__init__(self, id='', longname='Label')
|
Jp2kBox.__init__(self, id='lbl ', longname='Label')
|
||||||
self.__dict__.update(**kwargs)
|
self.__dict__.update(**kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -1516,15 +1470,13 @@ class LabelBox(Jp2kBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse Label box.
|
"""Parse Label box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1535,12 +1487,11 @@ class LabelBox(Jp2kBox):
|
||||||
kwargs : dictionary of parameter values
|
kwargs : dictionary of parameter values
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
num_bytes = offset + length - f.tell()
|
num_bytes = offset + length - fptr.tell()
|
||||||
read_buffer = f.read(num_bytes)
|
read_buffer = fptr.read(num_bytes)
|
||||||
kwargs['label'] = read_buffer.decode('utf-8')
|
kwargs['label'] = read_buffer.decode('utf-8')
|
||||||
box = LabelBox(**kwargs)
|
box = LabelBox(**kwargs)
|
||||||
return box
|
return box
|
||||||
|
|
@ -1594,7 +1545,7 @@ class XMLBox(Jp2kBox):
|
||||||
msg += '\n {0}'.format(xml)
|
msg += '\n {0}'.format(xml)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def _write(self, f):
|
def _write(self, fptr):
|
||||||
"""Write an XML box to file.
|
"""Write an XML box to file.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
|
@ -1602,20 +1553,18 @@ class XMLBox(Jp2kBox):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
read_buffer = ET.tostring(self.xml.getroot(), encoding='utf-8')
|
read_buffer = ET.tostring(self.xml.getroot(), encoding='utf-8')
|
||||||
|
|
||||||
f.write(struct.pack('>I', len(read_buffer) + 8))
|
fptr.write(struct.pack('>I', len(read_buffer) + 8))
|
||||||
f.write(self.id.encode())
|
fptr.write(self.id.encode())
|
||||||
f.write(read_buffer)
|
fptr.write(read_buffer)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(f, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse XML box.
|
"""Parse XML box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1626,12 +1575,11 @@ class XMLBox(Jp2kBox):
|
||||||
kwargs : dictionary of parameter values
|
kwargs : dictionary of parameter values
|
||||||
"""
|
"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
kwargs['id'] = id
|
|
||||||
kwargs['length'] = length
|
kwargs['length'] = length
|
||||||
kwargs['offset'] = offset
|
kwargs['offset'] = offset
|
||||||
|
|
||||||
num_bytes = offset + length - f.tell()
|
num_bytes = offset + length - fptr.tell()
|
||||||
read_buffer = f.read(num_bytes)
|
read_buffer = fptr.read(num_bytes)
|
||||||
text = read_buffer.decode('utf-8')
|
text = read_buffer.decode('utf-8')
|
||||||
|
|
||||||
# Strip out any trailing nulls.
|
# Strip out any trailing nulls.
|
||||||
|
|
@ -1678,15 +1626,13 @@ class UUIDListBox(Jp2kBox):
|
||||||
return(msg)
|
return(msg)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(fptr, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse UUIDList box.
|
"""Parse UUIDList box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
f : file
|
f : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1743,15 +1689,13 @@ class UUIDInfoBox(Jp2kBox):
|
||||||
return(msg)
|
return(msg)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(fptr, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse UUIDInfo super box.
|
"""Parse UUIDInfo super box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
fptr : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1813,15 +1757,13 @@ class DataEntryURLBox(Jp2kBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(fptr, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse Data Entry URL box.
|
"""Parse Data Entry URL box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
fptr : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : byte
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
@ -1934,15 +1876,13 @@ class UUIDBox(Jp2kBox):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(fptr, id, offset, length):
|
def _parse(fptr, offset, length):
|
||||||
"""Parse UUID box.
|
"""Parse UUID box.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
fptr : file
|
fptr : file
|
||||||
Open file object.
|
Open file object.
|
||||||
id : str
|
|
||||||
4-byte unique identifier for this box.
|
|
||||||
offset : int
|
offset : int
|
||||||
Start position of box in bytes.
|
Start position of box in bytes.
|
||||||
length : int
|
length : int
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue