pylint work, #99

This commit is contained in:
jevans 2013-08-11 20:08:15 -04:00
commit 1a84ca2386
2 changed files with 197 additions and 167 deletions

View file

@ -2,7 +2,12 @@
The tests here do not correspond directly to the OpenJPEG test suite, but The tests here do not correspond directly to the OpenJPEG test suite, but
seem like logical negative tests to add. seem like logical negative tests to add.
""" """
#pylint: disable-all # E1101: assertWarns introduced in python 3.2
# pylint: disable=E1101
# R0904: Not too many methods in unittest.
# pylint: disable=R0904
import os import os
import sys import sys
import tempfile import tempfile
@ -13,9 +18,6 @@ else:
import unittest import unittest
import numpy as np import numpy as np
import pkg_resources
from glymur.lib import openjp2 as opj2
from .fixtures import read_image, NO_READ_BACKEND, NO_READ_BACKEND_MSG from .fixtures import read_image, NO_READ_BACKEND, NO_READ_BACKEND_MSG
@ -23,9 +25,9 @@ from glymur import Jp2k
import glymur import glymur
try: try:
data_root = os.environ['OPJ_DATA_ROOT'] DATA_ROOT = os.environ['OPJ_DATA_ROOT']
except KeyError: except KeyError:
data_root = None DATA_ROOT = None
except: except:
raise raise
@ -33,9 +35,10 @@ except:
@unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, @unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None,
"Missing openjp2 library.") "Missing openjp2 library.")
@unittest.skipIf(NO_READ_BACKEND, NO_READ_BACKEND_MSG) @unittest.skipIf(NO_READ_BACKEND, NO_READ_BACKEND_MSG)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
class TestSuiteNegative(unittest.TestCase): class TestSuiteNegative(unittest.TestCase):
"""Test suite for certain negative tests from openjpeg suite."""
def setUp(self): def setUp(self):
self.jp2file = glymur.data.nemo() self.jp2file = glymur.data.nemo()
@ -45,45 +48,50 @@ class TestSuiteNegative(unittest.TestCase):
pass pass
@unittest.skipIf(os.name == "nt", "Temporary file issue on window.") @unittest.skipIf(os.name == "nt", "Temporary file issue on window.")
def test_negative_psnr_with_cratios(self): def test_psnr_with_cratios(self):
# Using psnr with cratios options is not allowed. """Using psnr with cratios options is not allowed."""
# Not an OpenJPEG test, but close. # Not an OpenJPEG test, but close.
infile = os.path.join(data_root, 'input/nonregression/Bretagne1.ppm') infile = os.path.join(DATA_ROOT, 'input/nonregression/Bretagne1.ppm')
data = read_image(infile) data = read_image(infile)
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile: with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
j = Jp2k(tfile.name, 'wb') j = Jp2k(tfile.name, 'wb')
with self.assertRaises(IOError): with self.assertRaises(IOError):
j.write(data, psnr=[30, 35, 40], cratios=[2, 3, 4]) j.write(data, psnr=[30, 35, 40], cratios=[2, 3, 4])
def test_NR_MarkerIsNotCompliant_j2k_dump(self): def test_nr_marker_not_compliant(self):
"""non-compliant marker, should still be able to read"""
relpath = 'input/nonregression/MarkerIsNotCompliant.j2k' relpath = 'input/nonregression/MarkerIsNotCompliant.j2k'
jfile = os.path.join(data_root, relpath) jfile = os.path.join(DATA_ROOT, relpath)
jp2k = Jp2k(jfile) jp2k = Jp2k(jfile)
c = jp2k.get_codestream(header_only=False) jp2k.get_codestream(header_only=False)
self.assertTrue(True)
@unittest.skipIf(sys.hexversion < 0x03020000, @unittest.skipIf(sys.hexversion < 0x03020000,
"Uses features introduced in 3.2.") "Uses features introduced in 3.2.")
def test_NR_illegalcolortransform_dump(self): def test_nr_illegalclrtransform(self):
# EOC marker is bad """EOC marker is bad"""
relpath = 'input/nonregression/illegalcolortransform.j2k' relpath = 'input/nonregression/illegalcolortransform.j2k'
jfile = os.path.join(data_root, relpath) jfile = os.path.join(DATA_ROOT, relpath)
jp2k = Jp2k(jfile) jp2k = Jp2k(jfile)
with self.assertWarns(UserWarning) as cw: with self.assertWarns(UserWarning):
c = jp2k.get_codestream(header_only=False) codestream = jp2k.get_codestream(header_only=False)
# Verify that the last segment returned in the codestream is SOD, # Verify that the last segment returned in the codestream is SOD,
# not EOC. Codestream parsing should stop when we try to jump to # not EOC. Codestream parsing should stop when we try to jump to
# the end of SOT. # the end of SOT.
self.assertEqual(c.segment[-1].marker_id, 'SOD') self.assertEqual(codestream.segment[-1].marker_id, 'SOD')
def test_NR_Cannotreaddatawithnosizeknown_j2k(self): def test_nr_cannotreadwnosizeknown(self):
"""not sure exactly what is wrong with this file"""
relpath = 'input/nonregression/Cannotreaddatawithnosizeknown.j2k' relpath = 'input/nonregression/Cannotreaddatawithnosizeknown.j2k'
jfile = os.path.join(data_root, relpath) jfile = os.path.join(DATA_ROOT, relpath)
jp2k = Jp2k(jfile) jp2k = Jp2k(jfile)
c = jp2k.get_codestream(header_only=False) jp2k.get_codestream(header_only=False)
self.assertTrue(True)
@unittest.skipIf(os.name == "nt", "Temporary file issue on window.") @unittest.skipIf(os.name == "nt", "Temporary file issue on window.")
def test_code_block_dimensions(self): def test_code_block_dimensions(self):
"""don't allow extreme codeblock sizes"""
# opj_compress doesn't allow the dimensions of a codeblock # opj_compress doesn't allow the dimensions of a codeblock
# to be too small or too big, so neither will we. # to be too small or too big, so neither will we.
data = np.zeros((256, 256), dtype=np.uint8) data = np.zeros((256, 256), dtype=np.uint8)
@ -91,55 +99,55 @@ class TestSuiteNegative(unittest.TestCase):
j = Jp2k(tfile.name, 'wb') j = Jp2k(tfile.name, 'wb')
# opj_compress doesn't allow code block area to exceed 4096. # opj_compress doesn't allow code block area to exceed 4096.
with self.assertRaises(IOError) as cr: with self.assertRaises(IOError):
j.write(data, cbsize=(256, 256)) j.write(data, cbsize=(256, 256))
# opj_compress doesn't allow either dimension to be less than 4. # opj_compress doesn't allow either dimension to be less than 4.
with self.assertRaises(IOError) as cr: with self.assertRaises(IOError):
j.write(data, cbsize=(2048, 2)) j.write(data, cbsize=(2048, 2))
with self.assertRaises(IOError) as cr: with self.assertRaises(IOError):
j.write(data, cbsize=(2, 2048)) j.write(data, cbsize=(2, 2048))
@unittest.skipIf(sys.hexversion < 0x03020000, @unittest.skipIf(sys.hexversion < 0x03020000,
"Uses features introduced in 3.2.") "Uses features introduced in 3.2.")
def test_exceeded_box(self): def test_exceeded_box(self):
"""should warn if reading past end of a box"""
# Verify that a warning is issued if we read past the end of a box # Verify that a warning is issued if we read past the end of a box
# This file has a palette (pclr) box whose length is impossibly # This file has a palette (pclr) box whose length is impossibly
# short. # short.
infile = os.path.join(data_root, infile = os.path.join(DATA_ROOT,
'input/nonregression/mem-b2ace68c-1381.jp2') 'input/nonregression/mem-b2ace68c-1381.jp2')
with self.assertWarns(UserWarning) as cw: with self.assertWarns(UserWarning):
j = Jp2k(infile) Jp2k(infile)
@unittest.skipIf(os.name == "nt", "Temporary file issue on window.") @unittest.skipIf(os.name == "nt", "Temporary file issue on window.")
def test_precinct_size_not_multiple_of_two(self): def test_precinct_size_not_p2(self):
# Seems like precinct sizes should be powers of two. """precinct sizes should be powers of two."""
ifile = Jp2k(self.j2kfile) ifile = Jp2k(self.j2kfile)
data = ifile.read(rlevel=2) data = ifile.read(rlevel=2)
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
ofile = Jp2k(tfile.name, 'wb') ofile = Jp2k(tfile.name, 'wb')
with self.assertRaises(IOError) as ce: with self.assertRaises(IOError):
ofile.write(data, psizes=[(13, 13)]) ofile.write(data, psizes=[(13, 13)])
@unittest.skipIf(os.name == "nt", "Temporary file issue on window.") @unittest.skipIf(os.name == "nt", "Temporary file issue on window.")
def test_codeblock_size_not_multiple_of_two(self): def test_cblk_size_not_power_of_two(self):
# Seems like code block sizes should be powers of two. """code block sizes should be powers of two."""
ifile = Jp2k(self.j2kfile) ifile = Jp2k(self.j2kfile)
data = ifile.read(rlevel=2) data = ifile.read(rlevel=2)
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
ofile = Jp2k(tfile.name, 'wb') ofile = Jp2k(tfile.name, 'wb')
with self.assertRaises(IOError) as ce: with self.assertRaises(IOError):
ofile.write(data, cbsize=(13, 12)) ofile.write(data, cbsize=(13, 12))
@unittest.skipIf(os.name == "nt", "Temporary file issue on window.") @unittest.skipIf(os.name == "nt", "Temporary file issue on window.")
def test_codeblock_size_with_precinct_size(self): def test_cblk_size_precinct_size(self):
# Seems like code block sizes should never exceed half that of """code block sizes should never exceed half that of precinct size."""
# precinct size.
ifile = Jp2k(self.j2kfile) ifile = Jp2k(self.j2kfile)
data = ifile.read(rlevel=2) data = ifile.read(rlevel=2)
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
ofile = Jp2k(tfile.name, 'wb') ofile = Jp2k(tfile.name, 'wb')
with self.assertRaises(IOError) as ce: with self.assertRaises(IOError):
ofile.write(data, ofile.write(data,
cbsize=(64, 64), cbsize=(64, 64),
psizes=[(64, 64)]) psizes=[(64, 64)])

View file

@ -1,7 +1,15 @@
#pylint: disable-all """Test suite for printing.
"""
# C0302: don't care too much about having too many lines in a test module
# pylint: disable=C0302
# E061: unittest.mock introduced in 3.3 (python-2.7/pylint issue)
# pylint: disable=E0611,F0401
# R0904: Not too many methods in unittest.
# pylint: disable=R0904
import os import os
import pkg_resources
import re
import struct import struct
import sys import sys
import tempfile import tempfile
@ -26,9 +34,9 @@ import glymur
from glymur import Jp2k from glymur import Jp2k
try: try:
data_root = os.environ['OPJ_DATA_ROOT'] DATA_ROOT = os.environ['OPJ_DATA_ROOT']
except KeyError: except KeyError:
data_root = None DATA_ROOT = None
except: except:
raise raise
@ -122,12 +130,13 @@ class TestPrintingNeedsLib(unittest.TestCase):
+ '(0, 10), (0, 9), (0, 9), (0, 10), (0, 9), (0, 9), ' + '(0, 10), (0, 9), (0, 9), (0, 10), (0, 9), (0, 9), '
+ '(0, 10), (0, 9), (0, 9), (0, 10), (0, 9), (0, 9), ' + '(0, 10), (0, 9), (0, 9), (0, 10), (0, 9), (0, 9), '
+ '(0, 10)]'] + '(0, 10)]']
self.expectedPlain = '\n'.join(lines) self.expected_plain = '\n'.join(lines)
def tearDown(self): def tearDown(self):
pass pass
def test_asoc_label_box(self): def test_asoc_label_box(self):
"""verify printing of asoc, label boxes"""
# Construct a fake file with an asoc and a label box, as # Construct a fake file with an asoc and a label box, as
# OpenJPEG doesn't have such a file. # OpenJPEG doesn't have such a file.
data = glymur.Jp2k(self.jp2file).read(rlevel=1) data = glymur.Jp2k(self.jp2file).read(rlevel=1)
@ -138,30 +147,30 @@ class TestPrintingNeedsLib(unittest.TestCase):
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile2: with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile2:
# Offset of the codestream is where we start. # Offset of the codestream is where we start.
buffer = tfile.read(77) wbuffer = tfile.read(77)
tfile2.write(buffer) tfile2.write(wbuffer)
# read the rest of the file, it's the codestream. # read the rest of the file, it's the codestream.
codestream = tfile.read() codestream = tfile.read()
# Write the asoc superbox. # Write the asoc superbox.
# Length = 36, id is 'asoc'. # Length = 36, id is 'asoc'.
buffer = struct.pack('>I4s', int(56), b'asoc') wbuffer = struct.pack('>I4s', int(56), b'asoc')
tfile2.write(buffer) tfile2.write(wbuffer)
# Write the contained label box # Write the contained label box
buffer = struct.pack('>I4s', int(13), b'lbl ') wbuffer = struct.pack('>I4s', int(13), b'lbl ')
tfile2.write(buffer) tfile2.write(wbuffer)
tfile2.write('label'.encode()) tfile2.write('label'.encode())
# Write the xml box # Write the xml box
# Length = 36, id is 'xml '. # Length = 36, id is 'xml '.
buffer = struct.pack('>I4s', int(35), b'xml ') wbuffer = struct.pack('>I4s', int(35), b'xml ')
tfile2.write(buffer) tfile2.write(wbuffer)
buffer = '<test>this is a test</test>' wbuffer = '<test>this is a test</test>'
buffer = buffer.encode() wbuffer = wbuffer.encode()
tfile2.write(buffer) tfile2.write(wbuffer)
# Now append the codestream. # Now append the codestream.
tfile2.write(codestream) tfile2.write(codestream)
@ -180,6 +189,7 @@ class TestPrintingNeedsLib(unittest.TestCase):
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_jp2dump(self): def test_jp2dump(self):
"""basic jp2dump test"""
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
glymur.jp2dump(self._plain_nemo_file) glymur.jp2dump(self._plain_nemo_file)
actual = fake_out.getvalue().strip() actual = fake_out.getvalue().strip()
@ -188,9 +198,10 @@ class TestPrintingNeedsLib(unittest.TestCase):
lst = actual.split('\n') lst = actual.split('\n')
lst = lst[1:] lst = lst[1:]
actual = '\n'.join(lst) actual = '\n'.join(lst)
self.assertEqual(actual, self.expectedPlain) self.assertEqual(actual, self.expected_plain)
def test_entire_file(self): def test_entire_file(self):
"""verify output from printing entire file"""
j = glymur.Jp2k(self._plain_nemo_file) j = glymur.Jp2k(self._plain_nemo_file)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(j) print(j)
@ -201,10 +212,11 @@ class TestPrintingNeedsLib(unittest.TestCase):
lst = lst[1:] lst = lst[1:]
actual = '\n'.join(lst) actual = '\n'.join(lst)
self.assertEqual(actual, self.expectedPlain) self.assertEqual(actual, self.expected_plain)
class TestPrinting(unittest.TestCase): class TestPrinting(unittest.TestCase):
"""Test suite for printing where the libraries are not needed"""
def setUp(self): def setUp(self):
# Save sys.stdout. # Save sys.stdout.
@ -213,7 +225,8 @@ class TestPrinting(unittest.TestCase):
def tearDown(self): def tearDown(self):
pass pass
def test_COC_segment(self): def test_coc_segment(self):
"""verify printing of COC segment"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -240,7 +253,8 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_COD_segment(self): def test_cod_segment(self):
"""verify printing of COD segment"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream() codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -271,14 +285,13 @@ class TestPrinting(unittest.TestCase):
' Segmentation symbols: False'] ' Segmentation symbols: False']
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.actual = actual
self.expected = expected
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_icc_profile(self): def test_icc_profile(self):
filename = os.path.join(data_root, 'input/nonregression/text_GBR.jp2') """verify printing of colr box with ICC profile"""
filename = os.path.join(DATA_ROOT, 'input/nonregression/text_GBR.jp2')
with warnings.catch_warnings(): with warnings.catch_warnings():
# brand is 'jp2 ', but has any icc profile. # brand is 'jp2 ', but has any icc profile.
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
@ -343,24 +356,26 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_CRG(self): def test_crg(self):
filename = os.path.join(data_root, 'input/conformance/p0_03.j2k') """verify printing of CRG segment"""
filename = os.path.join(DATA_ROOT, 'input/conformance/p0_03.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream() codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(codestream.segment[-5]) print(codestream.segment[-5])
actual = fake_out.getvalue().strip() actual = fake_out.getvalue().strip()
lines = ['CRG marker segment at (87, 6)', lines = ['CRG marker segment @ (87, 6)',
' Vertical, Horizontal offset: (0.50, 1.00)'] ' Vertical, Horizontal offset: (0.50, 1.00)']
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_RGN(self): def test_rgn(self):
filename = os.path.join(data_root, 'input/conformance/p0_03.j2k') """verify printing of RGN segment"""
filename = os.path.join(DATA_ROOT, 'input/conformance/p0_03.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -373,10 +388,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_SOP(self): def test_sop(self):
filename = os.path.join(data_root, 'input/conformance/p0_03.j2k') """verify printing of SOP segment"""
filename = os.path.join(DATA_ROOT, 'input/conformance/p0_03.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -387,11 +403,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_CME(self): def test_cme(self):
# Test printing a CME or comment marker segment. """Test printing a CME or comment marker segment."""
filename = os.path.join(data_root, 'input/conformance/p0_02.j2k') filename = os.path.join(DATA_ROOT, 'input/conformance/p0_02.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream() codestream = j.get_codestream()
# 2nd to last segment in the main header # 2nd to last segment in the main header
@ -403,7 +419,8 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_EOC_segment(self): def test_eoc_segment(self):
"""verify printing of eoc segment"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -414,10 +431,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_PLT_segment(self): def test_plt_segment(self):
filename = os.path.join(data_root, 'input/conformance/p0_07.j2k') """verify printing of PLT segment"""
filename = os.path.join(DATA_ROOT, 'input/conformance/p0_07.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -432,10 +450,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_POD_segment(self): def test_pod_segment(self):
filename = os.path.join(data_root, 'input/conformance/p0_13.j2k') """verify printing of POD segment"""
filename = os.path.join(DATA_ROOT, 'input/conformance/p0_13.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream() codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -461,10 +480,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_PPM_segment(self): def test_ppm_segment(self):
filename = os.path.join(data_root, 'input/conformance/p1_03.j2k') """verify printing of PPM segment"""
filename = os.path.join(DATA_ROOT, 'input/conformance/p1_03.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream() codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -478,10 +498,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_PPT_segment(self): def test_ppt_segment(self):
filename = os.path.join(data_root, 'input/conformance/p1_06.j2k') """verify printing of ppt segment"""
filename = os.path.join(DATA_ROOT, 'input/conformance/p1_06.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -495,7 +516,8 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_QCC_segment(self): def test_qcc_segment(self):
"""verify printing of qcc segment"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -510,7 +532,8 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_QCD_segment_5x3_transform(self): def test_qcd_segment_5x3_transform(self):
"""verify printing of qcd segment"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream() codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -524,7 +547,8 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_SIZ_segment(self): def test_siz_segment(self):
"""verify printing of SIZ segment"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream() codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -545,7 +569,8 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_SOC_segment(self): def test_soc_segment(self):
"""verify printing of SOC segment"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream() codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -556,7 +581,8 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_SOD_segment(self): def test_sod_segment(self):
"""verify printing of SOD segment"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -567,7 +593,8 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_SOT_segment(self): def test_sot_segment(self):
"""verify printing of SOT segment"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
codestream = j.get_codestream(header_only=False) codestream = j.get_codestream(header_only=False)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -581,13 +608,13 @@ class TestPrinting(unittest.TestCase):
' Number of tile parts: 1'] ' Number of tile parts: 1']
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.maxDiff = None
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_TLM_segment(self): def test_tlm_segment(self):
filename = os.path.join(data_root, 'input/conformance/p0_15.j2k') """verify printing of TLM segment"""
filename = os.path.join(DATA_ROOT, 'input/conformance/p0_15.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream() codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -605,7 +632,7 @@ class TestPrinting(unittest.TestCase):
@unittest.skipIf(sys.hexversion < 0x02070000, @unittest.skipIf(sys.hexversion < 0x02070000,
"Differences in XML printing between 2.6 and 2.7") "Differences in XML printing between 2.6 and 2.7")
def test_xmp(self): def test_xmp(self):
# Verify the printing of a UUID/XMP box. """Verify the printing of a UUID/XMP box."""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(j.box[4]) print(j.box[4])
@ -627,6 +654,7 @@ class TestPrinting(unittest.TestCase):
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_codestream(self): def test_codestream(self):
"""verify printing of entire codestream"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(j.get_codestream()) print(j.get_codestream())
@ -672,15 +700,15 @@ class TestPrinting(unittest.TestCase):
' CME marker segment @ (3209, 37)', ' CME marker segment @ (3209, 37)',
' "Created by OpenJPEG version 2.0.0"'] ' "Created by OpenJPEG version 2.0.0"']
expected = '\n'.join(lst) expected = '\n'.join(lst)
self.maxDiff = None
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(sys.hexversion < 0x02070000, @unittest.skipIf(sys.hexversion < 0x02070000,
"Differences in XML printing between 2.6 and 2.7") "Differences in XML printing between 2.6 and 2.7")
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_xml(self): def test_xml(self):
filename = os.path.join(data_root, 'input/conformance/file1.jp2') """verify printing of XML box"""
filename = os.path.join(DATA_ROOT, 'input/conformance/file1.jp2')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(j.box[2]) print(j.box[2])
@ -707,10 +735,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_channel_definition(self): def test_channel_definition(self):
filename = os.path.join(data_root, 'input/conformance/file2.jp2') """verify printing of cdef box"""
filename = os.path.join(DATA_ROOT, 'input/conformance/file2.jp2')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(j.box[2].box[2]) print(j.box[2].box[2])
@ -722,10 +751,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_component_mapping(self): def test_component_mapping(self):
filename = os.path.join(data_root, 'input/conformance/file9.jp2') """verify printing of cmap box"""
filename = os.path.join(DATA_ROOT, 'input/conformance/file9.jp2')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(j.box[2].box[2]) print(j.box[2].box[2])
@ -737,10 +767,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_palette(self): def test_palette7(self):
filename = os.path.join(data_root, 'input/conformance/file9.jp2') """verify printing of pclr box"""
filename = os.path.join(DATA_ROOT, 'input/conformance/file9.jp2')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(j.box[2].box[1]) print(j.box[2].box[1])
@ -750,10 +781,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_palette(self): def test_rreq(self):
filename = os.path.join(data_root, 'input/conformance/file7.jp2') """verify printing of reader requirements box"""
filename = os.path.join(DATA_ROOT, 'input/conformance/file7.jp2')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(j.box[2]) print(j.box[2])
@ -773,25 +805,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set")
def test_CRG(self):
filename = os.path.join(data_root, 'input/conformance/p0_03.j2k')
j = glymur.Jp2k(filename)
codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out:
print(codestream.segment[6])
actual = fake_out.getvalue().strip()
lines = ['CRG marker segment @ (87, 6)',
' Vertical, Horizontal offset: (0.50, 1.00)']
expected = '\n'.join(lines)
self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_differing_subsamples(self): def test_differing_subsamples(self):
# Issue 86. """verify printing of SIZ with different subsampling... Issue 86."""
filename = os.path.join(data_root, 'input/conformance/p0_05.j2k') filename = os.path.join(DATA_ROOT, 'input/conformance/p0_05.j2k')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
codestream = j.get_codestream() codestream = j.get_codestream()
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
@ -810,11 +828,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_palette_box(self): def test_palette_box(self):
# Verify that palette (pclr) boxes are printed without error. """Verify that palette (pclr) boxes are printed without error."""
filename = os.path.join(data_root, 'input/conformance/file9.jp2') filename = os.path.join(DATA_ROOT, 'input/conformance/file9.jp2')
j = glymur.Jp2k(filename) j = glymur.Jp2k(filename)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out:
print(j.box[2].box[1]) print(j.box[2].box[1])
@ -826,54 +844,56 @@ class TestPrinting(unittest.TestCase):
@unittest.skipIf(os.name == "nt", "Temporary file issue on window.") @unittest.skipIf(os.name == "nt", "Temporary file issue on window.")
def test_less_common_boxes(self): def test_less_common_boxes(self):
"""verify uinf, ulst, url, res, resd, resc box printing"""
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
with open(self.jp2file, 'rb') as ifile: with open(self.jp2file, 'rb') as ifile:
# Everything up until the jp2c box. # Everything up until the jp2c box.
buffer = ifile.read(77) wbuffer = ifile.read(77)
tfile.write(buffer) tfile.write(wbuffer)
# Write the UINF superbox # Write the UINF superbox
# Length = 50, id is uinf. # Length = 50, id is uinf.
buffer = struct.pack('>I4s', int(50), b'uinf') wbuffer = struct.pack('>I4s', int(50), b'uinf')
tfile.write(buffer) tfile.write(wbuffer)
# Write the ULST box. # Write the ULST box.
# Length is 26, 1 UUID, hard code that UUID as zeros. # Length is 26, 1 UUID, hard code that UUID as zeros.
buffer = struct.pack('>I4sHIIII', int(26), b'ulst', int(1), wbuffer = struct.pack('>I4sHIIII', int(26), b'ulst', int(1),
int(0), int(0), int(0), int(0)) int(0), int(0), int(0), int(0))
tfile.write(buffer) tfile.write(wbuffer)
# Write the URL box. # Write the URL box.
# Length is 16, version is one byte, flag is 3 bytes, url # Length is 16, version is one byte, flag is 3 bytes, url
# is the rest. # is the rest.
buffer = struct.pack('>I4sBBBB', wbuffer = struct.pack('>I4sBBBB',
int(16), b'url ', int(16), b'url ',
int(0), int(0), int(0), int(0)) int(0), int(0), int(0), int(0))
tfile.write(buffer) tfile.write(wbuffer)
buffer = struct.pack('>ssss', b'a', b'b', b'c', b'd')
tfile.write(buffer) wbuffer = struct.pack('>ssss', b'a', b'b', b'c', b'd')
tfile.write(wbuffer)
# Start the resolution superbox. # Start the resolution superbox.
buffer = struct.pack('>I4s', int(44), b'res ') wbuffer = struct.pack('>I4s', int(44), b'res ')
tfile.write(buffer) tfile.write(wbuffer)
# Write the capture resolution box. # Write the capture resolution box.
buffer = struct.pack('>I4sHHHHBB', wbuffer = struct.pack('>I4sHHHHBB',
int(18), b'resc', int(18), b'resc',
int(1), int(1), int(1), int(1), int(1), int(1), int(1), int(1),
int(0), int(1)) int(0), int(1))
tfile.write(buffer) tfile.write(wbuffer)
# Write the display resolution box. # Write the display resolution box.
buffer = struct.pack('>I4sHHHHBB', wbuffer = struct.pack('>I4sHHHHBB',
int(18), b'resd', int(18), b'resd',
int(1), int(1), int(1), int(1), int(1), int(1), int(1), int(1),
int(1), int(0)) int(1), int(0))
tfile.write(buffer) tfile.write(wbuffer)
# Get the rest of the input file. # Get the rest of the input file.
buffer = ifile.read() wbuffer = ifile.read()
tfile.write(buffer) tfile.write(wbuffer)
tfile.flush() tfile.flush()
jp2k = glymur.Jp2k(tfile.name) jp2k = glymur.Jp2k(tfile.name)
@ -901,12 +921,13 @@ class TestPrinting(unittest.TestCase):
@unittest.skipIf(sys.hexversion < 0x03000000, @unittest.skipIf(sys.hexversion < 0x03000000,
"Ordered dicts not printing well in 2.7") "Ordered dicts not printing well in 2.7")
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_jpx_approximation_with_icc_profile(self): def test_jpx_approx_icc_profile(self):
"""verify jpx with approx field equal to zero"""
# ICC profiles may be used in JP2, but the approximation field should # ICC profiles may be used in JP2, but the approximation field should
# be zero unless we have jpx. This file does both. # be zero unless we have jpx. This file does both.
filename = os.path.join(data_root, 'input/nonregression/text_GBR.jp2') filename = os.path.join(DATA_ROOT, 'input/nonregression/text_GBR.jp2')
with warnings.catch_warnings(): with warnings.catch_warnings():
# brand is 'jp2 ', but has any icc profile. # brand is 'jp2 ', but has any icc profile.
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
@ -945,11 +966,11 @@ class TestPrinting(unittest.TestCase):
expected = '\n'.join(lines) expected = '\n'.join(lines)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
@unittest.skipIf(data_root is None, @unittest.skipIf(DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set") "OPJ_DATA_ROOT environment variable not set")
def test_uuid(self): def test_uuid(self):
# UUID box """verify printing of UUID box"""
filename = os.path.join(data_root, 'input/nonregression/text_GBR.jp2') filename = os.path.join(DATA_ROOT, 'input/nonregression/text_GBR.jp2')
with warnings.catch_warnings(): with warnings.catch_warnings():
# brand is 'jp2 ', but has any icc profile. # brand is 'jp2 ', but has any icc profile.
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
@ -968,6 +989,7 @@ class TestPrinting(unittest.TestCase):
@unittest.skipIf(sys.hexversion < 0x03000000, @unittest.skipIf(sys.hexversion < 0x03000000,
"Ordered dicts not printing well in 2.7") "Ordered dicts not printing well in 2.7")
def test_exif_uuid(self): def test_exif_uuid(self):
"""Verify printing of exif information"""
j = glymur.Jp2k(self.jp2file) j = glymur.Jp2k(self.jp2file)
with patch('sys.stdout', new=StringIO()) as fake_out: with patch('sys.stdout', new=StringIO()) as fake_out: