ignore_pclr_cmap_cdef is now a property

This commit is contained in:
jevans 2014-11-13 13:39:36 -05:00
commit 4f2a896eff
4 changed files with 37 additions and 27 deletions

View file

@ -60,6 +60,11 @@ 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.
shape : tuple
Size of the image.
ignore_pclr_cmap_cdef : bool
Whether or not to ignore the pclr, cmap, or cdef boxes during any
color transformation. Defaults to False.
Examples Examples
-------- --------
@ -95,11 +100,20 @@ class Jp2k(Jp2kBox):
self._codec_format = None self._codec_format = None
self._colorspace = None self._colorspace = None
self._shape = None self._shape = None
self._ignore_pclr_cmap_cdef = False
# Parse the file for JP2/JPX contents only if we are reading it. # Parse the file for JP2/JPX contents only if we are reading it.
if mode == 'rb': if mode == 'rb':
self.parse() self.parse()
@property
def ignore_pclr_cmap_cdef(self):
return self._ignore_pclr_cmap_cdef
@ignore_pclr_cmap_cdef.setter
def ignore_pclr_cmap_cdef(self, ignore_pclr_cmap_cdef):
self._ignore_pclr_cmap_cdef = ignore_pclr_cmap_cdef
@property @property
def shape(self): def shape(self):
if self._shape is not None: if self._shape is not None:
@ -1009,9 +1023,6 @@ class Jp2k(Jp2kBox):
(first_row, first_col, last_row, last_col) (first_row, first_col, last_row, last_col)
tile : int, optional tile : int, optional
Number of tile to decode. Number of tile to decode.
ignore_pclr_cmap_cdef : bool
Whether or not to ignore the pclr, cmap, or cdef boxes during any
color transformation. Defaults to False.
verbose : bool, optional verbose : bool, optional
Print informational messages produced by the OpenJPEG library. Print informational messages produced by the OpenJPEG library.
@ -1025,6 +1036,8 @@ class Jp2k(Jp2kBox):
IOError IOError
If the image has differing subsample factors. If the image has differing subsample factors.
""" """
if 'ignore_pclr_cmap_cdef' in kwargs:
self.ignore_pclr_cmap_cdef = kwargs['ignore_pclr_cmap_cdef']
warnings.warn("Use array-style slicing instead.", DeprecationWarning) 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)
@ -1044,8 +1057,7 @@ class Jp2k(Jp2kBox):
msg += "the read_bands method instead." msg += "the read_bands method instead."
raise RuntimeError(msg) raise RuntimeError(msg)
def _read_openjpeg(self, rlevel=0, ignore_pclr_cmap_cdef=False, def _read_openjpeg(self, rlevel=0, verbose=False, area=None):
verbose=False, area=None):
"""Read a JPEG 2000 image using libopenjpeg. """Read a JPEG 2000 image using libopenjpeg.
Parameters Parameters
@ -1053,9 +1065,6 @@ class Jp2k(Jp2kBox):
rlevel : int, optional rlevel : int, optional
Factor by which to rlevel output resolution. Use -1 to get the Factor by which to rlevel output resolution. Use -1 to get the
lowest resolution thumbnail. lowest resolution thumbnail.
ignore_pclr_cmap_cdef : bool
Whether or not to ignore the pclr, cmap, or cdef boxes during any
color transformation. Defaults to False.
verbose : bool, optional verbose : bool, optional
Print informational messages produced by the OpenJPEG library. Print informational messages produced by the OpenJPEG library.
area : tuple, optional area : tuple, optional
@ -1074,7 +1083,7 @@ class Jp2k(Jp2kBox):
""" """
self._subsampling_sanity_check() self._subsampling_sanity_check()
self._populate_dparams(rlevel, ignore_pclr_cmap_cdef) self._populate_dparams(rlevel)
with ExitStack() as stack: with ExitStack() as stack:
try: try:
@ -1127,8 +1136,7 @@ class Jp2k(Jp2kBox):
return data return data
def _read_openjp2(self, rlevel=0, layer=0, area=None, tile=None, def _read_openjp2(self, rlevel=0, layer=0, area=None, tile=None, verbose=False):
verbose=False, ignore_pclr_cmap_cdef=False):
"""Read a JPEG 2000 image using libopenjp2. """Read a JPEG 2000 image using libopenjp2.
Parameters Parameters
@ -1158,8 +1166,7 @@ class Jp2k(Jp2kBox):
""" """
self._subsampling_sanity_check() self._subsampling_sanity_check()
self._populate_dparams(rlevel, ignore_pclr_cmap_cdef, self._populate_dparams(rlevel, layer=layer, tile=tile, area=area)
layer=layer, tile=tile, area=area)
with ExitStack() as stack: with ExitStack() as stack:
if re.match("2.1", version.openjpeg_version): if re.match("2.1", version.openjpeg_version):
@ -1203,8 +1210,7 @@ class Jp2k(Jp2kBox):
return img_array return img_array
def _populate_dparams(self, rlevel, ignore_pclr_cmap_cdef, tile=None, def _populate_dparams(self, rlevel, tile=None, layer=None, area=None):
layer=None, area=None):
"""Populate decompression structure with appropriate input parameters. """Populate decompression structure with appropriate input parameters.
Parameters Parameters
@ -1218,9 +1224,6 @@ class Jp2k(Jp2kBox):
(first_row, first_col, last_row, last_col) (first_row, first_col, last_row, last_col)
tile : int tile : int
Number of tile to decode. Number of tile to decode.
ignore_pclr_cmap_cdef : bool
Whether or not to ignore the pclr, cmap, or cdef boxes during any
color transformation. Defaults to False.
""" """
if opj2.OPENJP2 is not None: if opj2.OPENJP2 is not None:
dparam = opj2.set_default_decoder_parameters() dparam = opj2.set_default_decoder_parameters()
@ -1233,6 +1236,10 @@ class Jp2k(Jp2kBox):
infile += b'0' * nelts infile += b'0' * nelts
dparam.infile = infile dparam.infile = infile
if self.ignore_pclr_cmap_cdef:
# Return raw codestream components.
dparam.flags |= 1
dparam.decod_format = self._codec_format dparam.decod_format = self._codec_format
if layer is not None: if layer is not None:
@ -1267,7 +1274,7 @@ class Jp2k(Jp2kBox):
dparam.tile_index = tile dparam.tile_index = tile
dparam.nb_tile_to_decode = 1 dparam.nb_tile_to_decode = 1
if ignore_pclr_cmap_cdef is True: if self.ignore_pclr_cmap_cdef:
# Return raw codestream components. # Return raw codestream components.
dparam.flags |= 1 dparam.flags |= 1
@ -1319,8 +1326,8 @@ class Jp2k(Jp2kBox):
"OpenJPEG installed before using this " "OpenJPEG installed before using this "
"functionality.") "functionality.")
self._populate_dparams(rlevel, ignore_pclr_cmap_cdef, self.ignore_pclr_cmap_cdef = ignore_pclr_cmap_cdef
layer=layer, tile=tile, area=area) self._populate_dparams(rlevel, layer=layer, tile=tile, area=area)
with ExitStack() as stack: with ExitStack() as stack:
if re.match("2.1", version.openjpeg_version): if re.match("2.1", version.openjpeg_version):

View file

@ -548,7 +548,8 @@ class TestJp2k(unittest.TestCase):
"""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[:] rgb = j[:]
idx = j.read(ignore_pclr_cmap_cdef=True) j.ignore_pclr_cmap_cdef = True
idx = j[:]
nr, nc = 1024, 1024 nr, nc = 1024, 1024
self.assertEqual(rgb.shape, (nr, nc, 3)) self.assertEqual(rgb.shape, (nr, nc, 3))
self.assertEqual(idx.shape, (nr, nc)) self.assertEqual(idx.shape, (nr, nc))
@ -1373,7 +1374,8 @@ class TestJp2kOpjDataRoot(unittest.TestCase):
with self.assertWarns(UserWarning): with self.assertWarns(UserWarning):
jp2 = Jp2k(filename) jp2 = Jp2k(filename)
rgb = jp2[:] rgb = jp2[:]
idx = jp2.read(ignore_pclr_cmap_cdef=True) jp2.ignore_pclr_cmap_cdef = True
idx = jp2[:]
self.assertEqual(rgb.shape, (512, 768, 3)) self.assertEqual(rgb.shape, (512, 768, 3))
self.assertEqual(idx.shape, (512, 768)) self.assertEqual(idx.shape, (512, 768))
@ -1407,7 +1409,8 @@ class TestJp2kOpjDataRoot(unittest.TestCase):
# The file has a bad compatibility list entry. Not important here. # The file has a bad compatibility list entry. Not important here.
j = Jp2k(filename) j = Jp2k(filename)
ycbcr = j[:] ycbcr = j[:]
crcby = j.read(ignore_pclr_cmap_cdef=True) j.ignore_pclr_cmap_cdef = True
crcby = j[:]
expected = np.zeros(ycbcr.shape, ycbcr.dtype) expected = np.zeros(ycbcr.shape, ycbcr.dtype)
for k in range(crcby.shape[2]): for k in range(crcby.shape[2]):

View file

@ -134,7 +134,7 @@ class TestSuite2point1(unittest.TestCase):
jfile = opj_data_file('input/conformance/p1_04.j2k') jfile = opj_data_file('input/conformance/p1_04.j2k')
jp2k = Jp2k(jfile) jp2k = Jp2k(jfile)
tdata = jp2k.read(tile=63, rlevel=2) # last tile tdata = jp2k.read(tile=63, rlevel=2) # last tile
odata = jp2k.read(rlevel=2) odata = jp2k[::4, ::4]
np.testing.assert_array_equal(tdata, odata[224:256, 224:256]) np.testing.assert_array_equal(tdata, odata[224:256, 224:256])
def test_NR_DEC_p1_04_j2k_59_decode(self): def test_NR_DEC_p1_04_j2k_59_decode(self):
@ -148,7 +148,7 @@ class TestSuite2point1(unittest.TestCase):
jfile = opj_data_file('input/conformance/p1_04.j2k') jfile = opj_data_file('input/conformance/p1_04.j2k')
jp2k = Jp2k(jfile) jp2k = Jp2k(jfile)
tdata = jp2k.read(tile=12, rlevel=1) # 2nd row, 5th column tdata = jp2k.read(tile=12, rlevel=1) # 2nd row, 5th column
odata = jp2k.read(rlevel=1) odata = jp2k[::2, ::2]
np.testing.assert_array_equal(tdata, odata[64:128, 256:320]) np.testing.assert_array_equal(tdata, odata[64:128, 256:320])
@unittest.skipIf(WARNING_INFRASTRUCTURE_ISSUE, WARNING_INFRASTRUCTURE_MSG) @unittest.skipIf(WARNING_INFRASTRUCTURE_ISSUE, WARNING_INFRASTRUCTURE_MSG)

View file

@ -111,7 +111,7 @@ class TestPrinting(unittest.TestCase):
"""verify printing of asoc, label boxes""" """verify printing of asoc, label boxes"""
# Construct a fake file with an asoc and a label box, as # Construct a fake file with an asoc and a label box, as
# OpenJPEG doesn't have such a file. # OpenJPEG doesn't have such a file.
data = glymur.Jp2k(self.jp2file).read(rlevel=1) data = glymur.Jp2k(self.jp2file)[::2, ::2]
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
j = glymur.Jp2k(tfile.name, 'wb') j = glymur.Jp2k(tfile.name, 'wb')
j.write(data) j.write(data)