From decd07cbc3c652a6a326afb5dc35ff5b55f108d9 Mon Sep 17 00:00:00 2001 From: John Evans Date: Sat, 15 Jun 2013 12:36:27 -0400 Subject: [PATCH 1/6] Exif dictionaries changed to ordered dict, #46 --- CHANGES.txt | 5 +++- glymur/jp2box.py | 11 ++++---- glymur/test/test_printing.py | 54 ++++++++++++++++++------------------ 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 3e761d3..d878edc 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,7 @@ -Jun 11, 2013 - v0.1.8 Added reduce=-1 option to get lowest resolution +Jun 15, 2013 - v0.1.9 Reading ICC profile headers. Exif dictionaries changed + to ordered dicts. + +Jun 14, 2013 - v0.1.8 Added reduce=-1 option to get lowest resolution thumbnail. Jun 07, 2013 - v0.1.7 Changed Exif dictionary names from ['Exif', 'Photo', diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 820f869..c8b6c35 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -11,6 +11,7 @@ References Extensions """ +import collections import copy import datetime import math @@ -1713,8 +1714,8 @@ class UUIDBox(Jp2kBox): more verbose description of the box. uuid : uuid.UUID 16-byte UUID - data : bytes or dictionary or ElementTree.Element - Vendor-specific UUID data. Exif UUIDs are interpreted as dictionaries. + data : bytes or dict or ElementTree.Element + Vendor-specific data. Exif UUIDs are interpreted as dictionaries. XMP UUIDs are interpreted as standard XML. """ def __init__(self, **kwargs): @@ -1782,7 +1783,7 @@ class UUIDBox(Jp2kBox): kwargs['data'] = ET.fromstring(text) elif kwargs['uuid'].bytes == b'JpgTiffExif->JP2': e = Exif(buffer) - d = {} + d = collections.OrderedDict() d['Image'] = e.exif_image d['Photo'] = e.exif_photo d['GPSInfo'] = e.exif_gpsinfo @@ -1877,7 +1878,7 @@ class _Ifd: def __init__(self, endian, buffer, offset): self.endian = endian self.buffer = buffer - self.processed_ifd = {} + self.processed_ifd = collections.OrderedDict() self.num_tags, = struct.unpack(endian + 'H', buffer[offset:offset + 2]) @@ -1885,7 +1886,7 @@ class _Ifd: fmt = self.endian + 'HHII' * self.num_tags ifd_buffer = buffer[offset + 2:offset + 2 + self.num_tags * 12] data = struct.unpack(fmt, ifd_buffer) - self.raw_ifd = {} + self.raw_ifd = collections.OrderedDict() for j, tag in enumerate(data[0::4]): # The offset to the tag offset/payload is the offset to the IFD # plus 2 bytes for the number of tags plus 12 bytes for each diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index d6782b5..50b5033 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -837,13 +837,33 @@ class TestPrinting(unittest.TestCase): lines = ["UUID Box (uuid) @ (77, 638)", " UUID: 4a706754-6966-6645-7869-662d3e4a5032 (Exif)", " UUID Data: ", - "{'GPSInfo': {'GPSAltitude': 0.0,", - " 'GPSAltitudeRef': 0,", - " 'GPSDateStamp': '2013:02:09',", - " 'GPSLatitude': [42.0, 20.0, 33.61],", + "{'Image': {'Make': 'HTC',", + " 'Model': 'HTC Glacier',", + " 'XResolution': 72.0,", + " 'YResolution': 72.0,", + " 'ResolutionUnit': 2,", + " 'YCbCrPositioning': 1,", + " 'ExifTag': 138,", + " 'GPSTag': 354},", + " 'Photo': {'ISOSpeedRatings': 76,", + " 'ExifVersion': (48, 50, 50, 48),", + " 'DateTimeOriginal': '2013:02:09 14:47:53',", + " 'DateTimeDigitized': '2013:02:09 14:47:53',", + " 'ComponentsConfiguration': (1, 2, 3, 0),", + " 'FocalLength': 3.53,", + " 'FlashpixVersion': (48, 49, 48, 48),", + " 'ColorSpace': 1,", + " 'PixelXDimension': 2528,", + " 'PixelYDimension': 1424,", + " 'InteroperabilityTag': 324},", + " 'GPSInfo': {'GPSVersionID': (2, 2, 0),", " 'GPSLatitudeRef': 'N',", - " 'GPSLongitude': [71.0, 5.0, 17.32],", + " 'GPSLatitude': [42.0, 20.0, 33.61],", " 'GPSLongitudeRef': 'W',", + " 'GPSLongitude': [71.0, 5.0, 17.32],", + " 'GPSAltitudeRef': 0,", + " 'GPSAltitude': 0.0,", + " 'GPSTimeStamp': [19.0, 47.0, 53.0],", " 'GPSMapDatum': 'WGS-84',", " 'GPSProcessingMethod': (65,", " 83,", @@ -860,28 +880,8 @@ class TestPrinting(unittest.TestCase): " 79,", " 82,", " 75),", - " 'GPSTimeStamp': [19.0, 47.0, 53.0],", - " 'GPSVersionID': (2, 2, 0)},", - " 'Image': {'ExifTag': 138,", - " 'GPSTag': 354,", - " 'Make': 'HTC',", - " 'Model': 'HTC Glacier',", - " 'ResolutionUnit': 2,", - " 'XResolution': 72.0,", - " 'YCbCrPositioning': 1,", - " 'YResolution': 72.0},", - " 'Iop': None,", - " 'Photo': {'ColorSpace': 1,", - " 'ComponentsConfiguration': (1, 2, 3, 0),", - " 'DateTimeDigitized': '2013:02:09 14:47:53',", - " 'DateTimeOriginal': '2013:02:09 14:47:53',", - " 'ExifVersion': (48, 50, 50, 48),", - " 'FlashpixVersion': (48, 49, 48, 48),", - " 'FocalLength': 3.53,", - " 'ISOSpeedRatings': 76,", - " 'InteroperabilityTag': 324,", - " 'PixelXDimension': 2528,", - " 'PixelYDimension': 1424}}"] + " 'GPSDateStamp': '2013:02:09'},", + " 'Iop': None}"] expected = '\n'.join(lines) From f8ceb09a9b784ea7da25b25a755d3e7439d6d969 Mon Sep 17 00:00:00 2001 From: John Evans Date: Sat, 15 Jun 2013 15:05:56 -0400 Subject: [PATCH 2/6] ICC header fields now read as ordered dicts (#46) --- CHANGES.txt | 4 +- glymur/jp2box.py | 145 +++++++++++++--------------------- glymur/test/test_icc.py | 37 +++++---- glymur/test/test_opj_suite.py | 8 +- glymur/test/test_printing.py | 53 +++++++------ 5 files changed, 108 insertions(+), 139 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index d878edc..749716c 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,5 @@ -Jun 15, 2013 - v0.1.9 Reading ICC profile headers. Exif dictionaries changed - to ordered dicts. +Jun 15, 2013 - v0.1.9 Reading ICC profile headers as ordered dicts. Exif + dictionaries changed to ordered dicts. Jun 14, 2013 - v0.1.8 Added reduce=-1 option to get lowest resolution thumbnail. diff --git a/glymur/jp2box.py b/glymur/jp2box.py index c8b6c35..8d74635 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -155,9 +155,9 @@ class ColourSpecificationBox(Jp2kBox): colorspace : int or None Enumerated colorspace, corresponds to one of 'sRGB', 'greyscale', or 'YCC'. If not None, then icc_profile must be None. - icc_profile : _ICCProfile or None - ICC profile header according to ICC profile specification. If not - None, then color_space must be None. + icc_profile : dict + ICC profile header according to ICC profile specification. If + colorspace is not None, then icc_profile must be empty. """ def __init__(self, **kwargs): Jp2kBox.__init__(self, id='', longname='Colour Specification') @@ -176,7 +176,9 @@ class ColourSpecificationBox(Jp2kBox): x = _colorspace_map_display[self.colorspace] msg += '\n Colorspace: {0}'.format(x) else: - msg += '\n ICC Profile: {0}'.format(self.icc_profile.__str__()) + x = pprint.pformat(self.icc_profile) + lines = [' ' * 8 + y for y in x.split('\n')] + msg += '\n ICC Profile:\n{0}'.format('\n'.join(lines)) return msg @@ -229,7 +231,7 @@ class ColourSpecificationBox(Jp2kBox): kwargs['icc_profile'] = None else: icc_profile = _ICCProfile(f.read(n)) - kwargs['icc_profile'] = icc_profile + kwargs['icc_profile'] = icc_profile.header box = ColourSpecificationBox(**kwargs) return box @@ -279,110 +281,71 @@ class _ICCProfile: def __init__(self, buffer): self._raw_buffer = buffer + header = collections.OrderedDict() - self.size, = struct.unpack('>I', self._raw_buffer[0:4]) - self.preferred_cmm_type, = struct.unpack('>I', self._raw_buffer[4:8]) + data = struct.unpack('>IIBB', self._raw_buffer[0:10]) + header['Size'] = data[0] + header['Preferred CMM Type'] = data[1] + major = data[2] + minor = (data[3] & 0xf0) >> 4 + bugfix = (data[3] & 0x0f) + header['Version'] = '{0}.{1}.{2}'.format(major, minor, bugfix) - data = struct.unpack('>BB', self._raw_buffer[8:10]) - major = data[0] - minor = (data[1] & 0xf0) >> 4 - bugfix = (data[1] & 0x0f) - self.version = '{0}.{1}.{2}'.format(major, minor, bugfix) - - self.device_class = self.profile_class[self._raw_buffer[12:16]] - self.colour_space = self.colour_space_dict[self._raw_buffer[16:20]] - self.connection_space = self.colour_space_dict[self._raw_buffer[20:24]] + header['Device Class'] = self.profile_class[self._raw_buffer[12:16]] + header['Color Space'] = self.colour_space_dict[self._raw_buffer[16:20]] + data = self.colour_space_dict[self._raw_buffer[20:24]] + header['Connection Space'] = data data = struct.unpack('>HHHHHH', self._raw_buffer[24:36]) - self.datetime = datetime.datetime(*data) - self.file_signature = buffer[36:40].decode('utf-8') + header['Datetime'] = datetime.datetime(*data) + header['File Signature'] = buffer[36:40].decode('utf-8') if buffer[40:44] == b'\x00\x00\x00\x00': - self.platform = 'unrecognized' + header['Platform'] = 'unrecognized' else: - self.platform = buffer[40:44].decode('utf-8') + header['Platform'] = buffer[40:44].decode('utf-8') - self.flags, = struct.unpack('>I', buffer[44:48]) + x, = struct.unpack('>I', buffer[44:48]) + y = 'embedded, ' if x & 0x01 else 'not embedded, ' + y += 'cannot ' if x & 0x02 else 'can ' + y += 'be used independently' + header['Flags'] = y - self.device_manufacturer = buffer[48:52].decode('utf-8') + header['Device Manufacturer'] = buffer[48:52].decode('utf-8') if buffer[52:56] == b'\x00\x00\x00\x00': - self.device_model = '' + device_model = '' else: - self.device_model = buffer[52:56].decode('utf-8') - self.device_attributes, = struct.unpack('>Q', buffer[56:64]) - self.rendering_intent, = struct.unpack('>I', buffer[64:68]) + device_model = buffer[52:56].decode('utf-8') + header['Device Model'] = device_model + + x, = struct.unpack('>Q', buffer[56:64]) + y = 'transparency, ' if x & 0x01 else 'reflective, ' + y += 'matte, ' if x & 0x02 else 'glossy, ' + y += 'negative ' if x & 0x04 else 'positive ' + y += 'media polarity, ' + y += 'black and white media' if x & 0x08 else 'color media' + header['Device Attributes'] = y + + x, = struct.unpack('>I', buffer[64:68]) + try: + header['Rendering Intent'] = self.rendering_intent_dict[x] + except KeyError: + header['Rendering Intent'] = 'unknown' data = struct.unpack('>iii', buffer[68:80]) - self.illuminant = np.array(data, dtype=np.float64) / 65536 + header['Illuminant'] = np.array(data, dtype=np.float64) / 65536 if buffer[80:84] == b'\x00\x00\x00\x00': - self.creator = 'unrecognized' + creator = 'unrecognized' else: - self.creator = buffer[80:84].decode('utf-8') + creator = buffer[80:84].decode('utf-8') + header['Creator'] = creator - self.profile_id = buffer[84:100] - self.reserved = buffer[100:127] + if header['Version'][0] == '4': + header['Profile Id'] = buffer[84:100] - def __str__(self): - msg = "\n Size: {0}" - msg += "\n Preferred CMM type: {1:x}" - msg += "\n Version: {2}" - msg += "\n Device class signature: {3}" - msg += "\n Color space: {4}" - msg += "\n Connection space: {5}" - msg += "\n Creation time: {6}" - msg += "\n File signature: {7}" - msg += "\n Platform: {8}" - msg += "\n Flags: {9}" - msg += "\n Device manufacturer: {10}" - msg += "\n Device model: {11}" - msg += "\n Device attributes: {12}" - msg += "\n Rendering intent: {13}" - msg += "\n Illuminant: {14}" - msg += "\n Creator signature: {15}" + # Final 27 bytes are reserved. - if self.flags & 0x01: - flag_string = 'embedded, ' - else: - flag_string = 'not embedded, ' - if self.flags & 0x02: - flag_string += 'cannot be used independently' - else: - flag_string += 'can be used independently' - - if self.device_attributes & 0x01: - attr_string = 'transparency, ' - else: - attr_string = 'reflective, ' - if self.device_attributes & 0x02: - attr_string += 'matte, ' - else: - attr_string += 'glossy, ' - if self.device_attributes & 0x04: - attr_string += 'negative media polarity, ' - else: - attr_string += 'positive media polarity, ' - if self.device_attributes & 0x08: - attr_string += 'black and white media' - else: - attr_string += 'color media' - - msg = msg.format(self.size, - self.preferred_cmm_type, - self.version, - self.device_class, - self.colour_space, - self.connection_space, - self.datetime, - self.file_signature, - self.platform, - flag_string, - self.device_manufacturer, - self.device_model, - attr_string, - self.rendering_intent_dict[self.rendering_intent], - self.illuminant, - self.creator) - return(msg) + self.header = header class ComponentDefinitionBox(Jp2kBox): diff --git a/glymur/test/test_icc.py b/glymur/test/test_icc.py index c6081ff..bbce1d6 100644 --- a/glymur/test/test_icc.py +++ b/glymur/test/test_icc.py @@ -35,32 +35,31 @@ class TestICC(unittest.TestCase): filename = os.path.join(data_root, 'input/conformance/file5.jp2') j = Jp2k(filename) profile = j.box[3].box[1].icc_profile - self.assertEqual(profile.size, 546) - self.assertEqual(profile.preferred_cmm_type, 0) - self.assertEqual(profile.version, '2.2.0') - self.assertEqual(profile.device_class, 'input device profile') - self.assertEqual(profile.colour_space, 'RGB') - self.assertEqual(profile.datetime, + self.assertEqual(profile['Size'], 546) + self.assertEqual(profile['Preferred CMM Type'], 0) + self.assertEqual(profile['Version'], '2.2.0') + self.assertEqual(profile['Device Class'], 'input device profile') + self.assertEqual(profile['Color Space'], 'RGB') + self.assertEqual(profile['Datetime'], datetime.datetime(2001, 8, 30, 13, 32, 37)) - self.assertEqual(profile.file_signature, 'acsp') - self.assertEqual(profile.platform, 'unrecognized') - self.assertTrue(profile.flags & 0x01) # embedded - self.assertFalse(profile.flags & 0x02) # use anywhere + self.assertEqual(profile['File Signature'], 'acsp') + self.assertEqual(profile['Platform'], 'unrecognized') + self.assertEqual(profile['Flags'], + 'embedded, can be used independently') - self.assertEqual(profile.device_manufacturer, 'KODA') - self.assertEqual(profile.device_model, 'ROMM') + self.assertEqual(profile['Device Manufacturer'], 'KODA') + self.assertEqual(profile['Device Model'], 'ROMM') - self.assertFalse(profile.device_attributes & 0x01) # reflective - self.assertFalse(profile.device_attributes & 0x02) # glossy - self.assertFalse(profile.device_attributes & 0x04) # positive - self.assertFalse(profile.device_attributes & 0x08) # colour - self.assertEqual(profile.rendering_intent & 0x00ff, 0) # perceptual + self.assertEqual(profile['Device Attributes'], + 'reflective, glossy, positive media polarity, ' + + 'color media') + self.assertEqual(profile['Rendering Intent'], 'perceptual') - np.testing.assert_almost_equal(profile.illuminant, + np.testing.assert_almost_equal(profile['Illuminant'], (0.964203, 1.000000, 0.824905), decimal=6) - self.assertEqual(profile.creator, 'JPEG') + self.assertEqual(profile['Creator'], 'JPEG') @unittest.skipIf(sys.hexversion < 0x03020000, "Uses features introduced in 3.2.") diff --git a/glymur/test/test_opj_suite.py b/glymur/test/test_opj_suite.py index 80192f8..90e9b54 100644 --- a/glymur/test/test_opj_suite.py +++ b/glymur/test/test_opj_suite.py @@ -3579,7 +3579,7 @@ class TestSuite(unittest.TestCase): self.assertEqual(jp2.box[3].box[1].method, 2) # enumerated self.assertEqual(jp2.box[3].box[1].precedence, 0) self.assertEqual(jp2.box[3].box[1].approximation, 1) # JPX exact - self.assertEqual(jp2.box[3].box[1].icc_profile.size, 546) + self.assertEqual(jp2.box[3].box[1].icc_profile['Size'], 546) self.assertIsNone(jp2.box[3].box[1].colorspace) # Jp2 Header @@ -3674,7 +3674,7 @@ class TestSuite(unittest.TestCase): self.assertEqual(jp2.box[3].box[1].method, 2) self.assertEqual(jp2.box[3].box[1].precedence, 0) self.assertEqual(jp2.box[3].box[1].approximation, 1) # JPX exact - self.assertEqual(jp2.box[3].box[1].icc_profile.size, 13332) + self.assertEqual(jp2.box[3].box[1].icc_profile['Size'], 13332) self.assertIsNone(jp2.box[3].box[1].colorspace) # Jp2 Header @@ -3723,7 +3723,7 @@ class TestSuite(unittest.TestCase): self.assertEqual(jp2.box[2].box[1].method, 2) # enumerated self.assertEqual(jp2.box[2].box[1].precedence, 0) self.assertEqual(jp2.box[2].box[1].approximation, 1) # JPX exact - self.assertEqual(jp2.box[2].box[1].icc_profile.size, 414) + self.assertEqual(jp2.box[2].box[1].icc_profile['Size'], 414) self.assertIsNone(jp2.box[2].box[1].colorspace) # XML box @@ -6630,7 +6630,7 @@ class TestSuite(unittest.TestCase): self.assertEqual(jp2.box[3].box[1].method, 3) # any icc self.assertEqual(jp2.box[3].box[1].precedence, 2) self.assertEqual(jp2.box[3].box[1].approximation, 1) # JPX exact - self.assertEqual(jp2.box[3].box[1].icc_profile.size, 1328) + self.assertEqual(jp2.box[3].box[1].icc_profile['Size'], 1328) self.assertIsNone(jp2.box[3].box[1].colorspace) # UUID boxes. All mentioned in the RREQ box. diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index 50b5033..723070b 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -775,6 +775,8 @@ class TestPrinting(unittest.TestCase): expected = '\n'.join(lines) self.assertEqual(actual, expected) + @unittest.skipIf(sys.hexversion < 0x03000000, + "Ordered dicts not printing well in 2.7") @unittest.skipIf(data_root is None, "OPJ_DATA_ROOT environment variable not set") def test_jpx_approximation_with_icc_profile(self): @@ -785,29 +787,32 @@ class TestPrinting(unittest.TestCase): print(j.box[3].box[1]) actual = sys.stdout.getvalue().strip() - lines = ['Colour Specification Box (colr) @ (179, 1339)', - ' Method: any ICC profile', - ' Precedence: 2', - ' Approximation: accurately represents ' - + 'correct colorspace definition', - ' ICC Profile: ', - ' Size: 1328', - ' Preferred CMM type: 6170706c', - ' Version: 2.2.0', - ' Device class signature: display device profile', - ' Color space: RGB', - ' Connection space: XYZ', - ' Creation time: 2009-02-25 11:26:11', - ' File signature: acsp', - ' Platform: APPL', - ' Flags: not embedded, can be used independently', - ' Device manufacturer: appl', - ' Device model: ', - ' Device attributes: ' - + 'reflective, glossy, positive media polarity, color media', - ' Rendering intent: perceptual', - ' Illuminant: [ 0.96420288 1. 0.8249054 ]', - ' Creator signature: appl'] + lines = ["Colour Specification Box (colr) @ (179, 1339)", + " Method: any ICC profile", + " Precedence: 2", + " Approximation: accurately represents " + + "correct colorspace definition", + " ICC Profile:", + " {'Size': 1328,", + " 'Preferred CMM Type': 1634758764,", + " 'Version': '2.2.0',", + " 'Device Class': 'display device profile',", + " 'Color Space': 'RGB',", + " 'Connection Space': 'XYZ',", + " 'Datetime': " + + "datetime.datetime(2009, 2, 25, 11, 26, 11),", + " 'File Signature': 'acsp',", + " 'Platform': 'APPL',", + " 'Flags': 'not embedded, " + + "can be used independently',", + " 'Device Manufacturer': 'appl',", + " 'Device Model': '',", + " 'Device Attributes': 'reflective, glossy, " + + "positive media polarity, color media',", + " 'Rendering Intent': 'perceptual',", + " 'Illuminant': array([ 0.96420288, 1. ," + + " 0.8249054 ]),", + " 'Creator': 'appl'}"] expected = '\n'.join(lines) self.assertEqual(actual, expected) @@ -828,6 +833,8 @@ class TestPrinting(unittest.TestCase): expected = '\n'.join(lines) self.assertEqual(actual, expected) + @unittest.skipIf(sys.hexversion < 0x03000000, + "Ordered dicts not printing well in 2.7") def test_exif_uuid(self): j = glymur.Jp2k(self.jp2file) From c3016bd10792c21a8cb344008a0321724f190a39 Mon Sep 17 00:00:00 2001 From: John Evans Date: Sat, 15 Jun 2013 16:06:54 -0400 Subject: [PATCH 3/6] In 2.7, we prettyprint the ordered dicts as regular dicts. Closes #46 --- glymur/jp2box.py | 13 ++++++-- glymur/test/test_printing.py | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 8d74635..8ac562d 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -176,7 +176,13 @@ class ColourSpecificationBox(Jp2kBox): x = _colorspace_map_display[self.colorspace] msg += '\n Colorspace: {0}'.format(x) else: - x = pprint.pformat(self.icc_profile) + # 2.7 has trouble pretty-printing ordered dicts so we just have + # to print as a regular dict in this case. + if sys.hexversion < 0x03000000: + icc_profile = dict(self.icc_profile) + else: + icc_profile = self.icc_profile + x = pprint.pformat(icc_profile) lines = [' ' * 8 + y for y in x.split('\n')] msg += '\n ICC Profile:\n{0}'.format('\n'.join(lines)) @@ -1695,7 +1701,10 @@ class UUIDBox(Jp2kBox): uuid_data = _pretty_print_xml(self.data) elif self.uuid.bytes == b'JpgTiffExif->JP2': uuid_type = ' (Exif)' - uuid_data = '\n' + pprint.pformat(self.data) + # 2.7 has trouble pretty-printing ordered dicts, so print them + # as regular dicts. Not ideal, but at least it's good on 3.3+. + x = self.data if sys.hexversion >= 0x03000000 else dict(self.data) + uuid_data = '\n' + pprint.pformat(x) else: uuid_type = '' uuid_data = '{0} bytes'.format(len(self.data)) diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index 723070b..f9a7639 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -186,6 +186,70 @@ class TestPrinting(unittest.TestCase): self.expected = expected self.assertEqual(actual, expected) + @unittest.skipIf(data_root is None, + "OPJ_DATA_ROOT environment variable not set") + def test_icc_profile(self): + filename = os.path.join(data_root, 'input/nonregression/text_GBR.jp2') + j = glymur.Jp2k(filename) + print(j.box[3].box[1]) + actual = sys.stdout.getvalue().strip() + lin27 = ["Colour Specification Box (colr) @ (179, 1339)", + " Method: any ICC profile", + " Precedence: 2", + " Approximation: accurately represents correct " + + "colorspace definition", + " ICC Profile:", + " {'Color Space': 'RGB',", + " 'Connection Space': 'XYZ',", + " 'Creator': u'appl',", + " 'Datetime': " + + "datetime.datetime(2009, 2, 25, 11, 26, 11),", + " 'Device Attributes': 'reflective, glossy, " + + "positive media polarity, color media',", + " 'Device Class': 'display device profile',", + " 'Device Manufacturer': u'appl',", + " 'Device Model': '',", + " 'File Signature': u'acsp',", + " 'Flags': " + + "'not embedded, can be used independently',", + " 'Illuminant': " + + "array([ 0.96420288, 1. , 0.8249054 ]),", + " 'Platform': u'APPL',", + " 'Preferred CMM Type': 1634758764,", + " 'Rendering Intent': 'perceptual',", + " 'Size': 1328,", + " 'Version': '2.2.0'}"] + lin33 = ["Colour Specification Box (colr) @ (179, 1339)", + " Method: any ICC profile", + " Precedence: 2", + " Approximation: accurately represents correct " + + "colorspace definition", + " ICC Profile:", + " {'Size': 1328,", + " 'Preferred CMM Type': 1634758764,", + " 'Version': '2.2.0',", + " 'Device Class': 'display device profile',", + " 'Color Space': 'RGB',", + " 'Connection Space': 'XYZ',", + " 'Datetime': " + + "datetime.datetime(2009, 2, 25, 11, 26, 11),", + " 'File Signature': 'acsp',", + " 'Platform': 'APPL',", + " 'Flags': 'not embedded, can be used " + + "independently',", + " 'Device Manufacturer': 'appl',", + " 'Device Model': '',", + " 'Device Attributes': 'reflective, glossy, " + + "positive media polarity, color media',", + " 'Rendering Intent': 'perceptual',", + " 'Illuminant': " + + "array([ 0.96420288, 1. , 0.8249054 ]),", + " 'Creator': 'appl'}"] + + lines = lin27 if sys.hexversion < 0x03000000 else lin33 + expected = '\n'.join(lines) + self.assertEqual(actual, expected) + @unittest.skipIf(data_root is None, "OPJ_DATA_ROOT environment variable not set") def test_CRG(self): From 427cd272892a6fa350d53c15e40b3face73de420 Mon Sep 17 00:00:00 2001 From: John Evans Date: Sun, 16 Jun 2013 10:29:59 -0400 Subject: [PATCH 4/6] Tests that cannot run without library are not skipped. Closes #40 --- glymur/lib/test/__init__.py | 2 +- glymur/lib/test/test_openjp2.py | 6 +- glymur/test/test_callbacks.py | 9 +- glymur/test/test_icc.py | 2 +- glymur/test/test_jp2k.py | 16 +- glymur/test/test_opj_suite.py | 1429 ++++++++++++++------------- glymur/test/test_opj_suite_neg.py | 2 + glymur/test/test_opj_suite_write.py | 2 + glymur/test/test_printing.py | 150 +-- 9 files changed, 821 insertions(+), 797 deletions(-) diff --git a/glymur/lib/test/__init__.py b/glymur/lib/test/__init__.py index 9f68aec..5aea3ab 100644 --- a/glymur/lib/test/__init__.py +++ b/glymur/lib/test/__init__.py @@ -1 +1 @@ -from .test_openjp2 import TestOpenJP2 as openjp2 +#from .test_openjp2 import TestOpenJP2 as openjp2 diff --git a/glymur/lib/test/test_openjp2.py b/glymur/lib/test/test_openjp2.py index 0bee92f..90a5f08 100644 --- a/glymur/lib/test/test_openjp2.py +++ b/glymur/lib/test/test_openjp2.py @@ -12,12 +12,14 @@ import numpy as np import glymur -# Doc tests should be run as well. def load_tests(loader, tests, ignore): - tests.addTests(doctest.DocTestSuite('glymur.lib.openjp2')) + if glymur.lib.openjp2._OPENJP2 is not None: + tests.addTests(doctest.DocTestSuite('glymur.lib.openjp2')) return tests +@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, + "Missing openjp2 library.") class TestOpenJP2(unittest.TestCase): def setUp(self): diff --git a/glymur/test/test_callbacks.py b/glymur/test/test_callbacks.py index aae4ddc..9a1250b 100644 --- a/glymur/test/test_callbacks.py +++ b/glymur/test/test_callbacks.py @@ -12,14 +12,9 @@ else: import glymur -try: - data_root = os.environ['OPJ_DATA_ROOT'] -except KeyError: - data_root = None -except: - raise - +@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, + "Missing openjp2 library.") class TestCallbacks(unittest.TestCase): def setUp(self): diff --git a/glymur/test/test_icc.py b/glymur/test/test_icc.py index bbce1d6..b989bbe 100644 --- a/glymur/test/test_icc.py +++ b/glymur/test/test_icc.py @@ -67,7 +67,7 @@ class TestICC(unittest.TestCase): jfile = os.path.join(data_root, 'input/nonregression/orb-blue10-lin-jp2.jp2') with self.assertWarns(UserWarning) as cw: - data = Jp2k(jfile).read() + j = Jp2k(jfile) if __name__ == "__main__": unittest.main() diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 249bc5e..b4ca59a 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -34,21 +34,13 @@ except: # Doc tests should be run as well. def load_tests(loader, tests, ignore): - tests.addTests(doctest.DocTestSuite('glymur.jp2k')) + if glymur.lib.openjp2._OPENJP2 is not None: + tests.addTests(doctest.DocTestSuite('glymur.jp2k')) return tests -@contextlib.contextmanager -def chdir(dirname=None): - curdir = os.getcwd() - try: - if dirname is not None: - os.chdir(dirname) - yield - finally: - os.chdir(curdir) - - +@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, + "Missing openjp2 library.") class TestJp2k(unittest.TestCase): @classmethod diff --git a/glymur/test/test_opj_suite.py b/glymur/test/test_opj_suite.py index 90e9b54..9a7efde 100644 --- a/glymur/test/test_opj_suite.py +++ b/glymur/test/test_opj_suite.py @@ -120,6 +120,8 @@ def read_pgx(pgx_file): return(data.byteswap(swapbytes)) +@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, + "Missing openjp2 library.") @unittest.skipIf(data_root is None, "OPJ_DATA_ROOT environment variable not set") class TestSuite(unittest.TestCase): @@ -875,6 +877,725 @@ class TestSuite(unittest.TestCase): jpdata = jp2k.read() self.assertEqual(jpdata.shape, (512, 768, 3)) + def test_NR_DEC_Bretagne2_j2k_1_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/Bretagne2.j2k') + jp2 = Jp2k(jfile) + data = jp2.read() + self.assertTrue(True) + + def test_NR_DEC__00042_j2k_2_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/_00042.j2k') + jp2 = Jp2k(jfile) + data = jp2.read() + self.assertTrue(True) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_123_j2c_3_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/123.j2c') + jp2 = Jp2k(jfile) + data = jp2.read() + self.assertTrue(True) + + @unittest.skipIf(sys.hexversion < 0x03020000, + "Uses features introduced in 3.2.") + def test_NR_DEC_broken_jp2_4_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/broken.jp2') + with self.assertWarns(UserWarning) as cw: + # colr box has bad length. + jp2 = Jp2k(jfile) + with self.assertRaises(IOError): + data = jp2.read() + self.assertTrue(True) + + def test_NR_DEC_broken2_jp2_5_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/broken2.jp2') + with self.assertRaises(IOError): + data = Jp2k(jfile).read() + self.assertTrue(True) + + @unittest.skipIf(sys.hexversion < 0x03020000, + "Uses features introduced in 3.2.") + def test_NR_DEC_broken3_jp2_6_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/broken3.jp2') + with self.assertWarns(UserWarning) as cw: + # colr box has bad length. + j = Jp2k(jfile) + + with self.assertRaises(IOError) as ce: + d = j.read() + + def test_NR_DEC_broken4_jp2_7_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/broken4.jp2') + with self.assertRaises(IOError): + data = Jp2k(jfile).read() + self.assertTrue(True) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_bug_j2c_8_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/bug.j2c') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_buxI_j2k_9_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/buxI.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_buxR_j2k_10_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/buxR.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_Cannotreaddatawithnosizeknown_j2k_11_decode(self): + relpath = 'input/nonregression/Cannotreaddatawithnosizeknown.j2k' + jfile = os.path.join(data_root, relpath) + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_cthead1_j2k_12_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/cthead1.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_CT_Phillips_JPEG2K_Decompr_Problem_j2k_13_decode(self): + relpath = 'input/nonregression/CT_Phillips_JPEG2K_Decompr_Problem.j2k' + jfile = os.path.join(data_root, relpath) + data = Jp2k(jfile).read() + self.assertTrue(True) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_illegalcolortransform_j2k_14_decode(self): + # Stream too short, expected SOT. + jfile = os.path.join(data_root, + 'input/nonregression/illegalcolortransform.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_j2k32_j2k_15_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/j2k32.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_kakadu_v4_4_openjpegv2_broken_j2k_16_decode(self): + relpath = 'input/nonregression/kakadu_v4-4_openjpegv2_broken.j2k' + jfile = os.path.join(data_root, relpath) + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_MarkerIsNotCompliant_j2k_17_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/MarkerIsNotCompliant.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_Marrin_jp2_18_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/Marrin.jp2') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_merged_jp2_19_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/merged.jp2') + data = Jp2k(jfile).read_bands() + self.assertTrue(True) + + def test_NR_DEC_movie_00000_j2k_20_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/movie_00000.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_movie_00001_j2k_21_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/movie_00001.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_movie_00002_j2k_22_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/movie_00002.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_orb_blue_lin_j2k_j2k_23_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/orb-blue10-lin-j2k.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_orb_blue_win_j2k_j2k_24_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/orb-blue10-win-j2k.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_orb_blue_lin_jp2_25_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/orb-blue10-lin-jp2.jp2') + with warnings.catch_warnings(): + # This file has an invalid ICC profile + warnings.simplefilter("ignore") + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_orb_blue_win_jp2_26_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/orb-blue10-win-jp2.jp2') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_relax_jp2_27_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/relax.jp2') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_test_lossless_j2k_28_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/test_lossless.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_text_GBR_jp2_29_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/text_GBR.jp2') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_pacs_ge_j2k_30_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/pacs.ge.j2k') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_kodak_2layers_lrcp_j2c_31_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/kodak_2layers_lrcp.j2c') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_kodak_2layers_lrcp_j2c_32_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/kodak_2layers_lrcp.j2c') + data = Jp2k(jfile).read(layer=2) + self.assertTrue(True) + + def test_NR_DEC_issue104_jpxstream_jp2_33_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/issue104_jpxstream.jp2') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_mem_b2ace68c_1381_jp2_34_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/mem-b2ace68c-1381.jp2') + with warnings.catch_warnings(): + # This file has a bad pclr box, we test for this elsewhere. + warnings.simplefilter("ignore") + j = Jp2k(jfile) + data = j.read() + self.assertTrue(True) + + def test_NR_DEC_mem_b2b86b74_2753_jp2_35_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/mem-b2b86b74-2753.jp2') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_gdal_fuzzer_unchecked_num_resolutions_jp2_36_decode(self): + f = 'input/nonregression/gdal_fuzzer_unchecked_numresolutions.jp2' + jfile = os.path.join(data_root, f) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + j = Jp2k(jfile) + with self.assertRaises(IOError): + data = j.read() + + def test_NR_DEC_jp2_36_decode(self): + lst = ('input', + 'nonregression', + 'gdal_fuzzer_assert_in_opj_j2k_read_SQcd_SQcc.patch.jp2') + jfile = os.path.join(data_root, '/'.join(lst)) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + j = Jp2k(jfile) + with self.assertRaises(IOError): + data = j.read() + + def test_NR_DEC_gdal_fuzzer_check_number_of_tiles_jp2_38_decode(self): + relpath = 'input/nonregression/gdal_fuzzer_check_number_of_tiles.jp2' + jfile = os.path.join(data_root, relpath) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + j = Jp2k(jfile) + with self.assertRaises(IOError): + data = j.read() + + def test_NR_DEC_gdal_fuzzer_check_comp_dx_dy_jp2_39_decode(self): + relpath = 'input/nonregression/gdal_fuzzer_check_comp_dx_dy.jp2' + jfile = os.path.join(data_root, relpath) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + with self.assertRaises(IOError): + j = Jp2k(jfile).read() + + def test_NR_DEC_file_409752_jp2_40_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/file409752.jp2') + with self.assertRaises(IOError): + data = Jp2k(jfile).read() + + @unittest.skipIf(sys.hexversion < 0x03020000, + "Uses features introduced in 3.2.") + def test_NR_DEC_issue188_beach_64bitsbox_jp2_41_decode(self): + # Has an 'XML ' box instead of 'xml '. Yes that is pedantic, but it + # really does deserve a warning. + relpath = 'input/nonregression/issue188_beach_64bitsbox.jp2' + jfile = os.path.join(data_root, relpath) + with self.assertWarns(UserWarning) as cw: + data = Jp2k(jfile).read() + + def test_NR_DEC_issue206_image_000_jp2_42_decode(self): + jfile = os.path.join(data_root, + 'input/nonregression/issue206_image-000.jp2') + data = Jp2k(jfile).read() + self.assertTrue(True) + + def test_NR_DEC_p1_04_j2k_43_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(0, 0, 1024, 1024)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata) + + def test_NR_DEC_p1_04_j2k_44_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(640, 512, 768, 640)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[640:768, 512:640]) + + def test_NR_DEC_p1_04_j2k_45_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(896, 896, 1024, 1024)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[896:1024, 896:1024]) + + def test_NR_DEC_p1_04_j2k_46_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(500, 100, 800, 300)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[500:800, 100:300]) + + def test_NR_DEC_p1_04_j2k_47_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(520, 260, 600, 360)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[520:600, 260:360]) + + def test_NR_DEC_p1_04_j2k_48_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(520, 260, 660, 360)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[520:660, 260:360]) + + def test_NR_DEC_p1_04_j2k_49_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(520, 360, 600, 400)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[520:600, 360:400]) + + def test_NR_DEC_p1_04_j2k_50_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(0, 0, 1024, 1024), reduce=2) + odata = jp2k.read(reduce=2) + + np.testing.assert_array_equal(ssdata, odata[0:256, 0:256]) + + def test_NR_DEC_p1_04_j2k_51_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(640, 512, 768, 640), reduce=2) + odata = jp2k.read(reduce=2) + np.testing.assert_array_equal(ssdata, odata[160:192, 128:160]) + + def test_NR_DEC_p1_04_j2k_52_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(896, 896, 1024, 1024), reduce=2) + odata = jp2k.read(reduce=2) + np.testing.assert_array_equal(ssdata, odata[224:352, 224:352]) + + def test_NR_DEC_p1_04_j2k_53_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(500, 100, 800, 300), reduce=2) + odata = jp2k.read(reduce=2) + np.testing.assert_array_equal(ssdata, odata[125:200, 25:75]) + + def test_NR_DEC_p1_04_j2k_54_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(520, 260, 600, 360), reduce=2) + odata = jp2k.read(reduce=2) + np.testing.assert_array_equal(ssdata, odata[130:150, 65:90]) + + def test_NR_DEC_p1_04_j2k_55_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(520, 260, 660, 360), reduce=2) + odata = jp2k.read(reduce=2) + np.testing.assert_array_equal(ssdata, odata[130:165, 65:90]) + + def test_NR_DEC_p1_04_j2k_56_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(520, 360, 600, 400), reduce=2) + odata = jp2k.read(reduce=2) + np.testing.assert_array_equal(ssdata, odata[130:150, 90:100]) + + def test_NR_DEC_p1_04_j2k_57_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tdata = jp2k.read(tile=63) # last tile + odata = jp2k.read() + np.testing.assert_array_equal(tdata, odata[896:1024, 896:1024]) + + def test_NR_DEC_p1_04_j2k_58_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tdata = jp2k.read(tile=63, reduce=2) # last tile + odata = jp2k.read(reduce=2) + np.testing.assert_array_equal(tdata, odata[224:256, 224:256]) + + def test_NR_DEC_p1_04_j2k_59_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tdata = jp2k.read(tile=12) # 2nd row, 5th column + odata = jp2k.read() + np.testing.assert_array_equal(tdata, odata[128:256, 512:640]) + + def test_NR_DEC_p1_04_j2k_60_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') + jp2k = Jp2k(jfile) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tdata = jp2k.read(tile=12, reduce=1) # 2nd row, 5th column + odata = jp2k.read(reduce=1) + np.testing.assert_array_equal(tdata, odata[64:128, 256:320]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_61_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(0, 0, 12, 12)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[0:12, 0:12]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_62_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(1, 8, 8, 11)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[1:8, 8:11]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_63_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(9, 9, 12, 12)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[9:12, 9:12]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_64_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(10, 4, 12, 10)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[10:12, 4:10]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_65_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(3, 3, 9, 9)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[3:9, 3:9]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_66_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(4, 4, 7, 7)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[4:7, 4:7]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_67_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(4, 4, 5, 5)) + odata = jp2k.read() + np.testing.assert_array_equal(ssdata, odata[4:5, 4: 5]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_68_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(0, 0, 12, 12), reduce=1) + odata = jp2k.read(reduce=1) + np.testing.assert_array_equal(ssdata, odata[0:6, 0:6]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_69_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(1, 8, 8, 11), reduce=1) + self.assertEqual(ssdata.shape, (3, 2, 3)) + + def test_NR_DEC_p1_06_j2k_70_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(9, 9, 12, 12), reduce=1) + self.assertEqual(ssdata.shape, (1, 1, 3)) + + def test_NR_DEC_p1_06_j2k_71_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(10, 4, 12, 10), reduce=1) + self.assertEqual(ssdata.shape, (1, 3, 3)) + + def test_NR_DEC_p1_06_j2k_72_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(3, 3, 9, 9), reduce=1) + self.assertEqual(ssdata.shape, (3, 3, 3)) + + def test_NR_DEC_p1_06_j2k_73_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(4, 4, 7, 7), reduce=1) + self.assertEqual(ssdata.shape, (2, 2, 3)) + + def test_NR_DEC_p1_06_j2k_74_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(4, 4, 5, 5), reduce=1) + self.assertEqual(ssdata.shape, (1, 1, 3)) + + def test_NR_DEC_p1_06_j2k_75_decode(self): + # Image size would be 0 x 0. + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + with self.assertRaises((IOError, OSError)) as ce: + ssdata = jp2k.read(area=(9, 9, 12, 12), reduce=2) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_76_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + fulldata = jp2k.read() + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tiledata = jp2k.read(tile=0) + np.testing.assert_array_equal(tiledata, fulldata[0:3, 0:3]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_77_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + fulldata = jp2k.read() + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tiledata = jp2k.read(tile=5) + np.testing.assert_array_equal(tiledata, fulldata[3:6, 3:6]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_78_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + fulldata = jp2k.read() + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tiledata = jp2k.read(tile=9) + np.testing.assert_array_equal(tiledata, fulldata[6:9, 3:6]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_79_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + fulldata = jp2k.read() + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tiledata = jp2k.read(tile=15) + np.testing.assert_array_equal(tiledata, fulldata[9:12, 9:12]) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_80_decode(self): + # Just read the data, don't bother verifying. + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tiledata = jp2k.read(tile=0, reduce=2) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_81_decode(self): + # Just read the data, don't bother verifying. + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tiledata = jp2k.read(tile=5, reduce=2) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_82_decode(self): + # Just read the data, don't bother verifying. + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + tiledata = jp2k.read(tile=9, reduce=2) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_83_decode(self): + # tile size is 3x3. Reducing two levels results in no data. + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + with self.assertRaises((IOError, OSError)) as ce: + tiledata = jp2k.read(tile=15, reduce=2) + + @unittest.skip("fprintf stderr output in r2343.") + def test_NR_DEC_p1_06_j2k_84_decode(self): + # Just read the data, don't bother verifying. + jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') + jp2k = Jp2k(jfile) + data = jp2k.read(reduce=4) + + def test_NR_DEC_p0_04_j2k_85_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(0, 0, 256, 256)) + fulldata = jp2k.read() + np.testing.assert_array_equal(fulldata[0:256, 0:256], ssdata) + + def test_NR_DEC_p0_04_j2k_86_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(0, 128, 128, 256)) + fulldata = jp2k.read() + np.testing.assert_array_equal(fulldata[0:128, 128:256], ssdata) + + def test_NR_DEC_p0_04_j2k_87_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(10, 50, 200, 120)) + fulldata = jp2k.read() + np.testing.assert_array_equal(fulldata[10:200, 50:120], ssdata) + + def test_NR_DEC_p0_04_j2k_88_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(150, 10, 210, 190)) + fulldata = jp2k.read() + np.testing.assert_array_equal(fulldata[150:210, 10:190], ssdata) + + def test_NR_DEC_p0_04_j2k_89_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(80, 100, 150, 200)) + fulldata = jp2k.read() + np.testing.assert_array_equal(fulldata[80:150, 100:200], ssdata) + + def test_NR_DEC_p0_04_j2k_90_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(20, 150, 50, 200)) + fulldata = jp2k.read() + np.testing.assert_array_equal(fulldata[20:50, 150:200], ssdata) + + def test_NR_DEC_p0_04_j2k_91_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(0, 0, 256, 256), reduce=2) + fulldata = jp2k.read(reduce=2) + np.testing.assert_array_equal(fulldata[0:64, 0:64], ssdata) + + def test_NR_DEC_p0_04_j2k_92_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(0, 128, 128, 256), reduce=2) + fulldata = jp2k.read(reduce=2) + np.testing.assert_array_equal(fulldata[0:32, 32:64], ssdata) + + def test_NR_DEC_p0_04_j2k_93_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(10, 50, 200, 120), reduce=2) + fulldata = jp2k.read(reduce=2) + np.testing.assert_array_equal(fulldata[3:50, 13:30], ssdata) + + def test_NR_DEC_p0_04_j2k_94_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(150, 10, 210, 190), reduce=2) + fulldata = jp2k.read(reduce=2) + np.testing.assert_array_equal(fulldata[38:53, 3:48], ssdata) + + def test_NR_DEC_p0_04_j2k_95_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(80, 100, 150, 200), reduce=2) + fulldata = jp2k.read(reduce=2) + np.testing.assert_array_equal(fulldata[20:38, 25:50], ssdata) + + def test_NR_DEC_p0_04_j2k_96_decode(self): + jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') + jp2k = Jp2k(jfile) + ssdata = jp2k.read(area=(20, 150, 50, 200), reduce=2) + fulldata = jp2k.read(reduce=2) + np.testing.assert_array_equal(fulldata[5:13, 38:50], ssdata) + + +@unittest.skipIf(data_root is None, + "OPJ_DATA_ROOT environment variable not set") +class TestSuiteDump(unittest.TestCase): + + def setUp(self): + pass + + def tearDown(self): + pass + def test_NR_p0_01_dump(self): jfile = os.path.join(data_root, 'input/conformance/p0_01.j2k') c = Jp2k(jfile).get_codestream(header_only=False) @@ -6697,713 +7418,5 @@ class TestSuite(unittest.TestCase): self.assertEqual(c.segment[3]._exponent, [8, 9, 9, 10, 9, 9, 10, 9, 9, 10, 9, 9, 10, 9, 9, 10]) - def test_NR_DEC_Bretagne2_j2k_1_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/Bretagne2.j2k') - jp2 = Jp2k(jfile) - data = jp2.read() - self.assertTrue(True) - - def test_NR_DEC__00042_j2k_2_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/_00042.j2k') - jp2 = Jp2k(jfile) - data = jp2.read() - self.assertTrue(True) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_123_j2c_3_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/123.j2c') - jp2 = Jp2k(jfile) - data = jp2.read() - self.assertTrue(True) - - @unittest.skipIf(sys.hexversion < 0x03020000, - "Uses features introduced in 3.2.") - def test_NR_DEC_broken_jp2_4_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/broken.jp2') - with self.assertWarns(UserWarning) as cw: - # colr box has bad length. - jp2 = Jp2k(jfile) - with self.assertRaises(IOError): - data = jp2.read() - self.assertTrue(True) - - def test_NR_DEC_broken2_jp2_5_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/broken2.jp2') - with self.assertRaises(IOError): - data = Jp2k(jfile).read() - self.assertTrue(True) - - @unittest.skipIf(sys.hexversion < 0x03020000, - "Uses features introduced in 3.2.") - def test_NR_DEC_broken3_jp2_6_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/broken3.jp2') - with self.assertWarns(UserWarning) as cw: - # colr box has bad length. - j = Jp2k(jfile) - - with self.assertRaises(IOError) as ce: - d = j.read() - - def test_NR_DEC_broken4_jp2_7_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/broken4.jp2') - with self.assertRaises(IOError): - data = Jp2k(jfile).read() - self.assertTrue(True) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_bug_j2c_8_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/bug.j2c') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_buxI_j2k_9_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/buxI.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_buxR_j2k_10_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/buxR.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_Cannotreaddatawithnosizeknown_j2k_11_decode(self): - relpath = 'input/nonregression/Cannotreaddatawithnosizeknown.j2k' - jfile = os.path.join(data_root, relpath) - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_cthead1_j2k_12_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/cthead1.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_CT_Phillips_JPEG2K_Decompr_Problem_j2k_13_decode(self): - relpath = 'input/nonregression/CT_Phillips_JPEG2K_Decompr_Problem.j2k' - jfile = os.path.join(data_root, relpath) - data = Jp2k(jfile).read() - self.assertTrue(True) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_illegalcolortransform_j2k_14_decode(self): - # Stream too short, expected SOT. - jfile = os.path.join(data_root, - 'input/nonregression/illegalcolortransform.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_j2k32_j2k_15_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/j2k32.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_kakadu_v4_4_openjpegv2_broken_j2k_16_decode(self): - relpath = 'input/nonregression/kakadu_v4-4_openjpegv2_broken.j2k' - jfile = os.path.join(data_root, relpath) - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_MarkerIsNotCompliant_j2k_17_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/MarkerIsNotCompliant.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_Marrin_jp2_18_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/Marrin.jp2') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_merged_jp2_19_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/merged.jp2') - data = Jp2k(jfile).read_bands() - self.assertTrue(True) - - def test_NR_DEC_movie_00000_j2k_20_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/movie_00000.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_movie_00001_j2k_21_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/movie_00001.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_movie_00002_j2k_22_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/movie_00002.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_orb_blue_lin_j2k_j2k_23_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/orb-blue10-lin-j2k.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_orb_blue_win_j2k_j2k_24_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/orb-blue10-win-j2k.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_orb_blue_lin_jp2_25_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/orb-blue10-lin-jp2.jp2') - with warnings.catch_warnings(): - # This file has an invalid ICC profile - warnings.simplefilter("ignore") - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_orb_blue_win_jp2_26_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/orb-blue10-win-jp2.jp2') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_relax_jp2_27_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/relax.jp2') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_test_lossless_j2k_28_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/test_lossless.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_text_GBR_jp2_29_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/text_GBR.jp2') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_pacs_ge_j2k_30_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/pacs.ge.j2k') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_kodak_2layers_lrcp_j2c_31_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/kodak_2layers_lrcp.j2c') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_kodak_2layers_lrcp_j2c_32_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/kodak_2layers_lrcp.j2c') - data = Jp2k(jfile).read(layer=2) - self.assertTrue(True) - - def test_NR_DEC_issue104_jpxstream_jp2_33_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/issue104_jpxstream.jp2') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_mem_b2ace68c_1381_jp2_34_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/mem-b2ace68c-1381.jp2') - with warnings.catch_warnings(): - # This file has a bad pclr box, we test for this elsewhere. - warnings.simplefilter("ignore") - j = Jp2k(jfile) - data = j.read() - self.assertTrue(True) - - def test_NR_DEC_mem_b2b86b74_2753_jp2_35_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/mem-b2b86b74-2753.jp2') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_gdal_fuzzer_unchecked_num_resolutions_jp2_36_decode(self): - f = 'input/nonregression/gdal_fuzzer_unchecked_numresolutions.jp2' - jfile = os.path.join(data_root, f) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - j = Jp2k(jfile) - with self.assertRaises(IOError): - data = j.read() - - def test_NR_DEC_jp2_36_decode(self): - lst = ('input', - 'nonregression', - 'gdal_fuzzer_assert_in_opj_j2k_read_SQcd_SQcc.patch.jp2') - jfile = os.path.join(data_root, '/'.join(lst)) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - j = Jp2k(jfile) - with self.assertRaises(IOError): - data = j.read() - - def test_NR_DEC_gdal_fuzzer_check_number_of_tiles_jp2_38_decode(self): - relpath = 'input/nonregression/gdal_fuzzer_check_number_of_tiles.jp2' - jfile = os.path.join(data_root, relpath) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - j = Jp2k(jfile) - with self.assertRaises(IOError): - data = j.read() - - def test_NR_DEC_gdal_fuzzer_check_comp_dx_dy_jp2_39_decode(self): - relpath = 'input/nonregression/gdal_fuzzer_check_comp_dx_dy.jp2' - jfile = os.path.join(data_root, relpath) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - with self.assertRaises(IOError): - j = Jp2k(jfile).read() - - def test_NR_DEC_file_409752_jp2_40_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/file409752.jp2') - with self.assertRaises(IOError): - data = Jp2k(jfile).read() - - @unittest.skipIf(sys.hexversion < 0x03020000, - "Uses features introduced in 3.2.") - def test_NR_DEC_issue188_beach_64bitsbox_jp2_41_decode(self): - # Has an 'XML ' box instead of 'xml '. Yes that is pedantic, but it - # really does deserve a warning. - relpath = 'input/nonregression/issue188_beach_64bitsbox.jp2' - jfile = os.path.join(data_root, relpath) - with self.assertWarns(UserWarning) as cw: - data = Jp2k(jfile).read() - - def test_NR_DEC_issue206_image_000_jp2_42_decode(self): - jfile = os.path.join(data_root, - 'input/nonregression/issue206_image-000.jp2') - data = Jp2k(jfile).read() - self.assertTrue(True) - - def test_NR_DEC_p1_04_j2k_43_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 1024, 1024)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata) - - def test_NR_DEC_p1_04_j2k_44_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(640, 512, 768, 640)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[640:768, 512:640]) - - def test_NR_DEC_p1_04_j2k_45_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(896, 896, 1024, 1024)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[896:1024, 896:1024]) - - def test_NR_DEC_p1_04_j2k_46_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(500, 100, 800, 300)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[500:800, 100:300]) - - def test_NR_DEC_p1_04_j2k_47_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 260, 600, 360)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[520:600, 260:360]) - - def test_NR_DEC_p1_04_j2k_48_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 260, 660, 360)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[520:660, 260:360]) - - def test_NR_DEC_p1_04_j2k_49_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 360, 600, 400)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[520:600, 360:400]) - - def test_NR_DEC_p1_04_j2k_50_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 1024, 1024), reduce=2) - odata = jp2k.read(reduce=2) - - np.testing.assert_array_equal(ssdata, odata[0:256, 0:256]) - - def test_NR_DEC_p1_04_j2k_51_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(640, 512, 768, 640), reduce=2) - odata = jp2k.read(reduce=2) - np.testing.assert_array_equal(ssdata, odata[160:192, 128:160]) - - def test_NR_DEC_p1_04_j2k_52_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(896, 896, 1024, 1024), reduce=2) - odata = jp2k.read(reduce=2) - np.testing.assert_array_equal(ssdata, odata[224:352, 224:352]) - - def test_NR_DEC_p1_04_j2k_53_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(500, 100, 800, 300), reduce=2) - odata = jp2k.read(reduce=2) - np.testing.assert_array_equal(ssdata, odata[125:200, 25:75]) - - def test_NR_DEC_p1_04_j2k_54_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 260, 600, 360), reduce=2) - odata = jp2k.read(reduce=2) - np.testing.assert_array_equal(ssdata, odata[130:150, 65:90]) - - def test_NR_DEC_p1_04_j2k_55_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 260, 660, 360), reduce=2) - odata = jp2k.read(reduce=2) - np.testing.assert_array_equal(ssdata, odata[130:165, 65:90]) - - def test_NR_DEC_p1_04_j2k_56_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 360, 600, 400), reduce=2) - odata = jp2k.read(reduce=2) - np.testing.assert_array_equal(ssdata, odata[130:150, 90:100]) - - def test_NR_DEC_p1_04_j2k_57_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tdata = jp2k.read(tile=63) # last tile - odata = jp2k.read() - np.testing.assert_array_equal(tdata, odata[896:1024, 896:1024]) - - def test_NR_DEC_p1_04_j2k_58_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tdata = jp2k.read(tile=63, reduce=2) # last tile - odata = jp2k.read(reduce=2) - np.testing.assert_array_equal(tdata, odata[224:256, 224:256]) - - def test_NR_DEC_p1_04_j2k_59_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tdata = jp2k.read(tile=12) # 2nd row, 5th column - odata = jp2k.read() - np.testing.assert_array_equal(tdata, odata[128:256, 512:640]) - - def test_NR_DEC_p1_04_j2k_60_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tdata = jp2k.read(tile=12, reduce=1) # 2nd row, 5th column - odata = jp2k.read(reduce=1) - np.testing.assert_array_equal(tdata, odata[64:128, 256:320]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_61_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 12, 12)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[0:12, 0:12]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_62_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(1, 8, 8, 11)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[1:8, 8:11]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_63_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(9, 9, 12, 12)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[9:12, 9:12]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_64_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(10, 4, 12, 10)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[10:12, 4:10]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_65_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(3, 3, 9, 9)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[3:9, 3:9]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_66_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(4, 4, 7, 7)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[4:7, 4:7]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_67_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(4, 4, 5, 5)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[4:5, 4: 5]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_68_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 12, 12), reduce=1) - odata = jp2k.read(reduce=1) - np.testing.assert_array_equal(ssdata, odata[0:6, 0:6]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_69_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(1, 8, 8, 11), reduce=1) - self.assertEqual(ssdata.shape, (3, 2, 3)) - - def test_NR_DEC_p1_06_j2k_70_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(9, 9, 12, 12), reduce=1) - self.assertEqual(ssdata.shape, (1, 1, 3)) - - def test_NR_DEC_p1_06_j2k_71_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(10, 4, 12, 10), reduce=1) - self.assertEqual(ssdata.shape, (1, 3, 3)) - - def test_NR_DEC_p1_06_j2k_72_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(3, 3, 9, 9), reduce=1) - self.assertEqual(ssdata.shape, (3, 3, 3)) - - def test_NR_DEC_p1_06_j2k_73_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(4, 4, 7, 7), reduce=1) - self.assertEqual(ssdata.shape, (2, 2, 3)) - - def test_NR_DEC_p1_06_j2k_74_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(4, 4, 5, 5), reduce=1) - self.assertEqual(ssdata.shape, (1, 1, 3)) - - def test_NR_DEC_p1_06_j2k_75_decode(self): - # Image size would be 0 x 0. - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - with self.assertRaises((IOError, OSError)) as ce: - ssdata = jp2k.read(area=(9, 9, 12, 12), reduce=2) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_76_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - fulldata = jp2k.read() - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tiledata = jp2k.read(tile=0) - np.testing.assert_array_equal(tiledata, fulldata[0:3, 0:3]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_77_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - fulldata = jp2k.read() - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tiledata = jp2k.read(tile=5) - np.testing.assert_array_equal(tiledata, fulldata[3:6, 3:6]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_78_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - fulldata = jp2k.read() - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tiledata = jp2k.read(tile=9) - np.testing.assert_array_equal(tiledata, fulldata[6:9, 3:6]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_79_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - fulldata = jp2k.read() - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tiledata = jp2k.read(tile=15) - np.testing.assert_array_equal(tiledata, fulldata[9:12, 9:12]) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_80_decode(self): - # Just read the data, don't bother verifying. - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tiledata = jp2k.read(tile=0, reduce=2) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_81_decode(self): - # Just read the data, don't bother verifying. - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tiledata = jp2k.read(tile=5, reduce=2) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_82_decode(self): - # Just read the data, don't bother verifying. - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - tiledata = jp2k.read(tile=9, reduce=2) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_83_decode(self): - # tile size is 3x3. Reducing two levels results in no data. - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - with self.assertRaises((IOError, OSError)) as ce: - tiledata = jp2k.read(tile=15, reduce=2) - - @unittest.skip("fprintf stderr output in r2343.") - def test_NR_DEC_p1_06_j2k_84_decode(self): - # Just read the data, don't bother verifying. - jfile = os.path.join(data_root, 'input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - data = jp2k.read(reduce=4) - - def test_NR_DEC_p0_04_j2k_85_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 256, 256)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[0:256, 0:256], ssdata) - - def test_NR_DEC_p0_04_j2k_86_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 128, 128, 256)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[0:128, 128:256], ssdata) - - def test_NR_DEC_p0_04_j2k_87_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(10, 50, 200, 120)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[10:200, 50:120], ssdata) - - def test_NR_DEC_p0_04_j2k_88_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(150, 10, 210, 190)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[150:210, 10:190], ssdata) - - def test_NR_DEC_p0_04_j2k_89_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(80, 100, 150, 200)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[80:150, 100:200], ssdata) - - def test_NR_DEC_p0_04_j2k_90_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(20, 150, 50, 200)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[20:50, 150:200], ssdata) - - def test_NR_DEC_p0_04_j2k_91_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 256, 256), reduce=2) - fulldata = jp2k.read(reduce=2) - np.testing.assert_array_equal(fulldata[0:64, 0:64], ssdata) - - def test_NR_DEC_p0_04_j2k_92_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 128, 128, 256), reduce=2) - fulldata = jp2k.read(reduce=2) - np.testing.assert_array_equal(fulldata[0:32, 32:64], ssdata) - - def test_NR_DEC_p0_04_j2k_93_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(10, 50, 200, 120), reduce=2) - fulldata = jp2k.read(reduce=2) - np.testing.assert_array_equal(fulldata[3:50, 13:30], ssdata) - - def test_NR_DEC_p0_04_j2k_94_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(150, 10, 210, 190), reduce=2) - fulldata = jp2k.read(reduce=2) - np.testing.assert_array_equal(fulldata[38:53, 3:48], ssdata) - - def test_NR_DEC_p0_04_j2k_95_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(80, 100, 150, 200), reduce=2) - fulldata = jp2k.read(reduce=2) - np.testing.assert_array_equal(fulldata[20:38, 25:50], ssdata) - - def test_NR_DEC_p0_04_j2k_96_decode(self): - jfile = os.path.join(data_root, 'input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(20, 150, 50, 200), reduce=2) - fulldata = jp2k.read(reduce=2) - np.testing.assert_array_equal(fulldata[5:13, 38:50], ssdata) - if __name__ == "__main__": unittest.main() diff --git a/glymur/test/test_opj_suite_neg.py b/glymur/test/test_opj_suite_neg.py index 75c9c6f..03cb437 100644 --- a/glymur/test/test_opj_suite_neg.py +++ b/glymur/test/test_opj_suite_neg.py @@ -57,6 +57,8 @@ def read_image(infile): return data +@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, + "Missing openjp2 library.") @unittest.skipIf(no_read_backend, no_read_backend_msg) @unittest.skipIf(data_root is None, "OPJ_DATA_ROOT environment variable not set") diff --git a/glymur/test/test_opj_suite_write.py b/glymur/test/test_opj_suite_write.py index d511768..0526fc3 100644 --- a/glymur/test/test_opj_suite_write.py +++ b/glymur/test/test_opj_suite_write.py @@ -57,6 +57,8 @@ def read_image(infile): return data +@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, + "Missing openjp2 library.") @unittest.skipIf(no_read_backend, no_read_backend_msg) @unittest.skipIf(data_root is None, "OPJ_DATA_ROOT environment variable not set") diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index f9a7639..f948c3e 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -21,7 +21,11 @@ except: raise -class TestPrinting(unittest.TestCase): +@unittest.skipIf(glymur.lib.openjp2._OPENJP2 is None, + "Missing openjp2 library.") +class TestPrintingNeedsLib(unittest.TestCase): + """These tests require the library, mostly in order to just setup the test. + """ @classmethod def setUpClass(cls): @@ -40,11 +44,11 @@ class TestPrinting(unittest.TestCase): os.unlink(cls._plain_nemo_file) def setUp(self): + self.jp2file = pkg_resources.resource_filename(glymur.__name__, + "data/nemo.jp2") # Save sys.stdout. self.stdout = sys.stdout sys.stdout = StringIO() - self.jp2file = pkg_resources.resource_filename(glymur.__name__, - "data/nemo.jp2") # Save the output of dumping nemo.jp2 for more than one test. lines = ['JPEG 2000 Signature Box (jP ) @ (0, 12)', @@ -115,6 +119,57 @@ class TestPrinting(unittest.TestCase): # Restore stdout. sys.stdout = self.stdout + def test_asoc_label_box(self): + # Construct a fake file with an asoc and a label box, as + # OpenJPEG doesn't have such a file. + data = glymur.Jp2k(self.jp2file).read(reduce=3) + with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: + j = glymur.Jp2k(tfile.name, 'wb') + j.write(data) + + with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile2: + + # Offset of the codestream is where we start. + buffer = tfile.read(77) + tfile2.write(buffer) + + # read the rest of the file, it's the codestream. + codestream = tfile.read() + + # Write the asoc superbox. + # Length = 36, id is 'asoc'. + buffer = struct.pack('>I4s', int(56), b'asoc') + tfile2.write(buffer) + + # Write the contained label box + buffer = struct.pack('>I4s', int(13), b'lbl ') + tfile2.write(buffer) + tfile2.write('label'.encode()) + + # Write the xml box + # Length = 36, id is 'xml '. + buffer = struct.pack('>I4s', int(35), b'xml ') + tfile2.write(buffer) + + buffer = 'this is a test' + buffer = buffer.encode() + tfile2.write(buffer) + + # Now append the codestream. + tfile2.write(codestream) + tfile2.flush() + + jasoc = glymur.Jp2k(tfile2.name) + print(jasoc.box[3]) + actual = sys.stdout.getvalue().strip() + lines = ['Association Box (asoc) @ (77, 56)', + ' Label Box (lbl ) @ (85, 13)', + ' Label: label', + ' XML Box (xml ) @ (98, 35)', + ' this is a test'] + expected = '\n'.join(lines) + self.assertEqual(actual, expected) + def test_jp2dump(self): glymur.jp2dump(self._plain_nemo_file) actual = sys.stdout.getvalue().strip() @@ -126,6 +181,32 @@ class TestPrinting(unittest.TestCase): self.assertEqual(actual, self.expectedPlain) + def test_entire_file(self): + j = glymur.Jp2k(self._plain_nemo_file) + print(j) + actual = sys.stdout.getvalue().strip() + + # Get rid of the filename line, as it is not set in stone. + lst = actual.split('\n') + lst = lst[1:] + actual = '\n'.join(lst) + + self.assertEqual(actual, self.expectedPlain) + + +class TestPrinting(unittest.TestCase): + + def setUp(self): + # Save sys.stdout. + self.stdout = sys.stdout + sys.stdout = StringIO() + self.jp2file = pkg_resources.resource_filename(glymur.__name__, + "data/nemo.jp2") + + def tearDown(self): + # Restore stdout. + sys.stdout = self.stdout + def test_COC_segment(self): j = glymur.Jp2k(self.jp2file) codestream = j.get_codestream(header_only=False) @@ -516,18 +597,6 @@ class TestPrinting(unittest.TestCase): expected = '\n'.join(lst) self.assertEqual(actual, expected) - def test_entire_file(self): - j = glymur.Jp2k(self._plain_nemo_file) - print(j) - actual = sys.stdout.getvalue().strip() - - # Get rid of the filename line, as it is not set in stone. - lst = actual.split('\n') - lst = lst[1:] - actual = '\n'.join(lst) - - self.assertEqual(actual, self.expectedPlain) - def test_codestream(self): j = glymur.Jp2k(self.jp2file) print(j.get_codestream()) @@ -715,57 +784,6 @@ class TestPrinting(unittest.TestCase): expected = '\n'.join(lines) self.assertEqual(actual, expected) - def test_asoc_label_box(self): - # Construct a fake file with an asoc and a label box, as - # OpenJPEG doesn't have such a file. - data = glymur.Jp2k(self.jp2file).read(reduce=3) - with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: - j = glymur.Jp2k(tfile.name, 'wb') - j.write(data) - - with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile2: - - # Offset of the codestream is where we start. - buffer = tfile.read(77) - tfile2.write(buffer) - - # read the rest of the file, it's the codestream. - codestream = tfile.read() - - # Write the asoc superbox. - # Length = 36, id is 'asoc'. - buffer = struct.pack('>I4s', int(56), b'asoc') - tfile2.write(buffer) - - # Write the contained label box - buffer = struct.pack('>I4s', int(13), b'lbl ') - tfile2.write(buffer) - tfile2.write('label'.encode()) - - # Write the xml box - # Length = 36, id is 'xml '. - buffer = struct.pack('>I4s', int(35), b'xml ') - tfile2.write(buffer) - - buffer = 'this is a test' - buffer = buffer.encode() - tfile2.write(buffer) - - # Now append the codestream. - tfile2.write(codestream) - tfile2.flush() - - jasoc = glymur.Jp2k(tfile2.name) - print(jasoc.box[3]) - actual = sys.stdout.getvalue().strip() - lines = ['Association Box (asoc) @ (77, 56)', - ' Label Box (lbl ) @ (85, 13)', - ' Label: label', - ' XML Box (xml ) @ (98, 35)', - ' this is a test'] - expected = '\n'.join(lines) - self.assertEqual(actual, expected) - def test_less_common_boxes(self): with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: with open(self.jp2file, 'rb') as ifile: From 8c6bb1cc14f019268a2df8acf83418e0016610a3 Mon Sep 17 00:00:00 2001 From: John Evans Date: Sun, 16 Jun 2013 11:26:02 -0400 Subject: [PATCH 5/6] Trading in ~/.glymur for ~/.config/glymur. Closes #47 --- docs/source/introduction.rst | 4 ++-- glymur/__init__.py | 8 ++++---- glymur/test/test_jp2k.py | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/docs/source/introduction.rst b/docs/source/introduction.rst index 27beed2..7960c35 100644 --- a/docs/source/introduction.rst +++ b/docs/source/introduction.rst @@ -172,8 +172,8 @@ because ctypes access libraries in a platform-dependent manner, it is recommended that you create a configuration file to help glymur properly find the openjp2 library. You may create the configuration file as follows:: - $ mkdir ~/.glymur - $ cd ~/.glymur + $ mkdir -p ~/.config/glymur + $ cd ~/.config/glymur $ cat > glymurrc << EOF > [library] > openjp2: /opt/openjp2-svn/lib/libopenjp2.so diff --git a/glymur/__init__.py b/glymur/__init__.py index 3525b6a..795d3d9 100644 --- a/glymur/__init__.py +++ b/glymur/__init__.py @@ -8,7 +8,7 @@ def _glymurrc_fname(): Search order: 1) current working directory 2) environ var GLYMURCONFIGDIR - 3) HOME/.glymur/glymurrc + 3) $HOME/.config/glymur/glymurrc """ # Current directory. @@ -62,15 +62,15 @@ def _config(): def _get_configdir(): """Return string representing the configuration directory. - Default is HOME/.glymur. You can override this with the GLYMURCONFIGDIR - environment variable. + Default is $HOME/.config/glymur. You can override this with the + GLYMURCONFIGDIR environment variable. """ if 'GLYMURCONFIGDIR' in os.environ: return os.environ['GLYMURCONFIGDIR'] if 'HOME' in os.environ: - return os.path.join(os.environ['HOME'], '.glymur') + return os.path.join(os.environ['HOME'], '.config', 'glymur') import warnings import sys diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index b4ca59a..5579c8a 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -641,7 +641,7 @@ class TestJp2k(unittest.TestCase): @unittest.skipIf(sys.hexversion < 0x03020000, "Uses features introduced in 3.2.") def test_home_dir_missing_config_dir(self): - # Verify no exception is raised if $HOME is missing .glymur directory. + # Verify no exception is raised if $HOME is missing .config directory. with tempfile.TemporaryDirectory() as tdir: with patch.dict('os.environ', {'HOME': tdir}): # Misconfigured new configuration file should @@ -649,6 +649,24 @@ class TestJp2k(unittest.TestCase): with self.assertWarns(UserWarning) as cw: imp.reload(glymur) + @unittest.skipIf(sys.hexversion < 0x03020000, + "Uses features introduced in 3.2.") + def test_home_dir_missing_glymur_rc_dir(self): + # Should warn but not error if $HOME/.config but no glymurrc dir. + with tempfile.TemporaryDirectory() as tdir: + # We need the subdirectory to be specifically named as ".config" + # in order for this test to work. A specifically-named temporary + # directory does not seem to be possible, so try to symlink it. + # Supposedly the symlink gets cleaned up with tdir gets cleaned up. + with tempfile.TemporaryDirectory(suffix=".config", dir=tdir) \ + as tdir_config: + os.symlink(tdir_config, os.path.join(tdir, '.config')) + with patch.dict('os.environ', {'HOME': tdir}): + # Misconfigured new configuration file should + # be rejected. + with self.assertWarns(UserWarning) as cw: + imp.reload(glymur) + def test_xmp_attribute(self): # Verify that we can read the XMP packet in our shipping example file. j = Jp2k(self.jp2file) From 25559d53ff6f9dcbbf83de5e4bb72d98a7c16011 Mon Sep 17 00:00:00 2001 From: jevans Date: Sun, 16 Jun 2013 20:02:07 -0400 Subject: [PATCH 6/6] Closes #48 --- glymur/__init__.py | 8 ++++---- glymur/test/test_jp2k.py | 14 +++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/glymur/__init__.py b/glymur/__init__.py index 795d3d9..cc8297d 100644 --- a/glymur/__init__.py +++ b/glymur/__init__.py @@ -7,7 +7,7 @@ def _glymurrc_fname(): Search order: 1) current working directory - 2) environ var GLYMURCONFIGDIR + 2) environ var XDG_CONFIG_HOME 3) $HOME/.config/glymur/glymurrc """ @@ -63,11 +63,11 @@ def _get_configdir(): """Return string representing the configuration directory. Default is $HOME/.config/glymur. You can override this with the - GLYMURCONFIGDIR environment variable. + XDG_CONFIG_HOME environment variable. """ - if 'GLYMURCONFIGDIR' in os.environ: - return os.environ['GLYMURCONFIGDIR'] + if 'XDG_CONFIG_HOME' in os.environ: + return os.path.join(os.environ['XDG_CONFIG_HOME'], 'glymur') if 'HOME' in os.environ: return os.path.join(os.environ['HOME'], '.config', 'glymur') diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 5579c8a..3c25cca 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -599,13 +599,15 @@ class TestJp2k(unittest.TestCase): def test_config_file_via_environ(self): """Verify that we can read a configuration file set via environ var.""" with tempfile.TemporaryDirectory() as tdir: - filename = os.path.join(tdir, 'glymurrc') + configdir = os.path.join(tdir, 'glymur') + os.mkdir(configdir) + filename = os.path.join(configdir, 'glymurrc') with open(filename, 'wb') as tfile: tfile.write('[library]\n'.encode()) line = 'openjp2: {0}\n'.format(glymur._OPENJP2._name) tfile.write(line.encode()) tfile.flush() - with patch.dict('os.environ', {'GLYMURCONFIGDIR': tdir}): + with patch.dict('os.environ', {'XDG_CONFIG_HOME': tdir}): imp.reload(glymur) j = Jp2k(self.jp2file) @@ -614,13 +616,15 @@ class TestJp2k(unittest.TestCase): def test_config_file_via_environ_is_wrong(self): # A non-existant library location should be rejected. with tempfile.TemporaryDirectory() as tdir: - fname = os.path.join(tdir, 'glymurrc') + configdir = os.path.join(tdir, 'glymur') + os.mkdir(configdir) + fname = os.path.join(configdir, 'glymurrc') with open(fname, 'w') as fp: with tempfile.NamedTemporaryFile(suffix='.dylib') as tfile: fp.write('[library]\n') fp.write('openjp2: {0}.not.there\n'.format(tfile.name)) fp.flush() - with patch.dict('os.environ', {'GLYMURCONFIGDIR': tdir}): + with patch.dict('os.environ', {'XDG_CONFIG_HOME': tdir}): # Misconfigured new configuration file should # be rejected. with self.assertWarns(UserWarning) as cw: @@ -632,7 +636,7 @@ class TestJp2k(unittest.TestCase): # Verify that we error out properly if the configuration file # specified via environment variable is not found. with tempfile.TemporaryDirectory() as tdir: - with patch.dict('os.environ', {'GLYMURCONFIGDIR': tdir}): + with patch.dict('os.environ', {'XDG_CONFIG_HOME': tdir}): # Misconfigured new configuration file should # be rejected. with self.assertWarns(UserWarning) as cw: