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
| Name | What it is for |
|---|---|
ParseCodestream, Codestream | Read the main header (size, components, tiles, coding style) without decoding. |
Decode | Decode a parsed codestream into an Image. |
Image | One plane of int32 samples per component, with each component's depth and sign. |
Encode | Code an image losslessly (5/3 wavelet, RCT for three components). |
EncodeWithOptions, EncodeOptions | The same as HTJ2K, in RPCL order, as Part 2, or without the component transform. |
EncodeLossy, LossyOptions | Code to a compression ratio or a PSNR, in layers, optionally HTJ2K. |
ErrUnsupported | A feature outside what is decoded, refused by name. |
Component | One 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.WidthandHeightwith what you expect before callingDecode, 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
The pixel codecs and the encapsulation format. Every compressed transfer syntax but the video ones is decoded here in pure Go, with no CGO: RLE, JPEG Baseline and Extended, JPEG Lossless, JPEG-LS, JPEG 2000 and HTJ2K, JPEG XL and Deflated Image Frame Compression. Sixteen syntaxes are also encoded.
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.