Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/interface/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ extern int EncodeArray(
PyObject *signal_noise_ratios,
int codec_format,
int add_tlm,
int add_plt
int add_plt,
int transformation_type
)
{
/* Encode a numpy ndarray using JPEG 2000.
Expand Down Expand Up @@ -70,6 +71,10 @@ extern int EncodeArray(
Add tile-part data length markers (TLM). Supported values 0-1.
add_plt : int
Add packet length tile-part header markers (PLT). Supported values 0-1.
transformation_type: int
Set the transformation type. 0 = 5-3 reversible, 1 = 9-7 irreversible,
-1 = automatically determined from the compression_ratios or
signal_noise_ratios argument

Returns
-------
Expand Down Expand Up @@ -246,6 +251,7 @@ extern int EncodeArray(
if (nr_cr_layers > 0 || nr_snr_layers > 0) {
// Lossy compression using compression ratios
// For 1 quality layer we use reversible if CR is 1 or PSNR is 0
// unless overridden by the caller
parameters.irreversible = 1; // use DWT 9-7
if (nr_cr_layers > 0) {
if (nr_cr_layers > 100) {
Expand Down Expand Up @@ -307,6 +313,10 @@ extern int EncodeArray(
);
}
}
if (transformation_type != -1) {
// Explicit requirement
parameters.irreversible = transformation_type;
}

py_debug("Input validation complete, setting up for encoding");

Expand Down Expand Up @@ -516,7 +526,8 @@ extern int EncodeBuffer(
PyObject *signal_noise_ratios,
int codec_format,
int add_tlm,
int add_plt
int add_plt,
int transformation_type
)
{
/* Encode image data using JPEG 2000.
Expand Down Expand Up @@ -544,10 +555,10 @@ extern int EncodeBuffer(
use_mct : int
Supported values 0-1, can't be used with subsampling
compression_ratios : list[float]
Encode lossy with the specified compression ratio for each quality
Encode with the specified compression ratio for each quality
layer. The ratio should be decreasing with increasing layer.
signal_noise_ratios : list[float]
Encode lossy with the specified peak signal-to-noise ratio for each
Encode with the specified peak signal-to-noise ratio for each
quality layer. The ratio should be increasing with increasing layer.
codec_format : int
The format of the encoded JPEG 2000 data, one of:
Expand All @@ -557,6 +568,10 @@ extern int EncodeBuffer(
Add tile-part data length markers (TLM). Supported values 0-1.
add_plt : int
Add packet length tile-part header markers (PLT). Supported values 0-1.
transformation_type: int
Set the transformation type. 0 = 5-3 reversible, 1 = 9-7 irreversible,
-1 = automatically determined from the compression_ratios or
signal_noise_ratios argument

Returns
-------
Expand Down
20 changes: 20 additions & 0 deletions openjpeg/_openjpeg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cdef extern int EncodeArray(
int codec_format,
bint add_tlm,
bint add_plt,
int transformation_type,
)
cdef extern int EncodeBuffer(
PyObject* src,
Expand All @@ -50,6 +51,7 @@ cdef extern int EncodeBuffer(
int codec_format,
bint add_tlm,
bint add_plt,
int transformation_type,
)


Expand Down Expand Up @@ -219,6 +221,7 @@ def encode_array(
int codec_format,
bint add_tlm,
bint add_plt,
int transformation_type,
) -> Tuple[int, bytes]:
"""Return the JPEG 2000 compressed `arr`.

Expand Down Expand Up @@ -249,6 +252,10 @@ def encode_array(
If ``True`` then add tile-part length markers (TLM) to the codestream.
add_plt : bool
If ``True`` then add packet length tile-part header markers (PLT) to the codestream.
transformation_type: int
transformation type. 0 = 5-3 reversible, 1 = 9-7 irreversible,
-1 = automatically determined from the compression_ratios or signal_noise_ratios
argument

Returns
-------
Expand Down Expand Up @@ -330,6 +337,7 @@ def encode_array(
codec_format,
add_tlm,
add_plt,
transformation_type,
)
return return_code, dst.getvalue()

Expand All @@ -348,6 +356,7 @@ def encode_buffer(
int codec_format,
bint add_tlm,
bint add_plt,
int transformation_type,

) -> Tuple[int, bytes]:
"""Return the JPEG 2000 compressed `src`.
Expand Down Expand Up @@ -394,6 +403,10 @@ def encode_buffer(
If ``True`` then add tile-part length markers (TLM) to the codestream.
add_plt : bool
If ``True`` then add packet length tile-part header markers (PLT) to the codestream.
transformation_type: int
transformation type. 0 = 5-3 reversible, 1 = 9-7 irreversible,
-1 = automatically determined from the compression_ratios or signal_noise_ratios
argument

Returns
-------
Expand Down Expand Up @@ -471,6 +484,12 @@ def encode_buffer(
if len(compression_ratios) > 100 or len(signal_noise_ratios) > 100:
raise ValueError("More than 100 compression layers is not supported")

if transformation_type not in (-1, 0, 1):
raise ValueError(
f"Invalid 'transformation_type' value '{transformation_type}', must be 0, 1 "
"or -1"
)

dst = BytesIO()
return_code = EncodeBuffer(
<PyObject *> src,
Expand All @@ -487,5 +506,6 @@ def encode_buffer(
codec_format,
add_tlm,
add_plt,
transformation_type,
)
return return_code, dst.getvalue()
Loading
Loading