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,6 +615,7 @@ class Jp2k(Jp2kBox):
If the image has differing subsample factors. If the image has differing subsample factors.
""" """
with ExitStack() as stack:
# Set decoding parameters. # Set decoding parameters.
dparameters = opj.dparameters_t() dparameters = opj.dparameters_t()
opj._set_default_decoder_parameters(ctypes.byref(dparameters)) opj._set_default_decoder_parameters(ctypes.byref(dparameters))
@ -627,13 +628,20 @@ class Jp2k(Jp2kBox):
dparameters.infile = infile dparameters.infile = infile
dinfo = opj._create_decompress(dparameters.decod_format) dinfo = opj._create_decompress(dparameters.decod_format)
opj._setup_decoder(dinfo, dparameters) opj._setup_decoder(dinfo, dparameters)
with open(self.filename, 'rb') as fp: with open(self.filename, 'rb') as fp:
src = fp.read() src = fp.read()
cio = opj._cio_open(dinfo, src) cio = opj._cio_open(dinfo, src)
image = opj._decode(dinfo, cio) image = opj._decode(dinfo, cio)
stack.callback(opj._image_destroy, image)
stack.callback(opj._destroy_decompress, dinfo)
stack.callback(opj._cio_close, cio)
ncomps = image.contents.numcomps ncomps = image.contents.numcomps
component = image.contents.comps[0] component = image.contents.comps[0]
@ -675,10 +683,6 @@ class Jp2k(Jp2kBox):
x = np.ctypeslib.as_array( (ctypes.c_int32 * nrows * ncols).from_address(addr)) x = np.ctypeslib.as_array( (ctypes.c_int32 * nrows * ncols).from_address(addr))
data[:, :, k] = np.reshape(x.astype(dtype), (nrows, ncols)) 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]