From 90c027e5ca65c6ee2852c03fb7e65ca33945b03b Mon Sep 17 00:00:00 2001 From: jevans Date: Mon, 24 Jun 2013 19:04:12 -0400 Subject: [PATCH] Closes #62 --- glymur/jp2k.py | 9 +++++++++ glymur/test/test_jp2box.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 1a1e3f2..ea24b54 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -431,6 +431,15 @@ class Jp2k(Jp2kBox): msg += "must be the file type box." raise IOError(msg) + # jp2c must be preceeded by jp2h + jp2h_lst = [idx for (idx, box) in enumerate(boxes) if box.id == 'jp2h'] + jp2h_idx = jp2h_lst[0] + jp2c_lst = [idx for (idx, box) in enumerate(boxes) if box.id == 'jp2c'] + jp2c_idx = jp2c_lst[0] + if jp2h_idx >= jp2c_idx: + msg = "The codestream box must be preceeded by a jp2 header box." + raise IOError(msg) + with open(filename, 'wb') as ofile: for box in boxes: if box.id != 'jp2c': diff --git a/glymur/test/test_jp2box.py b/glymur/test/test_jp2box.py index 291b1ba..78591a4 100644 --- a/glymur/test/test_jp2box.py +++ b/glymur/test/test_jp2box.py @@ -289,6 +289,26 @@ class TestJp2Boxes(unittest.TestCase): with self.assertRaises(IOError): j2k.wrap(tfile.name, boxes=boxes) + def test_jp2h_not_preceeding_jp2c(self): + j2k = Jp2k(self.raw_codestream) + c = j2k.get_codestream() + height = c.segment[1].Ysiz + width = c.segment[1].Xsiz + num_components = len(c.segment[1].XRsiz) + + jP = JPEG2000SignatureBox() + ftyp = FileTypeBox() + jp2h = JP2HeaderBox() + jp2c = ContiguousCodestreamBox() + colr = ColourSpecificationBox(colorspace=glymur.core.SRGB) + ihdr = ImageHeaderBox(height=height, width=width, + num_components=num_components) + jp2h.box = [ihdr, colr] + boxes = [jP, ftyp, jp2c, jp2h] + with tempfile.NamedTemporaryFile(suffix=".jp2") as tfile: + #with self.assertRaises(IOError): + j2k.wrap(tfile.name, boxes=boxes) + if __name__ == "__main__": unittest.main()