Skip to content

Add Runtime J2K/HTJ2K Backend Plugins for blosc2_grok - #21

Closed
alemirone wants to merge 17 commits into
Blosc:mainfrom
alemirone:branch_add_htj2k_plugin_backends_for_mr
Closed

alemirone wants to merge 17 commits into
Blosc:mainfrom
alemirone:branch_add_htj2k_plugin_backends_for_mr

Conversation

@alemirone

@alemirone alemirone commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Add runtime J2K/HTJ2K backend plugins with simple HDF5 deployment

This PR adds a runtime backend plugin mechanism to blosc2_grok, relative to d60087aa88a71c3557b4c4ad60a6b9442d7bc878.

The main goal is to keep the user-facing deployment simple.

For a standard HDF5 reader, the runtime can be configured with the usual HDF5 plugin path plus the platform dynamic-library path:

export HDF5_PLUGIN_PATH=/path/to/hdf5/plugins
export LD_LIBRARY_PATH=/path/to/site-packages/blosc2_grok:/opt/openhtj2k/lib:/opt/kakadu/lib:${LD_LIBRARY_PATH}

With this setup, an application that only uses HDF5 can read datasets compressed with the grok Blosc2 codec. HDF5 finds the Blosc2 HDF5 filter through HDF5_PLUGIN_PATH; C-Blosc2 then loads libblosc2_grok.so through the platform loader path; blosc2_grok finds its backend plugins through the installed manifest and the default plugin root next to the shared library.

In Python, a simple reader can import the HDF5 plugin package before opening the file:

import hdf5plugin
import h5py

with h5py.File("compressed_with_blosc2_grok.h5", "r") as f:
    data = f["entry_0000/measurement/data"][0]

For a non-Python HDF5 application, the same mechanism works once the HDF5 plugin path and dynamic-library path are set. No Python import is involved in that case.

The backend mechanism is independent of Blosc codec selection. Blosc still sees and loads the grok codec. Inside libblosc2_grok.so, the runtime selects the concrete JPEG2000 backend: native Grok, the Grok replacement plugin, Kakadu J2K/HTJ2K, or OpenHTJ2K HTJ2K.

The standard J2K path remains unchanged by default: if no replacement backend is selected, blosc2_grok uses its bundled Grok implementation for regular J2K. HTJ2K requires an HTJ2K backend such as OpenHTJ2K or Kakadu.

Applications that want explicit control can use the new configuration API:

import blosc2_grok

blosc2_grok.configure(htj2k_backend="openhtj2k")

or, from C/C++:

#include "blosc2_grok_public.h"

blosc2_grok_runtime_config cfg = {0};
cfg.struct_size = sizeof(cfg);
cfg.htj2k_backend = "openhtj2k";

if (blosc2_grok_configure(&cfg) != 0) {
    fprintf(stderr, "%s\n", blosc2_grok_last_error());
}

Command-line and HDF5-only deployments can use environment variables instead:

export BLOSC2_GROK_J2K_BACKEND=kakadu
export BLOSC2_GROK_HTJ2K_BACKEND=openhtj2k

If backend plugins are installed outside the default location, set:

export BLOSC2_GROK_PLUGIN_PATH=/custom/blosc2_grok/plugins

Otherwise, the default plugin root is used automatically:

<libblosc2_grok directory>/plugins

Runtime configuration model

The runtime configuration model separates four concerns:

  1. The core codec stays backend-agnostic. It only knows the small J2K/HTJ2K C plugin ABI.
  2. Applications can configure backend selection explicitly before first use.
  3. HDF5-only deployments can use the installed manifest and environment variables without Python import.
  4. Diagnostics can report exactly which plugin roots, backends, libraries, and loader errors are involved.

Python example:

import blosc2_grok

blosc2_grok.configure(
    j2k_backend="grok",
    htj2k_backend="openhtj2k",
)

C/C++ example:

#include "blosc2_grok_public.h"

blosc2_grok_runtime_config cfg = {0};
cfg.struct_size = sizeof(cfg);
cfg.j2k_backend = "grok";
cfg.htj2k_backend = "openhtj2k";

if (blosc2_grok_configure(&cfg) != 0) {
    fprintf(stderr, "%s\n", blosc2_grok_last_error());
}

Named environment-variable example:

export BLOSC2_GROK_J2K_BACKEND=grok
export BLOSC2_GROK_HTJ2K_BACKEND=openhtj2k

For custom plugin locations only:

export BLOSC2_GROK_PLUGIN_PATH=/opt/blosc2_grok/plugins
export BLOSC2_GROK_J2K_BACKEND=grok
export BLOSC2_GROK_HTJ2K_BACKEND=openhtj2k

Legacy environment variables are still supported:

export BLOSC2_GROK_REPLACEMENT_DIR=/opt/blosc2_grok/plugins/j2k/grok
export BLOSC2_GROK_HTJ2K_REPLACEMENT_DIR=/opt/blosc2_grok/plugins/htj2k/openhtj2k

Backend-selection priority is:

  1. Explicit API configuration:
    blosc2_grok.configure() or blosc2_grok_configure().
  2. Legacy direct-directory variables:
    BLOSC2_GROK_REPLACEMENT_DIR,
    BLOSC2_GROK_HTJ2K_REPLACEMENT_DIR.
  3. Named backend variables:
    BLOSC2_GROK_J2K_BACKEND,
    BLOSC2_GROK_HTJ2K_BACKEND.
  4. Installed manifest priority, read from the manifest next to libblosc2_grok.
  5. Built-in defaults:
    native Grok for J2K, no backend for HTJ2K.

Plugin-root resolution is independent from backend selection. The plugin root is resolved as:

  1. Explicit API plugin_path, if provided.
  2. BLOSC2_GROK_PLUGIN_PATH, if set.
  3. Manifest plugin_path, if present.
  4. Default plugin root next to the shared library.

Configuration is finalized on first codec use. A later attempt to reconfigure fails with a clear error.

Default plugin root and installed manifest

The runtime configuration API does not require callers to pass an explicit plugin_path when plugins are installed in the normal package layout.

If plugin_path is NULL, empty, or omitted from Python, blosc2_grok automatically searches the default plugin root installed next to the shared library:

<libblosc2_grok directory>/plugins

For a standard wheel or package installation, this means the common Python case can simply be:

import blosc2_grok

blosc2_grok.configure(htj2k_backend="openhtj2k")

and the common C/C++ case can be:

blosc2_grok_runtime_config cfg = {0};
cfg.struct_size = sizeof(cfg);
cfg.htj2k_backend = "openhtj2k";

if (blosc2_grok_configure(&cfg) != 0) {
    fprintf(stderr, "%s\n", blosc2_grok_last_error());
}

plugin_path remains available for custom deployments, tests, or externally managed plugin directories.

The same rule applies to named environment-variable configuration. If BLOSC2_GROK_PLUGIN_PATH is not set, named backends are resolved relative to the default plugin root. For example:

export BLOSC2_GROK_HTJ2K_BACKEND=openhtj2k

is enough for a standard install.

Set BLOSC2_GROK_PLUGIN_PATH only when plugins live outside the default installed location.

The package also installs a manifest next to libblosc2_grok. This manifest can define the default plugin root and backend priorities. This is useful for HDF5-only deployments where no application code calls blosc2_grok_configure().

A typical manifest looks like:

{
  "version": 1,
  "plugin_path": "plugins",
  "priority": {
    "j2k": ["native", "grok", "kakadu"],
    "htj2k": ["kakadu", "openhtj2k"]
  }
}

Here, "plugin_path": "plugins" is relative to the directory containing libblosc2_grok, so it resolves to:

<libblosc2_grok directory>/plugins

The priority lists are family-specific. For J2K, this example tries native Grok first, then the Grok replacement plugin, then Kakadu if available. For HTJ2K, it tries Kakadu first, then OpenHTJ2K if available. Missing or unloadable optional plugins are skipped; diagnostics report what was found and what failed to load.

Diagnostics report the implicit root through default_plugin_root, and plugin_roots includes it. This makes:

python -m blosc2_grok --diagnose

useful for checking exactly where the runtime is looking for plugins.

Simple HDF5 deployment model

A standard HDF5-only deployment can work without importing blosc2_grok from Python and without LD_PRELOAD.

The loading chain is intentionally simple:

  1. HDF5_PLUGIN_PATH lets HDF5 find the Blosc2 HDF5 filter.
  2. The Blosc2 filter sees the grok codec in the compressed chunk.
  3. C-Blosc2 loads libblosc2_grok.so as the codec library.
  4. libblosc2_grok.so reads its installed manifest next to the shared library.
  5. Backend plugins are discovered under the default plugin root:
    <libblosc2_grok directory>/plugins.
  6. The J2K/HTJ2K backend is selected from explicit API configuration, environment variables, or the installed manifest priority.

Blosc itself only selects the Blosc codec, here grok. The choice between native Grok, the Grok plugin, Kakadu J2K, Kakadu HTJ2K, or OpenHTJ2K is made inside blosc2_grok.

For a normal install, an HDF5 reader can often be configured with only the HDF5 plugin path and the platform dynamic-library path:

export HDF5_PLUGIN_PATH=/path/to/hdf5/plugins
export LD_LIBRARY_PATH=/path/to/site-packages/blosc2_grok:/opt/openhtj2k/lib:/opt/kakadu/lib:${LD_LIBRARY_PATH}

No Python import is required in the reading application. No LD_PRELOAD is required in the normal case.

If the installed manifest already describes the desired backend priority, no BLOSC2_GROK_PLUGIN_PATH is needed either. The default plugin root is found relative to libblosc2_grok.so.

Example manifest for conservative default J2K and HTJ2K preference:

{
  "version": 1,
  "plugin_path": "plugins",
  "priority": {
    "j2k": ["native", "grok", "kakadu"],
    "htj2k": ["kakadu", "openhtj2k"]
  }
}

With this manifest, regular J2K uses the bundled native Grok path first. HTJ2K uses Kakadu if available, otherwise OpenHTJ2K if available.

Example manifest for preferring Kakadu for both J2K and HTJ2K:

{
  "version": 1,
  "plugin_path": "plugins",
  "priority": {
    "j2k": ["kakadu", "native", "grok"],
    "htj2k": ["kakadu", "openhtj2k"]
  }
}

With this manifest, J2K tries Kakadu first. If the Kakadu plugin or its dependent libraries are not loadable, the runtime falls back to native Grok and then to the Grok replacement plugin. HTJ2K tries Kakadu first and then OpenHTJ2K. No Blosc-side configuration is needed for this: Blosc loads the grok codec, and blosc2_grok selects the backend internally.

Optional named overrides remain available:

export BLOSC2_GROK_J2K_BACKEND=grok
export BLOSC2_GROK_HTJ2K_BACKEND=openhtj2k

For custom plugin locations only:

export BLOSC2_GROK_PLUGIN_PATH=/custom/blosc2_grok/plugins

Legacy direct-directory variables remain supported:

export BLOSC2_GROK_REPLACEMENT_DIR=/custom/blosc2_grok/plugins/j2k/grok
export BLOSC2_GROK_HTJ2K_REPLACEMENT_DIR=/custom/blosc2_grok/plugins/htj2k/openhtj2k

Main features

  • Adds runtime backend plugin loading for JPEG2000 codecs.
  • Splits the plugin ABI by codestream family:
    • J2K_CODEC_PLUGIN in src/plugin/j2k_codec_api.h.
    • HTJ2K_CODEC_PLUGIN in src/plugin/htj2k_codec_api.h.
  • Adds ABI validation through version and struct-size checks.
  • Adds cross-platform dynamic loading:
    • dlopen / dlsym / dlclose on Linux/macOS.
    • LoadLibraryA / GetProcAddress / FreeLibrary on Windows.
  • Makes blosc2_grok a shared library so external backend plugins can link to it.
  • Installs plugins under family-specific directories:
    • blosc2_grok/plugins/j2k/grok
    • blosc2_grok/plugins/j2k/kakadu
    • blosc2_grok/plugins/htj2k/kakadu
    • blosc2_grok/plugins/htj2k/openhtj2k
  • Adds an installed manifest next to libblosc2_grok for default plugin-root and backend-priority discovery.
  • Adds a reference Grok J2K backend, always built, so the plugin mechanism can be tested without any external codec.
  • Adds optional Kakadu J2K and HTJ2K backends when Kakadu headers/libraries are available.
  • Adds optional OpenHTJ2K HTJ2K backend when a PR #190-style uint16 API is detected by a real CMake compile/link probe.
  • Adds codestream detection to route decode requests to J2K or HTJ2K backends.
  • Allows J2K and HTJ2K backends to be selected independently in the same process.
  • Adds clear failure for HTJ2K when no HTJ2K backend is configured.
  • Adds runtime diagnostics and self-test:
    • blosc2_grok.list_plugins()
    • blosc2_grok.available_backends()
    • blosc2_grok.diagnose()
    • blosc2_grok.selftest()
    • python -m blosc2_grok --list-plugins
    • python -m blosc2_grok --diagnose
    • python -m blosc2_grok --selftest
  • Adds C diagnostics APIs returning JSON text:
    • blosc2_grok_list_plugins()
    • blosc2_grok_diagnose()
    • blosc2_grok_last_error()
  • Keeps Kakadu and OpenHTJ2K out of the core codec: the core only knows the plugin ABI.
  • Avoids runtime dependence on the external B2ND symbol b2nd_deserialize_meta.
  • Refactors internal responsibilities:
    • runtime configuration
    • plugin discovery/loading
    • codestream detection
    • B2ND layout extraction
    • codec request construction
    • J2K/HTJ2K dispatch paths

Backend capabilities

Backend Built when J2K HTJ2K uint8 uint16
native Grok always yes no yes J2K only
plugins/j2k/grok always yes no yes J2K only
plugins/htj2k/openhtj2k OpenHTJ2K PR #190 API found no yes yes yes
plugins/j2k/kakadu Kakadu found yes no yes yes
plugins/htj2k/kakadu Kakadu found no yes yes yes

The replacement mechanism is backend-agnostic. The always-built plugins/j2k/grok backend is intentionally idempotent with the native Grok path: it uses Grok again, but through the plugin ABI. Its purpose is to demonstrate and test the mechanism even when no external codec library is available.

OpenHTJ2K is provided as a practical redistributable HTJ2K plugin example, using the PR #190-style uint16 API. Kakadu plugins are also provided when Kakadu headers and libraries are available locally. Kakadu is not bundled and remains an externally supplied dependency.

Installation and backend builds

Minimal build without optional backends:

python -m pip install .

This gives native J2K plus:

blosc2_grok/plugins/j2k/grok

Build with OpenHTJ2K:

export OPENHTJ2K_ROOT=/opt/openhtj2k
export OPENHTJ2K_INCLUDE_DIR=/opt/openhtj2k/include/open_htj2k/interface
export OPENHTJ2K_LIBRARY_DIR=/opt/openhtj2k/lib

CMAKE_ARGS="-DOPENHTJ2K_ROOT=${OPENHTJ2K_ROOT} -DOPENHTJ2K_INCLUDE_DIR=${OPENHTJ2K_INCLUDE_DIR} -DOPENHTJ2K_LIBRARY_DIR=${OPENHTJ2K_LIBRARY_DIR}" \
  python -m pip install -v --no-build-isolation --force-reinstall .

Build with Kakadu, when local Kakadu libraries are available:

export KAKADU_ROOT=/opt/kakadu
export KAKADU_INCLUDE_DIR=/opt/kakadu/managed/all_includes
export KAKADU_LIBRARY_DIR=/opt/kakadu/lib

CMAKE_ARGS="-DKAKADU_ROOT=${KAKADU_ROOT} -DKAKADU_INCLUDE_DIR=${KAKADU_INCLUDE_DIR} -DKAKADU_LIBRARY_DIR=${KAKADU_LIBRARY_DIR}" \
  python -m pip install -v --no-build-isolation --force-reinstall .

Both optional backend families can be enabled in the same build by passing both sets of CMake variables.

At runtime, dependent libraries must be visible to the platform dynamic loader:

export LD_LIBRARY_PATH=/opt/kakadu/lib:/opt/openhtj2k/lib:${LD_LIBRARY_PATH}

On macOS use DYLD_LIBRARY_PATH; on Windows add the backend DLL directories to PATH.

Typical use cases

1. Standard open-source J2K usage

Users without Kakadu or OpenHTJ2K can continue to use blosc2_grok normally for J2K. No backend configuration is required.

2. Testing the plugin mechanism without external codecs

Use the always-built Grok replacement backend:

import blosc2_grok

blosc2_grok.configure(j2k_backend="grok")

or, without code changes:

export BLOSC2_GROK_J2K_BACKEND=grok

If the plugin directory is not the installed default root, also set:

export BLOSC2_GROK_PLUGIN_PATH=/path/to/blosc2_grok/plugins

3. HTJ2K through OpenHTJ2K

import blosc2_grok

blosc2_grok.configure(htj2k_backend="openhtj2k")

or, for HDF5-only / command-line deployments:

export BLOSC2_GROK_HTJ2K_BACKEND=openhtj2k

4. J2K or HTJ2K through Kakadu

import blosc2_grok

blosc2_grok.configure(
    j2k_backend="kakadu",
    htj2k_backend="kakadu",
)

or, for HDF5-only / command-line deployments:

export BLOSC2_GROK_J2K_BACKEND=kakadu
export BLOSC2_GROK_HTJ2K_BACKEND=kakadu

HDF5 runtime setup

When reading or writing through HDF5, the HDF5 Blosc2 filter must be discoverable:

export HDF5_PLUGIN_PATH=/path/to/hdf5plugin/plugins

For Python applications, it is still fine to import blosc2_grok and configure explicitly:

import blosc2_grok
import hdf5plugin
import h5py

blosc2_grok.configure(htj2k_backend="openhtj2k")

However, explicit Python import is not required just to make the codec library loadable. In an HDF5-only path, C-Blosc2 can load libblosc2_grok.so directly, provided that the platform dynamic loader can find it and any external backend libraries:

export HDF5_PLUGIN_PATH=/path/to/hdf5/plugins
export LD_LIBRARY_PATH=/path/to/site-packages/blosc2_grok:/opt/openhtj2k/lib:${LD_LIBRARY_PATH}

For C/C++ hosts that can call an initialization function, the most explicit path is still:

blosc2_grok_runtime_config cfg = {0};
cfg.struct_size = sizeof(cfg);
cfg.htj2k_backend = "openhtj2k";

if (blosc2_grok_configure(&cfg) != 0) {
    fprintf(stderr, "%s\n", blosc2_grok_last_error());
}

For command-line tools or viewers where no explicit initialization hook exists, the manifest and environment-variable path is enough in the normal case.

LD_PRELOAD is only a fallback for unusual host/plugin loader setups where the standard dynamic-loader search path cannot expose libblosc2_grok.so early enough.

Fallback example:

export BLOSC2_GROK_LIBRARY=/path/to/site-packages/blosc2_grok/libblosc2_grok.so
export LD_PRELOAD="${BLOSC2_GROK_LIBRARY}${LD_PRELOAD:+:$LD_PRELOAD}"

Direct HDF5 loading

Direct loading from the HDF5/Blosc2 path is supported without a Python-side preload step.

libblosc2_grok.so parses the small B2ND metalayer locally, so direct loading does not require the host process to export the external B2ND symbol b2nd_deserialize_meta. This makes the HDF5-only path robust: C-Blosc2 can load the codec library directly, and blosc2_grok can then discover backend plugins from the installed manifest.

A representative HDF5-only test is:

BLOSC_TRACE=1 BLOSC2_GROK_DEBUG=1 LD_BIND_NOW=1 python - <<'PY'
import hdf5plugin
from h5py import File

f = File("compressed_with_blosc2_grok.h5", "r")
d = f["entry_0000/measurement/data"]
print("dataset opened")
print(d[0].shape)
PY

This succeeds without import blosc2_grok, provided that HDF5_PLUGIN_PATH points to the HDF5 Blosc2 filter and the platform dynamic-loader path can find libblosc2_grok.so and any external backend libraries.

Diagnostics

From Python:

import blosc2_grok

print(blosc2_grok.available_backends())
print(blosc2_grok.list_plugins())
print(blosc2_grok.diagnose())
print(blosc2_grok.selftest())

From the command line:

python -m blosc2_grok --list-plugins
python -m blosc2_grok --diagnose
python -m blosc2_grok --selftest

The diagnostic JSON reports plugin roots, library paths, selected backends, loadability, ABI validity, dependent-loader errors, manifest status, default plugin root, and relevant environment variables.

Problems addressed

  • The core library does not need to know about Kakadu/OpenHTJ2K directly.
  • HTJ2K is handled by explicit HTJ2K backends instead of the regular J2K native path.
  • J2K and HTJ2K replacement backends are installed in family-specific directories.
  • J2K and HTJ2K backend selection are independent and can coexist in the same process.
  • Plugin ABI mismatches are rejected explicitly.
  • Runtime configuration is deterministic and can be inspected.
  • Lazy plugin initialization is protected by mutexes.
  • HDF5-only use cases are supported: C-Blosc2 can load libblosc2_grok.so directly, without requiring a prior Python import blosc2_grok.
  • libblosc2_grok.so avoids runtime dependence on the external b2nd_deserialize_meta symbol.
  • The installed manifest and default plugin root make standard deployments work without hard-coded plugin directories.
  • LD_PRELOAD is a fallback for unusual loader setups, not the normal HDF5 recommendation.
  • CI wheel builds are configured with CMAKE_BUILD_PARALLEL_LEVEL=10.

Quantization precision note

One practical compatibility issue was found with J2K lossy streams produced through the Kakadu backend and later decoded with Grok.

For uint16 data, a very small nominal quantization step such as 2^-21 can produce valid Kakadu 9/7 codestreams, but those streams may be rejected by the bundled Grok decoder because they exceed its practical Tier-1 precision/bit-plane limits.

To keep the Kakadu J2K lossy output Grok-readable, this PR keeps the default Kakadu quantization seed no smaller than 2^-18 when using the irreversible 9/7 path. The final compressed size is still controlled by the target layer size / compression ratio; this Qstep is only the quantization seed.

Local validation on synthetic uint16 images showed no measurable precision loss between 2^-21 and 2^-18 for the tested target ratios, while 2^-18 preserved interoperability with Grok. A much coarser value such as 2^-13 was also tested and did visibly degrade smooth data, so it was not used.

Tests

The PR adds tests for:

  • native J2K Grok roundtrip without replacement backend;
  • Python plugin listing, backend availability, diagnostics and self-test;
  • python -m blosc2_grok --list-plugins;
  • python -m blosc2_grok --diagnose;
  • J2K replacement through the Grok test backend;
  • named backend selection through BLOSC2_GROK_PLUGIN_PATH / configure();
  • default plugin-root discovery when plugin_path is omitted;
  • manifest-driven backend priority;
  • failure when reconfiguring after first codec use;
  • HTJ2K failure when no HTJ2K backend is configured;
  • unknown codestream rejection;
  • optional Kakadu J2K uint16 roundtrip;
  • optional Kakadu HTJ2K uint16 roundtrip and lossy checks;
  • optional OpenHTJ2K HTJ2K uint16 roundtrip and lossy checks.

Additional local validation was done on synthetic uint16 images, including ramps, low-frequency images, mixed-frequency images, checkerboards, Poisson-like data, and noisy data. The Kakadu J2K 2^-18 quantization floor was compared against the previous nominal 2^-21 value and showed no measurable precision loss in those tests, while preserving Grok readability.

Additional HDF5 loader validation was done with direct HDF5 reads where only hdf5plugin was imported. The codec was loaded through the HDF5/Blosc2 path, without an explicit Python import blosc2_grok, and with eager binding enabled through LD_BIND_NOW=1.

Future improvements

This PR adds an explicit and inspectable blosc2_grok-side runtime configuration API, an installed manifest, default plugin-root discovery, and robust direct loading from HDF5/Blosc2.

The remaining dynamic-library-path setup is mostly about external native dependencies: HDF5 must find its filter, and the platform loader must find libblosc2_grok.so plus optional backend libraries such as OpenHTJ2K or Kakadu.

Possible follow-up improvements are:

  • using install-time RPATH/RUNPATH with $ORIGIN / @loader_path where appropriate;
  • improving packaging for redistributable backends such as OpenHTJ2K;
  • adding a helper command that prints shell-ready runtime environment exports;
  • keeping non-redistributable backends such as Kakadu as externally supplied plugins.

LD_PRELOAD remains a fallback for host applications with unusual loader behavior or no practical way to expose the codec library through the standard dynamic-loader search path.

@alemirone
alemirone marked this pull request as draft May 13, 2026 05:52
@alemirone
alemirone marked this pull request as ready for review May 13, 2026 17:37
@FrancescAlted

Copy link
Copy Markdown
Member

Hi Alessandro. This is a well thought addition, but I think there are too many moving parts going on here. My main worry is that, by introducing support for new J2K codecs (Kakadu and OpenHTJ2K) in an already existing plugin (grok), that would create incompatibilities between them (testing all possible combinations of compressing with one codec, decompressing with another, and for a variety of images, is quite a big task if one wants to be sure of all possible paths working correctly).

What I suggest instead is to create a new plugin (e.g. blosc2_j2k) where you implement essentially the same machinery as here, and then, we will assign a new ID for it. That would allow for much better testing among the community without interfering with current blosc2_grok, and hence, avoiding issues with users that are already using it.

More info on how to create new plugins here: https://blosc.org/posts/dynamic-plugins/
Also, see this presentation: https://blosc.org/docs/PyData-Global-2023-Blosc2.pdf

Thoughts?

@alemirone

Copy link
Copy Markdown
Contributor Author

Hi Francesc,

this is a good idea. Which of these two options do you think would be better: having the blosc2_j2k codec handle both J2K and HTJ2K according to the compression request when writing, and according to the HDF5 content when reading, or instead having two separate plugins: blosc2_j2k and blosc2_htj2k?

Concerning the open-source fallback, would it be okay to use a Grok plugin for J2K and an OpenHTJ2K one for HTJ2K, so that everything remains usable even without the proprietary Kakadu library?

Best regards,
Alessandro

@FrancescAlted

Copy link
Copy Markdown
Member

Hi Francesc,

this is a good idea. Which of these two options do you think would be better: having the blosc2_j2k codec handle both J2K and HTJ2K according to the compression request when writing, and according to the HDF5 content when reading, or instead having two separate plugins: blosc2_j2k and blosc2_htj2k?

Yeah, I think having two separate plugins might make more sense precisely because of the next question.

Concerning the open-source fallback, would it be okay to use a Grok plugin for J2K and an OpenHTJ2K one for HTJ2K, so that everything remains usable even without the proprietary Kakadu library?

So if you find Grok codec is best for J2K and OpenHTJ2K is best for HTJ2K, this is another reason for keeping two separate plugins because this will easy the interoperability logic (less possible combinations) between codecs in the same plugin, IMO.

Best

@alemirone

Copy link
Copy Markdown
Contributor Author

Dear Francesc,

Following your suggestion, I have prepared two separate plugins:

  • blosc2_j2k
  • blosc2_htj2k

I am currently testing them with C-Blosc2 using temporary local registration IDs, so that we can validate the design before touching the official codec registry.

For an official integration, would it be OK if I prepare a small PR to c-blosc2 registering two official codec IDs? Following your suggestion they would be:

  • BLOSC_CODEC_J2K
  • BLOSC_CODEC_HTJ2K

the codec names describe the codestream family, while the actual codec implementation can select among available backends:

  • J2K: Grok by default, optionally Kakadu when available locally, or other custom codec.
  • HTJ2K: OpenHTJ2K by default, optionally Kakadu when available locally, or other custom codecs.

If this direction sounds reasonable, I can keep the two plugin repositories separate and prepare them as standard dynamic Blosc2 plugins. Ideally, they could eventually live under the Blosc organization as:

  • Blosc/blosc2_j2k
  • Blosc/blosc2_htj2k

For now, I can keep developing and testing them under my GitHub account and transfer them later if/when the design is accepted.

Best,

Alessandro

@FrancescAlted

Copy link
Copy Markdown
Member

Yes, your suggestions sound very good indeed. Go ahead and when plugins would be ready for public use, we will assing a couple of IDs, one for BLOSC_CODEC_J2K and another for BLOSC_CODEC_HTJ2K. Then I am fine with transferring the repos to Blosc/blosc2_j2k and Blosc/blosc2_htj2k; I'll give you admin rights there too.

@FrancescAlted

Copy link
Copy Markdown
Member

BTW, as far as I know, grok also supports HTJ2K (via blosc2_grok.set_params_defaults(mode=blosc2_grok.GrkMode.HT)). I am curious on why you prefer using just OpenHTJ2K for future blosc2_htj2k. Is because of simplicity? If only for that, that already makes sense.

@alemirone

Copy link
Copy Markdown
Contributor Author

Hi Francesc,
it went for openhtj2k after you told me of PR190 in OpenHtj2k. Now that you tell me I will readd the grok plugin also for htj2k
Best regards
Alessandro

@alemirone

Copy link
Copy Markdown
Contributor Author

Hi Francesc,
the plugins can already be tested and might be almost ready for a PR.
See the "Copy-past full source quickstart" block from the READMEs of

https://github.com/alemirone/blosc2_j2k
https://github.com/alemirone/blosc2_htj2k

and there are small python tests and even a C++ example which writes and read transparently through the plugins
Best regards
Alessandro

@FrancescAlted

Copy link
Copy Markdown
Member

Hi Alessandro. Ok, I think we can can go with:

BLOSC_CODEC_J2K = 39
BLOSC_CODEC_HTJ2K = 40

Tests plugins with those ID's with a patched C-Blosc2 library (see https://github.com/Blosc/c-blosc2/blob/main/plugins/README.md). If tests are fine, please send a PR for the C-Blosc2 repo for further discussion.

It would be nice if you can come with a series of tests in blosc2_j2k and blosc2_htj2k that guarantee that plugins are working as intended. Thanks!

@alemirone

Copy link
Copy Markdown
Contributor Author

Hi Francesc,

I opened the upstream c-blosc2 PR for the registry part:

  • BLOSC_CODEC_J2K = 39
  • BLOSC_CODEC_HTJ2K = 40

Blosc/c-blosc2#777

On the plugin side I added both automatic and manual tests.

Automatic tests include:

  • registry-aware checks for the global codec ids
  • real roundtrip tests for compression/decompression
  • small HDF5 end-to-end checks using numeric Blosc2 filter options

In addition, both plugin repos also contain manual copy-paste runnable checks in the README / quickstart material, so that the codec behavior can be reproduced explicitly outside CI as well. These manual checks cover the same general areas:

  • plugin registration and backend selection
  • in-memory roundtrip compression/decompression
  • small HDF5 roundtrip examples using Blosc2 numeric filter options

At the moment, the only transitional limitation is that these tests still install slightly modified c-blosc2 / python-blosc2 runtimes in order to expose the new ids during CI. I also had to keep a few temporary compatibility workarounds in the plugin repos so that their standard CI keeps working before the new ids are fully upstream.

These workarounds are intended to be temporary and can be simplified once the registry ids are available through upstream c-blosc2 / python-blosc2.

Current plugin work lives here:

Best

@FrancescAlted

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants