arithmetic

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: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DCContextS0 is for zero/sign decision
	DCContextS0 = 0
	// DCContextSS through DCContextSP are for sign-related decisions
	DCContextSS = 1
	DCContextSP = 2
	// DCContextX1 through DCContextX2 are for magnitude bins
	DCContextX1 = 3
	DCContextX2 = 4
	// DCContextMag is for magnitude bits
	DCContextMag = 5

	// DCContextsPerComponent is the total DC contexts per component
	DCContextsPerComponent = 6
)

DC context indices (per JPEG spec)

View Source
const (
	// ACContextSE is for End-of-Block decision
	ACContextSE = 0
	// ACContextS0 is for zero run decision
	ACContextS0 = 1
	// ACContextSN is for sign of negative
	ACContextSN = 2
	// ACContextSP is for sign of positive
	ACContextSP = 3
	// ACContextX1 is for |V|>1 decision
	ACContextX1 = 4
	// ACContextX2 is for |V|>2 decision
	ACContextX2 = 5
	// ACContextMag is for magnitude bits
	ACContextMag = 6
)

AC context indices

View Source
const (
	MarkerSOF9  = 0xC9 // Extended sequential DCT, arithmetic coding
	MarkerSOF10 = 0xCA // Progressive DCT, arithmetic coding
	MarkerSOF11 = 0xCB // Lossless (sequential), arithmetic coding
	MarkerDAC   = 0xCC // Define arithmetic coding conditioning(s)
	MarkerSOS   = 0xDA // Start of scan
)

Marker constants for arithmetic-coded JPEG

View Source
const (
	// MaxContexts is the maximum number of contexts
	MaxContexts = 256

	// MaxStates is the number of QM-coder states (0-112)
	MaxStates = 113

	// MaxIterationsPerBlock limits iterations to prevent infinite loops
	MaxIterationsPerBlock = 10000

	// MaxRenormIterations limits renormalization loops
	MaxRenormIterations = 32

	// InitialA is the initial value of the A register
	InitialA = 0x10000

	// MinA is the minimum valid A register value before renormalization
	MinA = 0x8000
)

Constants for arithmetic coding limits

Variables

View Source
var (
	ErrInvalidState   = fmt.Errorf("invalid QM-coder state")
	ErrInvalidContext = fmt.Errorf("invalid context index")
	ErrInvalidMarker  = fmt.Errorf("invalid marker")
	ErrMaxIterations  = fmt.Errorf("maximum iterations exceeded")
)

Errors for arithmetic coding

Functions

func DefaultConditioningTables

func DefaultConditioningTables() map[int]*ConditioningTable

DefaultConditioningTables returns the default conditioning parameters. These are used when no DAC marker is present.

func FrameTypeName

func FrameTypeName(marker byte) string

FrameTypeName returns the human-readable name for a frame type.

Types

type ACContextSelector

type ACContextSelector struct {
	// Kx is the AC conditioning parameter from DAC marker (1-63)
	Kx int
	// contains filtered or unexported fields
}

ACContextSelector selects AC coding contexts based on coefficient position and the sum of previously decoded coefficients (SE context). Per JPEG spec, the context depends on zigzag position and Kx parameter.

func NewACContextSelector

func NewACContextSelector(kx int) *ACContextSelector

NewACContextSelector creates a new AC context selector.

func (*ACContextSelector) GetBandSum

func (a *ACContextSelector) GetBandSum() int

GetBandSum returns the current band sum.

func (*ACContextSelector) GetMagnitudeContext

func (a *ACContextSelector) GetMagnitudeContext(baseContext int) int

GetMagnitudeContext returns the context for additional magnitude bits.

func (*ACContextSelector) GetS0Context

func (a *ACContextSelector) GetS0Context(baseContext, k int) int

GetS0Context returns the context for zero-run decision. This decides if the next coefficient in sequence is zero.

func (*ACContextSelector) GetSEContext

func (a *ACContextSelector) GetSEContext(baseContext, k int) int

GetSEContext returns the context for End-of-Block decision. Per spec, SE context depends on:

  • zigzag position k
  • accumulated band sum

Returns context index for SE decision.

func (*ACContextSelector) GetSignContext

func (a *ACContextSelector) GetSignContext(baseContext int, negative bool) int

GetSignContext returns the context for sign decision. negative: true for negative sign context, false for positive

func (*ACContextSelector) GetX1Context

func (a *ACContextSelector) GetX1Context(baseContext, k int) int

GetX1Context returns the context for |V|>1 decision.

func (*ACContextSelector) GetX2Context

func (a *ACContextSelector) GetX2Context(baseContext, k int) int

GetX2Context returns the context for |V|>2 decision.

func (*ACContextSelector) ResetBandSum

func (a *ACContextSelector) ResetBandSum()

ResetBandSum resets the band sum (called at start of each block).

func (*ACContextSelector) UpdateBandSum

func (a *ACContextSelector) UpdateBandSum(coeff int)

UpdateBandSum adds the absolute value of a decoded coefficient to the band sum.

type CoefficientDecoder

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

CoefficientDecoder decodes DC and AC coefficients from arithmetic-coded data.

func NewCoefficientDecoder

func NewCoefficientDecoder(data []byte, numComponents int, conditioning map[int]*ConditioningTable, validator Validator) (*CoefficientDecoder, error)

NewCoefficientDecoder creates a new coefficient decoder.

func (*CoefficientDecoder) DecodeAC

func (d *CoefficientDecoder) DecodeAC(component, spectralStart, spectralEnd int) ([]int, error)

DecodeAC decodes AC coefficients for a block. Returns up to 63 AC coefficients (indices 1-63 in zigzag order). For progressive mode, spectralStart and spectralEnd define the band.

func (*CoefficientDecoder) DecodeBlock

func (d *CoefficientDecoder) DecodeBlock(component int) ([]int, error)

DecodeBlock decodes a full 8x8 block (DC + 63 AC coefficients).

func (*CoefficientDecoder) DecodeDC

func (d *CoefficientDecoder) DecodeDC(component int) (int, error)

DecodeDC decodes a DC coefficient for the given component. Returns the decoded DC difference value.

func (*CoefficientDecoder) ResetAllDC

func (d *CoefficientDecoder) ResetAllDC()

ResetAllDC resets all DC predictors.

func (*CoefficientDecoder) ResetBlock

func (d *CoefficientDecoder) ResetBlock(component int)

ResetBlock prepares for a new block.

func (*CoefficientDecoder) ResetDC

func (d *CoefficientDecoder) ResetDC(component int)

ResetDC resets the DC predictor for the given component.

func (*CoefficientDecoder) SetLossless

func (d *CoefficientDecoder) SetLossless(lossless bool)

SetLossless sets lossless mode.

func (*CoefficientDecoder) SetProgressive

func (d *CoefficientDecoder) SetProgressive(progressive bool)

SetProgressive sets progressive mode.

type CoefficientEncoder

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

CoefficientEncoder encodes DC and AC coefficients using arithmetic coding.

func NewCoefficientEncoder

func NewCoefficientEncoder(numComponents int, conditioning map[int]*ConditioningTable, validator Validator) (*CoefficientEncoder, error)

NewCoefficientEncoder creates a new coefficient encoder.

func (*CoefficientEncoder) EncodeAC

func (e *CoefficientEncoder) EncodeAC(component int, coefficients []int, spectralStart, spectralEnd int) error

EncodeAC encodes AC coefficients for a block.

func (*CoefficientEncoder) EncodeBlock

func (e *CoefficientEncoder) EncodeBlock(component int, block []int) error

EncodeBlock encodes a full 8x8 block (DC + 63 AC coefficients).

func (*CoefficientEncoder) EncodeDC

func (e *CoefficientEncoder) EncodeDC(component, diff int) error

EncodeDC encodes a DC coefficient difference for the given component.

func (*CoefficientEncoder) Flush

func (e *CoefficientEncoder) Flush() error

Flush finalizes encoding.

func (*CoefficientEncoder) GetBytes

func (e *CoefficientEncoder) GetBytes() []byte

GetBytes returns the encoded data.

func (*CoefficientEncoder) ResetAllDC

func (e *CoefficientEncoder) ResetAllDC()

ResetAllDC resets all DC predictors.

func (*CoefficientEncoder) ResetBlock

func (e *CoefficientEncoder) ResetBlock(component int)

ResetBlock prepares for a new block.

func (*CoefficientEncoder) ResetDC

func (e *CoefficientEncoder) ResetDC(component int)

ResetDC resets the DC predictor for the given component.

func (*CoefficientEncoder) SetLossless

func (e *CoefficientEncoder) SetLossless(lossless bool)

SetLossless sets lossless mode.

func (*CoefficientEncoder) SetProgressive

func (e *CoefficientEncoder) SetProgressive(progressive bool)

SetProgressive sets progressive mode.

type Component

type Component struct {
	// ID is the component identifier (1-255)
	ID int

	// HorizontalSampling is the horizontal sampling factor (1-4)
	HorizontalSampling int

	// VerticalSampling is the vertical sampling factor (1-4)
	VerticalSampling int

	// QuantTableID is the quantization table selector (0-3)
	QuantTableID int
}

Component represents a single color component in the frame.

type ConditioningTable

type ConditioningTable struct {
	// L is the lower bound for DC conditioning (0-255)
	L uint8

	// U is the upper bound for DC conditioning (0-255)
	U uint8

	// Kx is the AC conditioning parameter (1-63)
	Kx uint8
}

ConditioningTable holds the conditioning parameters from DAC marker.

type Context

type Context struct {
	// State is the current state index (0-112)
	State uint8

	// MPS is the more probable symbol (0 or 1)
	MPS uint8
}

Context represents a single arithmetic coding context.

type ContextManager

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

ContextManager manages all contexts for arithmetic decoding. It provides the proper context indices for DC and AC coefficient decoding.

func NewContextManager

func NewContextManager(numComponents int, conditioning map[int]*ConditioningTable) *ContextManager

NewContextManager creates a new context manager for the given number of components. Returns nil if numComponents is invalid (less than 1).

func (*ContextManager) GetACSelector

func (cm *ContextManager) GetACSelector(component int) *ACContextSelector

GetACSelector returns the AC context selector for the given component.

func (*ContextManager) GetDCSelector

func (cm *ContextManager) GetDCSelector(component int) *DCContextSelector

GetDCSelector returns the DC context selector for the given component.

func (*ContextManager) ResetAll

func (cm *ContextManager) ResetAll()

ResetAll resets all context selectors.

func (*ContextManager) ResetBlock

func (cm *ContextManager) ResetBlock(component int)

ResetBlock resets context state for a new block.

func (*ContextManager) TotalContexts

func (cm *ContextManager) TotalContexts() int

TotalContexts returns the total number of contexts needed. This is used when initializing the QM decoder. Returns MaxContexts (256) as that's the limit defined by the QM-coder.

type DACParser

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

DACParser parses DAC (Define Arithmetic Coding Conditioning) markers.

func NewDACParser

func NewDACParser(validator Validator) *DACParser

NewDACParser creates a new DAC parser.

func (*DACParser) ParseDAC

func (p *DACParser) ParseDAC(r io.Reader) (map[int]*ConditioningTable, error)

ParseDAC parses a DAC marker. Returns a map of table class/destination to conditioning parameters. The reader should be positioned at the start of the marker (0xFF 0xCC).

type DCContextSelector

type DCContextSelector struct {
	// L is the lower conditioning bound from DAC marker
	L int
	// U is the upper conditioning bound from DAC marker
	U int
	// contains filtered or unexported fields
}

DCContextSelector selects DC coding contexts based on prior DC differences. Per JPEG spec (ITU-T T.81), the DC context is determined by comparing the predicted DC difference magnitude to conditioning bounds L and U.

func NewDCContextSelector

func NewDCContextSelector(l, u int) *DCContextSelector

NewDCContextSelector creates a new DC context selector with the given conditioning parameters from the DAC marker.

func (*DCContextSelector) Classify

func (d *DCContextSelector) Classify() int

Classify returns the classification (0, 1, or 2) of the previous DC difference. This is used to select among different context sets. Classification:

  • 0: |prevDiff| <= L (small difference)
  • 1: L < |prevDiff| <= U (medium difference)
  • 2: |prevDiff| > U (large difference)

func (*DCContextSelector) GetMagnitudeContext

func (d *DCContextSelector) GetMagnitudeContext(baseContext int) int

GetMagnitudeContext returns the context for magnitude bits.

func (*DCContextSelector) GetS0Context

func (d *DCContextSelector) GetS0Context(baseContext int) int

GetS0Context returns the context index for the zero/sign decision. The context depends on the classification of the previous difference.

func (*DCContextSelector) GetSPContext

func (d *DCContextSelector) GetSPContext(baseContext int) int

GetSPContext returns the context for positive sign detection.

func (*DCContextSelector) GetSSContext

func (d *DCContextSelector) GetSSContext(baseContext int) int

GetSSContext returns the context for negative sign detection.

func (*DCContextSelector) GetXContext

func (d *DCContextSelector) GetXContext(baseContext int, xIndex int) int

GetXContext returns the context for magnitude classification. xIndex: 0 for X1 (|V|>1 decision), 1 for X2 (|V|>2 decision)

func (*DCContextSelector) Reset

func (d *DCContextSelector) Reset()

Reset resets the DC context selector state.

func (*DCContextSelector) UpdatePreviousDifference

func (d *DCContextSelector) UpdatePreviousDifference(diff int)

UpdatePreviousDifference updates the stored previous difference.

type DefaultValidator

type DefaultValidator struct{}

DefaultValidator implements Validator with standard checks.

func NewValidator

func NewValidator() *DefaultValidator

NewValidator creates a new default validator.

func (*DefaultValidator) SafeAdd

func (*DefaultValidator) SafeAdd(a, b int) (int, error)

SafeAdd performs addition with overflow checking.

func (*DefaultValidator) SafeMultiply

func (*DefaultValidator) SafeMultiply(a, b int) (int, error)

SafeMultiply performs multiplication with overflow checking.

func (*DefaultValidator) SafeSubtract

func (*DefaultValidator) SafeSubtract(a, b int) (int, error)

SafeSubtract performs subtraction with underflow checking.

func (*DefaultValidator) ValidateContextIndex

func (*DefaultValidator) ValidateContextIndex(index int) error

ValidateContextIndex validates context index.

func (*DefaultValidator) ValidateDimensions

func (*DefaultValidator) ValidateDimensions(width, height int) error

ValidateDimensions validates width and height.

func (*DefaultValidator) ValidatePrecision

func (*DefaultValidator) ValidatePrecision(precision int) error

ValidatePrecision validates sample precision (8 or 12 for arithmetic JPEG).

func (*DefaultValidator) ValidateSpectralSelection

func (*DefaultValidator) ValidateSpectralSelection(ss, se int) error

ValidateSpectralSelection validates Ss and Se.

func (*DefaultValidator) ValidateStateIndex

func (*DefaultValidator) ValidateStateIndex(state int) error

ValidateStateIndex validates QM-coder state index (0-112).

func (*DefaultValidator) ValidateSuccessiveApprox

func (*DefaultValidator) ValidateSuccessiveApprox(ah, al int) error

ValidateSuccessiveApprox validates Ah and Al.

type Frame

type Frame struct {
	// Precision is the sample precision in bits (8 or 12)
	Precision int

	// Height is the image height in pixels
	Height int

	// Width is the image width in pixels
	Width int

	// Components contains the color component specifications
	Components []Component

	// IsProgressive indicates SOF10 (progressive arithmetic)
	IsProgressive bool

	// IsLossless indicates SOF11 (lossless arithmetic)
	IsLossless bool
}

Frame represents an arithmetic-coded JPEG frame (SOF9/10/11).

type FrameParser

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

FrameParser parses arithmetic-coded JPEG frame markers (SOF9/10/11).

func NewFrameParser

func NewFrameParser(validator Validator) *FrameParser

NewFrameParser creates a new frame parser with the given validator.

func (*FrameParser) ParseSOF

func (p *FrameParser) ParseSOF(r io.Reader, expectedMarker byte) (*Frame, error)

ParseSOF parses a SOF9, SOF10, or SOF11 marker. The reader should be positioned at the marker byte (0xC9, 0xCA, or 0xCB).

func (*FrameParser) ParseSOF9

func (p *FrameParser) ParseSOF9(r io.Reader) (*Frame, error)

ParseSOF9 parses a SOF9 (extended sequential DCT, arithmetic) marker.

func (*FrameParser) ParseSOF10

func (p *FrameParser) ParseSOF10(r io.Reader) (*Frame, error)

ParseSOF10 parses a SOF10 (progressive DCT, arithmetic) marker.

func (*FrameParser) ParseSOF11

func (p *FrameParser) ParseSOF11(r io.Reader) (*Frame, error)

ParseSOF11 parses a SOF11 (lossless, arithmetic) marker.

type LosslessDecoder

type LosslessDecoder struct {
	*CoefficientDecoder
	// contains filtered or unexported fields
}

LosslessDecoder wraps CoefficientDecoder for SOF11 lossless mode.

func NewLosslessDecoder

func NewLosslessDecoder(data []byte, numComponents int, conditioning map[int]*ConditioningTable, precision int, validator Validator) (*LosslessDecoder, error)

NewLosslessDecoder creates a decoder for SOF11 lossless arithmetic JPEG.

func (*LosslessDecoder) DecodeDifference

func (d *LosslessDecoder) DecodeDifference(component int) (int, error)

DecodeDifference decodes a single lossless difference value.

func (*LosslessDecoder) DecodeRow

func (d *LosslessDecoder) DecodeRow(component, width int) ([]int, error)

DecodeRow decodes a row of samples for the given component.

func (*LosslessDecoder) GetPrecision

func (d *LosslessDecoder) GetPrecision() int

GetPrecision returns the sample precision.

type LosslessEncoder

type LosslessEncoder struct {
	*CoefficientEncoder
	// contains filtered or unexported fields
}

LosslessEncoder wraps CoefficientEncoder for SOF11 lossless mode.

func NewLosslessEncoder

func NewLosslessEncoder(numComponents int, conditioning map[int]*ConditioningTable, precision int, validator Validator) (*LosslessEncoder, error)

NewLosslessEncoder creates an encoder for SOF11 lossless arithmetic JPEG.

func (*LosslessEncoder) EncodeDifference

func (e *LosslessEncoder) EncodeDifference(component, diff int) error

EncodeDifference encodes a single lossless difference value.

func (*LosslessEncoder) EncodeRow

func (e *LosslessEncoder) EncodeRow(component int, diffs []int) error

EncodeRow encodes a row of differences for the given component.

type ProgressiveDecoder

type ProgressiveDecoder struct {
	*CoefficientDecoder
	// contains filtered or unexported fields
}

ProgressiveDecoder wraps CoefficientDecoder for SOF10 progressive mode.

func NewProgressiveDecoder

func NewProgressiveDecoder(numComponents, blocksPerComponent int, _ map[int]*ConditioningTable, _ Validator) *ProgressiveDecoder

NewProgressiveDecoder creates a decoder for SOF10 progressive arithmetic JPEG.

func (*ProgressiveDecoder) DecodeACScan

func (d *ProgressiveDecoder) DecodeACScan(component, spectralStart, spectralEnd, al int) error

DecodeACScan decodes an AC scan (Ss>0).

func (*ProgressiveDecoder) DecodeDCScan

func (d *ProgressiveDecoder) DecodeDCScan(components []int, al int) error

DecodeDCScan decodes a DC-only scan (Ss=0, Se=0).

func (*ProgressiveDecoder) GetCoefficients

func (d *ProgressiveDecoder) GetCoefficients(component int) [][]int

GetCoefficients returns the accumulated coefficients for a component.

func (*ProgressiveDecoder) InitScan

func (d *ProgressiveDecoder) InitScan(data []byte, numComponents int, conditioning map[int]*ConditioningTable, validator Validator) error

InitScan initializes the decoder for a new scan with given data.

type QMDecoder

type QMDecoder struct {
	// A is the interval register (probability interval)
	A uint32

	// C is the code register (holds compressed data)
	C uint32

	// CT is the bit counter (bits remaining in current byte)
	CT int
	// contains filtered or unexported fields
}

QMDecoder implements the QM-coder arithmetic decoder.

func NewQMDecoder

func NewQMDecoder(data []byte, numContexts int, validator Validator) (*QMDecoder, error)

NewQMDecoder creates a new QM-coder decoder.

func (*QMDecoder) BytesRemaining

func (d *QMDecoder) BytesRemaining() int

BytesRemaining returns the number of unread bytes.

func (*QMDecoder) DecodeBit

func (d *QMDecoder) DecodeBit(contextIndex int) (int, error)

DecodeBit decodes a single bit using the specified context.

func (*QMDecoder) DecodeValue

func (d *QMDecoder) DecodeValue(contextIndex, numBits int) (int, error)

DecodeValue decodes an unsigned value of the given bit width.

func (*QMDecoder) GetContext

func (d *QMDecoder) GetContext(index int) (*Context, error)

GetContext returns the current state of a context.

func (*QMDecoder) Position

func (d *QMDecoder) Position() int

Position returns the current position in the data stream.

func (*QMDecoder) ResetAllContexts

func (d *QMDecoder) ResetAllContexts()

ResetAllContexts resets all contexts to initial state.

func (*QMDecoder) ResetContext

func (d *QMDecoder) ResetContext(contextIndex int) error

ResetContext resets a context to its initial state.

func (*QMDecoder) ResetIterationCount

func (d *QMDecoder) ResetIterationCount()

ResetIterationCount resets the iteration counter (call at block boundaries).

type QMEncoder

type QMEncoder struct {
	// A is the interval register (probability interval)
	A uint32

	// C is the code register (accumulates encoded bits)
	C uint32

	// CT is the bit counter
	CT int

	// SC is the stack counter for carry propagation.
	// It counts candidate 0xFF bytes that cannot yet be flushed to the output
	// because a later carry may turn them into 0x00 bytes. See T.81 D.1.7.
	SC int

	// BP is the output buffer position (kept for backward-compatible callers
	// that read BytesWritten while encoding is in progress; it tracks bytes
	// already committed to the output buffer).
	BP int
	// contains filtered or unexported fields
}

QMEncoder implements the QM-coder arithmetic encoder. This is the encoding counterpart to QMDecoder.

func NewQMEncoder

func NewQMEncoder(numContexts int, validator Validator) (*QMEncoder, error)

NewQMEncoder creates a new QM-coder encoder.

func (*QMEncoder) BytesWritten

func (e *QMEncoder) BytesWritten() int

BytesWritten returns the number of bytes written so far.

func (*QMEncoder) EncodeBit

func (e *QMEncoder) EncodeBit(contextIndex int, bit int) error

EncodeBit encodes a single bit using the specified context.

func (*QMEncoder) EncodeValue

func (e *QMEncoder) EncodeValue(contextIndex, value, numBits int) error

EncodeValue encodes an unsigned value of the given bit width.

func (*QMEncoder) Flush

func (e *QMEncoder) Flush() error

Flush finalizes the encoding and flushes remaining bits.

func (*QMEncoder) GetBytes

func (e *QMEncoder) GetBytes() []byte

GetBytes returns the encoded data.

func (*QMEncoder) GetContext

func (e *QMEncoder) GetContext(index int) (*Context, error)

GetContext returns the current state of a context.

func (*QMEncoder) Reset

func (e *QMEncoder) Reset()

Reset resets the encoder for reuse.

func (*QMEncoder) ResetAllContexts

func (e *QMEncoder) ResetAllContexts()

ResetAllContexts resets all contexts to initial state.

func (*QMEncoder) ResetContext

func (e *QMEncoder) ResetContext(contextIndex int) error

ResetContext resets a specific context to initial state.

func (*QMEncoder) WriteTo

func (e *QMEncoder) WriteTo(w io.Writer) (int64, error)

WriteTo writes the encoded data to a writer.

type QMState

type QMState struct {
	// Qe is the probability estimate (scaled by 2^16)
	Qe uint16

	// NMPS is the next state index after MPS (more probable symbol)
	NMPS uint8

	// NLPS is the next state index after LPS (less probable symbol)
	NLPS uint8

	// SwitchMPS indicates whether to swap MPS sense after LPS
	SwitchMPS bool
}

QMState represents a single state in the QM-coder state table.

func GetState

func GetState(index int) (*QMState, error)

GetState returns the QM-coder state table entry.

type Scan

type Scan struct {
	// Components lists the components in this scan
	Components []ScanComponent

	// SpectralStart is the first DCT coefficient (Ss)
	SpectralStart int

	// SpectralEnd is the last DCT coefficient (Se)
	SpectralEnd int

	// SuccApproxHigh is the successive approximation high bit (Ah)
	SuccApproxHigh int

	// SuccApproxLow is the successive approximation low bit (Al)
	SuccApproxLow int
}

Scan represents a single scan in an arithmetic-coded JPEG.

type ScanComponent

type ScanComponent struct {
	// ComponentSelector identifies the component (matches Component.ID)
	ComponentSelector int

	// DCConditioningTable is the DC conditioning table selector (0-3)
	DCConditioningTable int

	// ACConditioningTable is the AC conditioning table selector (0-3)
	ACConditioningTable int
}

ScanComponent represents a component's parameters within a scan.

type ScanParser

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

ScanParser parses SOS (Start of Scan) markers for arithmetic-coded JPEG.

func NewScanParser

func NewScanParser(validator Validator) *ScanParser

NewScanParser creates a new scan parser.

func (*ScanParser) ParseSOS

func (p *ScanParser) ParseSOS(r io.Reader) (*Scan, error)

ParseSOS parses a SOS marker for arithmetic-coded JPEG. The reader should be positioned at the start of the marker (0xFF 0xDA).

type SequentialDecoder

type SequentialDecoder struct {
	*CoefficientDecoder
}

SequentialDecoder wraps CoefficientDecoder for SOF9 sequential mode.

func NewSequentialDecoder

func NewSequentialDecoder(data []byte, numComponents int, conditioning map[int]*ConditioningTable, validator Validator) (*SequentialDecoder, error)

NewSequentialDecoder creates a decoder for SOF9 sequential arithmetic JPEG.

func (*SequentialDecoder) DecodeImage

func (d *SequentialDecoder) DecodeImage(width, height int, components []int) ([][][]int, error)

DecodeImage decodes all blocks in the image. width and height are in pixels, blockOrder specifies component interleaving.

type SequentialEncoder

type SequentialEncoder struct {
	*CoefficientEncoder
}

SequentialEncoder wraps CoefficientEncoder for SOF9 sequential mode.

func NewSequentialEncoder

func NewSequentialEncoder(numComponents int, conditioning map[int]*ConditioningTable, validator Validator) (*SequentialEncoder, error)

NewSequentialEncoder creates an encoder for SOF9 sequential arithmetic JPEG.

func (*SequentialEncoder) EncodeImage

func (e *SequentialEncoder) EncodeImage(blocks [][][]int) error

EncodeImage encodes all blocks in the image.

type Validator

type Validator interface {
	// ValidatePrecision validates sample precision (8 or 12 for arithmetic).
	ValidatePrecision(precision int) error

	// ValidateDimensions validates width and height.
	ValidateDimensions(width, height int) error

	// ValidateStateIndex validates QM-coder state index (0-112).
	ValidateStateIndex(state int) error

	// ValidateContextIndex validates context index.
	ValidateContextIndex(index int) error

	// ValidateSpectralSelection validates Ss and Se.
	ValidateSpectralSelection(ss, se int) error

	// ValidateSuccessiveApprox validates Ah and Al.
	ValidateSuccessiveApprox(ah, al int) error

	// SafeMultiply performs multiplication with overflow checking.
	SafeMultiply(a, b int) (int, error)

	// SafeAdd performs addition with overflow checking.
	SafeAdd(a, b int) (int, error)

	// SafeSubtract performs subtraction with underflow checking.
	SafeSubtract(a, b int) (int, error)
}

Validator provides validation for arithmetic coding parameters.

Jump to

Keyboard shortcuts

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