diff --git a/glymur/jp2k.py b/glymur/jp2k.py index ea24b54..50c53d8 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -440,6 +440,12 @@ class Jp2k(Jp2kBox): msg = "The codestream box must be preceeded by a jp2 header box." raise IOError(msg) + jp2h = boxes[jp2h_idx] + if jp2h.box[0].id != 'ihdr': + msg = "The first box in the jp2 header box must be the image " + msg += "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 9c0e56b..a55f90f 100644 --- a/glymur/test/test_jp2box.py +++ b/glymur/test/test_jp2box.py @@ -266,6 +266,26 @@ class TestJp2Boxes(unittest.TestCase): with self.assertRaises(IOError): j2k.wrap(tfile.name, boxes=boxes) + def test_ihdr_not_first_in_jp2h(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 = [colr, ihdr] + boxes = [jP, ftyp, jp2h, jp2c] + with tempfile.NamedTemporaryFile(suffix=".jp2") as tfile: + with self.assertRaises(IOError): + j2k.wrap(tfile.name, boxes=boxes) + if __name__ == "__main__": unittest.main()