From 47f42fdb88935bd8525999a046c653b165f2bf9d Mon Sep 17 00:00:00 2001 From: jevans Date: Mon, 24 Jun 2013 19:29:20 -0400 Subject: [PATCH] Closes #59 --- glymur/jp2box.py | 2 +- glymur/jp2k.py | 8 ++++++++ glymur/test/test_jp2box.py | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 09d5251..2b012b8 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -168,7 +168,7 @@ class ColourSpecificationBox(Jp2kBox): """ def __init__(self, method=ENUMERATED_COLORSPACE, precedence=0, approximation=0, colorspace=None, icc_profile=None, **kwargs): - Jp2kBox.__init__(self, id='', longname='Colour Specification') + Jp2kBox.__init__(self, id='colr', longname='Colour Specification') if colorspace is not None and icc_profile is not None: raise IOError("colorspace and icc_profile cannot both be set.") diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 50c53d8..015b63b 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -440,12 +440,20 @@ class Jp2k(Jp2kBox): msg = "The codestream box must be preceeded by a jp2 header box." raise IOError(msg) + # 1st jp2 header box must be ihdr 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) + # colr must be present in jp2 header box. + jp2hb = jp2h.box + colr_lst = [j for (j, box) in enumerate(jp2h.box) if box.id == 'colr'] + if len(colr_lst) == 0: + msg = "The jp2 header box must contain a color definition 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 a55f90f..8c0730b 100644 --- a/glymur/test/test_jp2box.py +++ b/glymur/test/test_jp2box.py @@ -286,6 +286,26 @@ class TestJp2Boxes(unittest.TestCase): with self.assertRaises(IOError): j2k.wrap(tfile.name, boxes=boxes) + def test_colr_box_not_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 = [ihdr] + boxes = [jP, ftyp, jp2h, jp2c, colr] + with tempfile.NamedTemporaryFile(suffix=".jp2") as tfile: + with self.assertRaises(IOError): + j2k.wrap(tfile.name, boxes=boxes) + if __name__ == "__main__": unittest.main()