Added ExitStack support to 1.5.1 reader.

This commit is contained in:
John Evans 2013-07-04 15:31:18 -04:00
commit 5035220a26

View file

@ -615,70 +615,74 @@ class Jp2k(Jp2kBox):
If the image has differing subsample factors. If the image has differing subsample factors.
""" """
# Set decoding parameters. with ExitStack() as stack:
dparameters = opj.dparameters_t() # Set decoding parameters.
opj._set_default_decoder_parameters(ctypes.byref(dparameters)) dparameters = opj.dparameters_t()
dparameters.cp_reduce = reduce opj._set_default_decoder_parameters(ctypes.byref(dparameters))
dparameters.decod_format = self._codec_format dparameters.cp_reduce = reduce
dparameters.decod_format = self._codec_format
infile = self.filename.encode()
nelts = opj._PATH_LEN - len(infile)
infile += b'0' * nelts
dparameters.infile = infile
dinfo = opj._create_decompress(dparameters.decod_format)
infile = self.filename.encode() opj._setup_decoder(dinfo, dparameters)
nelts = opj._PATH_LEN - len(infile)
infile += b'0' * nelts with open(self.filename, 'rb') as fp:
dparameters.infile = infile src = fp.read()
cio = opj._cio_open(dinfo, src)
dinfo = opj._create_decompress(dparameters.decod_format)
opj._setup_decoder(dinfo, dparameters)
with open(self.filename, 'rb') as fp: image = opj._decode(dinfo, cio)
src = fp.read()
cio = opj._cio_open(dinfo, src) stack.callback(opj._image_destroy, image)
image = opj._decode(dinfo, cio) stack.callback(opj._destroy_decompress, dinfo)
ncomps = image.contents.numcomps stack.callback(opj._cio_close, cio)
component = image.contents.comps[0] ncomps = image.contents.numcomps
if component.sgnd:
if component.prec <= 8: component = image.contents.comps[0]
dtype = np.int8 if component.sgnd:
elif component.prec <= 16: if component.prec <= 8:
dtype = np.int16 dtype = np.int8
elif component.prec <= 16:
dtype = np.int16
else:
raise RuntimeError("Unhandled precision, datatype")
else: else:
raise RuntimeError("Unhandled precision, datatype") if component.prec <= 8:
else: dtype = np.uint8
if component.prec <= 8: elif component.prec <= 16:
dtype = np.uint8 dtype = np.uint16
elif component.prec <= 16: else:
dtype = np.uint16 raise RuntimeError("Unhandled precision, datatype")
else:
raise RuntimeError("Unhandled precision, datatype") nrows = image.contents.comps[0].h
ncols = image.contents.comps[0].w
nrows = image.contents.comps[0].h ncomps = image.contents.numcomps
ncols = image.contents.comps[0].w data = np.zeros((nrows, ncols, ncomps), dtype)
ncomps = image.contents.numcomps
data = np.zeros((nrows, ncols, ncomps), dtype) for k in range(image.contents.numcomps):
component = image.contents.comps[k]
for k in range(image.contents.numcomps): nrows = component.h
component = image.contents.comps[k] ncols = component.w
nrows = component.h
ncols = component.w if nrows == 0 or ncols == 0:
# Letting this situation continue would segfault
if nrows == 0 or ncols == 0: # Python.
# Letting this situation continue would segfault msg = "Component {0} has dimensions {1} x {2}"
# Python. msg = msg.format(k, nrows, ncols)
msg = "Component {0} has dimensions {1} x {2}" raise IOError(msg)
msg = msg.format(k, nrows, ncols)
raise IOError(msg) addr = ctypes.addressof(component.data.contents)
with warnings.catch_warnings():
addr = ctypes.addressof(component.data.contents) warnings.simplefilter("ignore")
with warnings.catch_warnings(): x = np.ctypeslib.as_array( (ctypes.c_int32 * nrows * ncols).from_address(addr))
warnings.simplefilter("ignore") data[:, :, k] = np.reshape(x.astype(dtype), (nrows, ncols))
x = np.ctypeslib.as_array( (ctypes.c_int32 * nrows * ncols).from_address(addr))
data[:, :, k] = np.reshape(x.astype(dtype), (nrows, ncols))
opj._cio_close(cio)
opj._destroy_decompress(dinfo)
opj._image_destroy(image)
if data.shape[2] == 1: if data.shape[2] == 1:
data = data.view() data = data.view()
data.shape = data.shape[0:2] data.shape = data.shape[0:2]