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,41 +474,37 @@ class Jp2k(Jp2kBox):
comptparms = _populate_comptparms(img_array, cparams)
image = opj2.image_create(comptparms, colorspace)
_populate_image_struct(cparams, image, img_array)
codec = opj2.create_compress(cparams.codec_fmt)
info_handler = _INFO_CALLBACK if verbose else None
opj2.set_info_handler(codec, info_handler)
opj2.set_warning_handler(codec, _WARNING_CALLBACK)
opj2.set_error_handler(codec, _ERROR_CALLBACK)
opj2.setup_encoder(codec, cparams, image)
if _OPENJP2_IS_OFFICIAL_V2:
fptr = libc.fopen(self.filename, 'wb')
strm = opj2.stream_create_default_file_stream(fptr, False)
else:
# This routine introduced in 2.0 devel series.
strm = opj2.stream_create_default_file_stream_v3(self.filename,
False)
# Start to clean up after ourselves.
opj2.start_compress(codec, image, strm)
opj2.encode(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)
with ExitStack() as stack:
image = opj2.image_create(comptparms, colorspace)
stack.callback(opj2.image_destroy, image)
_populate_image_struct(cparams, image, img_array)
codec = opj2.create_compress(cparams.codec_fmt)
stack.callback(opj2.destroy_codec, codec)
info_handler = _INFO_CALLBACK if verbose else None
opj2.set_info_handler(codec, info_handler)
opj2.set_warning_handler(codec, _WARNING_CALLBACK)
opj2.set_error_handler(codec, _ERROR_CALLBACK)
opj2.setup_encoder(codec, cparams, image)
if _OPENJP2_IS_OFFICIAL_V2:
fptr = libc.fopen(self.filename, 'wb')
strm = opj2.stream_create_default_file_stream(fptr, False)
stack.callback(opj2.stream_destroy, strm)
stack.callback(libc.fclose, fptr)
else:
# This routine introduced in 2.0 devel series.
strm = opj2.stream_create_default_file_stream_v3(self.filename,
False)
stack.callback(opj2.stream_destroy_v3, strm)
opj2.start_compress(codec, image, strm)
opj2.encode(codec, strm)
opj2.end_compress(codec, strm)
# Refresh the metadata.
self.parse()