def test_invalid_colorspace(self): """ An invalid colorspace shouldn't cause an error. Original test file was edf_c2_1103421.jp2 """ colr = ColourSpecificationBox(colorspace=276) colr.colorspace = 276 str(colr)
def test_write_colr_with_bad_method(self): """ SCENARIO: A colr box has an invalid method value. EXPECTED RESULT: InvalidJp2kError """ with warnings.catch_warnings(): warnings.simplefilter('ignore') colr = ColourSpecificationBox(colorspace=SRGB, method=5) with tempfile.TemporaryFile() as tfile: with self.assertRaises(InvalidJp2kError): colr.write(tfile)
def test_colr_with_bad_color(self): """ SCENARIO: A colr box has an invalid colorspace. EXPECTED RESULT: An InvalidJp2kError is raised when attempting to write the box. """ with self.assertWarns(UserWarning): # A warning is issued due to the bad colorspace. colr = ColourSpecificationBox(colorspace=-1, approximation=0) with tempfile.TemporaryFile() as tfile: with self.assertRaises(InvalidJp2kError): colr.write(tfile)
def test_default_colr(self): """basic colr instantiation""" colr = ColourSpecificationBox(colorspace=SRGB) self.assertEqual(colr.method, glymur.core.ENUMERATED_COLORSPACE) self.assertEqual(colr.precedence, 0) self.assertEqual(colr.approximation, 0) self.assertEqual(colr.colorspace, SRGB) self.assertIsNone(colr.icc_profile)
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=SRGB, approximation=1) boxes[2].box = [self.ihdr, colr] with open(self.temp_jp2_filename, mode='wb') as tfile: with self.assertRaises(InvalidJp2kError): j2k.wrap(tfile.name, boxes=boxes)
def test_colr_with_out_enum_cspace(self): """must supply an enumerated colorspace when writing""" j2k = Jp2k(self.j2kfile) boxes = [self.jp2b, self.ftyp, self.jp2h, self.jp2c] boxes[2].box = [self.ihdr, ColourSpecificationBox(colorspace=None)] with open(self.temp_jp2_filename, mode='wb') as tfile: with self.assertRaises(InvalidJp2kError): j2k.wrap(tfile.name, boxes=boxes)
def test_cgrp(self): """Write a color group box.""" jp2 = Jp2k(self.jp2file) boxes = [jp2.box[idx] for idx in [0, 1, 2, 4]] # The ftyp box must be modified to jpx. boxes[1].brand = 'jpx ' boxes[1].compatibility_list = ['jp2 ', 'jpxb'] colr_rgb = ColourSpecificationBox(colorspace=glymur.core.SRGB) colr_gr = ColourSpecificationBox(colorspace=glymur.core.GREYSCALE) box = [colr_rgb, colr_gr] cgrp = glymur.jp2box.ColourGroupBox(box=box) boxes.append(cgrp) with open(self.temp_jpx_filename, mode='wb') as tfile: jpx = jp2.wrap(tfile.name, boxes=boxes) self.assertEqual(jpx.box[-1].box_id, 'cgrp') self.assertEqual(jpx.box[-1].box[0].box_id, 'colr') self.assertEqual(jpx.box[-1].box[1].box_id, 'colr')
def test_icc_profile(self): """basic colr box with ICC profile""" colr = ColourSpecificationBox(icc_profile=self.icc_profile) self.assertEqual(colr.method, glymur.core.ENUMERATED_COLORSPACE) self.assertEqual(colr.precedence, 0) self.assertEqual(colr.approximation, 0) icc_profile = _ICCProfile(colr.icc_profile) self.assertEqual(icc_profile.header['Version'], '2.1.0') self.assertEqual(icc_profile.header['Color Space'], 'gray') self.assertIsNone(icc_profile.header['Datetime']) # Only True for version4 self.assertFalse('Profile Id' in icc_profile.header.keys())
def test_bad_method_printing(self): """ SCENARIO: An ICC profile is both too short and has an invalid method value. EXPECTED RESULT: Warnings are issued. Printing the string representation should not error out. """ with ir.path(data, 'issue405.dat') as path: with path.open('rb') as f: f.seek(8) with warnings.catch_warnings(): # Lots of things wrong with this file. warnings.simplefilter('ignore') box = ColourSpecificationBox.parse(f, length=80, offset=0) str(box)
def setUp(self): super(TestXML, self).setUp() raw_xml = b"""<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data>""" path = self.test_dir_path / 'data.xml' with path.open(mode='wb') as tfile: tfile.write(raw_xml) tfile.flush() self.xmlfile_path = path self.xmlfile = str(path) j2k = Jp2k(self.j2kfile) codestream = j2k.get_codestream() height = codestream.segment[1].ysiz width = codestream.segment[1].xsiz num_components = len(codestream.segment[1].xrsiz) self.jp2b = JPEG2000SignatureBox() self.ftyp = FileTypeBox() self.jp2h = JP2HeaderBox() self.jp2c = ContiguousCodestreamBox() self.ihdr = ImageHeaderBox(height=height, width=width, num_components=num_components) self.colr = ColourSpecificationBox(colorspace=glymur.core.SRGB)
def setUp(self): self.jp2file = glymur.data.nemo() self.j2kfile = glymur.data.goodstuff() raw_xml = b"""<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data>""" with tempfile.NamedTemporaryFile(suffix=".xml", delete=False) as tfile: tfile.write(raw_xml) tfile.flush() self.xmlfile = tfile.name j2k = Jp2k(self.j2kfile) codestream = j2k.get_codestream() height = codestream.segment[1].ysiz width = codestream.segment[1].xsiz num_components = len(codestream.segment[1].xrsiz) self.jp2b = JPEG2000SignatureBox() self.ftyp = FileTypeBox() self.jp2h = JP2HeaderBox() self.jp2c = ContiguousCodestreamBox() self.ihdr = ImageHeaderBox(height=height, width=width, num_components=num_components) self.colr = ColourSpecificationBox(colorspace=glymur.core.SRGB)