jpegxr

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DCCoeffCount = 1   // Single DC coefficient
	LPCoeffCount = 63  // 15 from second-stage + 48 from first-stage
	HPCoeffCount = 192 // 12 per 4x4 block x 16 blocks
)

Coefficient counts for JPEG XR macroblock (16x16)

Variables

View Source
var (
	ErrInvalidSymbol = errors.New("invalid symbol for entropy coding")
	ErrBufferOverrun = errors.New("entropy buffer overrun")
)

Errors

View Source
var ErrInvalidDimensions = errors.New("invalid image dimensions")

ErrInvalidDimensions indicates the image dimensions are invalid for JPEG XR encoding.

Functions

This section is empty.

Types

type AdaptiveContext

type AdaptiveContext struct {
	// Symbol counts for probability estimation
	Counts map[int]int

	// Total count
	Total int

	// Context ID
	ID int
}

AdaptiveContext maintains context for adaptive coding

func NewAdaptiveContext

func NewAdaptiveContext(id int) *AdaptiveContext

NewAdaptiveContext creates a new adaptive context

func (*AdaptiveContext) GetProbability

func (ac *AdaptiveContext) GetProbability(symbol int) float64

GetProbability returns the probability of a symbol

func (*AdaptiveContext) Reset

func (ac *AdaptiveContext) Reset()

Reset clears the context state

func (*AdaptiveContext) Update

func (ac *AdaptiveContext) Update(symbol int)

Update updates context with observed symbol

type AdaptiveQuantizer

type AdaptiveQuantizer struct {
	BaseParams *QuantizationParams

	// Per-tile QP adjustments
	TileQPDelta map[int]int

	// Per-macroblock QP delta (for fine-grained control)
	MBQPDelta map[int]int
}

AdaptiveQuantizer supports spatially-varying quantization

func NewAdaptiveQuantizer

func NewAdaptiveQuantizer(baseParams *QuantizationParams) *AdaptiveQuantizer

NewAdaptiveQuantizer creates an adaptive quantizer

func (*AdaptiveQuantizer) GetEffectiveQP

func (aq *AdaptiveQuantizer) GetEffectiveQP(tileIdx, mbIdx int, band string) int

GetEffectiveQP returns the effective QP for a macroblock

func (*AdaptiveQuantizer) GetQuantizerForMB

func (aq *AdaptiveQuantizer) GetQuantizerForMB(tileIdx, mbIdx int) *Quantizer

GetQuantizerForMB returns a quantizer with effective parameters for a MB

func (*AdaptiveQuantizer) SetMBQPDelta

func (aq *AdaptiveQuantizer) SetMBQPDelta(mbIdx int, delta int)

SetMBQPDelta sets a QP adjustment for a specific macroblock

func (*AdaptiveQuantizer) SetTileQPDelta

func (aq *AdaptiveQuantizer) SetTileQPDelta(tileIdx int, delta int)

SetTileQPDelta sets a QP adjustment for a specific tile

type Code

type Code struct {
	Bits   uint32 // The code bits
	Length int    // Number of bits
}

Code represents a single VLC codeword

type DeadZoneQuantizer

type DeadZoneQuantizer struct {
	Params   *QuantizationParams
	DeadZone float64 // Dead zone factor (0.0-1.0, typically 0.5)
}

DeadZoneQuantizer implements quantization with a dead zone around zero

func NewDeadZoneQuantizer

func NewDeadZoneQuantizer(params *QuantizationParams, deadZone float64) *DeadZoneQuantizer

NewDeadZoneQuantizer creates a quantizer with dead zone

func (*DeadZoneQuantizer) Quantize

func (dq *DeadZoneQuantizer) Quantize(coeffs []int, step int) []int

Quantize applies dead-zone quantization

type Decoder

type Decoder struct {
	Info   *ImageInfo
	Config *EncoderConfig
	// contains filtered or unexported fields
}

Decoder is the JPEG XR decoder

func NewDecoder

func NewDecoder(data []byte) (*Decoder, error)

NewDecoder creates a new JPEG XR decoder

func (*Decoder) Decode

func (d *Decoder) Decode() ([]byte, error)

Decode decodes the image data

type Encoder

type Encoder struct {
	Config *EncoderConfig
	Info   *ImageInfo
	// contains filtered or unexported fields
}

Encoder is the main JPEG XR encoder

func NewEncoder

func NewEncoder(info *ImageInfo, config *EncoderConfig) (*Encoder, error)

NewEncoder creates a new JPEG XR encoder

func (*Encoder) Encode

func (e *Encoder) Encode(data []byte) ([]byte, error)

Encode encodes the image data

type EncoderConfig

type EncoderConfig struct {
	// Quality level (0-100, 100 = best quality/lossless)
	Quality int

	// Overlap mode
	UseOverlap bool

	// Tile configuration
	Tiles TileConfig

	// Frequency mode (DC only, DC+LP, or DC+LP+HP)
	FrequencyMode FrequencyMode

	// Alpha plane encoding
	EncodeAlpha  bool
	AlphaQuality int

	// Subsampling mode
	Subsampling SubsamplingMode
}

EncoderConfig holds encoder configuration

func DefaultEncoderConfig

func DefaultEncoderConfig() *EncoderConfig

DefaultEncoderConfig returns default encoder settings

type EntropyDecoder

type EntropyDecoder struct {

	// VLC tables
	DCTable *VLCTable
	LPTable *VLCTable
	// contains filtered or unexported fields
}

EntropyDecoder handles JPEG XR entropy decoding

func NewEntropyDecoder

func NewEntropyDecoder(data []byte) *EntropyDecoder

NewEntropyDecoder creates a new entropy decoder

func (*EntropyDecoder) DecodeDC

func (d *EntropyDecoder) DecodeDC(count int) ([]int, error)

DecodeDC decodes DC coefficients

func (*EntropyDecoder) DecodeHP

func (d *EntropyDecoder) DecodeHP(count int) ([]int, error)

DecodeHP decodes HP coefficients for a macroblock using run-length coding HP coefficients represent high-frequency details: - 192 HP coefficients per macroblock (12 per 4x4 block x 16 blocks) - 9 AC coefficients per 4x4 block (positions [1,1] to [3,3]) - 16 blocks x 12 HP per block = 192 total

func (*EntropyDecoder) DecodeLP

func (d *EntropyDecoder) DecodeLP(count int) ([]int, error)

DecodeLP decodes LP coefficients for a macroblock LP coefficients include: - 15 coefficients from second-stage transform (DC block minus the DC value) - 48 coefficients from first-stage transforms (3 LP per 4x4 block x 16 blocks) Total: 63 LP coefficients per macroblock

func (*EntropyDecoder) DecodeVLC

func (d *EntropyDecoder) DecodeVLC(table *VLCTable) (int, error)

DecodeVLC decodes a symbol using VLC table (simplified)

func (*EntropyDecoder) Reset

func (d *EntropyDecoder) Reset(data []byte)

Reset resets the decoder with new data

type EntropyEncoder

type EntropyEncoder struct {

	// VLC tables
	DCTable *VLCTable
	LPTable *VLCTable
	// contains filtered or unexported fields
}

EntropyEncoder handles JPEG XR entropy encoding

func NewEntropyEncoder

func NewEntropyEncoder() *EntropyEncoder

NewEntropyEncoder creates a new entropy encoder

func (*EntropyEncoder) Bytes

func (e *EntropyEncoder) Bytes() []byte

Bytes returns the encoded data

func (*EntropyEncoder) EncodeDC

func (e *EntropyEncoder) EncodeDC(dc []int) error

EncodeDC encodes DC coefficients

func (*EntropyEncoder) EncodeHP

func (e *EntropyEncoder) EncodeHP(hp []int) error

EncodeHP encodes HP coefficients using adaptive coding

func (*EntropyEncoder) EncodeLP

func (e *EntropyEncoder) EncodeLP(lp []int) error

EncodeLP encodes LP coefficients

func (*EntropyEncoder) EncodeVLC

func (e *EntropyEncoder) EncodeVLC(symbol int, table *VLCTable) error

EncodeVLC encodes a symbol using VLC table

func (*EntropyEncoder) Flush

func (e *EntropyEncoder) Flush()

Flush writes any remaining bits

func (*EntropyEncoder) Reset

func (e *EntropyEncoder) Reset()

Reset clears the encoder state

func (*EntropyEncoder) TotalBits

func (e *EntropyEncoder) TotalBits() int

TotalBits returns the total number of bits written

type FrequencyMode

type FrequencyMode int

FrequencyMode specifies which frequency bands to encode

const (
	FrequencyDC   FrequencyMode = iota // DC only (spatially scalable)
	FrequencyDCLP                      // DC + LP
	FrequencyAll                       // DC + LP + HP (full quality)
)

type HierarchicalTransform

type HierarchicalTransform struct {
	PCT *PhotoCoreTransform
	POT *PhotoOverlapTransform

	// DC coefficients from first stage
	DCCoeffs []int

	// Transform mode
	UseOverlap bool
}

HierarchicalTransform manages the two-stage JPEG XR transform

func NewHierarchicalTransform

func NewHierarchicalTransform(useOverlap bool) *HierarchicalTransform

NewHierarchicalTransform creates a new hierarchical transformer

func (*HierarchicalTransform) ForwardMacroblock

func (h *HierarchicalTransform) ForwardMacroblock(mb []int) (dc []int, lp []int, hp []int)

ForwardMacroblock transforms a 16x16 macroblock Returns DC (4x4), LP (12 coeffs per 4x4), HP (remaining)

func (*HierarchicalTransform) InverseMacroblock

func (h *HierarchicalTransform) InverseMacroblock(dc []int, lp []int, hp []int) []int

InverseMacroblock reconstructs a 16x16 macroblock from coefficients

type ImageInfo

type ImageInfo struct {
	Width       int
	Height      int
	NumChannels int
	BitDepth    int
	PixelFormat PixelFormat
	HasAlpha    bool
}

ImageInfo describes the input image

type PhotoCoreTransform

type PhotoCoreTransform struct {
	// Scaling factor for fixed-point arithmetic
	Scale int
}

PhotoCoreTransform implements the 4x4 PCT used in JPEG XR

func NewPhotoCoreTransform

func NewPhotoCoreTransform() *PhotoCoreTransform

NewPhotoCoreTransform creates a new PCT transformer

func (*PhotoCoreTransform) Forward4x4

func (t *PhotoCoreTransform) Forward4x4(block []int) []int

Forward4x4 applies the forward 4x4 Photo Core Transform The PCT is based on the Hadamard transform with rotations

func (*PhotoCoreTransform) Inverse4x4

func (t *PhotoCoreTransform) Inverse4x4(block []int) []int

Inverse4x4 applies the inverse 4x4 Photo Core Transform

type PhotoOverlapTransform

type PhotoOverlapTransform struct {
	// Filter coefficients
	S0, S1 int
}

PhotoOverlapTransform implements the overlap filter for JPEG XR

func NewPhotoOverlapTransform

func NewPhotoOverlapTransform() *PhotoOverlapTransform

NewPhotoOverlapTransform creates a new POT filter

func (*PhotoOverlapTransform) Post4x4

func (p *PhotoOverlapTransform) Post4x4(block []int) []int

Post4x4 applies the post-filter after inverse PCT

func (*PhotoOverlapTransform) Pre4x4

func (p *PhotoOverlapTransform) Pre4x4(block []int) []int

Pre4x4 applies the pre-filter before PCT This creates the lapped transform effect

type PixelFormat

type PixelFormat int

PixelFormat represents the pixel format of image data

const (
	PixelFormatGray8 PixelFormat = iota
	PixelFormatGray16
	PixelFormatRGB24
	PixelFormatRGB48
	PixelFormatRGBA32
	PixelFormatRGBA64
)

type QuantizationParams

type QuantizationParams struct {
	// Quantization parameter for each band
	DCQP int // DC quantization parameter (0-255)
	LPQP int // LP quantization parameter (0-255)
	HPQP int // HP quantization parameter (0-255)

	// Derived quantization step sizes
	DCStep int
	LPStep int
	HPStep int
}

QuantizationParams holds the quantization parameters for JPEG XR

func NewQuantizationParams

func NewQuantizationParams(quality int) *QuantizationParams

NewQuantizationParams creates quantization parameters from quality level

func NewQuantizationParamsFromQP

func NewQuantizationParamsFromQP(dcQP, lpQP, hpQP int) *QuantizationParams

NewQuantizationParamsFromQP creates parameters from explicit QP values

type Quantizer

type Quantizer struct {
	Params *QuantizationParams
}

Quantizer performs JPEG XR quantization and dequantization

func NewQuantizer

func NewQuantizer(params *QuantizationParams) *Quantizer

NewQuantizer creates a new quantizer with given parameters

func (*Quantizer) DequantizeDC

func (q *Quantizer) DequantizeDC(dc []int) []int

DequantizeDC dequantizes DC coefficients

func (*Quantizer) DequantizeHP

func (q *Quantizer) DequantizeHP(hp []int) []int

DequantizeHP dequantizes HP coefficients

func (*Quantizer) DequantizeLP

func (q *Quantizer) DequantizeLP(lp []int) []int

DequantizeLP dequantizes LP coefficients

func (*Quantizer) QuantizeDC

func (q *Quantizer) QuantizeDC(dc []int) []int

QuantizeDC quantizes DC coefficients

func (*Quantizer) QuantizeHP

func (q *Quantizer) QuantizeHP(hp []int) []int

QuantizeHP quantizes HP coefficients

func (*Quantizer) QuantizeLP

func (q *Quantizer) QuantizeLP(lp []int) []int

QuantizeLP quantizes LP coefficients

type SubsamplingMode

type SubsamplingMode int

SubsamplingMode specifies chroma subsampling

const (
	Subsampling444 SubsamplingMode = iota // No subsampling
	Subsampling422                        // 4:2:2
	Subsampling420                        // 4:2:0
)

type TileConfig

type TileConfig struct {
	Enabled    bool
	TileWidth  int
	TileHeight int
	NumTilesX  int
	NumTilesY  int
}

TileConfig specifies tile parameters

type VLCTable

type VLCTable struct {
	// Codes maps symbols to their VLC representation
	Codes map[int]Code

	// MaxCodeLength is the maximum code length in bits
	MaxCodeLength int

	// Name identifies this table
	Name string
}

VLCTable defines a variable-length code table

func DefaultDCVLCTable

func DefaultDCVLCTable() *VLCTable

DefaultDCVLCTable returns the default VLC table for DC coefficients

func DefaultLPVLCTable

func DefaultLPVLCTable() *VLCTable

DefaultLPVLCTable returns the default VLC table for LP coefficients

func NewVLCTable

func NewVLCTable(name string) *VLCTable

NewVLCTable creates a new VLC table

func (*VLCTable) AddCode

func (t *VLCTable) AddCode(symbol int, bits uint32, length int)

AddCode adds a code for a symbol

func (*VLCTable) GetCode

func (t *VLCTable) GetCode(symbol int) (Code, bool)

GetCode returns the code for a symbol

Jump to

Keyboard shortcuts

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