openjp2 write method rewritten for exitstack support. #45

This commit is contained in:
John Evans 2013-10-08 09:51:43 -04:00
commit f0db9dfa2d

View file

@ -474,10 +474,14 @@ class Jp2k(Jp2kBox):
comptparms = _populate_comptparms(img_array, cparams) comptparms = _populate_comptparms(img_array, cparams)
with ExitStack() as stack:
image = opj2.image_create(comptparms, colorspace) image = opj2.image_create(comptparms, colorspace)
stack.callback(opj2.image_destroy, image)
_populate_image_struct(cparams, image, img_array) _populate_image_struct(cparams, image, img_array)
codec = opj2.create_compress(cparams.codec_fmt) codec = opj2.create_compress(cparams.codec_fmt)
stack.callback(opj2.destroy_codec, codec)
info_handler = _INFO_CALLBACK if verbose else None info_handler = _INFO_CALLBACK if verbose else None
opj2.set_info_handler(codec, info_handler) opj2.set_info_handler(codec, info_handler)
@ -489,26 +493,18 @@ class Jp2k(Jp2kBox):
if _OPENJP2_IS_OFFICIAL_V2: if _OPENJP2_IS_OFFICIAL_V2:
fptr = libc.fopen(self.filename, 'wb') fptr = libc.fopen(self.filename, 'wb')
strm = opj2.stream_create_default_file_stream(fptr, False) strm = opj2.stream_create_default_file_stream(fptr, False)
stack.callback(opj2.stream_destroy, strm)
stack.callback(libc.fclose, fptr)
else: else:
# This routine introduced in 2.0 devel series. # This routine introduced in 2.0 devel series.
strm = opj2.stream_create_default_file_stream_v3(self.filename, strm = opj2.stream_create_default_file_stream_v3(self.filename,
False) False)
stack.callback(opj2.stream_destroy_v3, strm)
# Start to clean up after ourselves.
opj2.start_compress(codec, image, strm) opj2.start_compress(codec, image, strm)
opj2.encode(codec, strm) opj2.encode(codec, strm)
opj2.end_compress(codec, strm) opj2.end_compress(codec, strm)
if _OPENJP2_IS_OFFICIAL_V2:
opj2.stream_destroy(strm)
libc.fclose(fptr)
else:
# This routine introduced in 2.0 devel series.
opj2.stream_destroy_v3(strm)
opj2.destroy_codec(codec)
opj2.image_destroy(image)
# Refresh the metadata. # Refresh the metadata.
self.parse() self.parse()