From 96cb70e5a5f4d5b74402d44516eb05ba08f51ead Mon Sep 17 00:00:00 2001 From: jevans Date: Mon, 31 Mar 2014 21:23:11 -0400 Subject: [PATCH] Added negative test for writing with bad approximation. #202 --- glymur/jp2k.py | 14 ++++++++++++++ glymur/test/test_jp2box.py | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 1f37584..971c82d 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -1250,6 +1250,7 @@ def _validate_jp2_box_sequence(boxes): if boxes[1].brand == 'jpx ': _validate_jpx_box_sequence(boxes) else: + # Validate the JP2 box IDs. count = _collect_box_count(boxes) for box_id in count.keys(): if box_id not in JP2_IDS: @@ -1257,6 +1258,19 @@ def _validate_jp2_box_sequence(boxes): msg += "brand be set to 'jpx '." raise IOError(msg.format(box_id)) + _validate_jp2_colr(boxes) + +def _validate_jp2_colr(boxes): + """ + Validate JP2 requirements on colour specification boxes. + """ + lst = [box for box in boxes if box.box_id == 'jp2h'] + jp2h = lst[0] + for colr in [box for box in jp2h.box if box.box_id == 'colr']: + if colr.approximation != 0: + msg = "A JP2 colr box cannot have a non-zero approximation field." + raise IOError(msg) + def _validate_jpx_box_sequence(boxes): """Run through series of tests for JPX box legality.""" _validate_label(boxes) diff --git a/glymur/test/test_jp2box.py b/glymur/test/test_jp2box.py index f61bafc..1a00715 100644 --- a/glymur/test/test_jp2box.py +++ b/glymur/test/test_jp2box.py @@ -453,6 +453,18 @@ class TestColourSpecificationBox(unittest.TestCase): with self.assertRaises(IOError): j2k.wrap(tfile.name, boxes=boxes) + @unittest.skipIf(os.name == "nt", "Temporary file issue on window.") + def test_bad_approx_jp2_field(self): + """JP2 has requirements for approx field""" + j2k = Jp2k(self.j2kfile) + boxes = [self.jp2b, self.ftyp, self.jp2h, self.jp2c] + colr = ColourSpecificationBox(colorspace=glymur.core.SRGB, + approximation=1) + boxes[2].box = [self.ihdr, colr] + with tempfile.NamedTemporaryFile(suffix=".jp2") as tfile: + with self.assertRaises(IOError): + j2k.wrap(tfile.name, boxes=boxes) + def test_default_colr(self): """basic colr instantiation""" colr = ColourSpecificationBox(colorspace=glymur.core.SRGB)