jxl

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: BSD-3-Clause Imports: 14 Imported by: 13 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

jxl

Status Go Reference

JPEG XL image decoder and encoder in pure Go.

A port of jxl-rs, checked against libjxl. No CGo, no dependencies.

SIMD support for amd64 (AVX2, AVX-512), arm64 (NEON) and riscv64 (RVV, with GORISCV64=rva23u64). Build with -tags noasm for pure Go everywhere.

Decoding

img, err := jxl.Decode(r)

jxl.Decode returns *image.NRGBA, or *image.NRGBA64 above 8 bits, and registers itself with image.RegisterFormat. jxl.DecodeAll returns every frame with its delay.

Encoding

err := jxl.Encode(w, img, jxl.EncodeOptions{Quality: 95})

Lossy, within 1.02x to 1.15x of cjxl at matched ssimulacra2. Input is treated as sRGB and alpha is dropped.

Supported

Lossless and lossy, 8 to 32-bit, grayscale and color, alpha, animation, chroma subsampling, patches, splines, noise, progressive files, embedded ICC profiles, Exif and XMP.

Not implemented: color management through embedded ICC profiles and JPEG reconstruction.

License

The decoder is a port of jxl-rs and carries the JPEG XL Project Authors' BSD-3-Clause notice, in LICENSE, together with the patent grant in PATENTS.

This project is an implementation of a codec. It gives you no special rights on any patents that may read on JPEG XL.

Documentation

Overview

Package jxl decodes and encodes JPEG XL images.

Decoding covers both Modular and VarDCT codestreams. Encoding writes a single frame from sRGB input, lossy VarDCT by default and Modular when EncodeOptions.Lossless is set. An alpha channel is always kept losslessly.

DecodeExif parses the Exif metadata of a file, while RawExif and RawXMP return the payloads whole. All three report ErrNoExif or ErrNoXMP when the file carries no such box, and ErrUnsupported when it carries one Brotli compressed.

Index

Constants

View Source
const (
	// DefaultQuality is the quality used when EncodeOptions.Quality is zero.
	DefaultQuality = 90
	// DefaultEffort is the effort used when EncodeOptions.Effort is zero.
	DefaultEffort = 7
	// MaxEffort is the highest effort the encoder implements.
	MaxEffort = 7
)
View Source
const DefaultFrameSizeLimit = min(16384*16384, math.MaxInt>>6)

DefaultFrameSizeLimit is the frame area, in pixels, accepted when Options.FrameSizeLimit is zero.

View Source
const DefaultLevel = 5

DefaultLevel is the codestream level enforced when Options.Level is zero.

View Source
const DefaultMaxFrames = 1024

DefaultMaxFrames bounds the frames a decode reads from a codestream.

Variables

View Source
var (
	ErrInvalid     = errors.New("jxl: invalid bitstream")
	ErrUnsupported = errors.New("jxl: unsupported feature")
)

ErrInvalid means the bitstream is malformed. ErrUnsupported means it is well-formed but uses a feature this decoder does not implement.

View Source
var ErrFrameTooLarge = errors.New("jxl: frame exceeds size limit")

ErrFrameTooLarge is returned when a frame's area exceeds Options.FrameSizeLimit.

View Source
var ErrNoExif = errors.New("jxl: no exif data")

ErrNoExif is returned when the file carries no Exif box.

View Source
var ErrNoXMP = errors.New("jxl: no xmp data")

ErrNoXMP is returned when the file carries no XMP box.

Functions

func Decode

func Decode(r io.Reader, opts ...Options) (image.Image, error)

Decode reads a JPEG XL image as *image.NRGBA, or *image.NRGBA64 above 8 bits. Grayscale without alpha gives *image.Gray or *image.Gray16.

func DecodeConfig

func DecodeConfig(r io.Reader) (image.Config, error)

DecodeConfig reads the dimensions and color model without decoding pixels.

func Encode

func Encode(w io.Writer, m image.Image, opts ...EncodeOptions) error

Encode writes m as a JPEG XL codestream, lossy VarDCT by default and Modular when EncodeOptions.Lossless is set. Samples are treated as sRGB. An alpha channel is kept losslessly, and dropped when every pixel is opaque.

func RawExif added in v0.1.2

func RawExif(r io.Reader) ([]byte, error)

RawExif returns the TIFF payload of the Exif box, without the exif_tiff_header_offset the container puts in front of it. It aliases the input, so it must not be modified.

func RawXMP added in v0.1.2

func RawXMP(r io.Reader) ([]byte, error)

RawXMP returns the XMP packet of the file. It aliases the input, so it must not be modified.

Types

type ColorInfo

type ColorInfo struct {
	Primaries uint16
	Transfer  uint16
	Matrix    uint16
	ICCP      []byte
}

ColorInfo reports the color space of the decoded samples, as CICP code points or as the ICC profile the codestream carried.

func DecodeColor

func DecodeColor(r io.Reader, opts ...Options) (image.Image, ColorInfo, error)

DecodeColor reads a JPEG XL image and reports the color space it came from.

type EncodeOptions

type EncodeOptions struct {
	// Quality runs from 1 to 100, where 100 is the least lossy. Zero means
	// DefaultQuality.
	Quality int
	// Effort runs from 1 to MaxEffort, spending encode time on a wider block
	// search for a smaller file at the same quality. Zero means DefaultEffort,
	// higher values are clamped.
	Effort int
	// Lossless reproduces the source samples exactly, ignoring Quality.
	Lossless bool
	// Threads bounds the goroutines encoding one frame. Zero means GOMAXPROCS,
	// one encodes serially.
	Threads int
}

EncodeOptions controls the encoder. The zero value is a reasonable default.

type Exif added in v0.1.2

type Exif struct {
	// Orientation is the Exif orientation, 1 to 8, where 1 is upright.
	Orientation int
	// Width and Height are the dimensions the Exif tags report, which need
	// not be the dimensions the image decodes to.
	Width  int
	Height int

	// Make and Model name the camera, Software what wrote the file.
	Make     string
	Model    string
	Software string

	// DateTime and DateTimeOriginal are "YYYY:MM:DD HH:MM:SS", the first the
	// file's own time and the second the time the photo was taken.
	DateTime         string
	DateTimeOriginal string

	// ExposureTime is in seconds and FocalLength in millimetres.
	ExposureTime float64
	FNumber      float64
	ISOSpeed     int
	FocalLength  float64
	Flash        int

	// GPSLatitude and GPSLongitude are decimal degrees, positive north and
	// east. GPSAltitude is metres above sea level.
	GPSLatitude  float64
	GPSLongitude float64
	GPSAltitude  float64

	Copyright string
	Artist    string
}

Exif holds the Exif metadata decoded from a JPEG XL image.

func DecodeExif added in v0.1.2

func DecodeExif(r io.Reader) (*Exif, error)

DecodeExif reads the Exif metadata of a JPEG XL image. Tags the file omits stay zero. It returns ErrNoExif when the file carries no Exif box.

type Float

type Float struct {
	Width    int
	Height   int
	Channels int
	// Frames each hold Width*Height*Channels samples, row-major.
	Frames [][]float32
	Color  ColorInfo
}

Float is the decoder's native output: interleaved float32 samples per frame, in the image's own color space.

func DecodeFloat

func DecodeFloat(r io.Reader, opts ...Options) (*Float, error)

DecodeFloat reads a JPEG XL file into the decoder's native float samples, without converting to an image.Image.

type JXL

type JXL struct {
	Image []image.Image
	Delay []float64
	Loops int
	Color ColorInfo
}

JXL is every frame of a file, with the delay of each in seconds.

func DecodeAll

func DecodeAll(r io.Reader, opts ...Options) (*JXL, error)

DecodeAll reads every frame of a JPEG XL file. A still image gives one frame.

type Options

type Options struct {
	// Level is the codestream level to accept. Zero means DefaultLevel.
	Level int
	// Threads bounds the goroutines decoding one frame. Zero means GOMAXPROCS,
	// one decodes serially.
	Threads int
	// FrameSizeLimit bounds a frame's area in pixels, so that a corrupt or
	// hostile header cannot ask for an unbounded allocation. Zero means
	// DefaultFrameSizeLimit; a negative value removes the limit.
	FrameSizeLimit int
	// MaxFrames bounds the frames a decode reads from a codestream. Zero
	// means DefaultMaxFrames; a negative value removes the limit.
	MaxFrames int
}

Options configure a decode.

Directories

Path Synopsis
j40 module
j40c module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址