Some read support in 2.0.0. Some segfaults, though. #90
This commit is contained in:
parent
589dec5189
commit
b0566e4791
2 changed files with 65 additions and 15 deletions
|
|
@ -32,6 +32,7 @@ from .jp2box import ImageHeaderBox
|
||||||
from .jp2box import ColourSpecificationBox
|
from .jp2box import ColourSpecificationBox
|
||||||
from .lib import _openjpeg as _opj
|
from .lib import _openjpeg as _opj
|
||||||
from .lib import _openjp2 as _opj2
|
from .lib import _openjp2 as _opj2
|
||||||
|
from .lib import c
|
||||||
|
|
||||||
_COLORSPACE_MAP = {'rgb': _opj2.CLRSPC_SRGB,
|
_COLORSPACE_MAP = {'rgb': _opj2.CLRSPC_SRGB,
|
||||||
'gray': _opj2.CLRSPC_GRAY,
|
'gray': _opj2.CLRSPC_GRAY,
|
||||||
|
|
@ -882,9 +883,15 @@ class Jp2k(Jp2kBox):
|
||||||
dparam.nb_tile_to_decode = 1
|
dparam.nb_tile_to_decode = 1
|
||||||
|
|
||||||
with ExitStack() as stack:
|
with ExitStack() as stack:
|
||||||
stream = _opj2.stream_create_default_file_stream_v3(self.filename,
|
if hasattr(_opj2.OPENJPEG, 'opj_stream_create_default_file_stream_v3'):
|
||||||
True)
|
stream = _opj2.stream_create_default_file_stream_v3(self.filename,
|
||||||
stack.callback(_opj2.stream_destroy_v3, stream)
|
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)
|
codec = _opj2.create_decompress(self._codec_format)
|
||||||
stack.callback(_opj2.destroy_codec, codec)
|
stack.callback(_opj2.destroy_codec, codec)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -639,9 +639,16 @@ if OPENJP2 is not None:
|
||||||
ctypes.POINTER(ImageType)]
|
ctypes.POINTER(ImageType)]
|
||||||
OPENJP2.opj_setup_encoder.argtypes = ARGTYPES
|
OPENJP2.opj_setup_encoder.argtypes = ARGTYPES
|
||||||
|
|
||||||
ARGTYPES = [ctypes.c_char_p, ctypes.c_int32]
|
if hasattr(OPENJP2, 'opj_stream_create_default_file_stream_v3'):
|
||||||
OPENJP2.opj_stream_create_default_file_stream_v3.argtypes = ARGTYPES
|
ARGTYPES = [ctypes.c_char_p, ctypes.c_int32]
|
||||||
OPENJP2.opj_stream_create_default_file_stream_v3.restype = STREAM_TYPE_P
|
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]
|
ARGTYPES = [CODEC_TYPE, ctypes.POINTER(ImageType), STREAM_TYPE_P]
|
||||||
OPENJP2.opj_start_compress.argtypes = ARGTYPES
|
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_compress.argtypes = [CODEC_TYPE, STREAM_TYPE_P]
|
||||||
OPENJP2.opj_end_decompress.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]
|
OPENJP2.opj_destroy_codec.argtypes = [CODEC_TYPE]
|
||||||
|
|
||||||
ARGTYPES = [CODEC_TYPE,
|
ARGTYPES = [CODEC_TYPE,
|
||||||
|
|
@ -1267,16 +1273,17 @@ def start_compress(codec, image, stream):
|
||||||
OPENJP2.opj_start_compress(codec, image, stream)
|
OPENJP2.opj_start_compress(codec, image, stream)
|
||||||
|
|
||||||
|
|
||||||
def stream_create_default_file_stream_v3(fname, a_read_stream):
|
def stream_create_default_file_stream(fptr, isa_read_stream):
|
||||||
"""Wraps openjp2 library function opj_stream_create_default_vile_stream_v3.
|
"""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
|
Parameters
|
||||||
----------
|
----------
|
||||||
fname : str
|
fptr : ctypes.c_void_p
|
||||||
Specifies a file.
|
Corresponds to C file pointer. Must be obtained from libc.fopen.
|
||||||
a_read_stream: bool
|
isa_read_stream: bool
|
||||||
True (read) or False (write)
|
True (read) or False (write)
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
|
|
@ -1284,16 +1291,52 @@ def stream_create_default_file_stream_v3(fname, a_read_stream):
|
||||||
stream : stream_t
|
stream : stream_t
|
||||||
An OpenJPEG file stream.
|
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())
|
file_argument = ctypes.c_char_p(fname.encode())
|
||||||
stream = OPENJP2.opj_stream_create_default_file_stream_v3(file_argument,
|
stream = OPENJP2.opj_stream_create_default_file_stream_v3(file_argument,
|
||||||
read_stream)
|
read_stream)
|
||||||
return stream
|
return stream
|
||||||
|
|
||||||
|
|
||||||
def stream_destroy_v3(stream):
|
def stream_destroy(stream):
|
||||||
"""Wraps openjp2 library function opj_stream_destroy.
|
"""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.
|
Destroys the stream created by create_stream_v3.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue