From 4d09208f205b967f746ef25b728fe1c7813f27c9 Mon Sep 17 00:00:00 2001 From: jevans Date: Mon, 8 Jul 2013 21:22:20 -0400 Subject: [PATCH] pylint work, low-level library function work, #71 Change the library organization around such that the library module contents are visible, but the module is protected. --- glymur/jp2k.py | 132 +++--- glymur/lib/__init__.py | 4 +- glymur/lib/openjp2.py | 601 ++++++++++++++-------------- glymur/lib/openjpeg.py | 82 ++-- glymur/lib/test/test_openjp2.py | 140 ++++--- glymur/lib/test/test_openjpeg.py | 8 +- glymur/test/test_callbacks.py | 10 +- glymur/test/test_config.py | 4 +- glymur/test/test_jp2box.py | 4 +- glymur/test/test_jp2k.py | 12 +- glymur/test/test_opj_suite.py | 10 +- glymur/test/test_opj_suite_neg.py | 2 +- glymur/test/test_opj_suite_write.py | 2 +- glymur/test/test_printing.py | 2 +- 14 files changed, 503 insertions(+), 510 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 2cc8712..f9e4cc5 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -18,13 +18,13 @@ import numpy as np from .codestream import Codestream from .core import * from .jp2box import * -from .lib import openjpeg as opj -from .lib import openjp2 as opj2 +from .lib import _openjpeg as _opj +from .lib import _openjp2 as _opj2 -_cspace_map = {'rgb': opj2._CLRSPC_SRGB, - 'gray': opj2._CLRSPC_GRAY, - 'grey': opj2._CLRSPC_GRAY, - 'ycc': opj2._CLRSPC_YCC} +_cspace_map = {'rgb': _opj2.CLRSPC_SRGB, + 'gray': _opj2.CLRSPC_GRAY, + 'grey': _opj2.CLRSPC_GRAY, + 'ycc': _opj2.CLRSPC_YCC} # Setup the default callback handlers. See the callback functions subsection # in the ctypes section of the Python documentation for a solid explanation of @@ -34,7 +34,7 @@ _CMPFUNC = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p, ctypes.c_void_p) def _default_error_handler(msg, client_data): msg = "OpenJPEG library error: {0}".format(msg.decode('utf-8').rstrip()) - opj2._set_error_message(msg) + _opj2.set_error_message(msg) def _default_info_handler(msg, client_data): @@ -113,12 +113,12 @@ class Jp2k(Jp2kBox): buffer = f.read(2) signature, = struct.unpack('>H', buffer) if signature == 0xff4f: - self._codec_format = opj2._CODEC_J2K + self._codec_format = _opj2.CODEC_J2K # That's it, we're done. The codestream object is only # produced upon explicit request. return - self._codec_format = opj2._CODEC_JP2 + self._codec_format = _opj2.CODEC_JP2 # Should be JP2. # First 4 bytes should be 12, the length of the 'jP ' box. @@ -210,17 +210,17 @@ class Jp2k(Jp2kBox): >>> j.write(data.astype(np.uint8)) """ - cparams = opj2._set_default_encoder_parameters() + cparams = _opj2.set_default_encoder_parameters() outfile = self.filename.encode() - n = opj2._PATH_LEN - len(outfile) + n = _opj2.PATH_LEN - len(outfile) outfile += b'0' * n cparams.outfile = outfile if self.filename[-4:].lower() == '.jp2': - codec_fmt = opj2._CODEC_JP2 + codec_fmt = _opj2.CODEC_JP2 else: - codec_fmt = opj2._CODEC_J2K + codec_fmt = _opj2.CODEC_J2K cparams.cod_format = codec_fmt @@ -304,7 +304,7 @@ class Jp2k(Jp2kBox): if tilesize is not None: cparams.cp_tdx = tilesize[1] cparams.cp_tdy = tilesize[0] - cparams.tile_size_on = opj2._TRUE + cparams.tile_size_on = _opj2.TRUE if cratios is not None and psnr is not None: msg = "Cannot specify cratios and psnr together." @@ -323,12 +323,12 @@ class Jp2k(Jp2kBox): if colorspace is None: if img_array.shape[2] == 1 or img_array.shape[2] == 2: - colorspace = opj2._CLRSPC_GRAY + colorspace = _opj2.CLRSPC_GRAY else: # No YCC unless specifically told to do so. - colorspace = opj2._CLRSPC_SRGB + colorspace = _opj2.CLRSPC_SRGB else: - if codec_fmt == opj2._CODEC_J2K: + if codec_fmt == _opj2.CODEC_J2K: raise IOError('Do not specify a colorspace with J2K.') colorspace = colorspace.lower() if colorspace not in ('rgb', 'grey', 'gray'): @@ -341,12 +341,12 @@ class Jp2k(Jp2kBox): colorspace = _cspace_map[colorspace] if mct is None: - if colorspace == opj2._CLRSPC_SRGB: + if colorspace == _opj2.CLRSPC_SRGB: cparams.tcp_mct = 1 else: cparams.tcp_mct = 0 else: - if mct and colorspace == opj2._CLRSPC_GRAY: + if mct and colorspace == _opj2.CLRSPC_GRAY: msg = "Cannot specify usage of the multi component transform " msg += "if the colorspace is gray." raise IOError(msg) @@ -359,7 +359,7 @@ class Jp2k(Jp2kBox): else: raise RuntimeError("unhandled datatype") - comptparms = (opj2._image_comptparm_t * num_comps)() + comptparms = (_opj2.image_comptparm_t * num_comps)() for j in range(num_comps): comptparms[j].dx = cparams.subsampling_dx comptparms[j].dy = cparams.subsampling_dy @@ -371,7 +371,7 @@ class Jp2k(Jp2kBox): comptparms[j].bpp = comp_prec comptparms[j].sgnd = 0 - image = opj2._image_create(comptparms, colorspace) + image = _opj2.image_create(comptparms, colorspace) # set image offset and reference grid image.contents.x0 = cparams.image_offset_x0 @@ -388,23 +388,23 @@ class Jp2k(Jp2kBox): src = layer.ctypes.data ctypes.memmove(dest, src, layer.nbytes) - codec = opj2._create_compress(codec_fmt) + codec = _opj2.create_compress(codec_fmt) if verbose: - opj2._set_info_handler(codec, _info_callback) + _opj2.set_info_handler(codec, _info_callback) else: - opj2._set_info_handler(codec, None) + _opj2.set_info_handler(codec, None) - opj2._set_warning_handler(codec, _warning_callback) - opj2._set_error_handler(codec, _error_callback) - opj2._setup_encoder(codec, cparams, image) - strm = opj2._stream_create_default_file_stream_v3(self.filename, False) - opj2._start_compress(codec, image, strm) - opj2._encode(codec, strm) - opj2._end_compress(codec, strm) - opj2._stream_destroy_v3(strm) - opj2._destroy_codec(codec) - opj2._image_destroy(image) + _opj2.set_warning_handler(codec, _warning_callback) + _opj2.set_error_handler(codec, _error_callback) + _opj2.setup_encoder(codec, cparams, image) + strm = _opj2.stream_create_default_file_stream_v3(self.filename, False) + _opj2.start_compress(codec, image, strm) + _opj2.encode(codec, strm) + _opj2.end_compress(codec, strm) + _opj2.stream_destroy_v3(strm) + _opj2.destroy_codec(codec) + _opj2.image_destroy(image) self._parse() @@ -589,7 +589,7 @@ class Jp2k(Jp2kBox): >>> thumbnail.shape (728, 1296, 3) """ - if opj2._OPENJP2 is not None: + if _opj2.OPENJP2 is not None: img = self._read_openjp2(**kwargs) else: img = self._read_openjpeg(**kwargs) @@ -629,38 +629,38 @@ class Jp2k(Jp2kBox): with ExitStack() as stack: # Set decoding parameters. - dparameters = opj.dparameters_t() - opj._set_default_decoder_parameters(ctypes.byref(dparameters)) + dparameters = _opj.dparameters_t() + _opj.set_default_decoder_parameters(ctypes.byref(dparameters)) dparameters.cp_reduce = reduce dparameters.decod_format = self._codec_format infile = self.filename.encode() - nelts = opj._PATH_LEN - len(infile) + nelts = _opj.PATH_LEN - len(infile) infile += b'0' * nelts dparameters.infile = infile - dinfo = opj._create_decompress(dparameters.decod_format) + dinfo = _opj.create_decompress(dparameters.decod_format) - event_mgr = opj.event_mgr_t() + event_mgr = _opj.event_mgr_t() info_handler = ctypes.cast(_info_callback, ctypes.c_void_p) event_mgr.info_handler = info_handler if verbose else None event_mgr.warning_handler = ctypes.cast(_warning_callback, ctypes.c_void_p) event_mgr.error_handler = ctypes.cast(_error_callback, ctypes.c_void_p) - opj._set_event_mgr(dinfo, ctypes.byref(event_mgr)) + _opj.set_event_mgr(dinfo, ctypes.byref(event_mgr)) - opj._setup_decoder(dinfo, dparameters) + _opj.setup_decoder(dinfo, dparameters) with open(self.filename, 'rb') as fp: 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) + stack.callback(_opj.image_destroy, image) + stack.callback(_opj.destroy_decompress, dinfo) + stack.callback(_opj.cio_close, cio) ncomps = image.contents.numcomps component = image.contents.comps[0] @@ -784,10 +784,10 @@ class Jp2k(Jp2kBox): img_array : ndarray The individual image components or a single array. """ - dparam = opj2._set_default_decoder_parameters() + dparam = _opj2.set_default_decoder_parameters() infile = self.filename.encode() - nelts = opj2._PATH_LEN - len(infile) + nelts = _opj2.PATH_LEN - len(infile) infile += b'0' * nelts dparam.infile = infile @@ -820,31 +820,31 @@ class Jp2k(Jp2kBox): dparam.nb_tile_to_decode = 1 with ExitStack() as stack: - stream = opj2._stream_create_default_file_stream_v3(self.filename, + stream = _opj2.stream_create_default_file_stream_v3(self.filename, True) - stack.callback(opj2._stream_destroy_v3, stream) - codec = opj2._create_decompress(self._codec_format) - stack.callback(opj2._destroy_codec, codec) + stack.callback(_opj2.stream_destroy_v3, stream) + codec = _opj2.create_decompress(self._codec_format) + stack.callback(_opj2.destroy_codec, codec) - opj2._set_error_handler(codec, _error_callback) - opj2._set_warning_handler(codec, _warning_callback) + _opj2.set_error_handler(codec, _error_callback) + _opj2.set_warning_handler(codec, _warning_callback) if verbose: - opj2._set_info_handler(codec, _info_callback) + _opj2.set_info_handler(codec, _info_callback) else: - opj2._set_info_handler(codec, None) + _opj2.set_info_handler(codec, None) - opj2._setup_decoder(codec, dparam) - image = opj2._read_header(stream, codec) - stack.callback(opj2._image_destroy, image) + _opj2.setup_decoder(codec, dparam) + image = _opj2.read_header(stream, codec) + stack.callback(_opj2.image_destroy, image) if dparam.nb_tile_to_decode: - opj2._get_decoded_tile(codec, stream, image, dparam.tile_index) + _opj2.get_decoded_tile(codec, stream, image, dparam.tile_index) else: - opj2._set_decode_area(codec, image, + _opj2.set_decode_area(codec, image, dparam.DA_x0, dparam.DA_y0, dparam.DA_x1, dparam.DA_y1) - opj2._decode(codec, stream, image) - opj2._end_decompress(codec, stream) + _opj2.decode(codec, stream, image) + _opj2.end_decompress(codec, stream) component = image.contents.comps[0] if component.sgnd: @@ -937,7 +937,7 @@ class Jp2k(Jp2kBox): NotImplementedError If the openjp2 library is not available. """ - if opj2._OPENJP2 is None: + if _opj2.OPENJP2 is None: msg = "Requires openjp2 library." raise NotImplementedError(msg) @@ -986,7 +986,7 @@ class Jp2k(Jp2kBox): If the file is JPX with more than one codestream. """ with open(self.filename, 'rb') as fp: - if self._codec_format == opj2._CODEC_J2K: + if self._codec_format == _opj2.CODEC_J2K: codestream = Codestream(fp, header_only=header_only) else: box = [x for x in self.box if x.box_id == 'jp2c'] diff --git a/glymur/lib/__init__.py b/glymur/lib/__init__.py index 8484e19..ebd4fa7 100644 --- a/glymur/lib/__init__.py +++ b/glymur/lib/__init__.py @@ -1,4 +1,4 @@ """This package organizes individual libraries employed by glymur.""" -from . import openjp2 -from . import openjpeg +from . import openjp2 as _openjp2 +from . import openjpeg as _openjpeg #from . import test diff --git a/glymur/lib/openjp2.py b/glymur/lib/openjp2.py index 167fda2..48aa431 100644 --- a/glymur/lib/openjp2.py +++ b/glymur/lib/openjp2.py @@ -6,7 +6,7 @@ import platform from ctypes.util import find_library -def _glymurrc_fname(): +def glymurrc_fname(): """Return the path to the configuration file. Search order: @@ -21,7 +21,7 @@ def _glymurrc_fname(): return fname # Either GLYMURCONFIGDIR/glymurrc or $HOME/.glymur/glymurrc - confdir = _get_configdir() + confdir = get_configdir() if confdir is not None: fname = os.path.join(confdir, 'glymurrc') if os.path.exists(fname): @@ -31,7 +31,7 @@ def _glymurrc_fname(): return None -def _get_openjpeg_config(): +def get_openjpeg_config(): libopenjpeg_path = find_library('openjpeg') # If we could not find it, then look in some likely locations. @@ -48,25 +48,25 @@ def _get_openjpeg_config(): try: if os.name == "nt": - _OPENJPEG = ctypes.windll.LoadLibrary(libopenjpeg_path) + OPENJPEG = ctypes.windll.LoadLibrary(libopenjpeg_path) else: - _OPENJPEG = ctypes.CDLL(libopenjpeg_path) + OPENJPEG = ctypes.CDLL(libopenjpeg_path) except OSError: - _OPENJPEG = None + OPENJPEG = None - if _OPENJPEG is not None: + if OPENJPEG is not None: # Must be at least 1.5.0 - _OPENJPEG.opj_version.restype = ctypes.c_char_p - v = _OPENJPEG.opj_version() + OPENJPEG.opj_version.restype = ctypes.c_char_p + v = OPENJPEG.opj_version() v = v.decode('utf-8') major, minor, patch = v.split('.') if minor != '5': - _OPENJPEG = None - return _OPENJPEG + OPENJPEG = None + return OPENJPEG -def _get_openjp2_config(): - filename = _glymurrc_fname() +def get_openjp2_config(): + filename = glymurrc_fname() if filename is not None: # Read the configuration file for the library location. parser = ConfigParser() @@ -81,25 +81,25 @@ def _get_openjp2_config(): try: if os.name == "nt": - _OPENJP2 = ctypes.windll.LoadLibrary(libopenjp2_path) + OPENJP2 = ctypes.windll.LoadLibrary(libopenjp2_path) else: - _OPENJP2 = ctypes.CDLL(libopenjp2_path) + OPENJP2 = ctypes.CDLL(libopenjp2_path) except OSError: msg = '"Library {0}" could not be loaded. Operating in degraded mode.' msg = msg.format(libopenjp2_path) warnings.warn(msg, UserWarning) - _OPENJP2 = None - return _OPENJP2 + OPENJP2 = None + return OPENJP2 -def _config(): +def config(): """Read configuration file. """ - _OPENJP2 = _get_openjp2_config() - _OPENJPEG = _get_openjpeg_config() - return _OPENJP2, _OPENJPEG + OPENJP2 = get_openjp2_config() + OPENJPEG = get_openjpeg_config() + return OPENJP2, OPENJPEG -def _get_configdir(): +def get_configdir(): """Return string representing the configuration directory. Default is $HOME/.config/glymur. You can override this with the @@ -126,8 +126,8 @@ else: from configparser import ConfigParser from configparser import NoOptionError -_OPENJP2, _OPENJPEG = _config() -if _OPENJP2 is None and _OPENJPEG is None: +OPENJP2, OPENJPEG = config() +if OPENJP2 is None and OPENJPEG is None: msg = "Neither the glymur configuration file could not be located " msg += "nor could openjpeg 1.5.1 be located. Glymur can only " msg += "operate under extremely degraded conditions." @@ -143,42 +143,42 @@ PCRL = 3 CPRL = 4 -_ERROR_MSG_LST = [] +ERROR_MSG_LST = [] # Map certain atomic OpenJPEG datatypes to the ctypes equivalents. -_bool_t = ctypes.c_int32 -_codec_t_p = ctypes.c_void_p -_prog_order_t_p = ctypes.c_int32 -_cinema_mode_t = ctypes.c_int32 -_rsiz_capabilities_t = ctypes.c_int32 -_stream_t_p = ctypes.c_void_p +bool_t = ctypes.c_int32 +codec_t_p = ctypes.c_void_p +prog_order_t_p = ctypes.c_int32 +cinema_mode_t = ctypes.c_int32 +rsiz_capabilities_t = ctypes.c_int32 +stream_t_p = ctypes.c_void_p -_PATH_LEN = 4096 +PATH_LEN = 4096 J2K_MAXRLVLS = 33 -_J2K_MAXBANDS = (3 * J2K_MAXRLVLS - 2) +J2K_MAXBANDS = (3 * J2K_MAXRLVLS - 2) -_JPWL_MAX_NO_TILESPECS = 16 +JPWL_MAX_NO_TILESPECS = 16 -_TRUE = 1 -_FALSE = 0 +TRUE = 1 +FALSE = 0 # supported color spaces -_CLRSPC_UNKNOWN = -1 -_CLRSPC_UNSPECIFIED = 0 -_CLRSPC_SRGB = 1 -_CLRSPC_GRAY = 2 -_CLRSPC_YCC = 3 +CLRSPC_UNKNOWN = -1 +CLRSPC_UNSPECIFIED = 0 +CLRSPC_SRGB = 1 +CLRSPC_GRAY = 2 +CLRSPC_YCC = 3 color_space_t = ctypes.c_int # supported codec -_codec_format_t = ctypes.c_int -_CODEC_UNKNOWN = -1 -_CODEC_J2K = 0 -_CODEC_JPT = 1 -_CODEC_JP2 = 2 +codec_format_t = ctypes.c_int +CODEC_UNKNOWN = -1 +CODEC_J2K = 0 +CODEC_JPT = 1 +CODEC_JP2 = 2 -class _poc_t(ctypes.Structure): +class poc_t(ctypes.Structure): """Progression order changes.""" # Resolution num start, Component num start, given by POC _fields_ = [ @@ -196,8 +196,8 @@ class _poc_t(ctypes.Structure): ("precno1", ctypes.c_uint32), # Progression order enum - ("prg1", _prog_order_t_p), - ("prg", _prog_order_t_p), + ("prg1", prog_order_t_p), + ("prg", prog_order_t_p), # Progression order string ("progorder", ctypes.c_char * 5), @@ -241,7 +241,7 @@ class _poc_t(ctypes.Structure): ("ty0_t", ctypes.c_uint32)] -class _dparameters_t(ctypes.Structure): +class dparameters_t(ctypes.Structure): """Decompression parameters""" _fields_ = [ # Set the number of highest resolutio levels to be discarded. The @@ -262,10 +262,10 @@ class _dparameters_t(ctypes.Structure): ("cp_layer", ctypes.c_uint32), # input file name - ("infile", ctypes.c_char * _PATH_LEN), + ("infile", ctypes.c_char * PATH_LEN), # output file name - ("outfile", ctypes.c_char * _PATH_LEN), + ("outfile", ctypes.c_char * PATH_LEN), # input file format 0: PGX, 1: PxM, 2: BMP 3:TIF # output file format 0: J2K, 1: JP2, 2: JPT @@ -280,7 +280,7 @@ class _dparameters_t(ctypes.Structure): ("DA_y1", ctypes.c_uint32), # verbose mode - ("m_verbose", _bool_t), + ("m_verbose", bool_t), # tile number of the decoded tile ("tile_index", ctypes.c_uint32), @@ -289,7 +289,7 @@ class _dparameters_t(ctypes.Structure): ("nb_tile_to_decode", ctypes.c_uint32), # activates the JPWL correction capabilities - ("jpwl_correct", _bool_t), + ("jpwl_correct", bool_t), # activates the JPWL correction capabilities ("jpwl_exp_comps", ctypes.c_int32), @@ -301,13 +301,13 @@ class _dparameters_t(ctypes.Structure): ("flags", ctypes.c_uint32)] -class _cparameters_t(ctypes.Structure): +class cparameters_t(ctypes.Structure): """Compression parameters""" _fields_ = [ # size of tile: # tile_size_on = false (not in argument) or # = true (in argument) - ("tile_size_on", _bool_t), + ("tile_size_on", bool_t), # XTOsiz, YTOsiz ("cp_tx0", ctypes.c_int), @@ -339,7 +339,7 @@ class _cparameters_t(ctypes.Structure): ("prog_order", ctypes.c_int), # progression order changes - ("poc", _poc_t * 32), + ("poc", poc_t * 32), # number of progression order changes (POC), default to 0 ("numpocs", ctypes.c_uint), @@ -385,16 +385,16 @@ class _cparameters_t(ctypes.Structure): ("prch_init", ctypes.c_int * J2K_MAXRLVLS), # input file name - ("infile", ctypes.c_char * _PATH_LEN), + ("infile", ctypes.c_char * PATH_LEN), # output file name - ("outfile", ctypes.c_char * _PATH_LEN), + ("outfile", ctypes.c_char * PATH_LEN), # DEPRECATED. ("index_on", ctypes.c_int), # DEPRECATED. - ("index", ctypes.c_char * _PATH_LEN), + ("index", ctypes.c_char * PATH_LEN), # subimage encoding: origin image offset in x direction # subimage encoding: origin image offset in y direction @@ -413,25 +413,25 @@ class _cparameters_t(ctypes.Structure): # JPWL encoding parameters # enables writing of EPC in MH, thus activating JPWL - ("jpwl_epc_on", _bool_t), + ("jpwl_epc_on", bool_t), # error protection method for MH (0,1,16,32,37-128) ("jpwl_hprot_mh", ctypes.c_int), # tile number of header protection specification (>=0) - ("jpwl_hprot_tph_tileno", ctypes.c_int * _JPWL_MAX_NO_TILESPECS), + ("jpwl_hprot_tph_tileno", ctypes.c_int * JPWL_MAX_NO_TILESPECS), # error protection methods for TPHs (0,1,16,32,37-128) - ("jpwl_hprot_tph", ctypes.c_int * _JPWL_MAX_NO_TILESPECS), + ("jpwl_hprot_tph", ctypes.c_int * JPWL_MAX_NO_TILESPECS), # tile number of packet protection specification (>=0) - ("jpwl_pprot_tileno", ctypes.c_int * _JPWL_MAX_NO_TILESPECS), + ("jpwl_pprot_tileno", ctypes.c_int * JPWL_MAX_NO_TILESPECS), # packet number of packet protection specification (>=0) - ("jpwl_pprot_packno", ctypes.c_int * _JPWL_MAX_NO_TILESPECS), + ("jpwl_pprot_packno", ctypes.c_int * JPWL_MAX_NO_TILESPECS), # error protection methods for packets (0,1,16,32,37-128) - ("jpwl_pprot", ctypes.c_int * _JPWL_MAX_NO_TILESPECS), + ("jpwl_pprot", ctypes.c_int * JPWL_MAX_NO_TILESPECS), # enables writing of ESD, (0=no/1/2 bytes) ("jpwl_sens_size", ctypes.c_int), @@ -446,20 +446,20 @@ class _cparameters_t(ctypes.Structure): ("jpwl_sens_mh", ctypes.c_int), # tile number of sensitivity specification (>=0) - ("jpwl_sens_tph_tileno", ctypes.c_int * _JPWL_MAX_NO_TILESPECS), + ("jpwl_sens_tph_tileno", ctypes.c_int * JPWL_MAX_NO_TILESPECS), # sensitivity methods for TPHs (-1=no,0-7) - ("jpwl_sens_tph", ctypes.c_int * _JPWL_MAX_NO_TILESPECS), + ("jpwl_sens_tph", ctypes.c_int * JPWL_MAX_NO_TILESPECS), # Digital Cinema compliance 0-not compliant, 1-compliant - ("cp_cinema", _cinema_mode_t), + ("cp_cinema", cinema_mode_t), # Maximum rate for each component. # If == 0, component size limitation is not considered ("max_comp_size", ctypes.c_int), # Profile name - ("cp_rsiz", _rsiz_capabilities_t), + ("cp_rsiz", rsiz_capabilities_t), # Tile part generation ("tp_on", ctypes.c_uint8), @@ -471,14 +471,14 @@ class _cparameters_t(ctypes.Structure): ("tcp_mct", ctypes.c_uint8), # Enable JPIP indexing - ("jpip_on", _bool_t), + ("jpip_on", bool_t), # Naive implementation of MCT restricted to a single reversible array # based encoding without offset concerning all the components. ("mct_data", ctypes.c_void_p)] -class _image_comp_t(ctypes.Structure): +class image_comp_t(ctypes.Structure): """defines a single image component""" _fields_ = [ # XRsiz, YRsiz: horizontal, vertical separation of ith component with @@ -514,7 +514,7 @@ class _image_comp_t(ctypes.Structure): ("data", ctypes.POINTER(ctypes.c_int32))] -class _image_t(ctypes.Structure): +class image_t(ctypes.Structure): """defines image data and characteristics""" _fields_ = [ # XOsiz, YOsiz: horizontal and vertical offset from the origin of the @@ -533,7 +533,7 @@ class _image_t(ctypes.Structure): ("color_space", color_space_t), # image components - ("comps", ctypes.POINTER(_image_comp_t)), + ("comps", ctypes.POINTER(image_comp_t)), # restricted ICC profile buffer ("icc_profile_buf", ctypes.POINTER(ctypes.c_uint8)), @@ -542,7 +542,7 @@ class _image_t(ctypes.Structure): ("icc_profile_len", ctypes.c_uint32)] -class _image_comptparm_t(ctypes.Structure): +class image_comptparm_t(ctypes.Structure): """component parameters structure used by image_create function""" _fields_ = [ # XRsiz, YRsiz: horizontal, vertical separation of a sample of ith @@ -568,7 +568,7 @@ class _image_comptparm_t(ctypes.Structure): ("sgnd", ctypes.c_uint32)] -class _tccp_info_t(ctypes.Structure): +class tccp_info_t(ctypes.Structure): """tile-component coding parameters information""" _fields_ = [ # component index @@ -596,8 +596,8 @@ class _tccp_info_t(ctypes.Structure): ("qntsty", ctypes.c_uint32), # stepsizes used for quantization - ("stepsizes_mant", ctypes.c_uint32 * _J2K_MAXBANDS), - ("stepsizes_expn", ctypes.c_uint32 * _J2K_MAXBANDS), + ("stepsizes_mant", ctypes.c_uint32 * J2K_MAXBANDS), + ("stepsizes_expn", ctypes.c_uint32 * J2K_MAXBANDS), # stepsizes used for quantization ("numgbits", ctypes.c_uint32), @@ -612,7 +612,7 @@ class _tccp_info_t(ctypes.Structure): ("prch", ctypes.c_uint32 * J2K_MAXRLVLS)] -class _tile_info_v2_t(ctypes.Structure): +class tile_info_v2_t(ctypes.Structure): """tile coding parameters information""" _fields_ = [ # number (index) of tile @@ -622,7 +622,7 @@ class _tile_info_v2_t(ctypes.Structure): ("csty", ctypes.c_uint32), # progression order - ("prg", _prog_order_t_p), + ("prg", prog_order_t_p), # number of layers ("numlayers", ctypes.c_uint32), @@ -631,10 +631,10 @@ class _tile_info_v2_t(ctypes.Structure): ("mct", ctypes.c_uint32), # information concerning tile component parameters - ("tccp_info", ctypes.POINTER(_tccp_info_t))] + ("tccp_info", ctypes.POINTER(tccp_info_t))] -class _codestream_info_v2_t(ctypes.Structure): +class codestream_info_v2_t(ctypes.Structure): """information about the codestream""" _fields_ = [ # tile info @@ -654,152 +654,151 @@ class _codestream_info_v2_t(ctypes.Structure): ("nbcomps", ctypes.c_uint32), # default information regarding tiles inside of image - ("m_default_tile_info", _tile_info_v2_t), + ("m_default_tile_info", tile_info_v2_t), # information regarding tiles inside of image - ("tile_info", ctypes.POINTER(_tile_info_v2_t))] + ("tile_info", ctypes.POINTER(tile_info_v2_t))] # Restrict the input and output argument types for each function used in the # API. -if _OPENJP2 is not None: - _OPENJP2.opj_create_compress.restype = _codec_t_p - _OPENJP2.opj_create_compress.argtypes = [_codec_format_t] +if OPENJP2 is not None: + OPENJP2.opj_create_compress.restype = codec_t_p + OPENJP2.opj_create_compress.argtypes = [codec_format_t] - _OPENJP2.opj_create_decompress.argtypes = [_codec_format_t] - _OPENJP2.opj_create_decompress.restype = _codec_t_p + OPENJP2.opj_create_decompress.argtypes = [codec_format_t] + OPENJP2.opj_create_decompress.restype = codec_t_p - _argtypes = [_codec_t_p, _stream_t_p, ctypes.POINTER(_image_t)] - _OPENJP2.opj_decode.argtypes = _argtypes + argtypes = [codec_t_p, stream_t_p, ctypes.POINTER(image_t)] + OPENJP2.opj_decode.argtypes = argtypes - _argtypes = [_codec_t_p, ctypes.c_uint32, - ctypes.POINTER(ctypes.c_uint8), - ctypes.c_uint32, - _stream_t_p] - _OPENJP2.opj_decode_tile_data.argtypes = _argtypes + argtypes = [codec_t_p, ctypes.c_uint32, + ctypes.POINTER(ctypes.c_uint8), + ctypes.c_uint32, + stream_t_p] + OPENJP2.opj_decode_tile_data.argtypes = argtypes - _argtypes = [ctypes.POINTER(ctypes.POINTER(_codestream_info_v2_t))] - _OPENJP2.opj_destroy_cstr_info.argtypes = _argtypes - _OPENJP2.opj_destroy_cstr_info.restype = ctypes.c_void_p + argtypes = [ctypes.POINTER(ctypes.POINTER(codestream_info_v2_t))] + OPENJP2.opj_destroy_cstr_info.argtypes = argtypes + OPENJP2.opj_destroy_cstr_info.restype = ctypes.c_void_p - _argtypes = [_codec_t_p, _stream_t_p] - _OPENJP2.opj_encode.argtypes = _argtypes + argtypes = [codec_t_p, stream_t_p] + OPENJP2.opj_encode.argtypes = argtypes - _OPENJP2.opj_get_cstr_info.argtypes = [_codec_t_p] - _OPENJP2.opj_get_cstr_info.restype = ctypes.POINTER(_codestream_info_v2_t) + OPENJP2.opj_get_cstr_info.argtypes = [codec_t_p] + OPENJP2.opj_get_cstr_info.restype = ctypes.POINTER(codestream_info_v2_t) - _argtypes = [_codec_t_p, - _stream_t_p, - ctypes.POINTER(_image_t), - ctypes.c_uint32] - _OPENJP2.opj_get_decoded_tile.argtypes = _argtypes + argtypes = [codec_t_p, + stream_t_p, + ctypes.POINTER(image_t), + ctypes.c_uint32] + OPENJP2.opj_get_decoded_tile.argtypes = argtypes - _argtypes = [ctypes.c_uint32, - ctypes.POINTER(_image_comptparm_t), - color_space_t] - _OPENJP2.opj_image_create.argtypes = _argtypes - _OPENJP2.opj_image_create.restype = ctypes.POINTER(_image_t) + argtypes = [ctypes.c_uint32, + ctypes.POINTER(image_comptparm_t), + color_space_t] + OPENJP2.opj_image_create.argtypes = argtypes + OPENJP2.opj_image_create.restype = ctypes.POINTER(image_t) - _argtypes = [ctypes.c_uint32, - ctypes.POINTER(_image_comptparm_t), - color_space_t] - _OPENJP2.opj_image_tile_create.argtypes = _argtypes - _OPENJP2.opj_image_tile_create.restype = ctypes.POINTER(_image_t) + argtypes = [ctypes.c_uint32, + ctypes.POINTER(image_comptparm_t), + color_space_t] + OPENJP2.opj_image_tile_create.argtypes = argtypes + OPENJP2.opj_image_tile_create.restype = ctypes.POINTER(image_t) - _OPENJP2.opj_image_destroy.argtypes = [ctypes.POINTER(_image_t)] + OPENJP2.opj_image_destroy.argtypes = [ctypes.POINTER(image_t)] - _argtypes = [_stream_t_p, _codec_t_p, - ctypes.POINTER(ctypes.POINTER(_image_t))] - _OPENJP2.opj_read_header.argtypes = _argtypes + argtypes = [stream_t_p, codec_t_p, + ctypes.POINTER(ctypes.POINTER(image_t))] + OPENJP2.opj_read_header.argtypes = argtypes - _argtypes = [_codec_t_p, - _stream_t_p, - ctypes.POINTER(ctypes.c_uint32), - ctypes.POINTER(ctypes.c_uint32), - ctypes.POINTER(ctypes.c_int32), - ctypes.POINTER(ctypes.c_int32), - ctypes.POINTER(ctypes.c_int32), - ctypes.POINTER(ctypes.c_int32), - ctypes.POINTER(ctypes.c_uint32), - ctypes.POINTER(_bool_t)] - _OPENJP2.opj_read_tile_header.argtypes = _argtypes + argtypes = [codec_t_p, + stream_t_p, + ctypes.POINTER(ctypes.c_uint32), + ctypes.POINTER(ctypes.c_uint32), + ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_uint32), + ctypes.POINTER(bool_t)] + OPENJP2.opj_read_tile_header.argtypes = argtypes - _argtypes = [_codec_t_p, ctypes.POINTER(_image_t), ctypes.c_int32, - ctypes.c_int32, ctypes.c_int32, ctypes.c_int32] - _OPENJP2.opj_set_decode_area.argtypes = _argtypes + argtypes = [codec_t_p, ctypes.POINTER(image_t), ctypes.c_int32, + ctypes.c_int32, ctypes.c_int32, ctypes.c_int32] + OPENJP2.opj_set_decode_area.argtypes = argtypes - _argtypes = [ctypes.POINTER(_cparameters_t)] - _OPENJP2.opj_set_default_encoder_parameters.argtypes = _argtypes + argtypes = [ctypes.POINTER(cparameters_t)] + OPENJP2.opj_set_default_encoder_parameters.argtypes = argtypes - _argtypes = [ctypes.POINTER(_dparameters_t)] - _OPENJP2.opj_set_default_decoder_parameters.argtypes = _argtypes + argtypes = [ctypes.POINTER(dparameters_t)] + OPENJP2.opj_set_default_decoder_parameters.argtypes = argtypes - _argtypes = [_codec_t_p, ctypes.c_void_p, ctypes.c_void_p] - _OPENJP2.opj_set_error_handler.argtypes = _argtypes - _OPENJP2.opj_set_info_handler.argtypes = _argtypes - _OPENJP2.opj_set_warning_handler.argtypes = _argtypes + argtypes = [codec_t_p, ctypes.c_void_p, ctypes.c_void_p] + OPENJP2.opj_set_error_handler.argtypes = argtypes + OPENJP2.opj_set_info_handler.argtypes = argtypes + OPENJP2.opj_set_warning_handler.argtypes = argtypes - _argtypes = [_codec_t_p, ctypes.POINTER(_dparameters_t)] - _OPENJP2.opj_setup_decoder.argtypes = _argtypes + argtypes = [codec_t_p, ctypes.POINTER(dparameters_t)] + OPENJP2.opj_setup_decoder.argtypes = argtypes - _argtypes = [_codec_t_p, - ctypes.POINTER(_cparameters_t), - ctypes.POINTER(_image_t)] - _OPENJP2.opj_setup_encoder.argtypes = _argtypes + argtypes = [codec_t_p, + ctypes.POINTER(cparameters_t), + ctypes.POINTER(image_t)] + 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_t_p + 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_t_p - _argtypes = [_codec_t_p, ctypes.POINTER(_image_t), _stream_t_p] - _OPENJP2.opj_start_compress.argtypes = _argtypes + argtypes = [codec_t_p, ctypes.POINTER(image_t), stream_t_p] + OPENJP2.opj_start_compress.argtypes = argtypes - _OPENJP2.opj_end_compress.argtypes = [_codec_t_p, _stream_t_p] - _OPENJP2.opj_end_decompress.argtypes = [_codec_t_p, _stream_t_p] + OPENJP2.opj_end_compress.argtypes = [codec_t_p, stream_t_p] + OPENJP2.opj_end_decompress.argtypes = [codec_t_p, stream_t_p] - _OPENJP2.opj_stream_destroy_v3.argtypes = [_stream_t_p] - _OPENJP2.opj_destroy_codec.argtypes = [_codec_t_p] + OPENJP2.opj_stream_destroy_v3.argtypes = [stream_t_p] + OPENJP2.opj_destroy_codec.argtypes = [codec_t_p] - _argtypes = [_codec_t_p, - ctypes.c_uint32, - ctypes.POINTER(ctypes.c_uint8), - ctypes.c_uint32, - _stream_t_p] - _OPENJP2.opj_write_tile.argtypes = _argtypes + argtypes = [codec_t_p, + ctypes.c_uint32, + ctypes.POINTER(ctypes.c_uint8), + ctypes.c_uint32, + stream_t_p] + OPENJP2.opj_write_tile.argtypes = argtypes -def _check_error(status): +def check_error(status): """Set a generic function as the restype attribute of all OpenJPEG - functions that return a _bool_t value. This way we do not have to check + functions that return a bool_t value. This way we do not have to check for error status in each wrapping function and an exception will always be appropriately raised. """ - global _ERROR_MSG_LST + global ERROR_MSG_LST if status != 1: - if len(_ERROR_MSG_LST) > 0: + if len(ERROR_MSG_LST) > 0: # clear out the existing error message so that we don't pick up # a bad one next time around. - msg = '\n'.join(_ERROR_MSG_LST) - _ERROR_MSG_LST = [] + msg = '\n'.join(ERROR_MSG_LST) + ERROR_MSG_LST = [] raise IOError(msg) else: raise IOError("OpenJPEG function failure.") # These library functions all return an error status. Circumvent that and # force # them to raise an exception. -_fcns = ['opj_decode', 'opj_decode_tile_data', 'opj_end_compress', - 'opj_encode', 'opj_end_decompress', 'opj_get_decoded_tile', - 'opj_read_header', 'opj_read_tile_header', 'opj_set_decode_area', - 'opj_set_error_handler', 'opj_set_info_handler', - 'opj_set_warning_handler', - 'opj_setup_decoder', 'opj_setup_encoder', 'opj_start_compress', - 'opj_write_tile'] -if _OPENJP2 is not None: - for _fcn in _fcns: - _attr = getattr(_OPENJP2, _fcn) - setattr(_attr, 'restype', _check_error) +fcns = ['opj_decode', 'opj_decode_tile_data', 'opj_end_compress', + 'opj_encode', 'opj_end_decompress', 'opj_get_decoded_tile', + 'opj_read_header', 'opj_read_tile_header', 'opj_set_decode_area', + 'opj_set_error_handler', 'opj_set_info_handler', + 'opj_set_warning_handler', + 'opj_setup_decoder', 'opj_setup_encoder', 'opj_start_compress', + 'opj_write_tile'] +if OPENJP2 is not None: + for fcn in fcns: + setattr(getattr(OPENJP2, fcn), 'restype', check_error) -def _create_compress(codec_format): +def create_compress(codec_format): """Creates a J2K/JP2 compress structure. Wraps the openjp2 library function opj_create_compress. @@ -807,28 +806,28 @@ def _create_compress(codec_format): Parameters ---------- codec_format : int - Specifies codec to select. Should be one of _CODEC_J2K or _CODEC_JP2. + Specifies codec to select. Should be one of CODEC_J2K or CODEC_JP2. Returns ------- - codec : Reference to _codec_t_p instance. + codec : Reference to codec_t_p instance. """ - codec = _OPENJP2.opj_create_compress(codec_format) + codec = OPENJP2.opj_create_compress(codec_format) return codec -def _decode(codec, stream, image): +def decode(codec, stream, image): """Reads an entire image. Wraps the openjp2 library function opj_decode. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p The JPEG2000 codec - stream : _stream_t_p + stream : stream_t_p The stream to decode. - image : _image_t + image : image_t Output image structure. Raises @@ -836,17 +835,17 @@ def _decode(codec, stream, image): RuntimeError If the OpenJPEG library routine opj_decode fails. """ - _OPENJP2.opj_decode(codec, stream, image) + OPENJP2.opj_decode(codec, stream, image) -def _decode_tile_data(codec, tidx, data, data_size, stream): +def decode_tile_data(codec, tidx, data, data_size, stream): """Reads tile data. Wraps the openjp2 library function opj_decode_tile_data. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p The JPEG2000 codec tile_index : int The index of the tile being decoded @@ -854,7 +853,7 @@ def _decode_tile_data(codec, tidx, data, data_size, stream): Holds a memory block into which data will be decoded. data_size : int The size of data in bytes - stream : _stream_t_p + stream : stream_t_p The stream to decode. Raises @@ -863,15 +862,15 @@ def _decode_tile_data(codec, tidx, data, data_size, stream): If the OpenJPEG library routine opj_decode fails. """ datap = data.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)) - _OPENJP2.opj_decode_tile_data(codec, - ctypes.c_uint32(tidx), - datap, - ctypes.c_uint32(data_size), - stream) + OPENJP2.opj_decode_tile_data(codec, + ctypes.c_uint32(tidx), + datap, + ctypes.c_uint32(data_size), + stream) return codec -def _create_decompress(codec_format): +def create_decompress(codec_format): """Creates a J2K/JP2 decompress structure. Wraps the openjp2 library function opj_create_decompress. @@ -879,39 +878,39 @@ def _create_decompress(codec_format): Parameters ---------- codec_format : int - Specifies codec to select. Should be one of _CODEC_J2K or _CODEC_JP2. + Specifies codec to select. Should be one of CODEC_J2K or CODEC_JP2. Returns ------- - codec : Reference to _codec_t_p instance. + codec : Reference to codec_t_p instance. """ - codec = _OPENJP2.opj_create_decompress(codec_format) + codec = OPENJP2.opj_create_decompress(codec_format) return codec -def _destroy_codec(codec): +def destroy_codec(codec): """Destroy a decompressor handle. Wraps the openjp2 library function opj_destroy_codec. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p Decompressor handle to destroy. """ - _OPENJP2.opj_destroy_codec(codec) + OPENJP2.opj_destroy_codec(codec) -def _encode(codec, stream): +def encode(codec, stream): """Wraps openjp2 library function opj_encode. Encode an image into a JPEG 2000 codestream. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p The jpeg2000 codec. - stream : _stream_t_p + stream : stream_t_p The stream to which data is written. Raises @@ -919,40 +918,40 @@ def _encode(codec, stream): RuntimeError If the OpenJPEG library routine opj_encode fails. """ - _OPENJP2.opj_encode(codec, stream) + OPENJP2.opj_encode(codec, stream) -def _get_cstr_info(codec): +def get_cstr_info(codec): """get the codestream information from the codec Wraps the openjp2 library function opj_get_cstr_info. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p The jpeg2000 codec. Returns ------- - cstr_info_p : _codestream_info_v2_t + cstr_info_p : codestream_info_v2_t Reference to codestream information. """ - cstr_info_p = _OPENJP2.opj_get_cstr_info(codec) + cstr_info_p = OPENJP2.opj_get_cstr_info(codec) return cstr_info_p -def _get_decoded_tile(codec, stream, imagep, tile_index): +def get_decoded_tile(codec, stream, imagep, tile_index): """get the decoded tile from the codec Wraps the openjp2 library function opj_get_decoded_tile. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p The jpeg2000 codec. - stream : _stream_t_p + stream : stream_t_p The input stream. - image : _image_t + image : image_t Output image structure. tiler_index : int Index of the tile which will be decoded. @@ -962,32 +961,32 @@ def _get_decoded_tile(codec, stream, imagep, tile_index): RuntimeError If the OpenJPEG library routine opj_get_decoded_tile fails. """ - _OPENJP2.opj_get_decoded_tile(codec, stream, imagep, tile_index) + OPENJP2.opj_get_decoded_tile(codec, stream, imagep, tile_index) -def _destroy_cstr_info(cstr_info_p): +def destroy_cstr_info(cstr_info_p): """destroy codestream information after compression or decompression Wraps the openjp2 library function opj_destroy_cstr_info. Parameters ---------- - cstr_info_p : _codestream_info_v2_t pointer + cstr_info_p : codestream_info_v2_t pointer Pointer to codestream info structure. """ - _OPENJP2.opj_destroy_cstr_info(ctypes.byref(cstr_info_p)) + OPENJP2.opj_destroy_cstr_info(ctypes.byref(cstr_info_p)) -def _end_compress(codec, stream): +def end_compress(codec, stream): """End of compressing the current image. Wraps the openjp2 library function opj_end_compress. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p Compressor handle. - stream : _stream_t_p + stream : stream_t_p Output stream buffer. Raises @@ -995,19 +994,19 @@ def _end_compress(codec, stream): RuntimeError If the OpenJPEG library routine opj_end_compress fails. """ - _OPENJP2.opj_end_compress(codec, stream) + OPENJP2.opj_end_compress(codec, stream) -def _end_decompress(codec, stream): +def end_decompress(codec, stream): """End of decompressing the current image. Wraps the openjp2 library function opj_end_decompress. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p Compressor handle. - stream : _stream_t_p + stream : stream_t_p Output stream buffer. Raises @@ -1015,23 +1014,23 @@ def _end_decompress(codec, stream): RuntimeError If the OpenJPEG library routine opj_end_decompress fails. """ - _OPENJP2.opj_end_decompress(codec, stream) + OPENJP2.opj_end_decompress(codec, stream) -def _image_destroy(image): +def image_destroy(image): """Deallocate any resources associated with an image. Wraps the openjp2 library function opj_image_destroy. Parameters ---------- - image : _image_t pointer + image : image_t pointer Image resource to be disposed. """ - _OPENJP2.opj_image_destroy(image) + OPENJP2.opj_image_destroy(image) -def _image_create(comptparms, clrspc): +def image_create(comptparms, clrspc): """Creates a new image structure. Wraps the openjp2 library function opj_image_create. @@ -1045,16 +1044,16 @@ def _image_create(comptparms, clrspc): Returns ------- - image : _image_t - Reference to _image_t instance. + image : image_t + Reference to image_t instance. """ - image = _OPENJP2.opj_image_create(len(comptparms), + image = OPENJP2.opj_image_create(len(comptparms), comptparms, clrspc) return image -def _image_tile_create(comptparms, clrspc): +def image_tile_create(comptparms, clrspc): """Creates a new image structure. Wraps the openjp2 library function opj_image_tile_create. @@ -1068,30 +1067,30 @@ def _image_tile_create(comptparms, clrspc): Returns ------- - image : _image_t - Reference to _image_t instance. + image : image_t + Reference to image_t instance. """ - image = _OPENJP2.opj_image_tile_create(len(comptparms), + image = OPENJP2.opj_image_tile_create(len(comptparms), comptparms, clrspc) return image -def _read_header(stream, codec): +def read_header(stream, codec): """Decodes an image header. Wraps the openjp2 library function opj_read_header. Parameters ---------- - stream: _stream_t_p + stream: stream_t_p The JPEG2000 stream. codec: codec_t The JPEG2000 codec to read. Returns ------- - imagep : reference to _image_t instance + imagep : reference to image_t instance The image structure initialized with image characteristics. Raises @@ -1099,12 +1098,12 @@ def _read_header(stream, codec): RuntimeError If the OpenJPEG library routine opj_read_header fails. """ - imagep = ctypes.POINTER(_image_t)() - _OPENJP2.opj_read_header(stream, codec, ctypes.byref(imagep)) + imagep = ctypes.POINTER(image_t)() + OPENJP2.opj_read_header(stream, codec, ctypes.byref(imagep)) return imagep -def _read_tile_header(codec, stream): +def read_tile_header(codec, stream): """Reads a tile header. Wraps the openjp2 library function opj_read_tile_header. @@ -1113,7 +1112,7 @@ def _read_tile_header(codec, stream): ---------- codec : codec_t The JPEG2000 codec to read. - stream : _stream_t_p + stream : stream_t_p The JPEG2000 stream. Returns @@ -1143,8 +1142,8 @@ def _read_tile_header(codec, stream): x1 = ctypes.c_int32() y1 = ctypes.c_int32() ncomps = ctypes.c_uint32() - go_on = _bool_t() - _OPENJP2.opj_read_tile_header(codec, + go_on = bool_t() + OPENJP2.opj_read_tile_header(codec, stream, ctypes.byref(tile_index), ctypes.byref(data_size), @@ -1165,7 +1164,7 @@ def _read_tile_header(codec, stream): go_on) -def _set_decode_area(codec, image, start_x=0, start_y=0, end_x=0, end_y=0): +def set_decode_area(codec, image, start_x=0, start_y=0, end_x=0, end_y=0): """Wraps openjp2 library function opj_set_decode area. Sets the given area to be decoded. This function should be called right @@ -1173,9 +1172,9 @@ def _set_decode_area(codec, image, start_x=0, start_y=0, end_x=0, end_y=0): Parameters ---------- - codec : _codec_t_p + codec : codec_t_p Codec initialized by create_decompress function. - image : _image_t pointer + image : image_t pointer The decoded image previously set by read_header. start_x, start_y : optional, int The left and upper position of the rectangle to decode. @@ -1187,29 +1186,29 @@ def _set_decode_area(codec, image, start_x=0, start_y=0, end_x=0, end_y=0): RuntimeError If the OpenJPEG library routine opj_set_decode_area fails. """ - _OPENJP2.opj_set_decode_area(codec, image, - ctypes.c_int32(start_x), - ctypes.c_int32(start_y), - ctypes.c_int32(end_x), - ctypes.c_int32(end_y)) + OPENJP2.opj_set_decode_area(codec, image, + ctypes.c_int32(start_x), + ctypes.c_int32(start_y), + ctypes.c_int32(end_x), + ctypes.c_int32(end_y)) -def _set_default_decoder_parameters(): +def set_default_decoder_parameters(): """Wraps openjp2 library function opj_set_default_decoder_parameters. Sets decoding parameters to default values. Returns ------- - dparam : _dparameters_t + dparam : dparameters_t Decompression parameters. """ - dparams = _dparameters_t() - _OPENJP2.opj_set_default_decoder_parameters(ctypes.byref(dparams)) + dparams = dparameters_t() + OPENJP2.opj_set_default_decoder_parameters(ctypes.byref(dparams)) return dparams -def _set_default_encoder_parameters(): +def set_default_encoder_parameters(): """Wraps openjp2 library function opj_set_default_encoder_parameters. Sets encoding parameters to default values. That means @@ -1236,22 +1235,22 @@ def _set_default_encoder_parameters(): Returns ------- - cparameters : _cparameters_t + cparameters : cparameters_t Compression parameters. """ - cparams = _cparameters_t() - _OPENJP2.opj_set_default_encoder_parameters(ctypes.byref(cparams)) + cparams = cparameters_t() + OPENJP2.opj_set_default_encoder_parameters(ctypes.byref(cparams)) return cparams -def _set_error_handler(codec, handler, data=None): +def set_error_handler(codec, handler, data=None): """Wraps openjp2 library function opj_set_error_handler. Set the error handler use by openjpeg. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p Codec initialized by create_compress function. handler : python function The callback function to be used. @@ -1263,17 +1262,17 @@ def _set_error_handler(codec, handler, data=None): RuntimeError If the OpenJPEG library routine opj_set_error_handler fails. """ - _OPENJP2.opj_set_error_handler(codec, handler, data) + OPENJP2.opj_set_error_handler(codec, handler, data) -def _set_info_handler(codec, handler, data=None): +def set_info_handler(codec, handler, data=None): """Wraps openjp2 library function opj_set_info_handler. Set the info handler use by openjpeg. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p Codec initialized by create_compress function. handler : python function The callback function to be used. @@ -1285,17 +1284,17 @@ def _set_info_handler(codec, handler, data=None): RuntimeError If the OpenJPEG library routine opj_set_info_handler fails. """ - _OPENJP2.opj_set_info_handler(codec, handler, data) + OPENJP2.opj_set_info_handler(codec, handler, data) -def _set_warning_handler(codec, handler, data=None): +def set_warning_handler(codec, handler, data=None): """Wraps openjp2 library function opj_set_warning_handler. Set the warning handler use by openjpeg. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p Codec initialized by create_compress function. handler : python function The callback function to be used. @@ -1307,19 +1306,19 @@ def _set_warning_handler(codec, handler, data=None): RuntimeError If the OpenJPEG library routine opj_set_warning_handler fails. """ - _OPENJP2.opj_set_warning_handler(codec, handler, data) + OPENJP2.opj_set_warning_handler(codec, handler, data) -def _setup_decoder(codec, dparams): +def setup_decoder(codec, dparams): """Wraps openjp2 library function opj_setup_decoder. Setup the decoder with decompression parameters. Parameters ---------- - codec: _codec_t_p + codec: codec_t_p Codec initialized by create_compress function. - dparams: _dparameters_t + dparams: dparameters_t Decompression parameters. Raises @@ -1327,10 +1326,10 @@ def _setup_decoder(codec, dparams): RuntimeError If the OpenJPEG library routine opj_setup_decoder fails. """ - _OPENJP2.opj_setup_decoder(codec, ctypes.byref(dparams)) + OPENJP2.opj_setup_decoder(codec, ctypes.byref(dparams)) -def _setup_encoder(codec, cparams, image): +def setup_encoder(codec, cparams, image): """Wraps openjp2 library function opj_setup_encoder. Setup the encoder parameters using the current image and using user @@ -1338,11 +1337,11 @@ def _setup_encoder(codec, cparams, image): Parameters ---------- - codec : _codec_t_p + codec : codec_t_p codec initialized by create_compress function - cparams : _cparameters_t + cparams : cparameters_t compression parameters - image : _image_t + image : image_t input-filled image Raises @@ -1350,21 +1349,21 @@ def _setup_encoder(codec, cparams, image): RuntimeError If the OpenJPEG library routine opj_setup_encoder fails. """ - _OPENJP2.opj_setup_encoder(codec, ctypes.byref(cparams), image) + OPENJP2.opj_setup_encoder(codec, ctypes.byref(cparams), image) -def _start_compress(codec, image, stream): +def start_compress(codec, image, stream): """Wraps openjp2 library function opj_start_compress. Start to compress the current image. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p Compressor handle. - image : pointer to _image_t + image : pointer to image_t Input filled image. - stream : _stream_t_p + stream : stream_t_p Input stream. Raises @@ -1372,10 +1371,10 @@ def _start_compress(codec, image, stream): RuntimeError If the OpenJPEG library routine opj_start_compress fails. """ - _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_v3(fname, a_read_stream): """Wraps openjp2 library function opj_stream_create_default_vile_stream_v3. Sets the stream to be a file stream. @@ -1394,31 +1393,31 @@ def _stream_create_default_file_stream_v3(fname, a_read_stream): """ tf = 1 if a_read_stream else 0 fn = ctypes.c_char_p(fname.encode()) - stream = _OPENJP2.opj_stream_create_default_file_stream_v3(fn, tf) + stream = OPENJP2.opj_stream_create_default_file_stream_v3(fn, tf) return stream -def _stream_destroy_v3(stream): +def stream_destroy_v3(stream): """Wraps openjp2 library function opj_stream_destroy. Destroys the stream created by create_stream_v3. Parameters ---------- - stream : _stream_t_p + stream : stream_t_p The file stream. """ - _OPENJP2.opj_stream_destroy_v3(stream) + OPENJP2.opj_stream_destroy_v3(stream) -def _write_tile(codec, tile_index, data, data_size, stream): +def write_tile(codec, tile_index, data, data_size, stream): """Wraps openjp2 library function opj_write_tile. Write a tile into an image. Parameters ---------- - codec : _codec_t_p + codec : codec_t_p The jpeg2000 codec tile_index : int The index of the tile to write, zero-indexing assumed @@ -1426,7 +1425,7 @@ def _write_tile(codec, tile_index, data, data_size, stream): Image data arranged in usual C-order data_size : int Size of a tile in bytes - stream : _stream_t_p + stream : stream_t_p The stream to write data to Raises @@ -1435,14 +1434,14 @@ def _write_tile(codec, tile_index, data, data_size, stream): If the OpenJPEG library routine opj_write_tile fails. """ datap = data.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)) - _OPENJP2.opj_write_tile(codec, + OPENJP2.opj_write_tile(codec, ctypes.c_uint32(int(tile_index)), datap, ctypes.c_uint32(int(data_size)), stream) -def _set_error_message(msg): +def set_error_message(msg): """The openjpeg error handler has recorded an error message.""" - global _ERROR_MSG_LST - _ERROR_MSG_LST.append(msg) + global ERROR_MSG_LST + ERROR_MSG_LST.append(msg) diff --git a/glymur/lib/openjpeg.py b/glymur/lib/openjpeg.py index f818e1a..fc95861 100644 --- a/glymur/lib/openjpeg.py +++ b/glymur/lib/openjpeg.py @@ -9,14 +9,14 @@ import os if os.name == "nt": path = os.path.join('C:\\', 'Program files', 'OpenJPEG 1.5', 'bin', 'openjpeg.dll') - _OPENJPEG = ctypes.windll.LoadLibrary(path) + OPENJPEG = ctypes.windll.LoadLibrary(path) else: if platform.system() == 'Darwin': - _OPENJPEG = ctypes.CDLL('/opt/local/lib/libopenjpeg.dylib') + OPENJPEG = ctypes.CDLL('/opt/local/lib/libopenjpeg.dylib') elif platform.system() == 'Linux': - _OPENJPEG = ctypes.CDLL(find_library('openjpeg')) + OPENJPEG = ctypes.CDLL(find_library('openjpeg')) -_PATH_LEN = 4096 # maximum allowed size for filenames +PATH_LEN = 4096 # maximum allowed size for filenames class event_mgr_t(ctypes.Structure): @@ -68,9 +68,9 @@ class dparameters_t(ctypes.Structure): # cp_layer: the maximum number of quality layers to decode ("cp_layer", ctypes.c_int), # infile: input file name - ("infile", ctypes.c_char * _PATH_LEN), + ("infile", ctypes.c_char * PATH_LEN), # outfile: output file name - ("outfile", ctypes.c_char * _PATH_LEN), + ("outfile", ctypes.c_char * PATH_LEN), # decod_format: input file format 0: J2K, 1: JP2, 2: JPT ("decod_format", ctypes.c_int), # cod_format: output file format 0: PGX, 1: PxM, 2: BMP @@ -115,75 +115,75 @@ class image_t(ctypes.Structure): ("icc_profile_buf", ctypes.c_char_p), ("icc_profile_len", ctypes.c_int)] -def _cio_open(cinfo, src): +def cio_open(cinfo, src): """Wrapper for openjpeg library function opj_cio_open.""" argtypes = [ctypes.POINTER(common_struct_t), ctypes.c_char_p, ctypes.c_int] - _OPENJPEG.opj_cio_open.argtypes = argtypes - _OPENJPEG.opj_cio_open.restype = ctypes.POINTER(cio_t) + OPENJPEG.opj_cio_open.argtypes = argtypes + OPENJPEG.opj_cio_open.restype = ctypes.POINTER(cio_t) - cio = _OPENJPEG.opj_cio_open(ctypes.cast(cinfo, ctypes.POINTER(common_struct_t)), - src, len(src)) + cio = OPENJPEG.opj_cio_open(ctypes.cast(cinfo, ctypes.POINTER(common_struct_t)), + src, len(src)) return cio -def _cio_close(cio): +def cio_close(cio): """Wraps openjpeg library function cio_close. """ - _OPENJPEG.opj_cio_close.argtypes = [ctypes.POINTER(cio_t)] - _OPENJPEG.opj_cio_close(cio) + OPENJPEG.opj_cio_close.argtypes = [ctypes.POINTER(cio_t)] + OPENJPEG.opj_cio_close(cio) -def _create_decompress(fmt): +def create_decompress(fmt): """Wraps openjpeg library function opj_create_decompress. """ - _OPENJPEG.opj_create_decompress.argtypes = [ctypes.c_int] - _OPENJPEG.opj_create_decompress.restype = ctypes.POINTER(dinfo_t) - dinfo = _OPENJPEG.opj_create_decompress(fmt) + OPENJPEG.opj_create_decompress.argtypes = [ctypes.c_int] + OPENJPEG.opj_create_decompress.restype = ctypes.POINTER(dinfo_t) + dinfo = OPENJPEG.opj_create_decompress(fmt) return dinfo -def _decode(dinfo, cio): +def decode(dinfo, cio): """Wrapper for opj_decode. """ argtypes = [ctypes.POINTER(dinfo_t), ctypes.POINTER(cio_t)] - _OPENJPEG.opj_decode.argtypes = argtypes - _OPENJPEG.opj_decode.restype = ctypes.POINTER(image_t) - image = _OPENJPEG.opj_decode(dinfo, cio) + OPENJPEG.opj_decode.argtypes = argtypes + OPENJPEG.opj_decode.restype = ctypes.POINTER(image_t) + image = OPENJPEG.opj_decode(dinfo, cio) return image -def _destroy_decompress(dinfo): +def destroy_decompress(dinfo): """Wraps openjpeg library function opj_destroy_decompress.""" - _OPENJPEG.opj_destroy_decompress.argtypes = [ctypes.POINTER(dinfo_t)] - _OPENJPEG.opj_destroy_decompress(dinfo) + OPENJPEG.opj_destroy_decompress.argtypes = [ctypes.POINTER(dinfo_t)] + OPENJPEG.opj_destroy_decompress(dinfo) -def _image_destroy(image): +def image_destroy(image): """Wraps openjpeg library function opj_image_destroy.""" - _OPENJPEG.opj_image_destroy.argtypes = [ctypes.POINTER(image_t)] - _OPENJPEG.opj_image_destroy(image) + OPENJPEG.opj_image_destroy.argtypes = [ctypes.POINTER(image_t)] + OPENJPEG.opj_image_destroy(image) -def _set_default_decoder_parameters(dparams_p): +def set_default_decoder_parameters(dparams_p): """Wrapper for opj_set_default_decoder_parameters. """ argtypes = [ctypes.POINTER(dparameters_t)] - _OPENJPEG.opj_set_default_decoder_parameters.argtypes = argtypes - _OPENJPEG.opj_set_default_decoder_parameters(dparams_p) + OPENJPEG.opj_set_default_decoder_parameters.argtypes = argtypes + OPENJPEG.opj_set_default_decoder_parameters(dparams_p) -def _set_event_mgr(dinfo, event_mgr, context=None): +def set_event_mgr(dinfo, event_mgr, context=None): """Wrapper for openjpeg library function opj_set_event_mgr. """ argtypes = [ctypes.POINTER(common_struct_t), ctypes.POINTER(event_mgr_t), ctypes.c_void_p] - _OPENJPEG.opj_set_event_mgr(ctypes.cast(dinfo, - ctypes.POINTER(common_struct_t)), - event_mgr, context) + OPENJPEG.opj_set_event_mgr(ctypes.cast(dinfo, + ctypes.POINTER(common_struct_t)), + event_mgr, context) -def _setup_decoder(dinfo, dparams): +def setup_decoder(dinfo, dparams): """Wrapper for openjpeg library function opj_setup_decoder.""" argtypes = [ctypes.POINTER(dinfo_t), ctypes.POINTER(dparameters_t)] - _OPENJPEG.opj_setup_decoder.argtypes = argtypes - _OPENJPEG.opj_setup_decoder(dinfo, dparams) + OPENJPEG.opj_setup_decoder.argtypes = argtypes + OPENJPEG.opj_setup_decoder(dinfo, dparams) -def _version(): +def version(): """Wrapper for opj_version library routine.""" - _OPENJPEG.opj_version.restype = ctypes.c_char_p - v = _OPENJPEG.opj_version() + OPENJPEG.opj_version.restype = ctypes.c_char_p + v = OPENJPEG.opj_version() return v.decode('utf-8') diff --git a/glymur/lib/test/test_openjp2.py b/glymur/lib/test/test_openjp2.py index 90a5f08..6c4c2cd 100644 --- a/glymur/lib/test/test_openjp2.py +++ b/glymur/lib/test/test_openjp2.py @@ -12,13 +12,7 @@ import numpy as np import glymur -def load_tests(loader, tests, ignore): - if glymur.lib.openjp2._OPENJP2 is not None: - tests.addTests(doctest.DocTestSuite('glymur.lib.openjp2')) - return tests - - -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib._openjp2.OPENJP2 is None, "Missing openjp2 library.") class TestOpenJP2(unittest.TestCase): @@ -29,7 +23,7 @@ class TestOpenJP2(unittest.TestCase): pass def test_set_default_encoder_parameters(self): - cparams = glymur.lib.openjp2._set_default_encoder_parameters() + cparams = glymur.lib._openjp2.set_default_encoder_parameters() self.assertEqual(cparams.res_spec, 0) self.assertEqual(cparams.cblockw_init, 64) @@ -38,7 +32,7 @@ class TestOpenJP2(unittest.TestCase): self.assertEqual(cparams.subsampling_dx, 1) self.assertEqual(cparams.subsampling_dy, 1) self.assertEqual(cparams.mode, 0) - self.assertEqual(cparams.prog_order, glymur.lib.openjp2.LRCP) + self.assertEqual(cparams.prog_order, glymur.lib._openjp2.LRCP) self.assertEqual(cparams.roi_shift, 0) self.assertEqual(cparams.cp_tx0, 0) self.assertEqual(cparams.cp_ty0, 0) @@ -46,7 +40,7 @@ class TestOpenJP2(unittest.TestCase): self.assertEqual(cparams.irreversible, 0) def test_set_default_decoder_parameters(self): - dparams = glymur.lib.openjp2._set_default_decoder_parameters() + dparams = glymur.lib._openjp2.set_default_decoder_parameters() self.assertEqual(dparams.DA_x0, 0) self.assertEqual(dparams.DA_y0, 0) @@ -55,34 +49,34 @@ class TestOpenJP2(unittest.TestCase): def tile_macro(self, codec, stream, imagep, tidx): # called only by j2k_random_tile_access - glymur.lib.openjp2._get_decoded_tile(codec, stream, imagep, tidx) + glymur.lib._openjp2.get_decoded_tile(codec, stream, imagep, tidx) for j in range(imagep.contents.numcomps): self.assertIsNotNone(imagep.contents.comps[j].data) def j2k_random_tile_access(self, filename, codec_format=None): # called by the test_rtaX methods - dparam = glymur.lib.openjp2._set_default_decoder_parameters() + dparam = glymur.lib._openjp2.set_default_decoder_parameters() infile = filename.encode() - nelts = glymur.lib.openjp2._PATH_LEN - len(infile) + nelts = glymur.lib._openjp2.PATH_LEN - len(infile) infile += b'0' * nelts dparam.infile = infile dparam.decod_format = codec_format - codec = glymur.lib.openjp2._create_decompress(codec_format) + codec = glymur.lib._openjp2.create_decompress(codec_format) - glymur.lib.openjp2._set_info_handler(codec, None) - glymur.lib.openjp2._set_warning_handler(codec, None) - glymur.lib.openjp2._set_error_handler(codec, None) + glymur.lib._openjp2.set_info_handler(codec, None) + glymur.lib._openjp2.set_warning_handler(codec, None) + glymur.lib._openjp2.set_error_handler(codec, None) x = (filename, True) - stream = glymur.lib.openjp2._stream_create_default_file_stream_v3(*x) + stream = glymur.lib._openjp2.stream_create_default_file_stream_v3(*x) - glymur.lib.openjp2._setup_decoder(codec, dparam) - image = glymur.lib.openjp2._read_header(stream, codec) + glymur.lib._openjp2.setup_decoder(codec, dparam) + image = glymur.lib._openjp2.read_header(stream, codec) - cstr_info = glymur.lib.openjp2._get_cstr_info(codec) + cstr_info = glymur.lib._openjp2.get_cstr_info(codec) tile_ul = 0 tile_ur = cstr_info.contents.tw - 1 @@ -94,18 +88,18 @@ class TestOpenJP2(unittest.TestCase): self.tile_macro(codec, stream, image, tile_lr) self.tile_macro(codec, stream, image, tile_ll) - glymur.lib.openjp2._destroy_cstr_info(cstr_info) + glymur.lib._openjp2.destroy_cstr_info(cstr_info) - glymur.lib.openjp2._end_decompress(codec, stream) - glymur.lib.openjp2._destroy_codec(codec) - glymur.lib.openjp2._stream_destroy_v3(stream) - glymur.lib.openjp2._image_destroy(image) + glymur.lib._openjp2.end_decompress(codec, stream) + glymur.lib._openjp2.destroy_codec(codec) + glymur.lib._openjp2.stream_destroy_v3(stream) + glymur.lib._openjp2.image_destroy(image) def tile_decoder(self, x0=None, y0=None, x1=None, y1=None, filename=None, codec_format=None): x = (filename, True) - stream = glymur.lib.openjp2._stream_create_default_file_stream_v3(*x) - dparam = glymur.lib.openjp2._set_default_decoder_parameters() + stream = glymur.lib._openjp2.stream_create_default_file_stream_v3(*x) + dparam = glymur.lib._openjp2.set_default_decoder_parameters() dparam.decod_format = codec_format @@ -115,30 +109,30 @@ class TestOpenJP2(unittest.TestCase): # do not use resolution reductions. dparam.cp_reduce = 0 - codec = glymur.lib.openjp2._create_decompress(codec_format) + codec = glymur.lib._openjp2.create_decompress(codec_format) - glymur.lib.openjp2._set_info_handler(codec, None) - glymur.lib.openjp2._set_warning_handler(codec, None) - glymur.lib.openjp2._set_error_handler(codec, None) + glymur.lib._openjp2.set_info_handler(codec, None) + glymur.lib._openjp2.set_warning_handler(codec, None) + glymur.lib._openjp2.set_error_handler(codec, None) - glymur.lib.openjp2._setup_decoder(codec, dparam) - image = glymur.lib.openjp2._read_header(stream, codec) - glymur.lib.openjp2._set_decode_area(codec, image, x0, y0, x1, y1) + glymur.lib._openjp2.setup_decoder(codec, dparam) + image = glymur.lib._openjp2.read_header(stream, codec) + glymur.lib._openjp2.set_decode_area(codec, image, x0, y0, x1, y1) data = np.zeros((1150, 2048, 3), dtype=np.uint8) while True: - rargs = glymur.lib.openjp2._read_tile_header(codec, stream) + rargs = glymur.lib._openjp2.read_tile_header(codec, stream) tidx = rargs[0] sz = rargs[1] go_on = rargs[-1] if not go_on: break - glymur.lib.openjp2._decode_tile_data(codec, tidx, data, sz, stream) + glymur.lib._openjp2.decode_tile_data(codec, tidx, data, sz, stream) - glymur.lib.openjp2._end_decompress(codec, stream) - glymur.lib.openjp2._destroy_codec(codec) - glymur.lib.openjp2._stream_destroy_v3(stream) - glymur.lib.openjp2._image_destroy(image) + glymur.lib._openjp2.end_decompress(codec, stream) + glymur.lib._openjp2.destroy_codec(codec) + glymur.lib._openjp2.stream_destroy_v3(stream) + glymur.lib._openjp2.image_destroy(image) def tile_encoder(self, num_comps=None, tile_width=None, tile_height=None, filename=None, codec=None, comp_prec=None, @@ -150,7 +144,7 @@ class TestOpenJP2(unittest.TestCase): data = np.random.random((tile_height, tile_width, num_comps)) data = (data * 255).astype(np.uint8) - l_param = glymur.lib.openjp2._set_default_encoder_parameters() + l_param = glymur.lib._openjp2.set_default_encoder_parameters() l_param.tcp_numlayers = 1 l_param.cp_fixed_quality = 1 @@ -170,9 +164,9 @@ class TestOpenJP2(unittest.TestCase): l_param.numresolution = 6 - l_param.prog_order = glymur.lib.openjp2.LRCP + l_param.prog_order = glymur.lib._openjp2.LRCP - l_params = (glymur.lib.openjp2._image_comptparm_t * num_comps)() + l_params = (glymur.lib._openjp2.image_comptparm_t * num_comps)() for j in range(num_comps): l_params[j].dx = 1 l_params[j].dy = 1 @@ -183,38 +177,38 @@ class TestOpenJP2(unittest.TestCase): l_params[j].x0 = 0 l_params[j].y0 = 0 - codec = glymur.lib.openjp2._create_compress(codec) + codec = glymur.lib._openjp2.create_compress(codec) - glymur.lib.openjp2._set_info_handler(codec, None) - glymur.lib.openjp2._set_warning_handler(codec, None) - glymur.lib.openjp2._set_error_handler(codec, None) + glymur.lib._openjp2.set_info_handler(codec, None) + glymur.lib._openjp2.set_warning_handler(codec, None) + glymur.lib._openjp2.set_error_handler(codec, None) - cspace = glymur.lib.openjp2._CLRSPC_SRGB - l_image = glymur.lib.openjp2._image_tile_create(l_params, cspace) + cspace = glymur.lib._openjp2.CLRSPC_SRGB + l_image = glymur.lib._openjp2.image_tile_create(l_params, cspace) l_image.contents.x0 = 0 l_image.contents.y0 = 0 l_image.contents.x1 = image_width l_image.contents.y1 = image_height - l_image.contents.color_space = glymur.lib.openjp2._CLRSPC_SRGB + l_image.contents.color_space = glymur.lib._openjp2.CLRSPC_SRGB - glymur.lib.openjp2._setup_encoder(codec, l_param, l_image) + glymur.lib._openjp2.setup_encoder(codec, l_param, l_image) x = (filename, False) - stream = glymur.lib.openjp2._stream_create_default_file_stream_v3(*x) - glymur.lib.openjp2._start_compress(codec, l_image, stream) + stream = glymur.lib._openjp2.stream_create_default_file_stream_v3(*x) + glymur.lib._openjp2.start_compress(codec, l_image, stream) for j in np.arange(num_tiles): - glymur.lib.openjp2._write_tile(codec, j, data, tile_size, stream) + glymur.lib._openjp2.write_tile(codec, j, data, tile_size, stream) - glymur.lib.openjp2._end_compress(codec, stream) - glymur.lib.openjp2._stream_destroy_v3(stream) - glymur.lib.openjp2._destroy_codec(codec) - glymur.lib.openjp2._image_destroy(l_image) + glymur.lib._openjp2.end_compress(codec, stream) + glymur.lib._openjp2.stream_destroy_v3(stream) + glymur.lib._openjp2.destroy_codec(codec) + glymur.lib._openjp2.image_destroy(l_image) def tte0_setup(self, filename): kwargs = {'filename': filename, - 'codec': glymur.lib.openjp2._CODEC_J2K, + 'codec': glymur.lib._openjp2.CODEC_J2K, 'comp_prec': 8, 'irreversible': 1, 'num_comps': 3, @@ -241,12 +235,12 @@ class TestOpenJP2(unittest.TestCase): 'x1': 1000, 'y1': 1000, 'filename': tfile.name, - 'codec_format': glymur.lib.openjp2._CODEC_J2K} + 'codec_format': glymur.lib._openjp2.CODEC_J2K} self.tile_decoder(**kwargs) def tte1_setup(self, filename): kwargs = {'filename': filename, - 'codec': glymur.lib.openjp2._CODEC_J2K, + 'codec': glymur.lib._openjp2.CODEC_J2K, 'comp_prec': 8, 'irreversible': 1, 'num_comps': 3, @@ -273,7 +267,7 @@ class TestOpenJP2(unittest.TestCase): 'x1': 128, 'y1': 128, 'filename': tfile.name, - 'codec_format': glymur.lib.openjp2._CODEC_J2K} + 'codec_format': glymur.lib._openjp2.CODEC_J2K} self.tile_decoder(**kwargs) def test_rta1(self): @@ -281,12 +275,12 @@ class TestOpenJP2(unittest.TestCase): # Runs test designated rta1 in OpenJPEG test suite. self.tte1_setup(tfile.name) - kwargs = {'codec_format': glymur.lib.openjp2._CODEC_J2K} + kwargs = {'codec_format': glymur.lib._openjp2.CODEC_J2K} self.j2k_random_tile_access(tfile.name, **kwargs) def tte2_setup(self, filename): kwargs = {'filename': filename, - 'codec': glymur.lib.openjp2._CODEC_JP2, + 'codec': glymur.lib._openjp2.CODEC_JP2, 'comp_prec': 8, 'irreversible': 1, 'num_comps': 3, @@ -312,7 +306,7 @@ class TestOpenJP2(unittest.TestCase): 'x1': 128, 'y1': 128, 'filename': tfile.name, - 'codec_format': glymur.lib.openjp2._CODEC_JP2} + 'codec_format': glymur.lib._openjp2.CODEC_JP2} self.tile_decoder(**kwargs) def test_rta2(self): @@ -320,12 +314,12 @@ class TestOpenJP2(unittest.TestCase): # Runs test designated rta2 in OpenJPEG test suite. self.tte2_setup(tfile.name) - kwargs = {'codec_format': glymur.lib.openjp2._CODEC_JP2} + kwargs = {'codec_format': glymur.lib._openjp2.CODEC_JP2} self.j2k_random_tile_access(tfile.name, **kwargs) def tte3_setup(self, filename): kwargs = {'filename': filename, - 'codec': glymur.lib.openjp2._CODEC_J2K, + 'codec': glymur.lib._openjp2.CODEC_J2K, 'comp_prec': 8, 'irreversible': 1, 'num_comps': 1, @@ -345,12 +339,12 @@ class TestOpenJP2(unittest.TestCase): with tempfile.NamedTemporaryFile(suffix=".j2k") as tfile: self.tte3_setup(tfile.name) - kwargs = {'codec_format': glymur.lib.openjp2._CODEC_J2K} + kwargs = {'codec_format': glymur.lib._openjp2.CODEC_J2K} self.j2k_random_tile_access(tfile.name, **kwargs) def tte4_setup(self, filename): kwargs = {'filename': filename, - 'codec': glymur.lib.openjp2._CODEC_J2K, + 'codec': glymur.lib._openjp2.CODEC_J2K, 'comp_prec': 8, 'irreversible': 0, 'num_comps': 1, @@ -370,12 +364,12 @@ class TestOpenJP2(unittest.TestCase): with tempfile.NamedTemporaryFile(suffix=".j2k") as tfile: self.tte4_setup(tfile.name) - kwargs = {'codec_format': glymur.lib.openjp2._CODEC_J2K} + kwargs = {'codec_format': glymur.lib._openjp2.CODEC_J2K} self.j2k_random_tile_access(tfile.name, **kwargs) def tte5_setup(self, filename): kwargs = {'filename': filename, - 'codec': glymur.lib.openjp2._CODEC_J2K, + 'codec': glymur.lib._openjp2.CODEC_J2K, 'comp_prec': 8, 'irreversible': 0, 'num_comps': 1, @@ -395,7 +389,7 @@ class TestOpenJP2(unittest.TestCase): with tempfile.NamedTemporaryFile(suffix=".j2k") as tfile: self.tte5_setup(tfile.name) - kwargs = {'codec_format': glymur.lib.openjp2._CODEC_J2K} + kwargs = {'codec_format': glymur.lib._openjp2.CODEC_J2K} self.j2k_random_tile_access(tfile.name, **kwargs) if __name__ == "__main__": diff --git a/glymur/lib/test/test_openjpeg.py b/glymur/lib/test/test_openjpeg.py index 9307191..2bdc5db 100644 --- a/glymur/lib/test/test_openjpeg.py +++ b/glymur/lib/test/test_openjpeg.py @@ -3,7 +3,7 @@ import unittest import glymur -@unittest.skipIf(glymur.lib.openjpeg._OPENJPEG is None, +@unittest.skipIf(glymur.lib._openjpeg.OPENJPEG is None, "Missing openjpeg library.") class TestOpenJPEG(unittest.TestCase): @@ -14,15 +14,15 @@ class TestOpenJPEG(unittest.TestCase): pass def test_version(self): - v = glymur.lib.openjpeg._version() + v = glymur.lib._openjpeg.version() parts = v.split('.') self.assertEqual(parts[0], '1') self.assertEqual(parts[1], '5') def test_set_default_decoder_parameters(self): # Verify that we properly set the default decode parameters. - dp = glymur.lib.openjpeg.dparameters_t() - glymur.lib.openjpeg._set_default_decoder_parameters(ctypes.byref(dp)) + dp = glymur.lib._openjpeg.dparameters_t() + glymur.lib._openjpeg.set_default_decoder_parameters(ctypes.byref(dp)) self.assertEqual(dp.cp_reduce, 0) self.assertEqual(dp.cp_layer, 0) diff --git a/glymur/test/test_callbacks.py b/glymur/test/test_callbacks.py index fd2f58c..b203f6b 100644 --- a/glymur/test/test_callbacks.py +++ b/glymur/test/test_callbacks.py @@ -14,7 +14,7 @@ else: import glymur -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, "Missing openjp2 library.") class TestCallbacks(unittest.TestCase): @@ -60,7 +60,7 @@ class TestCallbacks(unittest.TestCase): self.assertEqual(actual, expected) -@unittest.skipIf(glymur.lib.openjp2._OPENJPEG is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJPEG is None, "Missing openjpeg library.") class TestCallbacks15(unittest.TestCase): """This test suite is for OpenJPEG 1.5.1 properties. @@ -69,13 +69,13 @@ class TestCallbacks15(unittest.TestCase): @classmethod def setUpClass(cls): # Monkey patch the package so as to use OPENJPEG instead of OPENJP2 - cls.openjp2 = glymur.lib.openjp2._OPENJP2 - glymur.lib.openjp2._OPENJP2 = None + cls.openjp2 = glymur.lib.openjp2.OPENJP2 + glymur.lib.openjp2.OPENJP2 = None @classmethod def tearDownClass(cls): # Restore OPENJP2 - glymur.lib.openjp2._OPENJP2 = cls.openjp2 + glymur.lib.openjp2.OPENJP2 = cls.openjp2 def setUp(self): # Save sys.stdout. diff --git a/glymur/test/test_config.py b/glymur/test/test_config.py index 9e5374a..206da05 100644 --- a/glymur/test/test_config.py +++ b/glymur/test/test_config.py @@ -21,7 +21,7 @@ from glymur.lib import openjp2 as opj2 @unittest.skip("Cannot work when both OPENJPEG and OPENJP2 are both present.") -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, "Needs openjp2 library first before these tests make sense.") @unittest.skipIf(sys.hexversion < 0x03020000, "Uses features introduced in 3.2.") @@ -55,7 +55,7 @@ class TestSuite(unittest.TestCase): filename = os.path.join(configdir, 'glymurrc') with open(filename, 'wb') as tfile: tfile.write('[library]\n'.encode()) - libloc = glymur.lib.openjp2._OPENJP2._name + libloc = glymur.lib.openjp2.OPENJP2._name line = 'openjp2: {0}\n'.format(libloc) tfile.write(line.encode()) tfile.flush() diff --git a/glymur/test/test_jp2box.py b/glymur/test/test_jp2box.py index 512acf2..3a38002 100644 --- a/glymur/test/test_jp2box.py +++ b/glymur/test/test_jp2box.py @@ -18,7 +18,7 @@ def load_tests(loader, tests, ignore): return tests -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, "Missing openjp2 library.") class TestChannelDefinition(unittest.TestCase): @@ -409,7 +409,7 @@ class TestColourSpecificationBox(unittest.TestCase): approximation=approx) -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, "Missing openjp2 library.") class TestJp2Boxes(unittest.TestCase): diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 424d079..bbb4428 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -33,12 +33,12 @@ except: # Doc tests should be run as well. def load_tests(loader, tests, ignore): - if glymur.lib.openjp2._OPENJP2 is not None: + if glymur.lib.openjp2.OPENJP2 is not None: tests.addTests(doctest.DocTestSuite('glymur.jp2k')) return tests -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, "Missing openjp2 library.") class TestJp2k(unittest.TestCase): @@ -648,20 +648,20 @@ class TestJp2k(unittest.TestCase): self.assertFalse('Make' in exif['Image'].keys()) -@unittest.skipIf(glymur.lib.openjpeg._OPENJPEG is None, +@unittest.skipIf(glymur.lib.openjpeg.OPENJPEG is None, "Missing openjpeg library.") class TestJp2k15(unittest.TestCase): @classmethod def setUpClass(cls): # Monkey patch the package so as to use OPENJPEG instead of OPENJP2 - cls.openjp2 = glymur.lib.openjp2._OPENJP2 - glymur.lib.openjp2._OPENJP2 = None + cls.openjp2 = glymur.lib.openjp2.OPENJP2 + glymur.lib.openjp2.OPENJP2 = None @classmethod def tearDownClass(cls): # Restore OPENJP2 - glymur.lib.openjp2._OPENJP2 = cls.openjp2 + glymur.lib.openjp2.OPENJP2 = cls.openjp2 def setUp(self): self.jp2file = glymur.data.nemo() diff --git a/glymur/test/test_opj_suite.py b/glymur/test/test_opj_suite.py index b866489..f882811 100644 --- a/glymur/test/test_opj_suite.py +++ b/glymur/test/test_opj_suite.py @@ -34,7 +34,7 @@ except: raise -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, "Missing openjp2 library.") @unittest.skipIf(data_root is None, "OPJ_DATA_ROOT environment variable not set") @@ -7333,7 +7333,7 @@ class TestSuiteDump(unittest.TestCase): [8, 9, 9, 10, 9, 9, 10, 9, 9, 10, 9, 9, 10, 9, 9, 10]) -@unittest.skipIf(glymur.lib.openjpeg._OPENJPEG is None, +@unittest.skipIf(glymur.lib.openjpeg.OPENJPEG is None, "Missing openjpeg library.") @unittest.skipIf(data_root is None, "OPJ_DATA_ROOT environment variable not set") @@ -7343,13 +7343,13 @@ class TestSuite15(unittest.TestCase): @classmethod def setUpClass(cls): # Monkey patch the package so as to use OPENJPEG instead of OPENJP2 - cls.openjp2 = glymur.lib.openjp2._OPENJP2 - glymur.lib.openjp2._OPENJP2 = None + cls.openjp2 = glymur.lib.openjp2.OPENJP2 + glymur.lib.openjp2.OPENJP2 = None @classmethod def tearDownClass(cls): # Restore OPENJP2 - glymur.lib.openjp2._OPENJP2 = cls.openjp2 + glymur.lib.openjp2.OPENJP2 = cls.openjp2 def setUp(self): pass diff --git a/glymur/test/test_opj_suite_neg.py b/glymur/test/test_opj_suite_neg.py index b9294be..6027ea2 100644 --- a/glymur/test/test_opj_suite_neg.py +++ b/glymur/test/test_opj_suite_neg.py @@ -57,7 +57,7 @@ def read_image(infile): return data -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, "Missing openjp2 library.") @unittest.skipIf(no_read_backend, no_read_backend_msg) @unittest.skipIf(data_root is None, diff --git a/glymur/test/test_opj_suite_write.py b/glymur/test/test_opj_suite_write.py index 9c7ac56..edbd893 100644 --- a/glymur/test/test_opj_suite_write.py +++ b/glymur/test/test_opj_suite_write.py @@ -57,7 +57,7 @@ def read_image(infile): return data -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, "Missing openjp2 library.") @unittest.skipIf(no_read_backend, no_read_backend_msg) @unittest.skipIf(data_root is None, diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index 91b1741..54176cd 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -21,7 +21,7 @@ except: raise -@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, +@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, "Missing openjp2 library.") class TestPrintingNeedsLib(unittest.TestCase): """These tests require the library, mostly in order to just setup the test.