Lossless Multispectral and Hyperspectral Image Compression (CCSDS 123.0-B)

Files
README.md

hcomp #

CCSDS 123.0-B-2 lossless and near-lossless compression of multispectral and hyperspectral images, in pure OCaml.

An imaging spectrometer on a satellite produces tens or hundreds of spectral bands per pixel, far more data than a downlink carries, and neighbouring samples are strongly correlated in space and across bands. CCSDS 123.0-B-2 standardises the compressor that exploits this: an adaptive linear predictor estimates each sample from its neighbours in the same and preceding bands, the prediction residual is quantized (exactly, or within a user-set absolute and/or relative error bound) and mapped to an unsigned index, and an entropy coder writes the indices after a header recording every parameter.

hcomp implements the whole Recommended Standard, Technical Corrigenda 1 to 3 included: signed and unsigned samples of 2 to 32 bits, full and reduced prediction, the four local sums, default and custom weight initialization, weight exponent offsets, band-dependent and periodically updated error limits, sample representatives, supplementary information tables, the band-interleaved orders (the bands of one pixel, or of one row, sit next to each other before the next) and the band-sequential order (one whole band after another), and all three entropy coders: sample-adaptive, hybrid (with the 16 low-entropy codes of Annex B) and block-adaptive, which is CCSDS 121.0-B coding through the rice library. Tables a header may leave out are supplied to the decompressor by the caller.

Its streams are checked byte for byte, in both directions, against the CCSDS 123.0-B-2 high-level model of the NTNU SmallSat Lab, whose authors verified it against the CCSDS test vector set, and its low-entropy code tables against the machine-readable tables the CCSDS distributes.

Installation #

Install with opam:

$ opam install hcomp

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install hcomp

Usage #

(* Four 64x64 bands of 12-bit samples, compressed losslessly with the
   sample-adaptive coder, band-interleaved by pixel. *)
let image =
  Hcomp.Image.v ~nx:64 ~ny:64 ~nz:4 (fun ~z ~y ~x ->
      (100 * z) + (3 * x) + (5 * y))

let header : Hcomp.Header.t =
  {
    user_data = 0;
    nx = 64;
    ny = 64;
    nz = 4;
    sample_type = Unsigned;
    dynamic_range = 12;
    order = Interleaved 4;
    word_size = 1;
    supplementary = [];
    predictor =
      {
        bands = 3;
        mode = Full;
        local_sum = Wide_neighbor;
        register_size = 64;
        weight_resolution = 13;
        interval_exponent = 6;
        vmin = -1;
        vmax = 3;
        exponent_offsets = None;
        weight_init = Default;
        quantizer =
          { absolute = None; relative = None; update_exponent = None; limits = [||] };
        representative = None;
      };
    coder =
      Sample_adaptive
        ({ unary_limit = 16; rescaling_size = 6; initial_count = 1 }, Constant 4);
  }

let compressed = Hcomp.compress header image

let () =
  match Hcomp.decompress compressed with
  | Ok (h, restored) ->
      assert (h = header);
      assert (Hcomp.Image.equal restored image)
  | Error e -> failwith e

References #

  • CCSDS 123.0-B-2, Low-Complexity Lossless and Near-Lossless Multispectral and Hyperspectral Image Compression. Consultative Committee for Space Data Systems, 2019, with Technical Corrigenda 1 to 3.
  • CCSDS 121.0-B-3, Lossless Data Compression, for the block-adaptive coder.

License #

ISC. See LICENSE.md.