Skip to content

Commit 419fcbc

Browse files
authored
Add support for encoding J2K (#78)
1 parent cdbd616 commit 419fcbc

15 files changed

+5413
-895
lines changed

README.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ python -m pip install pylibjpeg-openjpeg
4141
| [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/) |
4242

4343
#### Encoding
44-
Encoding of JPEG 2000 images is not currently supported
44+
45+
Encoding of NumPy ndarrays is supported for the following:
46+
47+
* Array dtype: bool, uint8, int8, uint16, int16, uint32 and int32 (1-24 bit-depth only)
48+
* Array shape: (rows, columns) and (rows, columns, planes)
49+
* Number of rows/columns: up to 65535
50+
* Number of planes: 1, 3 or 4
4551

4652

4753
### Transfer Syntaxes
@@ -81,3 +87,46 @@ with open('filename.j2k', 'rb') as f:
8187
# Or simply...
8288
arr = decode('filename.j2k')
8389
```
90+
91+
#### Standalone JPEG encoding
92+
93+
Lossless encoding of RGB with multiple-component transformation:
94+
95+
```python
96+
97+
import numpy as np
98+
from openjpeg import encode
99+
100+
arr = np.random.randint(low=0, high=65535, size=(100, 100, 3), dtype="uint8")
101+
encode(arr, photometric_interpretation=1) # 1: sRGB
102+
```
103+
104+
Lossy encoding of a monochrome image using compression ratios:
105+
106+
```python
107+
108+
import numpy as np
109+
from openjpeg import encode
110+
111+
arr = np.random.randint(low=-2**15, high=2**15 - 1, size=(100, 100), dtype="int8")
112+
# You must determine your own values for `compression_ratios`
113+
# as these are for illustration purposes only
114+
encode(arr, compression_ratios=[2, 4, 6])
115+
```
116+
117+
Lossy encoding of a monochrome image using peak signal-to-noise ratios:
118+
119+
```python
120+
121+
import numpy as np
122+
from openjpeg import encode
123+
124+
arr = np.random.randint(low=-2**15, high=2**15 - 1, size=(100, 100), dtype="int8")
125+
# You must determine your own values for `signal_noise_ratios`
126+
# as these are for illustration purposes only
127+
encode(arr, signal_noise_ratios=[50, 80, 100])
128+
```
129+
130+
See the docstring for the [encode() function][2] for full details.
131+
132+
[2]: https://github.com/pydicom/pylibjpeg-openjpeg/blob/main/openjpeg/utils.py#L428

build.py

+2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ def get_source_files() -> List[Path]:
6363
"""Return a list of paths to the source files to be compiled."""
6464
source_files = [
6565
INTERFACE_SRC / "decode.c",
66+
INTERFACE_SRC / "encode.c",
6667
INTERFACE_SRC / "color.c",
68+
INTERFACE_SRC / "utils.c",
6769
]
6870
for fname in OPENJPEG_SRC.glob("*"):
6971
if fname.parts[-1].startswith("test"):

docs/changes/v2.1.0.rst

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.. _v2.1.0:
2+
3+
2.1.0
4+
=====
5+
6+
Changes
7+
.......
8+
9+
* Added support for encoding a numpy ndarray using JPEG2000 lossless and lossy
10+
* Supported array shapes are (rows, columns) and (rows, columns, planes)
11+
* Supported number of planes is 1, 3 and 4
12+
* Supported dtypes are bool, u1, i1, u2, i2 for bit-depths 1-16
13+
* Also supported are u4 and i4 for bit-depths 1-24
14+
* Added support for decoding JPEG2000 data with precision up to 24-bits

0 commit comments

Comments
 (0)