From d27f807680a17497313dd93e6b4dc0c133abd6e6 Mon Sep 17 00:00:00 2001 From: scaramallion Date: Mon, 25 Mar 2024 18:34:16 +1100 Subject: [PATCH] Fix using MCT with RGB, update type hints to remove bytearray for encoding (#92) --- openjpeg/tests/test_encode.py | 19 ++++++++++--------- openjpeg/utils.py | 14 ++++++-------- pyproject.toml | 2 +- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/openjpeg/tests/test_encode.py b/openjpeg/tests/test_encode.py index 4d49040..5347def 100644 --- a/openjpeg/tests/test_encode.py +++ b/openjpeg/tests/test_encode.py @@ -1713,7 +1713,7 @@ def test_photometric_interpretation(self): "pixel_representation": 0, "bits_stored": 8, } - for pi in ("RGB", "YBR_ICT", "YBR_RCT"): + for pi in ("YBR_ICT", "YBR_RCT"): buffer = encode_pixel_data( arr.tobytes(), photometric_interpretation=pi, @@ -1722,14 +1722,15 @@ def test_photometric_interpretation(self): param = parse_j2k(buffer) assert param["mct"] is True - buffer = encode_pixel_data( - arr.tobytes(), - photometric_interpretation="YBR_FULL", - use_mct=True, - **kwargs, - ) - param = parse_j2k(buffer) - assert param["mct"] is False + for pi in ("RGB", "YBR_FULL", "MONOCHROME1"): + buffer = encode_pixel_data( + arr.tobytes(), + photometric_interpretation=pi, + use_mct=True, + **kwargs, + ) + param = parse_j2k(buffer) + assert param["mct"] is False def test_codec_format_ignored(self): """Test that codec_format gets ignored.""" diff --git a/openjpeg/utils.py b/openjpeg/utils.py index 61e123c..e64195e 100644 --- a/openjpeg/utils.py +++ b/openjpeg/utils.py @@ -567,7 +567,7 @@ def encode_array( def encode_buffer( - src: Union[bytes, bytearray], + src: bytes, columns: int, rows: int, samples_per_pixel: int, @@ -609,7 +609,7 @@ def encode_buffer( Parameters ---------- - src : bytes | bytearray + src : bytes A single frame of little endian, colour-by-pixel ordered image data to be JPEG 2000 encoded. Each pixel should be encoded using the following (each pixel has 1 or more samples): @@ -712,16 +712,14 @@ def encode_buffer( return cast(bytes, buffer) -def encode_pixel_data( - src: Union[bytes, bytearray], **kwargs: Any -) -> bytes: +def encode_pixel_data(src: bytes, **kwargs: Any) -> bytes: """Return the JPEG 2000 compressed `src`. .. versionadded:: 2.2 Parameters ---------- - src : bytes | bytearray + src : bytes A single frame of little endian, colour-by-pixel ordered image data to be JPEG2000 encoded. Each pixel should be encoded using the following: @@ -743,7 +741,7 @@ def encode_pixel_data( * ``'use_mct'``: bool: ``True`` to use MCT with RGB images (default) ``False`` otherwise. Will be ignored if `photometric_interpretation` - is not RGB, YBR_RCT or YBR_ICT. + is not YBR_RCT or YBR_ICT. * ''`compression_ratios'``: list[float] - required for lossy encoding if `signal_noise_ratios` is not used. The desired compression ratio to use for each quality layer. @@ -759,7 +757,7 @@ def encode_pixel_data( # A J2K codestream doesn't track the colour space, so the photometric # interpretation is only used to help with setting MCT pi = kwargs["photometric_interpretation"] - if pi in ("RGB", "YBR_ICT", "YBR_RCT"): + if pi in ("YBR_ICT", "YBR_RCT"): kwargs["photometric_interpretation"] = 1 else: kwargs["photometric_interpretation"] = 0 diff --git a/pyproject.toml b/pyproject.toml index 5d6c79f..28a2b4f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,7 +48,7 @@ packages = [ {include = "openjpeg" }, ] readme = "README.md" -version = "2.2.0" +version = "2.2.1" [tool.poetry.dependencies]