Skip to Content
go-dicom(3)
Packages

compress/jpegxl

A pure Go JPEG XL decoder and lossless encoder, for DICOM's JPEG XL transfer syntaxes (1.2.840.10008.1.2.4.110, .111 and .112): Modular and VarDCT frames, recompressed JPEG files, bare codestreams and containers.

import "github.com/amrshadid/go-dicom/compress/jpegxl" · pkg.go.dev

When to use it

For a JPEG XL codestream you hold yourself, to read its header, decode it to integer planes, or code gray or RGB samples losslessly.

For DICOM pixel data, use dataset (PixelArrayInterpreted, CompressJPEGXLLosslessInPlace) or compress.EncodeJPEGXLLossless, which take native Pixel Data bytes.

Key types and functions

NameWhat it is for
DecodeConfig, ConfigThe header alone: size, bit depth, channels, XYB, alpha.
DecodeDecode to an Image.
ImageColor planes then extra channels, int32 samples; Lossless says whether they are exact.
EncodeLosslessA bare Modular codestream, gray or RGB, 1 to 16 bits, that decodes exactly.
IsJPEGXLWhether bytes start as a codestream or a container.
UnsupportedErrorA feature that is not decoded, refused by name.

Example

// Code a small RGB image as lossless JPEG XL and decode it back.
package main

import (
	"bytes"
	"fmt"
	"log"

	"github.com/amrshadid/go-dicom/compress/jpegxl"
)

func main() {
	const w, h, channels = 32, 16, 3
	samples := make([]uint16, w*h*channels) // interleaved R, G, B
	for i := range samples {
		samples[i] = uint16(i % 256)
	}

	var buf bytes.Buffer
	if err := jpegxl.EncodeLossless(&buf, w, h, channels, 8, samples); err != nil {
		log.Fatal(err)
	}

	cfg, err := jpegxl.DecodeConfig(buf.Bytes())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(cfg.Width, "x", cfg.Height, cfg.ColorChannels, "channels at", cfg.BitDepth, "bits")

	img, err := jpegxl.Decode(buf.Bytes())
	if err != nil {
		log.Fatal(err)
	}
	same := true
	for i, s := range samples {
		if img.Planes[i%channels][i/channels] != int32(s) {
			same = false
		}
	}
	fmt.Println("lossless:", img.Lossless, "same samples:", same)
}

It prints 32 x 16 3 channels at 8 bits and lossless: true same samples: true.

Notes

  • Lossless frames are checked sample for sample against libjxl's djxl; lossy output is within one level at 8 bits and four at 16 of djxl's.
  • Every size and count in a stream is bounded before anything is allocated (2^26 pixels, 2^27 samples across channels); reading past the end is an error.
  • EncodeLossless takes unsigned samples. Signed DICOM samples are coded as their bit pattern, with Pixel Representation saying so.

Note

Not decoded, and refused with an *UnsupportedError: splines, animation and layers (cropped or blended frames), floating point samples, the alpha-weighted patch modes, the HLG transfer function, and XYB images with an ICC profile or primaries other than sRGB's. Through the data set these are dataset.ErrUndecodedSyntax.

See also

compress · compress/jpeg2000 · dataset · Pixel data

On this page