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
| Name | What it is for |
|---|---|
DecodeConfig, Config | The header alone: size, bit depth, channels, XYB, alpha. |
Decode | Decode to an Image. |
Image | Color planes then extra channels, int32 samples; Lossless says whether they are exact. |
EncodeLossless | A bare Modular codestream, gray or RGB, 1 to 16 bits, that decodes exactly. |
IsJPEGXL | Whether bytes start as a codestream or a container. |
UnsupportedError | A 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 ofdjxl'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.
EncodeLosslesstakes 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/jpeg2000
A pure Go JPEG 2000 codec: decodes Part 1 codestreams, High-Throughput JPEG 2000 (Part 15) and the Part 2 multiple component transforms DICOM uses, and encodes losslessly, lossily to a ratio or PSNR, and as HTJ2K.
config
Library-wide settings: where the library logs, and how strictly values are validated when read and written, either for the whole program or for one operation through a context.Context.