Skip to content

About

Pure Go JPEG2000 encoder and decoder

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

49 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jpeg2000

A fork of ajroetker/go-jpeg2000, under the same Apache-2.0 licence. It exists so that a fix can be used without waiting on an upstream that has not published since February. The import path is github.com/go-images/jpeg2000. Every change made here is stated in NOTICE, as Apache-2.0 section 4(b) requires.

What differs today (the full list, with the files, is in NOTICE):

  • a four-component picture whose JP2 header declares the enumerated colour space CMYK decodes to an *image.CMYK, not an *image.RGBA built from its first three components, which discarded the black plate. Against poppler on the reference picture, 255 levels of disagreement on every pixel became at most 2, over 2 440 044 samples;
  • six decoder and encoder corrections: an odd origin no longer crashes the 9/7 synthesis; 9/7 coefficients are carried as float32, as OpenJPEG carries them; tiled encodes place their code blocks and tiles where the decoder and OpenJPEG read them; a tile whose blocks all quantise away is accepted;
  • the SIMD dependency is go-simd/highway, a fork of go-highway that consumers can go mod tidy against;
  • the lossless colour transform and the inverse 5/3 wavelet are plain loops, not highway calls. On amd64 highway took an allocating fallback, because nothing sets GOEXPERIMENT=simd. A whole lossless 1920×1080 decode on amd64 went from 638 ms and 14.55 million allocations to 269 ms and 93 thousand, the same allocation count as arm64. arm64 is not slower (−1.5 %);
  • fuzzed (FuzzDecode, FuzzDecodeConfig), and the headers validated as OpenJPEG validates them: fuzzing found three panics reachable from DecodeConfig, all fixed in v0.13.8;
  • a sample ceiling (v0.14.0): DecodeOptions.MaxSamples, zero meaning DefaultMaxSamples (2²⁸, one GiB of coefficients), refused with ErrTooLarge before anything is allocated. A pixel limit alone does not bound a JPEG 2000 decode: a codestream may declare 16384 components, all allocated at full size even with no data. Measured before: an 846-byte codestream (256 components of 512×512) allocated 277.5 MB and decoded without complaint. A subsampled component counts at the size it is upsampled to (XRsiz/YRsiz up to 255 understated it 65025 times). Fuzzing found a fourth panic, a component 0 samples wide upsampled, also fixed;
  • many decode-speed and memory changes that leave the output identical.

Go

A pure Go JPEG2000 codec. Decode and encode JPEG2000 codestreams (.j2k/.j2c) and JP2 files (.jp2) with zero C dependencies.

Requirements

  • Go 1.27+

Installation

go get github.com/go-images/jpeg2000

Quick Start

Decoding

package main

import (
	"image/png"
	"log"
	"os"

	"github.com/go-images/jpeg2000"
)

func main() {
	f, err := os.Open("input.jp2")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

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

	out, _ := os.Create("output.png")
	defer out.Close()
	png.Encode(out, img)
}

Encoding

package main

import (
	"image"
	"image/png"
	"log"
	"os"

	"github.com/go-images/jpeg2000"
)

func main() {
	f, err := os.Open("input.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
	}

	out, _ := os.Create("output.j2k")
	defer out.Close()

	// Lossless encoding
	err = jpeg2000.Encode(out, img, &jpeg2000.EncodeOptions{
		Lossless: true,
	})
	if err != nil {
		log.Fatal(err)
	}
}

Automatic Format Detection

import (
	"image"
	_ "github.com/go-images/jpeg2000" // register format
)

img, format, err := image.Decode(reader) // format = "jpeg2000"

Features

Feature Status
Decode J2K codestreams Supported
Decode JP2 files Supported
Encode J2K codestreams Supported
Encode JP2 files Supported
5/3 reversible wavelet (lossless) Supported
9/7 irreversible wavelet (lossy) Supported
Multi-component color transform (RCT/ICT) Supported
EBCOT Tier-1 (MQ arithmetic coding) Supported
EBCOT Tier-2 (packet assembly) Supported
Rate control (PCRD-opt) Supported
Quality layers Supported
Grayscale and RGB images Supported
Multi-tile images Supported

Encode Options

type EncodeOptions struct {
	Lossless       bool      // true = 5/3 + RCT, false = 9/7 + ICT
	Quality        float64   // 0.0-1.0 (lossy only, default: 0.8)
	TargetSize     int       // target file size in bytes (overrides Quality)
	NumLayers      int       // quality layers (default: 1)
	TileWidth      int       // 0 = single tile
	TileHeight     int       // 0 = single tile
	NumResolutions int       // decomposition levels + 1 (default: 6)
	CodeBlockWidth int       // default: 64
	CodeBlockHeight int      // default: 64
	FileFormat     FileFormat // FormatJ2K or FormatJP2
}

Building

go build ./...
go test ./...
go test -bench=. -benchmem ./...

License

Apache 2.0

About

Pure Go JPEG2000 encoder and decoder

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages