def test_unknown_superbox(self): """Verify that we can handle an unknown superbox.""" with tempfile.NamedTemporaryFile(suffix='.jpx') as tfile: with open(self.jpxfile, 'rb') as ifile: tfile.write(ifile.read()) # Add the header for an unknown superbox. write_buffer = struct.pack('>I4s', 20, 'grp '.encode()) tfile.write(write_buffer) # Add a free box inside of it. We won't be able to identify it, # but it's there. write_buffer = struct.pack('>I4sI', 12, 'free'.encode(), 0) tfile.write(write_buffer) tfile.flush() with warnings.catch_warnings(): warnings.simplefilter("ignore") jpx = Jp2k(tfile.name) glymur.set_option('print.short', True) actual = str(jpx.box[-1]) if sys.hexversion < 0x03000000: expected = "Unknown Box (grp ) @ (1399071, 20)" else: expected = "Unknown Box (b'grp ') @ (1399071, 20)" self.assertEqual(actual, expected)
def test_reset_single_option(self): """ Verify a single option can be reset. """ glymur.set_option('print.codestream', True) glymur.reset_option('print.codestream') self.assertTrue(glymur.get_option('print.codestream'))
def __init__(self, path, **kwargs): """ Initialize the tile class. See the base class for other available parameters. :param path: a filesystem path for the tile source. """ super().__init__(path, **kwargs) self._largeImagePath = str(self._getLargeImagePath()) self._pixelInfo = {} try: self._openjpeg = glymur.Jp2k(self._largeImagePath) if not self._openjpeg.shape: if not os.path.isfile(self._largeImagePath): raise FileNotFoundError() raise TileSourceError( 'File cannot be opened via Glymur and OpenJPEG.') except (glymur.jp2box.InvalidJp2kError, struct.error): raise TileSourceError( 'File cannot be opened via Glymur and OpenJPEG.') except FileNotFoundError: if not os.path.isfile(self._largeImagePath): raise TileSourceFileNotFoundError( self._largeImagePath) from None raise glymur.set_option('lib.num_threads', multiprocessing.cpu_count()) self._openjpegHandles = queue.LifoQueue() for _ in range(self._maxOpenHandles - 1): self._openjpegHandles.put(None) self._openjpegHandles.put(self._openjpeg) try: self.sizeY, self.sizeX = self._openjpeg.shape[:2] except IndexError: raise TileSourceError( 'File cannot be opened via Glymur and OpenJPEG.') self.levels = int(self._openjpeg.codestream.segment[2].num_res) + 1 self._minlevel = 0 self.tileWidth = self.tileHeight = 2**int( math.ceil( max( math.log(float(self.sizeX)) / math.log(2) - self.levels + 1, math.log(float(self.sizeY)) / math.log(2) - self.levels + 1))) # Small and large tiles are both inefficient. Large tiles don't work # with some viewers (leaflet and Slide Atlas, for instance) if self.tileWidth < self._minTileSize or self.tileWidth > self._maxTileSize: self.tileWidth = self.tileHeight = min( self._maxTileSize, max(self._minTileSize, self.tileWidth)) self.levels = int( math.ceil( math.log( float(max(self.sizeX, self.sizeY)) / self.tileWidth) / math.log(2))) + 1 self._minlevel = self.levels - self._openjpeg.codestream.segment[ 2].num_res - 1 self._getAssociatedImages()
def test_full_codestream(self): """ Verify printing with the full blown codestream """ jp2 = Jp2k(self.jp2file) glymur.set_option('parse.full_codestream', True) # Get rid of the file line actual = '\n'.join(str(jp2).splitlines()[1:]) expected = fixtures.nemo self.assertEqual(actual, expected) opt = glymur.get_option('print.codestream') self.assertTrue(opt)
def test_suppress_codestream(self): """ Verify printing with codestream suppressed """ jp2 = Jp2k(self.jp2file) glymur.set_option('print.codestream', False) # Get rid of the file line actual = '\n'.join(str(jp2).splitlines()[1:]) expected = fixtures.nemo_dump_no_codestream self.assertEqual(actual, expected) opt = glymur.get_option('print.codestream') self.assertFalse(opt)
def test_suppress_xml(self): """ Verify printing with xml suppressed """ jp2 = Jp2k(self.jp2file) glymur.set_option('print.xml', False) with patch('sys.stdout', new=StringIO()) as fake_out: print(jp2) actual = fake_out.getvalue().strip() # Get rid of the file line, that's kind of volatile. actual = '\n'.join(actual.splitlines()[1:]) # shave off the XML and non-main-header segments expected = fixtures.nemo_dump_no_xml self.assertEqual(actual, expected) opt = glymur.get_option('print.xml') self.assertFalse(opt)
def test_suppress_xml(self): """ Verify printing with xml suppressed """ jp2 = Jp2k(self.jp2file) glymur.set_option('print.xml', False) actual = str(jp2) # Get rid of the file line, that's kind of volatile. actual = '\n'.join(actual.splitlines()[1:]) # shave off the XML and non-main-header segments expected = fixtures.nemo_dump_no_xml self.assertEqual(actual, expected) opt = glymur.get_option('print.xml') self.assertFalse(opt)
def test_bad_rsiz(self): """ Should not warn if RSIZ when parsing is turned off. This test was originally written for OPJ_DATA file input/nonregression/edf_c2_1002767.jp2' It had an RSIZ value of 32, so that's what we use here. Issue196 """ with tempfile.NamedTemporaryFile(suffix='.jp2', mode='wb') as ofile: with open(self.jp2file, 'rb') as ifile: # Copy up until the RSIZ value. ofile.write(ifile.read(3237)) # Write the bad RSIZ value. buffer = struct.pack('>H', 32) ofile.write(buffer) ifile.seek(3239) # Get the rest of the file. ofile.write(ifile.read()) ofile.seek(0) glymur.set_option('parse.full_codestream', False) Jp2k(ofile.name) glymur.set_option('parse.full_codestream', True) if sys.hexversion < 0x03000000: with warnings.catch_warnings(record=True) as w: Jp2k(ofile.name) assert issubclass(w[-1].category, UserWarning) else: with self.assertWarns(UserWarning): Jp2k(ofile.name)
def test_printoptions_bad_argument(self): """Verify error when bad parameter to set_printoptions""" with self.assertRaises(KeyError): glymur.set_option('hi', 'low')