Skip to content

Commit

Permalink
Add support for encoding J2K (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
scaramallion committed Jan 13, 2024
1 parent cdbd616 commit 419fcbc
Show file tree
Hide file tree
Showing 15 changed files with 5,413 additions and 895 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ python -m pip install pylibjpeg-openjpeg
| [15444-1](https://www.iso.org/standard/78321.html) | [T.800](https://www.itu.int/rec/T-REC-T.800/en) | [JPEG 2000](https://jpeg.org/jpeg2000/) |

#### Encoding
Encoding of JPEG 2000 images is not currently supported

Encoding of NumPy ndarrays is supported for the following:

* Array dtype: bool, uint8, int8, uint16, int16, uint32 and int32 (1-24 bit-depth only)
* Array shape: (rows, columns) and (rows, columns, planes)
* Number of rows/columns: up to 65535
* Number of planes: 1, 3 or 4


### Transfer Syntaxes
Expand Down Expand Up @@ -81,3 +87,46 @@ with open('filename.j2k', 'rb') as f:
# Or simply...
arr = decode('filename.j2k')
```

#### Standalone JPEG encoding

Lossless encoding of RGB with multiple-component transformation:

```python

import numpy as np
from openjpeg import encode

arr = np.random.randint(low=0, high=65535, size=(100, 100, 3), dtype="uint8")
encode(arr, photometric_interpretation=1) # 1: sRGB
```

Lossy encoding of a monochrome image using compression ratios:

```python

import numpy as np
from openjpeg import encode

arr = np.random.randint(low=-2**15, high=2**15 - 1, size=(100, 100), dtype="int8")
# You must determine your own values for `compression_ratios`
# as these are for illustration purposes only
encode(arr, compression_ratios=[2, 4, 6])
```

Lossy encoding of a monochrome image using peak signal-to-noise ratios:

```python

import numpy as np
from openjpeg import encode

arr = np.random.randint(low=-2**15, high=2**15 - 1, size=(100, 100), dtype="int8")
# You must determine your own values for `signal_noise_ratios`
# as these are for illustration purposes only
encode(arr, signal_noise_ratios=[50, 80, 100])
```

See the docstring for the [encode() function][2] for full details.

[2]: https://github.com/pydicom/pylibjpeg-openjpeg/blob/main/openjpeg/utils.py#L428
2 changes: 2 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def get_source_files() -> List[Path]:
"""Return a list of paths to the source files to be compiled."""
source_files = [
INTERFACE_SRC / "decode.c",
INTERFACE_SRC / "encode.c",
INTERFACE_SRC / "color.c",
INTERFACE_SRC / "utils.c",
]
for fname in OPENJPEG_SRC.glob("*"):
if fname.parts[-1].startswith("test"):
Expand Down
14 changes: 14 additions & 0 deletions docs/changes/v2.1.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.. _v2.1.0:

2.1.0
=====

Changes
.......

* Added support for encoding a numpy ndarray using JPEG2000 lossless and lossy
* Supported array shapes are (rows, columns) and (rows, columns, planes)
* Supported number of planes is 1, 3 and 4
* Supported dtypes are bool, u1, i1, u2, i2 for bit-depths 1-16
* Also supported are u4 and i4 for bit-depths 1-24
* Added support for decoding JPEG2000 data with precision up to 24-bits
Loading

0 comments on commit 419fcbc

Please sign in to comment.