Expand description
Internal pure-Rust JPEG 2000 codec engine for j2k.
This module tree was imported from the dicom-toolkit-jpeg2000 0.5.0 crate
and adapted in-repo so j2k no longer depends on an external
production decoder crate.
dicom-toolkit-jpeg2000 is the JPEG 2000 engine used by dicom-toolkit-rs.
It is a maintained fork of the original hayro-jpeg2000 project with
DICOM-focused extensions, including native-bit-depth decode for 8/12/16-bit
images and pure-Rust JPEG 2000 encoding.
The crate can decode both raw JPEG 2000 codestreams (.j2c) and images wrapped
inside the JP2 container format. The decoder supports the vast majority of features
defined in the JPEG 2000 core coding system (ISO/IEC 15444-1) as well as some color
spaces from the extensions (ISO/IEC 15444-2). There are still some missing pieces
for some “obscure” features (for example support for progression order
changes in tile-parts), but the features that commonly appear in real-world
images are supported.
The crate offers both a high-level 8-bit decode path for general image use and a native-bit-depth decode path for integrations such as DICOM, plus encoder APIs for emitting raw JPEG 2000 and HTJ2K codestreams.
§Example
use j2k_native::{DecodeSettings, Image};
let data = std::fs::read("image.jp2").unwrap();
let image = Image::new(&data, &DecodeSettings::default()).unwrap();
println!(
"{}x{} image in {:?} with alpha={}",
image.width(),
image.height(),
image.color_space(),
image.has_alpha(),
);
let bitmap = image.decode().unwrap();If you want to see a more comprehensive example, please take a look at the example in GitHub, which shows the main steps needed to convert a JPEG 2000 image into PNG.
§Testing
The decoder has been tested against 20.000+ images scraped from random PDFs
on the internet and also passes a large part of the OpenJPEG test suite. So you
can expect the crate to perform decently in terms of decoding correctness.
§Performance
A decent amount of effort has already been put into optimizing this crate (both in terms of raw performance but also memory allocations). However, there are some more important optimizations that have not been implemented yet, so there is definitely still room for improvement (and I am planning on implementing them eventually).
Overall, you should expect this crate to have worse performance than OpenJPEG,
but the difference gap should not be too large.
§Safety
By default, the crate has the simd feature enabled, which uses the
fearless_simd crate to accelerate
important parts of the pipeline. If you want to eliminate any usage of unsafe
in this crate as well as its dependencies, you can simply disable this
feature, at the cost of worse decoding performance. Unsafe code is forbidden
via a crate-level attribute.
The crate is no_std compatible but requires an allocator to be available.
Re-exports§
pub use error::ColorError;pub use error::DecodeError;pub use error::DecodingError;pub use error::FormatError;pub use error::MarkerError;pub use error::Result;pub use error::TileError;pub use error::ValidationError;
Modules§
- error
- Error types for JPEG 2000 decoding.
Structs§
- Bitmap
- A bitmap storing the decoded result of the image.
- Component
Plane - A borrowed decoded component plane.
- CpuOnly
J2kEncode Stage Accelerator - Adapter CPU-only encode accelerator that always falls back to native stages.
- Decode
Settings - Settings to apply during decoding.
- Decoded
Components - Borrowed decoded component planes for an image.
- Decoder
Context - A decoder context for decoding JPEG2000 images.
- Encode
Options - Encoding options for JPEG 2000.
- Encoded
HtJ2k Code Block - Adapter encoded HTJ2K cleanup code-block payload for backend experimentation.
- Encoded
J2kCode Block - Adapter encoded classic J2K code-block payload for backend experimentation.
- HtCleanup
Encode Distribution - Adapter HTJ2K cleanup-encode shape counters for backend benchmarking.
- HtCode
Block Batch Job - Adapter HTJ2K batched code-block decode job for one sub-band.
- HtCode
Block Decode Job - Adapter HTJ2K code-block job description for backend experimentation.
- HtCode
Block Decode Profile - Adapter scalar HTJ2K phase timings for backend experimentation.
- HtCode
Block Decode Workspace - Adapter reusable scalar HTJ2K decode workspace for backend experimentation.
- HtOwned
Code Block Batch Job - Adapter owned HTJ2K batched code-block decode job.
- HtOwned
SubBand Plan - Adapter owned HTJ2K sub-band decode job.
- HtSig
Prop Benchmark State - Adapter HTJ2K SigProp benchmark state for backend experimentation.
- HtSub
Band Decode Job - Adapter HTJ2K batched sub-band decode request for backend experimentation.
- HtUvlc
Table Entry - Adapter HTJ2K UVLC encoder table row for backend experimentation.
- Image
- A JPEG2000 image or codestream.
- Irreversible
Quantization Step - Public JPEG 2000 irreversible quantization step-size tuple.
- Irreversible
Quantization Subband Scales - Multipliers applied to irreversible 9/7 quantization step sizes by subband.
- J2kCode
Block Batch Job - Adapter classic J2K batched code-block decode job for one sub-band.
- J2kCode
Block Decode Job - Adapter classic J2K code-block job description for backend experimentation.
- J2kCode
Block Decode Profile - Adapter scalar classic J2K pass timings for backend experimentation.
- J2kCode
Block Decode Workspace - Reusable scratch for scalar classic J2K code-block decoding.
- J2kCode
Block Segment - Adapter classic J2K coded segment for backend experimentation.
- J2kCode
Block Style - Adapter classic J2K code-block style for backend experimentation.
- J2kCodestream
Component Header - Parsed SIZ component metadata.
- J2kCodestream
Header Metadata - Parsed JPEG 2000 codestream metadata from the main header.
- J2kDeinterleave
ToF32 Job - Adapter pixel deinterleave/level-shift job for backend experimentation.
- J2kDirect
Color Plan - Adapter RGB direct device plan for backend experimentation.
- J2kDirect
CpuScratch - Adapter reusable scratch for executing direct J2K RGB plans on the CPU.
- J2kDirect
Grayscale Plan - Adapter grayscale-only direct device plan for backend experimentation.
- J2kDirect
Idwt Step - Adapter single grayscale IDWT step for a direct device plan.
- J2kDirect
Store Step - Adapter grayscale store step for a direct device plan.
- J2kEncode
Dispatch Report - Adapter encode-stage dispatch counters for backend experimentation.
- J2kForward
Dwt53 Job - Adapter forward 5/3 DWT job for backend experimentation.
- J2kForward
Dwt53 Level - Adapter forward 5/3 DWT detail level for backend experimentation.
- J2kForward
Dwt53 Output - Adapter forward 5/3 DWT output for backend experimentation.
- J2kForward
Dwt97 Job - Adapter forward irreversible 9/7 DWT job for backend experimentation.
- J2kForward
Dwt97 Level - Adapter forward 9/7 DWT detail level for backend experimentation.
- J2kForward
Dwt97 Output - Adapter forward 9/7 DWT output for backend experimentation.
- J2kForward
IctJob - Adapter forward ICT job for backend experimentation.
- J2kForward
RctJob - Adapter forward RCT job for backend experimentation.
- J2kHt
Code Block Encode Job - Adapter HTJ2K code-block encode job for backend experimentation.
- J2kHt
Subband Encode Job - Adapter HTJ2K cleanup encode job for one unquantized sub-band.
- J2kHtj2k
Tile Encode Job - Adapter HTJ2K tile-body encode job for backend-resident full-tile paths.
- J2kIdwt
Band - Adapter single sub-band payload for backend experimentation.
- J2kInverse
MctJob - Adapter inverse MCT job for backend experimentation.
- J2kOwned
Code Block Batch Job - Adapter owned classic J2K batched code-block decode job.
- J2kOwned
SubBand Plan - Adapter owned classic J2K sub-band decode job.
- J2kPacketization
Code Block - Adapter LRCP packetization code-block contribution for backend experimentation.
- J2kPacketization
Encode Job - Adapter LRCP packetization job for backend experimentation.
- J2kPacketization
Packet Descriptor - Adapter explicit packet descriptor for backend packetization experimentation.
- J2kPacketization
Resolution - Adapter LRCP packetization resolution packet for backend experimentation.
- J2kPacketization
Subband - Adapter LRCP packetization subband precinct for backend experimentation.
- J2kQuantize
Subband Job - Adapter sub-band quantization job for backend experimentation.
- J2kRect
- Adapter integer rectangle for backend experimentation.
- J2kSingle
Decomposition Idwt Job - Adapter single-decomposition IDWT job for backend experimentation.
- J2kStore
Component Job - Adapter component-store job for backend experimentation.
- J2kSub
Band Decode Job - Adapter classic J2K batched sub-band decode request for backend experimentation.
- J2kTier1
Code Block Encode Job - Adapter Tier-1 classic J2K code-block encode job for backend experimentation.
- J2kTier1
Token Segment - Adapter Classic Tier-1 compact token segment for backend experimentation.
- Precomputed
Htj2k53 Component - Precomputed reversible 5/3 wavelet coefficients for one component.
- Precomputed
Htj2k53 Image - Precomputed reversible 5/3 wavelet image.
- Precomputed
Htj2k97 Component - Precomputed irreversible 9/7 wavelet coefficients for one component.
- Precomputed
Htj2k97 Image - Precomputed irreversible 9/7 wavelet image.
- Preencoded
Htj2k97 Code Block - One preencoded HTJ2K code-block.
- Preencoded
Htj2k97 Compact Code Block - One compact preencoded HTJ2K code-block.
- Preencoded
Htj2k97 Compact Component - Preencoded compact irreversible 9/7 HTJ2K component.
- Preencoded
Htj2k97 Compact Image - Preencoded irreversible 9/7 HTJ2K code-block image backed by one compact payload buffer.
- Preencoded
Htj2k97 Compact Resolution - One component resolution’s compact preencoded HTJ2K subbands.
- Preencoded
Htj2k97 Compact Subband - One compact preencoded HTJ2K subband split into code-blocks.
- Preencoded
Htj2k97 Component - Preencoded irreversible 9/7 HTJ2K component.
- Preencoded
Htj2k97 Image - Preencoded irreversible 9/7 HTJ2K code-block image.
- Preencoded
Htj2k97 Resolution - One component resolution’s preencoded HTJ2K subbands.
- Preencoded
Htj2k97 Subband - One preencoded HTJ2K subband split into code-blocks.
- Prequantized
Htj2k97 Code Block - One prequantized HTJ2K code-block.
- Prequantized
Htj2k97 Component - Prequantized irreversible 9/7 HTJ2K component.
- Prequantized
Htj2k97 Image - Prequantized irreversible 9/7 HTJ2K code-block image.
- Prequantized
Htj2k97 Resolution - One component resolution’s prequantized HTJ2K subbands.
- Prequantized
Htj2k97 Subband - One prequantized HTJ2K subband split into code-blocks.
- RawBitmap
- Raw decoded pixel data at native bit depth (no 8-bit scaling).
- Reversible53
Coefficient Image - Reversible 5/3 source coefficients ready for HTJ2K code-block recoding.
Enums§
- Color
Space - The color space of the image.
- CpuDecode
Parallelism - CPU parallelism policy for native JPEG 2000 decode.
- Encode
Progression Order - JPEG 2000 packet progression orders supported by the encoder.
- HtCode
Block Decode Phase Limit - Adapter HTJ2K scalar decode phase limit for backend experimentation.
- J2kCodestream
Header Error - Error returned by
inspect_j2k_codestream_header. - J2kDirect
Grayscale Step - Adapter grayscale-only direct device-plan step for backend experimentation.
- J2kPacketization
Block Coding Mode - Adapter packetization block coding mode for backend experimentation.
- J2kPacketization
Progression Order - Adapter packet progression order for backend packetization experimentation.
- J2kSub
Band Type - Adapter classic J2K sub-band kind for backend experimentation.
- J2kWavelet
Transform - Adapter wavelet transform selector for backend experimentation.
Traits§
- HtCode
Block Decoder - Adapter HTJ2K code-block decode hook for backend experimentation.
- J2kEncode
Stage Accelerator - Adapter JPEG 2000 encode-stage accelerator for backend experimentation.
Functions§
- collect_
ht_ cleanup_ encode_ distribution - Adapter HTJ2K cleanup-encode distribution helper for benchmark tuning.
- decode_
ht_ code_ block_ scalar - Adapter scalar HTJ2K decoder helper for backend experimentation.
- decode_
ht_ code_ block_ scalar_ until_ phase - Adapter scalar HTJ2K decoder helper that stops after the selected phase.
- decode_
ht_ code_ block_ scalar_ with_ workspace - Adapter scalar HTJ2K decoder helper that reuses caller-owned scratch buffers.
- decode_
ht_ code_ block_ scalar_ with_ workspace_ profiled - Adapter scalar HTJ2K decoder helper that reuses scratch and records phase timings.
- decode_
ht_ sigprop_ benchmark_ state - Adapter helper that runs only the HTJ2K significance-propagation phase.
- decode_
j2k_ code_ block_ scalar - Adapter scalar classic J2K decoder helper for backend experimentation.
- decode_
j2k_ code_ block_ scalar_ profiled - Adapter scalar classic J2K decoder helper that records pass timings.
- decode_
j2k_ code_ block_ scalar_ with_ workspace - Adapter scalar classic J2K decoder helper that reuses caller-provided scratch.
- decode_
j2k_ code_ block_ scalar_ with_ workspace_ profiled - Adapter scalar classic J2K decoder helper that records pass timings and reuses scratch.
- decode_
j2k_ sub_ band_ scalar - Adapter scalar classic J2K batched decoder helper for backend experimentation.
- deinterleave_
reference - Adapter scalar pixel deinterleave/level-shift reference for CUDA stage parity.
- encode
- Encode pixel data into a JPEG 2000 codestream.
- encode_
ht_ code_ block_ scalar - Adapter scalar HTJ2K cleanup-only encoder helper for backend experimentation.
- encode_
htj2k - Encode pixel data into an HTJ2K codestream.
- encode_
j2k_ code_ block_ scalar_ with_ style - Adapter scalar classic J2K encoder helper for backend experimentation.
- encode_
j2k_ packetization_ scalar - Adapter scalar Tier-2 packetization helper for backend experimentation.
- encode_
precomputed_ htj2k_ 53 - Encode precomputed reversible 5/3 wavelet coefficients into an HTJ2K codestream.
- encode_
precomputed_ htj2k_ 97 - Encode precomputed irreversible 9/7 wavelet coefficients into an HTJ2K codestream.
- encode_
precomputed_ htj2k_ 53_ with_ accelerator - Encode precomputed reversible 5/3 wavelet coefficients into an HTJ2K codestream using optional block encode and packetization hooks.
- encode_
precomputed_ htj2k_ 53_ with_ mct - Encode precomputed reversible 5/3 wavelet coefficients into an HTJ2K codestream while controlling the output COD multi-component transform flag.
- encode_
precomputed_ htj2k_ 53_ with_ mct_ and_ accelerator - Encode precomputed reversible 5/3 wavelet coefficients while controlling the output COD multi-component transform flag and using optional encode stage hooks.
- encode_
precomputed_ htj2k_ 97_ batch_ with_ accelerator - Encode multiple precomputed irreversible 9/7 wavelet images while sharing one HT code-block batch across all prepared tiles.
- encode_
precomputed_ htj2k_ 97_ with_ accelerator - Encode precomputed irreversible 9/7 wavelet coefficients into an HTJ2K codestream using optional block encode and packetization hooks.
- encode_
preencoded_ htj2k_ 97 - Encode preencoded irreversible 9/7 HTJ2K code-block payloads into a codestream.
- encode_
preencoded_ htj2k_ 97_ compact_ owned_ with_ accelerator - Encode compact preencoded irreversible 9/7 HTJ2K code-block payloads into a codestream, borrowing code-block ranges from one image-level payload buffer during packetization.
- encode_
preencoded_ htj2k_ 97_ owned_ with_ accelerator - Encode preencoded irreversible 9/7 HTJ2K code-block payloads into a codestream, consuming the image so code-block payloads can move into packet preparation without cloning.
- encode_
preencoded_ htj2k_ 97_ with_ accelerator - Encode preencoded irreversible 9/7 HTJ2K code-block payloads into a codestream using optional packetization hooks.
- encode_
prequantized_ htj2k_ 97 - Encode prequantized irreversible 9/7 code-block coefficients into an HTJ2K codestream.
- encode_
prequantized_ htj2k_ 97_ with_ accelerator - Encode prequantized irreversible 9/7 code-block coefficients into an HTJ2K codestream using optional block encode and packetization hooks.
- encode_
with_ accelerator - Encode pixel data into a JPEG 2000 codestream using optional encode-stage hooks.
- execute_
direct_ color_ plan_ rgb8_ into - Execute a adapter direct RGB plan on the CPU and write an RGB8 output region.
- execute_
direct_ color_ plan_ rgba8_ into - Execute a adapter direct RGB plan on the CPU and write an RGBA8 output region.
- forward_
dwt53_ reference - Adapter scalar forward 5/3 DWT reference for CUDA stage parity.
- forward_
rct_ reference - Adapter scalar forward RCT reference for CUDA stage parity.
- ht_
uvlc_ encode_ table - Adapter HTJ2K cleanup encoder UVLC table for backend experimentation.
- ht_
uvlc_ encode_ table_ bytes - Adapter HTJ2K cleanup encoder UVLC table packed for byte-addressed backends.
- ht_
uvlc_ table0 - Adapter HTJ2K UVLC table 0 for backend experimentation.
- ht_
uvlc_ table1 - Adapter HTJ2K UVLC table 1 for backend experimentation.
- ht_
vlc_ encode_ table0 - Adapter HTJ2K cleanup encoder VLC table 0 for backend experimentation.
- ht_
vlc_ encode_ table1 - Adapter HTJ2K cleanup encoder VLC table 1 for backend experimentation.
- ht_
vlc_ table0 - Adapter HTJ2K VLC table 0 for backend experimentation.
- ht_
vlc_ table1 - Adapter HTJ2K VLC table 1 for backend experimentation.
- idwt_
band_ index - Maps an output coordinate within an IDWT step to the source sub-band index.
- inspect_
j2k_ codestream_ header - Inspect a raw JPEG 2000 codestream main header without decoding tile data.
- irreversible_
quantization_ step_ for_ subband - Compute the exact irreversible 9/7 quantization step tuple the native encoder writes for one subband under a global plus per-subband profile.
- looks_
like_ j2k_ codestream - Return whether bytes start with the raw JPEG 2000 SOC marker.
- pack_
j2k_ code_ block_ scalar_ from_ tier1_ tokens - Adapter scalar Classic Tier-1 compact token packer for backend experimentation.
- prepare_
ht_ sigprop_ benchmark_ state - Adapter helper that precomputes cleanup-derived SigProp inputs for benchmarks.
- quantize_
reversible_ reference - Adapter scalar reversible sub-band quantization reference for CUDA stage parity.
Type Aliases§
- J2kDirect
Band Id - Adapter identifier for one device-owned grayscale coefficient band.