Skip to Content
go-dicom(3)
Packages

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.

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

When to use it

For a codestream you hold yourself: a JPEG 2000 file outside DICOM, or a frame taken from encapsulated Pixel Data, which you want as integer planes, or planes you want coded.

For DICOM pixel data, use dataset (PixelArrayInterpreted, CompressJPEG2000LosslessInPlace, CompressHTJ2KLosslessInPlace) or the byte-oriented EncodeJPEG2000Lossless of compress, which take and return native Pixel Data bytes and set the attributes that go with them.

Key types and functions

NameWhat it is for
ParseCodestream, CodestreamRead the main header (size, components, tiles, coding style) without decoding.
DecodeDecode a parsed codestream into an Image.
ImageOne plane of int32 samples per component, with each component's depth and sign.
EncodeCode an image losslessly (5/3 wavelet, RCT for three components).
EncodeWithOptions, EncodeOptionsThe same as HTJ2K, in RPCL order, as Part 2, or without the component transform.
EncodeLossy, LossyOptionsCode to a compression ratio or a PSNR, in layers, optionally HTJ2K.
ErrUnsupportedA feature outside what is decoded, refused by name.
ComponentOne component of the SIZ segment: depth, sign, subsampling.

Example

// Code a small gray image as lossless JPEG 2000, read its header, decode it.
package main

import (
	"fmt"
	"log"
	"slices"

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

func main() {
	const w, h = 64, 48
	plane := make([]int32, w*h)
	for i := range plane {
		plane[i] = int32(i % 4096) // 12-bit samples
	}
	img := &jpeg2000.Image{
		Width: w, Height: h,
		Components: [][]int32{plane},
		Depths:     []int{12},
		Signed:     []bool{false},
	}

	codestream, err := jpeg2000.Encode(img)
	if err != nil {
		log.Fatal(err)
	}

	c, err := jpeg2000.ParseCodestream(codestream) // header only
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("size:", c.Width, "x", c.Height, "components:", len(c.Components))

	out, err := jpeg2000.Decode(c)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("lossless:", slices.Equal(out.Components[0], plane))
}

It prints size: 64 x 48 components: 1 and lossless: true.

Notes

  • The image is at most 2^16 samples on a side and 2^28 samples in all. What a decode allocates is in proportion to the bytes the codestream holds or to the image's size, and every count is checked against the bytes that remain.
  • A small codestream can still describe a large image honestly. Compare Codestream.Width and Height with what you expect before calling Decode, as the data set's pixel path does with Rows and Columns.
  • What is decoded, and how it is checked against OpenJPEG, is in CONFORMANCE.md §8.1.

Warning

EncodeLossy loses detail. Exactly one of Ratio and PSNR must be set.

Note

The block-level functions (DecodeBlock, EncodeBlock, MagnitudeBits, SubbandIndex) and the two …ForTest functions are exported for the package's own tests and tools, not for applications.

See also

compress · compress/jpegxl · dataset · Pixel data

On this page