diff --git a/glymur/jp2box.py b/glymur/jp2box.py
index 2b012b8..99a1dee 100644
--- a/glymur/jp2box.py
+++ b/glymur/jp2box.py
@@ -1538,7 +1538,7 @@ class XMLBox(Jp2kBox):
XML section.
"""
def __init__(self, **kwargs):
- Jp2kBox.__init__(self, id='', longname='XML')
+ Jp2kBox.__init__(self, id='xml ', longname='XML')
self.__dict__.update(**kwargs)
def __str__(self):
@@ -1550,6 +1550,14 @@ class XMLBox(Jp2kBox):
msg += '\n {0}'.format(xml)
return msg
+ def _write(self, f):
+ """Write an XML box to file.
+ """
+ buffer = ET.tostring(self.xml, encoding='utf-8')
+ f.write(struct.pack('>I', len(buffer) + 8))
+ f.write(self.id.encode())
+ f.write(buffer)
+
@staticmethod
def _parse(f, id, offset, length):
"""Parse XML box.
diff --git a/glymur/test/test_jp2box.py b/glymur/test/test_jp2box.py
index 8c0730b..2562339 100644
--- a/glymur/test/test_jp2box.py
+++ b/glymur/test/test_jp2box.py
@@ -1,5 +1,6 @@
import doctest
import tempfile
+import xml.etree.cElementTree as ET
import unittest
import numpy as np
@@ -47,6 +48,13 @@ class TestJp2Boxes(unittest.TestCase):
def tearDown(self):
pass
+ def test_default_XML(self):
+ # Should be able to instantiate an XMLBox
+ xml = ET.fromstring('0')
+ b = glymur.jp2box.XMLBox(xml=xml)
+ self.assertEqual(ET.tostring(b.xml),
+ b'0')
+
def test_default_JPEG2000SignatureBox(self):
# Should be able to instantiate a JPEG2000SignatureBox
b = glymur.jp2box.JPEG2000SignatureBox()
@@ -217,10 +225,6 @@ class TestJp2Boxes(unittest.TestCase):
with self.assertRaises(NotImplementedError):
j2k.wrap(tfile.name, boxes=boxes)
- def test_default_xml(self):
- # Should be able to write an xml box.
- self.assertTrue(False)
-
def test_default_component_definition(self):
# Should be able to specify a component definition box in order to,
# say, create an image with an alpha layer.
@@ -306,6 +310,49 @@ class TestJp2Boxes(unittest.TestCase):
with self.assertRaises(IOError):
j2k.wrap(tfile.name, boxes=boxes)
+ def test_missing_colr_box(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()
+ ihdr = ImageHeaderBox(height=height, width=width,
+ num_components=num_components)
+ jp2h.box = [ihdr]
+ boxes = [jP, ftyp, jp2h, jp2c]
+ with tempfile.NamedTemporaryFile(suffix=".jp2") as tfile:
+ with self.assertRaises(IOError):
+ j2k.wrap(tfile.name, boxes=boxes)
+
+ def test_basic_xml(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()
+ ihdr = ImageHeaderBox(height=height, width=width,
+ num_components=num_components)
+ colr = ColourSpecificationBox(colorspace=glymur.core.SRGB)
+ jp2h.box = [ihdr, colr]
+
+ the_xml = ET.fromstring('0')
+ xml = glymur.jp2box.XMLBox(xml=the_xml)
+ boxes = [jP, ftyp, jp2h, xml, jp2c]
+ with tempfile.NamedTemporaryFile(suffix=".jp2") as tfile:
+ j2k.wrap(tfile.name, boxes=boxes)
+ jp2 = Jp2k(tfile.name)
+ self.assertEqual(jp2.box[3].id, 'xml ')
+
if __name__ == "__main__":
unittest.main()
diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py
index b15ca35..479c1d7 100644
--- a/glymur/test/test_jp2k.py
+++ b/glymur/test/test_jp2k.py
@@ -15,7 +15,6 @@ if sys.hexversion <= 0x03030000:
else:
from unittest.mock import patch
import warnings
-from xml.etree import cElementTree as ET
import numpy as np
import pkg_resources