refactor shape property to be less dependent on property, closes #315

This commit is contained in:
John Evans 2015-01-07 08:40:08 -05:00 • committed by jevans
commit 4d680cf90b
2 changed files with 31 additions and 24 deletions

View file

@ -15,7 +15,7 @@ or if you use windows, then read on.
Glymur uses ctypes to access the openjp2/openjpeg libraries, and Glymur uses ctypes to access the openjp2/openjpeg libraries, and
because ctypes accesses libraries in a platform-dependent manner, because ctypes accesses libraries in a platform-dependent manner,
it is recommended that if you compile and install OpenJPEG into a it is recommended that **if** you compile and install OpenJPEG into a
non-standard location, you should then create a configuration file non-standard location, you should then create a configuration file
to help Glymur properly find the openjpeg or openjp2 libraries to help Glymur properly find the openjpeg or openjp2 libraries
(linux users or macports users don’t need to bother with this if (linux users or macports users don’t need to bother with this if
@ -50,6 +50,9 @@ installed in a non-standard place, i.e. ::
[library] [library]
openjpeg: /somewhere/lib/libopenjpeg.so openjpeg: /somewhere/lib/libopenjpeg.so
Once again, you should not have to bother with a configuration file if you use
mac or linux and OpenJPEG is provided by your package manager.
''''''' '''''''
Testing Testing
''''''' '''''''

View file

@ -205,28 +205,30 @@ class Jp2k(Jp2kBox):
if self._shape is not None: if self._shape is not None:
return self._shape return self._shape
cstr = self.codestream if self._codec_format == opj2.CODEC_J2K:
height = cstr.segment[1].ysiz # get the image size from the codestream
width = cstr.segment[1].xsiz cstr = self.codestream
num_components = len(cstr.segment[1].xrsiz) height = cstr.segment[1].ysiz
width = cstr.segment[1].xsiz
num_components = len(cstr.segment[1].xrsiz)
else:
# try to get the image size from the IHDR box
jp2h = [box for box in self.box if box.box_id == 'jp2h'][0]
ihdr = [box for box in jp2h.box if box.box_id == 'ihdr'][0]
height, width = ihdr.height, ihdr.width
num_components = ihdr.num_components
if num_components == 1:
# but if there is a PCLR box, then we need to check that as
# well, as that turns a single-channel image into a
# multi-channel image
pclr = [box for box in jp2h.box if box.box_id == 'pclr']
if len(pclr) > 0:
num_components = len(pclr[0].signed)
# If JP2 and a palette box is present, then determine the shape from
# that.
if num_components == 1: if num_components == 1:
if self._codec_format == opj2.CODEC_J2K: self.shape = (height, width)
# There's no palette box or component mapping in a J2K file.
# The 3rd component in the shape would then be 1, but we'll
# ignore that.
self.shape = (height, width)
else:
jp2h = [box for box in self.box if box.box_id == 'jp2h'][0]
pclr = [box for box in jp2h.box if box.box_id == 'pclr']
if len(pclr) == 0:
# No palette box, so just one component, which we will
# ignore.
self.shape = (height, width)
else:
self.shape = (height, width, len(pclr[0].signed))
else: else:
self.shape = (height, width, num_components) self.shape = (height, width, num_components)
@ -918,9 +920,11 @@ class Jp2k(Jp2kBox):
""" """
Slicing protocol. Slicing protocol.
""" """
numrows = self.codestream.segment[1].ysiz if len(self.shape) == 2:
numcols = self.codestream.segment[1].xsiz numrows, numcols = self.shape
numbands = self.codestream.segment[1].Csiz numbands = 1
else:
numrows, numcols, numbands = self.shape
if isinstance(pargs, int): if isinstance(pargs, int):
# Not a very good use of this protocol, but technically legal. # Not a very good use of this protocol, but technically legal.