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
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
to help Glymur properly find the openjpeg or openjp2 libraries
(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]
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
'''''''

View file

@ -205,28 +205,30 @@ class Jp2k(Jp2kBox):
if self._shape is not None:
return self._shape
cstr = self.codestream
height = cstr.segment[1].ysiz
width = cstr.segment[1].xsiz
num_components = len(cstr.segment[1].xrsiz)
if self._codec_format == opj2.CODEC_J2K:
# get the image size from the codestream
cstr = self.codestream
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 self._codec_format == opj2.CODEC_J2K:
# 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))
self.shape = (height, width)
else:
self.shape = (height, width, num_components)
@ -918,9 +920,11 @@ class Jp2k(Jp2kBox):
"""
Slicing protocol.
"""
numrows = self.codestream.segment[1].ysiz
numcols = self.codestream.segment[1].xsiz
numbands = self.codestream.segment[1].Csiz
if len(self.shape) == 2:
numrows, numcols = self.shape
numbands = 1
else:
numrows, numcols, numbands = self.shape
if isinstance(pargs, int):
# Not a very good use of this protocol, but technically legal.