Repository navigation
Conversation
|
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/ Thoughts? |
|
Hi Francesc, this is a good idea. Which of these two options do you think would be better: having the 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, |
Yeah, I think having two separate plugins might make more sense precisely because of the next question.
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 |
|
Dear Francesc, Following your suggestion, I have prepared two separate plugins:
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:
the codec names describe the codestream family, while the actual codec implementation can select among available backends:
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:
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 |
|
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. |
|
BTW, as far as I know, grok also supports HTJ2K (via |
|
Hi Francesc, |
|
Hi Francesc, https://github.com/alemirone/blosc2_j2k and there are small python tests and even a C++ example which writes and read transparently through the plugins |
|
Hi Alessandro. Ok, I think we can can go with: 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 |
|
Hi Francesc, I opened the upstream
On the plugin side I added both automatic and manual tests. Automatic tests include:
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:
At the moment, the only transitional limitation is that these tests still install slightly modified These workarounds are intended to be temporary and can be simplified once the registry ids are available through upstream Current plugin work lives here:
Best |
|
Closing this in favor of https://github.com/alemirone/blosc2_j2k and https://github.com/alemirone/blosc2_htj2k |
Add runtime J2K/HTJ2K backend plugins with simple HDF5 deployment
This PR adds a runtime backend plugin mechanism to
blosc2_grok, relative tod60087aa88a71c3557b4c4ad60a6b9442d7bc878.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:
With this setup, an application that only uses HDF5 can read datasets compressed with the
grokBlosc2 codec. HDF5 finds the Blosc2 HDF5 filter throughHDF5_PLUGIN_PATH; C-Blosc2 then loadslibblosc2_grok.sothrough the platform loader path;blosc2_grokfinds 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:
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
grokcodec. Insidelibblosc2_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_grokuses 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:
or, from C/C++:
Command-line and HDF5-only deployments can use environment variables instead:
If backend plugins are installed outside the default location, set:
Otherwise, the default plugin root is used automatically:
Runtime configuration model
The runtime configuration model separates four concerns:
Python example:
C/C++ example:
Named environment-variable example:
For custom plugin locations only:
Legacy environment variables are still supported:
Backend-selection priority is:
blosc2_grok.configure()orblosc2_grok_configure().BLOSC2_GROK_REPLACEMENT_DIR,BLOSC2_GROK_HTJ2K_REPLACEMENT_DIR.BLOSC2_GROK_J2K_BACKEND,BLOSC2_GROK_HTJ2K_BACKEND.libblosc2_grok.native Grok for J2K, no backend for HTJ2K.
Plugin-root resolution is independent from backend selection. The plugin root is resolved as:
plugin_path, if provided.BLOSC2_GROK_PLUGIN_PATH, if set.plugin_path, if present.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_pathwhen plugins are installed in the normal package layout.If
plugin_pathisNULL, empty, or omitted from Python,blosc2_grokautomatically searches the default plugin root installed next to the shared library:For a standard wheel or package installation, this means the common Python case can simply be:
and the common C/C++ case can be:
plugin_pathremains available for custom deployments, tests, or externally managed plugin directories.The same rule applies to named environment-variable configuration. If
BLOSC2_GROK_PLUGIN_PATHis not set, named backends are resolved relative to the default plugin root. For example:is enough for a standard install.
Set
BLOSC2_GROK_PLUGIN_PATHonly 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 callsblosc2_grok_configure().A typical manifest looks like:
Here,
"plugin_path": "plugins"is relative to the directory containinglibblosc2_grok, so it resolves to: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, andplugin_rootsincludes it. This makes: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_grokfrom Python and withoutLD_PRELOAD.The loading chain is intentionally simple:
HDF5_PLUGIN_PATHlets HDF5 find the Blosc2 HDF5 filter.grokcodec in the compressed chunk.libblosc2_grok.soas the codec library.libblosc2_grok.soreads its installed manifest next to the shared library.<libblosc2_grok directory>/plugins.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 insideblosc2_grok.For a normal install, an HDF5 reader can often be configured with only the HDF5 plugin path and the platform dynamic-library path:
No Python import is required in the reading application. No
LD_PRELOADis required in the normal case.If the installed manifest already describes the desired backend priority, no
BLOSC2_GROK_PLUGIN_PATHis needed either. The default plugin root is found relative tolibblosc2_grok.so.Example manifest for conservative default J2K and HTJ2K preference:
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:
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
grokcodec, andblosc2_grokselects the backend internally.Optional named overrides remain available:
For custom plugin locations only:
Legacy direct-directory variables remain supported:
Main features
J2K_CODEC_PLUGINinsrc/plugin/j2k_codec_api.h.HTJ2K_CODEC_PLUGINinsrc/plugin/htj2k_codec_api.h.dlopen/dlsym/dlcloseon Linux/macOS.LoadLibraryA/GetProcAddress/FreeLibraryon Windows.blosc2_groka shared library so external backend plugins can link to it.blosc2_grok/plugins/j2k/grokblosc2_grok/plugins/j2k/kakadublosc2_grok/plugins/htj2k/kakadublosc2_grok/plugins/htj2k/openhtj2klibblosc2_grokfor default plugin-root and backend-priority discovery.uint16API is detected by a real CMake compile/link probe.blosc2_grok.list_plugins()blosc2_grok.available_backends()blosc2_grok.diagnose()blosc2_grok.selftest()python -m blosc2_grok --list-pluginspython -m blosc2_grok --diagnosepython -m blosc2_grok --selftestblosc2_grok_list_plugins()blosc2_grok_diagnose()blosc2_grok_last_error()b2nd_deserialize_meta.Backend capabilities
plugins/j2k/grokplugins/htj2k/openhtj2kplugins/j2k/kakaduplugins/htj2k/kakaduThe replacement mechanism is backend-agnostic. The always-built
plugins/j2k/grokbackend 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
uint16API. 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:
This gives native J2K plus:
Build with OpenHTJ2K:
Build with Kakadu, when local Kakadu libraries are available:
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:
On macOS use
DYLD_LIBRARY_PATH; on Windows add the backend DLL directories toPATH.Typical use cases
1. Standard open-source J2K usage
Users without Kakadu or OpenHTJ2K can continue to use
blosc2_groknormally for J2K. No backend configuration is required.2. Testing the plugin mechanism without external codecs
Use the always-built Grok replacement backend:
or, without code changes:
If the plugin directory is not the installed default root, also set:
3. HTJ2K through OpenHTJ2K
or, for HDF5-only / command-line deployments:
4. J2K or HTJ2K through Kakadu
or, for HDF5-only / command-line deployments:
HDF5 runtime setup
When reading or writing through HDF5, the HDF5 Blosc2 filter must be discoverable:
For Python applications, it is still fine to import
blosc2_grokand configure explicitly: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.sodirectly, provided that the platform dynamic loader can find it and any external backend libraries:For C/C++ hosts that can call an initialization function, the most explicit path is still:
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_PRELOADis only a fallback for unusual host/plugin loader setups where the standard dynamic-loader search path cannot exposelibblosc2_grok.soearly enough.Fallback example:
Direct HDF5 loading
Direct loading from the HDF5/Blosc2 path is supported without a Python-side preload step.
libblosc2_grok.soparses the small B2ND metalayer locally, so direct loading does not require the host process to export the external B2ND symbolb2nd_deserialize_meta. This makes the HDF5-only path robust: C-Blosc2 can load the codec library directly, andblosc2_grokcan then discover backend plugins from the installed manifest.A representative HDF5-only test is:
This succeeds without
import blosc2_grok, provided thatHDF5_PLUGIN_PATHpoints to the HDF5 Blosc2 filter and the platform dynamic-loader path can findlibblosc2_grok.soand any external backend libraries.Diagnostics
From Python:
From the command line:
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
libblosc2_grok.sodirectly, without requiring a prior Pythonimport blosc2_grok.libblosc2_grok.soavoids runtime dependence on the externalb2nd_deserialize_metasymbol.LD_PRELOADis a fallback for unusual loader setups, not the normal HDF5 recommendation.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
uint16data, a very small nominal quantization step such as2^-21can 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^-18when using the irreversible 9/7 path. The final compressed size is still controlled by the target layer size / compression ratio; thisQstepis only the quantization seed.Local validation on synthetic
uint16images showed no measurable precision loss between2^-21and2^-18for the tested target ratios, while2^-18preserved interoperability with Grok. A much coarser value such as2^-13was also tested and did visibly degrade smooth data, so it was not used.Tests
The PR adds tests for:
python -m blosc2_grok --list-plugins;python -m blosc2_grok --diagnose;BLOSC2_GROK_PLUGIN_PATH/configure();plugin_pathis omitted;uint16roundtrip;uint16roundtrip and lossy checks;uint16roundtrip and lossy checks.Additional local validation was done on synthetic
uint16images, including ramps, low-frequency images, mixed-frequency images, checkerboards, Poisson-like data, and noisy data. The Kakadu J2K2^-18quantization floor was compared against the previous nominal2^-21value 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
hdf5pluginwas imported. The codec was loaded through the HDF5/Blosc2 path, without an explicit Pythonimport blosc2_grok, and with eager binding enabled throughLD_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.soplus optional backend libraries such as OpenHTJ2K or Kakadu.Possible follow-up improvements are:
$ORIGIN/@loader_pathwhere appropriate;LD_PRELOADremains 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.