# J2K: JPEG 2000 and HTJ2K in Rust

Canonical page: https://frames-sg.com/j2k/

J2K is an open-source Rust codec stack for JPEG 2000 Part 1 and HTJ2K Part 15. It provides safe public APIs for inspection, decoding, encoding, recoding, and supported coefficient-domain JPEG-to-HTJ2K transcode workflows. The portable CPU path is the correctness baseline. Apple Metal and NVIDIA CUDA accelerate supported image shapes and operations.

## Install

Most applications should use the public facade crate:

```sh
cargo add j2k
```

The command-line tool supports inspection and conservative transcode smoke tests:

```sh
cargo install j2k-cli
j2k inspect input.jp2
j2k transcode input.jpg output.j2k --htj2k --lossless-53
```

## Formats

- **JPEG 2000 Part 1:** Raw J2K/J2C codestream parsing, inspection, decoding, encoding, regions of interest, progression orders, tile parts, packet markers, signed and high-bit samples, and component-plane surfaces are tracked in the public support matrix.
- **HTJ2K Part 15:** HT cleanup and refinement decoding and encoding, quality layers, rate behavior, raw HT codestreams, and JPH still-image wrapping are in scope.
- **Still-image wrappers:** JP2 and JPH metadata are supported. JPX and JPEG 2000 Part 2 extensions are not supported except where ordinary JP2 or JPH decoding requires them.

The [public support matrix](https://github.com/frames-sg/j2k/blob/main/docs/public-support.md) is authoritative for exact feature coverage.

## GPU selection

`BackendRequest::Auto` may use the CPU when a GPU path is unavailable, unsupported, or not measured to be faster for the requested shape. Explicit Metal or CUDA requests return an error for unsupported shapes instead of silently switching to another device. GPU availability does not imply that every codec stage or image shape runs on the GPU.

## Package map

- `j2k`: Public facade for JPEG 2000 Part 1 and HTJ2K Part 15 inspection, decode, encode, recode, JP2, and JPH.
- `j2k-metal`: Apple Metal acceleration for supported macOS GPU routes.
- `j2k-cuda`: NVIDIA CUDA acceleration for supported JPEG 2000 and HTJ2K routes.
- `j2k-transcode`: JPEG-to-HTJ2K coefficient-domain transcode primitives and shared contracts.
- `j2k-jpeg`: JPEG decode, strict about pixel integrity by default, and encode, plus dense RGB8 batch decode.
- `j2k-ml`: Burn tensors from JPEG 2000 batches, or mixed JPEG and JPEG 2000 batches on the CPU.
- `j2k-python`: PyTorch/NumPy extension, built as the `j2k` wheel, with CPU output and optional CUDA or Apple Silicon MPS output.
- `j2k-cli`: Command-line inspection and JPEG-to-HTJ2K transcode smoke tests.

## Safety and licensing

Applications use safe public Rust APIs. Unsafe implementation code is limited to documented FFI, GPU, SIMD, allocation, pointer, and buffer boundaries. The workspace is dual-licensed under MIT or Apache-2.0.

## Documentation

- [Pure-Rust JPEG 2000 codec](https://frames-sg.com/j2k/rust-jpeg2000-codec/)
- [GPU acceleration overview](https://frames-sg.com/j2k/gpu-jpeg2000-rust/)
- [Apple Metal acceleration](https://frames-sg.com/j2k/metal-jpeg2000-rust/)
- [NVIDIA CUDA acceleration](https://frames-sg.com/j2k/cuda-jpeg2000-rust/)
- [HTJ2K Part 15](https://frames-sg.com/j2k/htj2k-rust/)
- [Rust API documentation](https://docs.rs/j2k)
- [Source repository](https://github.com/frames-sg/j2k)
- [Release changelog](https://github.com/frames-sg/j2k/blob/main/CHANGELOG.md)
- [Benchmark methodology and evidence](https://github.com/frames-sg/j2k/blob/main/docs/benchmark-evidence.md)
