From b0566e47915f198b2d0c202b343091e669e7bf63 Mon Sep 17 00:00:00 2001 From: jevans Date: Sun, 28 Jul 2013 16:14:20 -0400 Subject: [PATCH] Some read support in 2.0.0. Some segfaults, though. #90 --- glymur/jp2k.py | 13 +++++++-- glymur/lib/openjp2.py | 67 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 2d9b220..15d4123 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -32,6 +32,7 @@ from .jp2box import ImageHeaderBox from .jp2box import ColourSpecificationBox from .lib import _openjpeg as _opj from .lib import _openjp2 as _opj2 +from .lib import c _COLORSPACE_MAP = {'rgb': _opj2.CLRSPC_SRGB, 'gray': _opj2.CLRSPC_GRAY, @@ -882,9 +883,15 @@ class Jp2k(Jp2kBox): dparam.nb_tile_to_decode = 1 with ExitStack() as stack: - stream = _opj2.stream_create_default_file_stream_v3(self.filename, - True) - stack.callback(_opj2.stream_destroy_v3, stream) + if hasattr(_opj2.OPENJPEG, 'opj_stream_create_default_file_stream_v3'): + stream = _opj2.stream_create_default_file_stream_v3(self.filename, + True) + stack.callback(_opj2.stream_destroy_v3, stream) + else: + fptr = c.fopen(self.filename, 'rb') + stack.callback(c.fclose, fptr) + stream = _opj2.stream_create_default_file_stream(fptr, True) + stack.callback(_opj2.stream_destroy, stream) codec = _opj2.create_decompress(self._codec_format) stack.callback(_opj2.destroy_codec, codec) diff --git a/glymur/lib/openjp2.py b/glymur/lib/openjp2.py index be9981b..946b2e8 100644 --- a/glymur/lib/openjp2.py +++ b/glymur/lib/openjp2.py @@ -639,9 +639,16 @@ if OPENJP2 is not None: ctypes.POINTER(ImageType)] OPENJP2.opj_setup_encoder.argtypes = ARGTYPES - ARGTYPES = [ctypes.c_char_p, ctypes.c_int32] - OPENJP2.opj_stream_create_default_file_stream_v3.argtypes = ARGTYPES - OPENJP2.opj_stream_create_default_file_stream_v3.restype = STREAM_TYPE_P + if hasattr(OPENJP2, 'opj_stream_create_default_file_stream_v3'): + ARGTYPES = [ctypes.c_char_p, ctypes.c_int32] + OPENJP2.opj_stream_create_default_file_stream_v3.argtypes = ARGTYPES + OPENJP2.opj_stream_create_default_file_stream_v3.restype = STREAM_TYPE_P + OPENJP2.opj_stream_destroy_v3.argtypes = [STREAM_TYPE_P] + else: + ARGTYPES = [ctypes.c_void_p, ctypes.c_int32] + OPENJP2.opj_stream_create_default_file_stream.argtypes = ARGTYPES + OPENJP2.opj_stream_create_default_file_stream.restype = STREAM_TYPE_P + OPENJP2.opj_stream_destroy.argtypes = [STREAM_TYPE_P] ARGTYPES = [CODEC_TYPE, ctypes.POINTER(ImageType), STREAM_TYPE_P] OPENJP2.opj_start_compress.argtypes = ARGTYPES @@ -649,7 +656,6 @@ if OPENJP2 is not None: OPENJP2.opj_end_compress.argtypes = [CODEC_TYPE, STREAM_TYPE_P] OPENJP2.opj_end_decompress.argtypes = [CODEC_TYPE, STREAM_TYPE_P] - OPENJP2.opj_stream_destroy_v3.argtypes = [STREAM_TYPE_P] OPENJP2.opj_destroy_codec.argtypes = [CODEC_TYPE] ARGTYPES = [CODEC_TYPE, @@ -1267,16 +1273,17 @@ def start_compress(codec, image, stream): OPENJP2.opj_start_compress(codec, image, stream) -def stream_create_default_file_stream_v3(fname, a_read_stream): - """Wraps openjp2 library function opj_stream_create_default_vile_stream_v3. +def stream_create_default_file_stream(fptr, isa_read_stream): + """Wraps openjp2 library function opj_stream_create_default_vile_stream. - Sets the stream to be a file stream. + Sets the stream to be a file stream. This is valid only for version 2.0.0 + of OpenJPEG. Parameters ---------- - fname : str - Specifies a file. - a_read_stream: bool + fptr : ctypes.c_void_p + Corresponds to C file pointer. Must be obtained from libc.fopen. + isa_read_stream: bool True (read) or False (write) Returns @@ -1284,16 +1291,52 @@ def stream_create_default_file_stream_v3(fname, a_read_stream): stream : stream_t An OpenJPEG file stream. """ - read_stream = 1 if a_read_stream else 0 + read_stream = 1 if isa_read_stream else 0 + stream = OPENJP2.opj_stream_create_default_file_stream(fptr, read_stream) + return stream + + +def stream_create_default_file_stream_v3(fname, isa_read_stream): + """Wraps openjp2 library function opj_stream_create_default_vile_stream_v3. + + Sets the stream to be a file stream. This function is only valid for the + trunk/development 2.0+ version of the openjp2 library. + + Parameters + ---------- + fname : str + Specifies a file. + isa_read_stream: bool + True (read) or False (write) + + Returns + ------- + stream : stream_t + An OpenJPEG file stream. + """ + read_stream = 1 if isa_read_stream else 0 file_argument = ctypes.c_char_p(fname.encode()) stream = OPENJP2.opj_stream_create_default_file_stream_v3(file_argument, read_stream) return stream -def stream_destroy_v3(stream): +def stream_destroy(stream): """Wraps openjp2 library function opj_stream_destroy. + Destroys the stream created by create_stream. + + Parameters + ---------- + stream : STREAM_TYPE_P + The file stream. + """ + OPENJP2.opj_stream_destroy(stream) + + +def stream_destroy_v3(stream): + """Wraps openjp2 library function opj_stream_destroy_v3. + Destroys the stream created by create_stream_v3. Parameters