ebcot

package
v1.0.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	MaxLayers                = 255  // Maximum quality layers
	MaxPrecincts             = 4096 // Maximum precincts per tile
	MaxCodeBlocksPerPrecinct = 4096
)

Tier-2 constants

View Source
const (
	// CtxZCLLLH0 and related constants are context labels for JPEG 2000 EBCOT.
	// Zero coding contexts (LL/LH bands)
	CtxZCLLLH0 = 0 // No significant neighbors
	CtxZCLLLH1 = 1 // 1 significant neighbor (H or V)
	CtxZCLLLH2 = 2 // 1 significant neighbor (D)
	CtxZCLLLH3 = 3 // 2 significant neighbors (H+V or H+D or V+D)
	CtxZCLLLH4 = 4 // 2+ significant neighbors

	// CtxZCHL0 and related constants are zero coding contexts (HL band)
	CtxZCHL0 = 5
	CtxZCHL1 = 6
	CtxZCHL2 = 7
	CtxZCHL3 = 8
	CtxZCHL4 = 9

	// CtxZCHH0 and related constants are zero coding contexts (HH band)
	CtxZCHH0 = 10
	CtxZCHH1 = 11
	CtxZCHH2 = 12
	CtxZCHH3 = 13
	CtxZCHH4 = 14

	// CtxSign is the sign coding context
	CtxSign = 15

	// CtxMagFirst is the magnitude refinement context for first refinement
	CtxMagFirst = 16
	CtxMagOther = 17 // Subsequent refinements

	// CtxRunLength is the run-length context
	CtxRunLength = 18

	// NumEBCOTContexts is the number of contexts used
	NumEBCOTContexts = 19

	// PassSigProp is the Significance Propagation Pass
	PassSigProp = 0
	PassMagRef  = 1 // Magnitude Refinement Pass
	PassCleanup = 2 // Cleanup Pass

	// FlagSig indicates that a sample is significant
	FlagSig     = 1 << 0
	FlagVisited = 1 << 1 // Visited in current pass
	FlagRefined = 1 << 2 // Has been refined at least once
	FlagSign    = 1 << 3 // Sign bit (1 = negative)

	// MaxCodeBlockWidth is the maximum code block width (safety limit)
	MaxCodeBlockWidth  = 64
	MaxCodeBlockHeight = 64
	MaxBitPlanes       = 32
)

EBCOT constants

Variables

View Source
var (
	ErrInvalidPacket   = errors.New("invalid packet data")
	ErrInvalidTagTree  = errors.New("invalid tag tree")
	ErrPacketTruncated = errors.New("packet data truncated")
)

Tier-2 specific errors

View Source
var (
	ErrInvalidCodeBlock = errors.New("invalid code block dimensions")
	ErrInvalidBitPlane  = errors.New("invalid bit plane")
	ErrInvalidPassType  = errors.New("invalid pass type")
)

Errors

Functions

This section is empty.

Types

type CodeBlock

type CodeBlock struct {
	Width        int         // Block width (max 64)
	Height       int         // Block height (max 64)
	Data         []int32     // Coefficient values (width * height)
	State        []uint8     // State flags per coefficient
	NumBitPlanes int         // Number of bit planes
	SubbandType  SubbandType // LL, HL, LH, or HH
}

CodeBlock represents a JPEG 2000 code-block

func NewCodeBlock

func NewCodeBlock(width, height int, subbandType SubbandType) (*CodeBlock, error)

NewCodeBlock creates a new code block

func (*CodeBlock) ClearVisited

func (cb *CodeBlock) ClearVisited()

ClearVisited clears visited flags for all coefficients

func (*CodeBlock) InBounds

func (cb *CodeBlock) InBounds(x, y int) bool

InBounds checks if coordinate (x, y) is within block bounds

func (*CodeBlock) Index

func (cb *CodeBlock) Index(x, y int) int

Index returns the array index for coordinate (x, y)

func (*CodeBlock) IsSignificant

func (cb *CodeBlock) IsSignificant(x, y int) bool

IsSignificant returns true if the coefficient at (x, y) is significant

func (*CodeBlock) Reset

func (cb *CodeBlock) Reset()

Reset clears all state flags

func (*CodeBlock) SetSignificant

func (cb *CodeBlock) SetSignificant(x, y int)

SetSignificant marks the coefficient at (x, y) as significant

type CodeBlockPartitioner

type CodeBlockPartitioner struct {
	BlockWidth  int // Nominal code block width (max 64)
	BlockHeight int // Nominal code block height (max 64)
}

CodeBlockPartitioner partitions wavelet subbands into code blocks

func NewCodeBlockPartitioner

func NewCodeBlockPartitioner(blockWidth, blockHeight int) *CodeBlockPartitioner

NewCodeBlockPartitioner creates a new partitioner

func (*CodeBlockPartitioner) ExtractCodeBlock

func (p *CodeBlockPartitioner) ExtractCodeBlock(
	subbandData []int32,
	subbandWidth, subbandHeight int,
	blockX, blockY int,
	subbandType SubbandType,
) (*CodeBlock, error)

ExtractCodeBlock extracts a code block from subband data

func (*CodeBlockPartitioner) InsertCodeBlock

func (p *CodeBlockPartitioner) InsertCodeBlock(
	subbandData []int32,
	subbandWidth int,
	blockX, blockY int,
	cb *CodeBlock,
)

InsertCodeBlock inserts a code block back into subband data

func (*CodeBlockPartitioner) NumBlocksX

func (p *CodeBlockPartitioner) NumBlocksX(subbandWidth int) int

NumBlocksX returns the number of code blocks in X direction

func (*CodeBlockPartitioner) NumBlocksY

func (p *CodeBlockPartitioner) NumBlocksY(subbandHeight int) int

NumBlocksY returns the number of code blocks in Y direction

func (*CodeBlockPartitioner) PartitionSubband

func (p *CodeBlockPartitioner) PartitionSubband(
	subbandData []int32,
	subbandWidth, subbandHeight int,
	subbandType SubbandType,
) ([]*CodeBlock, error)

PartitionSubband partitions an entire subband into code blocks

type CodeBlockState

type CodeBlockState struct {
	Included      bool // Has been included in a previous layer
	NumPasses     int  // Total passes decoded so far
	ZeroBitPlanes int  // Number of leading zero bit-planes
}

CodeBlockState tracks state for code-blocks across layers

type CodingPass

type CodingPass struct {
	Type      int  // PassSigProp, PassMagRef, or PassCleanup
	BitPlane  int  // Which bit plane
	NumBytes  int  // Bytes used for this pass
	Truncated bool // Whether pass is truncated
}

CodingPass represents a single coding pass

type CodingPassInfo

type CodingPassInfo struct {
	CodeBlockID int     // Code block identifier
	PassIndex   int     // Pass index within the code block
	PassType    int     // Pass type (0=sig prop, 1=mag ref, 2=cleanup)
	Rate        int     // Cumulative bytes for this pass
	Distortion  float64 // Reduction in distortion from this pass
	Slope       float64 // Rate-distortion slope
	Layer       int     // Assigned layer (-1 if not assigned)
}

CodingPassInfo contains information about a single coding pass

type EncodedCodeBlockInfo

type EncodedCodeBlockInfo struct {
	Index         int
	Included      bool
	NewPasses     int
	DataLength    int
	ZeroBitPlanes int
	Data          []byte
}

EncodedCodeBlockInfo contains encoded code block information

type EncodedPacket

type EncodedPacket struct {
	Header     []byte
	Data       []byte
	Layer      int
	Resolution int
	Component  int
	Precinct   int
	Empty      bool
	CodeBlocks []EncodedCodeBlockInfo
}

EncodedPacket represents an encoded JPEG 2000 packet

type EncodedPass

type EncodedPass struct {
	Type     int    // PassSigProp, PassMagRef, or PassCleanup
	BitPlane int    // Which bit plane
	Data     []byte // Encoded data
}

EncodedPass represents an encoded coding pass

type Layer

type Layer struct {
	Index      int       // Layer index (0 = base quality)
	Packets    []*Packet // Packets in this layer
	TotalBytes int       // Total bytes in layer
}

Layer represents a quality layer

type LayerInfo

type LayerInfo struct {
	Index      int
	Slope      float64 // Threshold slope for this layer
	Rate       int     // Total bytes in this layer
	Distortion float64 // Total distortion reduction
	Passes     []*CodingPassInfo
}

LayerInfo contains information about a quality layer

type OptimalTruncation

type OptimalTruncation struct {
	CodeBlockID     int
	TruncationPoint int // Number of passes to include
	Rate            int // Bytes used
	Distortion      float64
}

OptimalTruncation finds optimal truncation points for rate control

type Packet

type Packet struct {
	LayerIndex     int    // Quality layer index
	ResLevel       int    // Resolution level
	ComponentIndex int    // Component index
	PrecinctIndex  int    // Precinct index
	Empty          bool   // True if packet has no data
	Data           []byte // Packet body (code-block data)
	CodeBlocks     []PacketCodeBlock
}

Packet represents a JPEG 2000 packet

type PacketCodeBlock

type PacketCodeBlock struct {
	Index         int  // Code-block index within precinct
	Included      bool // Whether code-block is included in this layer
	NumPasses     int  // Number of coding passes included
	DataLength    int  // Length of code-block data in this packet
	NewPasses     int  // New passes in this layer
	ZeroBitPlanes int  // Number of zero bit planes (first inclusion)
}

PacketCodeBlock represents code-block info within a packet

type PacketEncoder

type PacketEncoder struct {
	// contains filtered or unexported fields
}

PacketEncoder encodes a complete packet

func NewPacketEncoder

func NewPacketEncoder(cbWidth, cbHeight int) (*PacketEncoder, error)

NewPacketEncoder creates a new packet encoder

func (*PacketEncoder) EncodePacket

func (p *PacketEncoder) EncodePacket(
	layer int,
	codeBlocks []EncodedCodeBlockInfo,
	cbWidth, _ int,
) *EncodedPacket

EncodePacket encodes a packet for the given code blocks and layer

func (*PacketEncoder) Reset

func (p *PacketEncoder) Reset(cbWidth, cbHeight int) error

Reset resets the packet encoder for a new precinct

type PacketEncoderCBState

type PacketEncoderCBState struct {
	Included      bool // Has been included in any previous layer
	NumPasses     int  // Total passes encoded so far
	ZeroBitPlanes int  // Number of leading zero bit-planes
}

PacketEncoderCBState tracks code block state for packet encoding

type Precinct

type Precinct struct {
	Index          int // Precinct index
	ResLevel       int // Resolution level
	NumCodeBlocksX int // Code-blocks horizontally
	NumCodeBlocksY int // Code-blocks vertically
}

Precinct represents a precinct (spatial subdivision)

type ProgressionOrder

type ProgressionOrder int

ProgressionOrder represents packet progression order

const (
	ProgressionLRCP ProgressionOrder = iota // Layer-Resolution-Component-Position
	ProgressionRLCP                         // Resolution-Layer-Component-Position
	ProgressionRPCL                         // Resolution-Position-Component-Layer
	ProgressionPCRL                         // Position-Component-Resolution-Layer
	ProgressionCPRL                         // Component-Position-Resolution-Layer
)

func (ProgressionOrder) String

func (p ProgressionOrder) String() string

String returns the progression order name

type RateAllocator

type RateAllocator struct {
	// Target parameters
	NumLayers   int       // Number of quality layers
	TargetRates []float64 // Target bitrate for each layer (bits per pixel)
	TargetBytes []int     // Target bytes for each layer (alternative to rates)

	// Image parameters
	ImageWidth    int
	ImageHeight   int
	NumComponents int
	// contains filtered or unexported fields
}

RateAllocator implements Post-Compression Rate-Distortion (PCRD) optimization for JPEG 2000 quality layer formation.

func NewRateAllocator

func NewRateAllocator(numLayers int) *RateAllocator

NewRateAllocator creates a new rate allocator

func (*RateAllocator) AddCodeBlockPasses

func (ra *RateAllocator) AddCodeBlockPasses(cbID int, passes []EncodedPass)

AddCodeBlockPasses adds all coding passes from an encoded code block

func (*RateAllocator) AddCodingPass

func (ra *RateAllocator) AddCodingPass(cbID, passIndex, passType, rate int, distortion float64)

AddCodingPass adds coding pass information for rate allocation

func (*RateAllocator) AllocateLayers

func (ra *RateAllocator) AllocateLayers() []*LayerInfo

AllocateLayers performs PCRD-opt layer allocation

func (*RateAllocator) ComputeSlopes

func (ra *RateAllocator) ComputeSlopes()

ComputeSlopes computes rate-distortion slopes for all passes

func (*RateAllocator) FindOptimalTruncations

func (ra *RateAllocator) FindOptimalTruncations(targetBytes int) []OptimalTruncation

FindOptimalTruncations finds optimal truncation points for a target rate

func (*RateAllocator) GetLayerAssignments

func (ra *RateAllocator) GetLayerAssignments() map[int][]int

GetLayerAssignments returns layer assignments for code blocks

func (*RateAllocator) Reset

func (ra *RateAllocator) Reset()

Reset clears all stored passes

func (*RateAllocator) SetImageSize

func (ra *RateAllocator) SetImageSize(width, height int)

SetImageSize sets the image dimensions

func (*RateAllocator) SetTargetBytes

func (ra *RateAllocator) SetTargetBytes(layer int, bytes int)

SetTargetBytes sets the target bytes for a layer

func (*RateAllocator) SetTargetRate

func (ra *RateAllocator) SetTargetRate(layer int, bpp float64)

SetTargetRate sets the target bitrate for a layer

type SubbandType

type SubbandType int

SubbandType represents the subband orientation

const (
	SubbandLL SubbandType = iota
	SubbandHL
	SubbandLH
	SubbandHH
)

type TagTree

type TagTree struct {
	Width  int     // Number of leaves horizontally
	Height int     // Number of leaves vertically
	Levels int     // Number of levels in tree
	Values [][]int // Values at each level
	States [][]int // Current states at each level (for decoding)
}

TagTree represents a tag tree for packet header coding Tag trees are used for efficient coding of code-block inclusion and zero bit-plane information

func NewTagTree

func NewTagTree(width, height int) (*TagTree, error)

NewTagTree creates a new tag tree for the given dimensions

func (*TagTree) GetValue

func (tt *TagTree) GetValue(x, y int) int

GetValue returns the value at leaf position (x, y)

func (*TagTree) Reset

func (tt *TagTree) Reset()

Reset resets the tag tree state for a new decode

func (*TagTree) SetValue

func (tt *TagTree) SetValue(x, y, value int)

SetValue sets the value at leaf position (x, y)

type TagTreeEncoder

type TagTreeEncoder struct {
	// contains filtered or unexported fields
}

TagTreeEncoder encodes values using tag trees

func NewTagTreeEncoder

func NewTagTreeEncoder(width, height int) (*TagTreeEncoder, error)

NewTagTreeEncoder creates a new tag tree encoder

func (*TagTreeEncoder) Encode

func (t *TagTreeEncoder) Encode(w *Tier2Encoder, x, y, threshold int)

Encode encodes a value through the tag tree

func (*TagTreeEncoder) EncodeNotIncluded

func (t *TagTreeEncoder) EncodeNotIncluded(w *Tier2Encoder, x, y, threshold int)

EncodeNotIncluded encodes that a value exceeds threshold

func (*TagTreeEncoder) Reset

func (t *TagTreeEncoder) Reset()

Reset resets the tag tree encoder state

func (*TagTreeEncoder) SetValue

func (t *TagTreeEncoder) SetValue(x, y, value int)

SetValue sets the value at leaf position

type Tier1Decoder

type Tier1Decoder struct {
	// contains filtered or unexported fields
}

Tier1Decoder decodes EBCOT Tier-1 coded data

func NewTier1Decoder

func NewTier1Decoder(data []byte) *Tier1Decoder

NewTier1Decoder creates a new Tier-1 decoder

func (*Tier1Decoder) DecodeCodeBlock

func (t *Tier1Decoder) DecodeCodeBlock(block *CodeBlock, passes []CodingPass) error

DecodeCodeBlock decodes an entire code block

func (*Tier1Decoder) DecodePass

func (t *Tier1Decoder) DecodePass(passType int, bitPlane int) error

DecodePass decodes a single coding pass

func (*Tier1Decoder) GetMQDecoder

func (t *Tier1Decoder) GetMQDecoder() *mq.Decoder

GetMQDecoder returns the underlying MQ decoder

func (*Tier1Decoder) Init

func (t *Tier1Decoder) Init(block *CodeBlock) error

Init initializes the Tier-1 decoder with a code block

type Tier1Encoder

type Tier1Encoder struct {
	// contains filtered or unexported fields
}

Tier1Encoder encodes wavelet coefficients using EBCOT Tier-1 algorithm

func NewTier1Encoder

func NewTier1Encoder() *Tier1Encoder

NewTier1Encoder creates a new Tier-1 encoder

func (*Tier1Encoder) EncodeCodeBlock

func (t *Tier1Encoder) EncodeCodeBlock(block *CodeBlock) ([]EncodedPass, error)

EncodeCodeBlock encodes an entire code block and returns the encoded passes

func (*Tier1Encoder) GetMQEncoder

func (t *Tier1Encoder) GetMQEncoder() *mq.Encoder

GetMQEncoder returns the underlying MQ encoder

func (*Tier1Encoder) Init

func (t *Tier1Encoder) Init()

Init initializes the encoder for a new code block

func (*Tier1Encoder) SetBlock

func (t *Tier1Encoder) SetBlock(block *CodeBlock) error

SetBlock sets the code block to encode

type Tier2Decoder

type Tier2Decoder struct {
	// contains filtered or unexported fields
}

Tier2Decoder decodes EBCOT Tier-2 packet headers

func NewTier2Decoder

func NewTier2Decoder(data []byte) *Tier2Decoder

NewTier2Decoder creates a new Tier-2 decoder

func (*Tier2Decoder) ByteAlign

func (t *Tier2Decoder) ByteAlign()

ByteAlign aligns reading to the next byte boundary

func (*Tier2Decoder) DecodePacketHeader

func (t *Tier2Decoder) DecodePacketHeader(layer int, cbWidth, cbHeight int) (*Packet, error)

DecodePacketHeader decodes a packet header

func (*Tier2Decoder) DecodeTagTreeValue

func (t *Tier2Decoder) DecodeTagTreeValue(tt *TagTree, x, y, threshold int) (int, error)

DecodeTagTreeValue decodes a value from a tag tree using the packet header bits

func (*Tier2Decoder) InitPrecinct

func (t *Tier2Decoder) InitPrecinct(cbWidth, cbHeight int) error

InitPrecinct initializes tag trees for a precinct

func (*Tier2Decoder) Position

func (t *Tier2Decoder) Position() int

Position returns current byte position

func (*Tier2Decoder) ReadBit

func (t *Tier2Decoder) ReadBit() (int, error)

ReadBit reads a single bit from the packet data

func (*Tier2Decoder) ReadBits

func (t *Tier2Decoder) ReadBits(n int) (int, error)

ReadBits reads n bits from the packet data

func (*Tier2Decoder) Remaining

func (t *Tier2Decoder) Remaining() int

Remaining returns bytes remaining

func (*Tier2Decoder) Reset

func (t *Tier2Decoder) Reset(data []byte)

Reset resets the decoder for new data

type Tier2Encoder

type Tier2Encoder struct {
	// contains filtered or unexported fields
}

Tier2Encoder encodes EBCOT Tier-2 packets

func NewTier2Encoder

func NewTier2Encoder() *Tier2Encoder

NewTier2Encoder creates a new Tier-2 encoder

func (*Tier2Encoder) ByteAlign

func (t *Tier2Encoder) ByteAlign()

ByteAlign aligns output to byte boundary (pads with zeros)

func (*Tier2Encoder) Bytes

func (t *Tier2Encoder) Bytes() []byte

Bytes returns the encoded data

func (*Tier2Encoder) Reset

func (t *Tier2Encoder) Reset()

Reset resets the encoder for a new packet

func (*Tier2Encoder) WriteBit

func (t *Tier2Encoder) WriteBit(bit int)

WriteBit writes a single bit

func (*Tier2Encoder) WriteBits

func (t *Tier2Encoder) WriteBits(value, n int)

WriteBits writes n bits from value (MSB first)

Jump to

Keyboard shortcuts

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