progress, working on rlevel

This commit is contained in:
John Evans 2014-11-11 10:46:59 -05:00
commit 89a1b0bb1c
2 changed files with 64 additions and 34 deletions

View file

@ -60,6 +60,23 @@ class Jp2k(Jp2kBox):
List of top-level boxes in the file. Each box may in turn contain List of top-level boxes in the file. Each box may in turn contain
its own list of boxes. Will be empty if the file consists only of a its own list of boxes. Will be empty if the file consists only of a
raw codestream. raw codestream.
Examples
--------
>>> import glymur
>>> jfile = glymur.data.nemo()
>>> jp2 = glymur.Jp2k(jfile)
>>> jp2.shape
(1456, 2592, 3)
>>> image = jp2[:]
>>> image.shape
(1456, 2592, 3)
Read a lower resolution thumbnail.
>>> thumbnail = jp2[::2, ::2]
>>> thumbnail.shape
(728, 1296, 3)
""" """
def __init__(self, filename, mode='rb'): def __init__(self, filename, mode='rb'):
@ -957,6 +974,25 @@ class Jp2k(Jp2kBox):
return data[:, :, bands] return data[:, :, bands]
def _read(self):
"""Read a JPEG 2000 image.
Returns
-------
img_array : ndarray
The image data.
Raises
------
IOError
If the image has differing subsample factors.
"""
if version.openjpeg_version_tuple[0] < 2:
img = self._read_openjpeg(**kwargs)
else:
img = self._read_openjp2(**kwargs)
return img
def read(self, **kwargs): def read(self, **kwargs):
"""Read a JPEG 2000 image. """Read a JPEG 2000 image.
@ -988,22 +1024,8 @@ class Jp2k(Jp2kBox):
------ ------
IOError IOError
If the image has differing subsample factors. If the image has differing subsample factors.
Examples
--------
>>> import glymur
>>> jfile = glymur.data.nemo()
>>> jp = glymur.Jp2k(jfile)
>>> image = jp.read()
>>> image.shape
(1456, 2592, 3)
Read the lowest resolution thumbnail.
>>> thumbnail = jp.read(rlevel=-1)
>>> thumbnail.shape
(728, 1296, 3)
""" """
warnings.warn("Use array-style slicing instead.", DeprecationWarning)
if version.openjpeg_version_tuple[0] < 2: if version.openjpeg_version_tuple[0] < 2:
img = self._read_openjpeg(**kwargs) img = self._read_openjpeg(**kwargs)
else: else:

View file

@ -67,11 +67,13 @@ class SliceProtocolBase(unittest.TestCase):
def setUpClass(self): def setUpClass(self):
self.jp2 = Jp2k(glymur.data.nemo()) self.jp2 = Jp2k(glymur.data.nemo())
self.jp2_data = self.jp2.read() self.jp2_data = self.jp2[:]
self.jp2_data_r1 = self.jp2[::2, ::2]
self.j2k = Jp2k(glymur.data.goodstuff()) self.j2k = Jp2k(glymur.data.goodstuff())
self.j2k_data = self.j2k.read() self.j2k_data = self.j2k[:]
self.j2k_data_r1 = self.j2k[::2, ::2]
@unittest.skipIf(os.name == "nt", fixtures.WINDOWS_TMP_FILE_MSG) @unittest.skipIf(os.name == "nt", fixtures.WINDOWS_TMP_FILE_MSG)
class TestSliceProtocolBaseWrite(SliceProtocolBase): class TestSliceProtocolBaseWrite(SliceProtocolBase):
@ -82,7 +84,7 @@ class TestSliceProtocolBaseWrite(SliceProtocolBase):
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile: with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
j = Jp2k(tfile.name, 'wb') j = Jp2k(tfile.name, 'wb')
j[...] = self.j2k_data j[...] = self.j2k_data
actual = j.read() actual = j[:]
np.testing.assert_array_equal(actual, expected) np.testing.assert_array_equal(actual, expected)
@ -92,7 +94,7 @@ class TestSliceProtocolBaseWrite(SliceProtocolBase):
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile: with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
j = Jp2k(tfile.name, 'wb') j = Jp2k(tfile.name, 'wb')
j[:] = self.j2k_data j[:] = self.j2k_data
actual = j.read() actual = j[:]
np.testing.assert_array_equal(actual, expected) np.testing.assert_array_equal(actual, expected)
@ -167,9 +169,9 @@ class TestSliceProtocolRead(SliceProtocolBase):
np.testing.assert_array_equal(actual, expected) np.testing.assert_array_equal(actual, expected)
def test_reduce_resolution_and_slice_in_third_dimension(self): def test_reduce_resolution_and_slice_in_third_dimension(self):
d = self.j2k[::2, ::2, 1:3] actual = self.j2k[::2, ::2, 1:3]
all = self.j2k.read(rlevel=1) expected = self.j2k_data_r1[:, :, 1:3]
np.testing.assert_array_equal(all[:,:,1:3], d) np.testing.assert_array_equal(actual, expected)
def test_retrieve_single_row(self): def test_retrieve_single_row(self):
actual = self.jp2[0] actual = self.jp2[0]
@ -231,24 +233,24 @@ class TestSliceProtocolRead(SliceProtocolBase):
expected = self.jp2_data[728:, :] expected = self.jp2_data[728:, :]
np.testing.assert_array_equal(actual, expected) np.testing.assert_array_equal(actual, expected)
def test_region_rlevel1(self): def test_region_rlevel1_odd(self):
actual = self.jp2[0:201:2, 0:201:2] actual = self.jp2[0:201:2, 0:201:2]
expected = self.jp2.read(area=(0, 0, 201, 201), rlevel=1) expected = self.jp2_data_r1[:101, :101, :]
np.testing.assert_array_equal(actual, expected)
def test_region_rlevel1_even(self):
actual = self.jp2[0:202:2, 0:202:2]
expected = self.jp2_data_r1[:101, :101, :]
np.testing.assert_array_equal(actual, expected) np.testing.assert_array_equal(actual, expected)
def test_region_rlevel1_slice_start_is_none(self): def test_region_rlevel1_slice_start_is_none(self):
actual = self.jp2[:201:2, :201:2] actual = self.jp2[:201:2, :201:2]
expected = self.jp2.read(area=(0, 0, 201, 201), rlevel=1) expected = self.jp2_data_r1[:101, :101, :]
np.testing.assert_array_equal(actual, expected) np.testing.assert_array_equal(actual, expected)
def test_region_rlevel1_slice_stop_is_none(self): def test_region_rlevel1_slice_stop_is_none(self):
actual = self.jp2[201::2, 201::2] actual = self.jp2[201::2, 201::2]
expected = self.jp2.read(area=(201, 201, 1456, 2592), rlevel=1) expected = self.jp2_data_r1[101:, 101:, :]
np.testing.assert_array_equal(actual, expected)
def test_region_rlevel1(self):
actual = self.jp2[0:202:2, 0:202:2]
expected = self.jp2.read(area=(0, 0, 202, 202), rlevel=1)
np.testing.assert_array_equal(actual, expected) np.testing.assert_array_equal(actual, expected)
def test_ellipsis_full_read(self): def test_ellipsis_full_read(self):
@ -476,6 +478,12 @@ class TestJp2k(unittest.TestCase):
def tearDown(self): def tearDown(self):
pass pass
@unittest.skipIf(WARNING_INFRASTRUCTURE_ISSUE, WARNING_INFRASTRUCTURE_MSG)
def test_warn_if_using_read_method(self):
"""Should warn if read method is called"""
with self.assertWarns(DeprecationWarning):
Jp2k(self.jp2file).read()
def test_shape_jp2(self): def test_shape_jp2(self):
"""verify shape attribute for JP2 file """verify shape attribute for JP2 file
""" """
@ -519,7 +527,7 @@ class TestJp2k(unittest.TestCase):
def test_irreversible(self): def test_irreversible(self):
"""Irreversible""" """Irreversible"""
j = Jp2k(self.jp2file) j = Jp2k(self.jp2file)
expdata = j.read() expdata = j[:]
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile: with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
j2 = Jp2k(tfile.name, 'wb') j2 = Jp2k(tfile.name, 'wb')
j2.write(expdata, irreversible=True) j2.write(expdata, irreversible=True)
@ -528,7 +536,7 @@ class TestJp2k(unittest.TestCase):
self.assertEqual(codestream.segment[2].spcod[8], self.assertEqual(codestream.segment[2].spcod[8],
glymur.core.WAVELET_XFORM_9X7_IRREVERSIBLE) glymur.core.WAVELET_XFORM_9X7_IRREVERSIBLE)
actdata = j2.read() actdata = j2[:]
self.assertTrue(fixtures.mse(actdata[0], expdata[0]) < 0.38) self.assertTrue(fixtures.mse(actdata[0], expdata[0]) < 0.38)
@unittest.skipIf(re.match('1.[0-4]', openjpeg_version) is not None, @unittest.skipIf(re.match('1.[0-4]', openjpeg_version) is not None,
@ -538,7 +546,7 @@ class TestJp2k(unittest.TestCase):
def test_no_cxform_pclr_jpx(self): def test_no_cxform_pclr_jpx(self):
"""Indices for pclr jpxfile if no color transform""" """Indices for pclr jpxfile if no color transform"""
j = Jp2k(self.jpxfile) j = Jp2k(self.jpxfile)
rgb = j.read() rgb = j[:]
idx = j.read(ignore_pclr_cmap_cdef=True) idx = j.read(ignore_pclr_cmap_cdef=True)
nr, nc = 1024, 1024 nr, nc = 1024, 1024
self.assertEqual(rgb.shape, (nr, nc, 3)) self.assertEqual(rgb.shape, (nr, nc, 3))