Merge branch 'issue40' into devel

This commit is contained in:
John Evans 2013-06-16 10:31:08 -04:00
commit 3783ea8e7a
9 changed files with 821 additions and 797 deletions

View file

@ -1 +1 @@
from .test_openjp2 import TestOpenJP2 as openjp2 #from .test_openjp2 import TestOpenJP2 as openjp2

View file

@ -12,12 +12,14 @@ import numpy as np
import glymur import glymur
# Doc tests should be run as well.
def load_tests(loader, tests, ignore): def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite('glymur.lib.openjp2')) if glymur.lib.openjp2._OPENJP2 is not None:
tests.addTests(doctest.DocTestSuite('glymur.lib.openjp2'))
return tests return tests
@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None,
"Missing openjp2 library.")
class TestOpenJP2(unittest.TestCase): class TestOpenJP2(unittest.TestCase):
def setUp(self): def setUp(self):

View file

@ -12,14 +12,9 @@ else:
import glymur import glymur
try:
data_root = os.environ['OPJ_DATA_ROOT']
except KeyError:
data_root = None
except:
raise
@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None,
"Missing openjp2 library.")
class TestCallbacks(unittest.TestCase): class TestCallbacks(unittest.TestCase):
def setUp(self): def setUp(self):

View file

@ -67,7 +67,7 @@ class TestICC(unittest.TestCase):
jfile = os.path.join(data_root, jfile = os.path.join(data_root,
'input/nonregression/orb-blue10-lin-jp2.jp2') 'input/nonregression/orb-blue10-lin-jp2.jp2')
with self.assertWarns(UserWarning) as cw: with self.assertWarns(UserWarning) as cw:
data = Jp2k(jfile).read() j = Jp2k(jfile)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View file

@ -34,21 +34,13 @@ except:
# Doc tests should be run as well. # Doc tests should be run as well.
def load_tests(loader, tests, ignore): def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite('glymur.jp2k')) if glymur.lib.openjp2._OPENJP2 is not None:
tests.addTests(doctest.DocTestSuite('glymur.jp2k'))
return tests return tests
@contextlib.contextmanager @unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None,
def chdir(dirname=None): "Missing openjp2 library.")
curdir = os.getcwd()
try:
if dirname is not None:
os.chdir(dirname)
yield
finally:
os.chdir(curdir)
class TestJp2k(unittest.TestCase): class TestJp2k(unittest.TestCase):
@classmethod @classmethod

File diff suppressed because it is too large Load diff

View file

@ -57,6 +57,8 @@ def read_image(infile):
return data return data
@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None,
"Missing openjp2 library.")
@unittest.skipIf(no_read_backend, no_read_backend_msg) @unittest.skipIf(no_read_backend, no_read_backend_msg)
@unittest.skipIf(data_root is None, @unittest.skipIf(data_root is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")

View file

@ -57,6 +57,8 @@ def read_image(infile):
return data return data
@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None,
"Missing openjp2 library.")
@unittest.skipIf(no_read_backend, no_read_backend_msg) @unittest.skipIf(no_read_backend, no_read_backend_msg)
@unittest.skipIf(data_root is None, @unittest.skipIf(data_root is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")

View file

@ -21,7 +21,11 @@ except:
raise raise
class TestPrinting(unittest.TestCase): @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.
"""
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
@ -40,11 +44,11 @@ class TestPrinting(unittest.TestCase):
os.unlink(cls._plain_nemo_file) os.unlink(cls._plain_nemo_file)
def setUp(self): def setUp(self):
self.jp2file = pkg_resources.resource_filename(glymur.__name__,
"data/nemo.jp2")
# Save sys.stdout. # Save sys.stdout.
self.stdout = sys.stdout self.stdout = sys.stdout
sys.stdout = StringIO() sys.stdout = StringIO()
self.jp2file = pkg_resources.resource_filename(glymur.__name__,
"data/nemo.jp2")
# Save the output of dumping nemo.jp2 for more than one test. # Save the output of dumping nemo.jp2 for more than one test.
lines = ['JPEG 2000 Signature Box (jP ) @ (0, 12)', lines = ['JPEG 2000 Signature Box (jP ) @ (0, 12)',
@ -115,6 +119,57 @@ class TestPrinting(unittest.TestCase):
# Restore stdout. # Restore stdout.
sys.stdout = self.stdout sys.stdout = self.stdout
def test_asoc_label_box(self):
# Construct a fake file with an asoc and a label box, as
# OpenJPEG doesn't have such a file.
data = glymur.Jp2k(self.jp2file).read(reduce=3)
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
j = glymur.Jp2k(tfile.name, 'wb')
j.write(data)
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile2:
# Offset of the codestream is where we start.
buffer = tfile.read(77)
tfile2.write(buffer)
# read the rest of the file, it's the codestream.
codestream = tfile.read()
# Write the asoc superbox.
# Length = 36, id is 'asoc'.
buffer = struct.pack('>I4s', int(56), b'asoc')
tfile2.write(buffer)
# Write the contained label box
buffer = struct.pack('>I4s', int(13), b'lbl ')
tfile2.write(buffer)
tfile2.write('label'.encode())
# Write the xml box
# Length = 36, id is 'xml '.
buffer = struct.pack('>I4s', int(35), b'xml ')
tfile2.write(buffer)
buffer = '<test>this is a test</test>'
buffer = buffer.encode()
tfile2.write(buffer)
# Now append the codestream.
tfile2.write(codestream)
tfile2.flush()
jasoc = glymur.Jp2k(tfile2.name)
print(jasoc.box[3])
actual = sys.stdout.getvalue().strip()
lines = ['Association Box (asoc) @ (77, 56)',
' Label Box (lbl ) @ (85, 13)',
' Label: label',
' XML Box (xml ) @ (98, 35)',
' <test>this is a test</test>']
expected = '\n'.join(lines)
self.assertEqual(actual, expected)
def test_jp2dump(self): def test_jp2dump(self):
glymur.jp2dump(self._plain_nemo_file) glymur.jp2dump(self._plain_nemo_file)
actual = sys.stdout.getvalue().strip() actual = sys.stdout.getvalue().strip()
@ -126,6 +181,32 @@ class TestPrinting(unittest.TestCase):
self.assertEqual(actual, self.expectedPlain) self.assertEqual(actual, self.expectedPlain)
def test_entire_file(self):
j = glymur.Jp2k(self._plain_nemo_file)
print(j)
actual = sys.stdout.getvalue().strip()
# Get rid of the filename line, as it is not set in stone.
lst = actual.split('\n')
lst = lst[1:]
actual = '\n'.join(lst)
self.assertEqual(actual, self.expectedPlain)
class TestPrinting(unittest.TestCase):
def setUp(self):
# Save sys.stdout.
self.stdout = sys.stdout
sys.stdout = StringIO()
self.jp2file = pkg_resources.resource_filename(glymur.__name__,
"data/nemo.jp2")
def tearDown(self):
# Restore stdout.
sys.stdout = self.stdout
def test_COC_segment(self): def test_COC_segment(self):
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
@ -516,18 +597,6 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lst) expected = '\n'.join(lst)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_entire_file(self):
j = glymur.Jp2k(self._plain_nemo_file)
print(j)
actual = sys.stdout.getvalue().strip()
# Get rid of the filename line, as it is not set in stone.
lst = actual.split('\n')
lst = lst[1:]
actual = '\n'.join(lst)
self.assertEqual(actual, self.expectedPlain)
def test_codestream(self): def test_codestream(self):
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
print(j.get_codestream()) print(j.get_codestream())
@ -715,57 +784,6 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_asoc_label_box(self):
# Construct a fake file with an asoc and a label box, as
# OpenJPEG doesn't have such a file.
data = glymur.Jp2k(self.jp2file).read(reduce=3)
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
j = glymur.Jp2k(tfile.name, 'wb')
j.write(data)
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile2:
# Offset of the codestream is where we start.
buffer = tfile.read(77)
tfile2.write(buffer)
# read the rest of the file, it's the codestream.
codestream = tfile.read()
# Write the asoc superbox.
# Length = 36, id is 'asoc'.
buffer = struct.pack('>I4s', int(56), b'asoc')
tfile2.write(buffer)
# Write the contained label box
buffer = struct.pack('>I4s', int(13), b'lbl ')
tfile2.write(buffer)
tfile2.write('label'.encode())
# Write the xml box
# Length = 36, id is 'xml '.
buffer = struct.pack('>I4s', int(35), b'xml ')
tfile2.write(buffer)
buffer = '<test>this is a test</test>'
buffer = buffer.encode()
tfile2.write(buffer)
# Now append the codestream.
tfile2.write(codestream)
tfile2.flush()
jasoc = glymur.Jp2k(tfile2.name)
print(jasoc.box[3])
actual = sys.stdout.getvalue().strip()
lines = ['Association Box (asoc) @ (77, 56)',
' Label Box (lbl ) @ (85, 13)',
' Label: label',
' XML Box (xml ) @ (98, 35)',
' <test>this is a test</test>']
expected = '\n'.join(lines)
self.assertEqual(actual, expected)
def test_less_common_boxes(self): def test_less_common_boxes(self):
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
with open(self.jp2file, 'rb') as ifile: with open(self.jp2file, 'rb') as ifile: