diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 97ceefa..7ba6659 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -23,6 +23,7 @@ jobs: python -m pip install --upgrade pip pip install -r requirements.txt pip install -r requirements-test.txt + sudo apt-get install libopenjp2-7 - name: Run Flake8 run: | flake8 . diff --git a/tests/base/test_jp2.py b/tests/base/test_jp2.py new file mode 100644 index 0000000..bd01061 --- /dev/null +++ b/tests/base/test_jp2.py @@ -0,0 +1,41 @@ +import os.path +import unittest + +import glymur +from glymur.codestream import CMEsegment + +from hippolyzer.lib.base.jp2_utils import BufferedJp2k + +BASE_PATH = os.path.dirname(os.path.abspath(__file__)) + + +@unittest.skipIf(glymur.jp2k.opj2.OPENJP2 is None, "OpenJPEG library missing") +class TestJP2Utils(unittest.TestCase): + @classmethod + def setUpClass(cls) -> None: + # Use a rigged cube SLM from the upload process as a test file + with open(os.path.join(BASE_PATH, "test_resources", "plywood.j2c"), "rb") as f: + cls.j2c_bytes = f.read() + + def test_load_j2c(self): + j = BufferedJp2k(contents=self.j2c_bytes) + j.parse() + # Last segment in the header is the comment section + com: CMEsegment = j.codestream.segment[-1] + self.assertEqual("CME", com.marker_id) + # In this case the comment is the encoder version + self.assertEqual(b'Kakadu-3.0.3', com.ccme) + + def test_read_j2c_data(self): + j = BufferedJp2k(self.j2c_bytes) + pixels = j[::] + self.assertEqual((512, 512, 3), pixels.shape) + + def test_save_j2c_data(self): + j = BufferedJp2k(self.j2c_bytes) + pixels = j[::] + j[::] = pixels + new_j2c_bytes = bytes(j) + self.assertNotEqual(self.j2c_bytes, new_j2c_bytes) + # Glymur will have replaced the CME section with its own + self.assertIn(b"Created by OpenJPEG", new_j2c_bytes) diff --git a/tests/base/test_resources/plywood.j2c b/tests/base/test_resources/plywood.j2c new file mode 100644 index 0000000..654d0b0 Binary files /dev/null and b/tests/base/test_resources/plywood.j2c differ