huffman

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

Documentation

Index

Constants

View Source
const (
	SOFUnknown                        = 0   // Unknown or not yet parsed
	SOF0BaselineDCT                   = 192 // Baseline DCT
	SOF1ExtendedSequentialDCT         = 193 // Extended Sequential DCT
	SOF2ProgressiveDCT                = 194 // Progressive DCT
	SOF3LosslessSequential            = 195 // Lossless Sequential
	SOF5DifferentialSequentialDCT     = 197 // Differential Sequential DCT
	SOF6DifferentialProgressiveDCT    = 198 // Differential Progressive DCT
	SOF7DifferentialLossless          = 199 // Differential Lossless
	SOF9ArithmeticSequentialDCT       = 201 // Arithmetic Sequential DCT
	SOF10ArithmeticProgressiveDCT     = 202 // Arithmetic Progressive DCT
	SOF11ArithmeticLossless           = 203 // Arithmetic Lossless
	SOF13DiffArithmeticSequentialDCT  = 205 // Differential Arithmetic Sequential DCT
	SOF14DiffArithmeticProgressiveDCT = 206 // Differential Arithmetic Progressive DCT
	SOF15DiffArithmeticLossless       = 207 // Differential Arithmetic Lossless
)

SOF marker type constants for decoder routing

Variables

This section is empty.

Functions

func SOFTypeName

func SOFTypeName(sofType int) string

SOFTypeName returns a human-readable name for an SOF marker type.

Types

type ByteReader

type ByteReader interface {
	// ReadByte reads and returns the next byte.
	ReadByte() (byte, error)

	// Remaining returns the number of unread bytes.
	Remaining() int
}

ByteReader defines an interface for reading bytes with position tracking. This abstraction allows the decoder to work with any byte source.

type Decoder

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

Decoder performs Huffman decoding of JPEG entropy-coded data to extract DCT coefficients. This structure is optimized for memory alignment (largest to smallest).

func NewDecoder

func NewDecoder(_ []byte, reader ByteReader, validator Validator) *Decoder

NewDecoder creates a new Huffman decoder with the given validator. Follows Dependency Inversion Principle by injecting validator.

func NewDecoderForSOF

func NewDecoderForSOF(sofType int, data []byte, reader ByteReader, validator Validator) (*Decoder, error)

NewDecoderForSOF creates a decoder appropriate for the given SOF marker type. Returns the decoder and an error if the SOF type is not supported. This factory function maintains backward compatibility while enabling routing to specialized decoders for different JPEG encoding modes.

func (*Decoder) Decode

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

Decode extracts DCT coefficients from the JPEG data. Returns array of quantized DCT coefficients or error. For progressive JPEG (SOF2), this decodes all scans and returns accumulated coefficients. For lossless JPEG (SOF3), this returns raw pixel values (not DCT coefficients). For differential JPEG (SOF5-7, SOF13-15), this decodes differential values.

func (*Decoder) GetComponentCount

func (d *Decoder) GetComponentCount() int

GetComponentCount returns the number of color components.

func (*Decoder) GetImageHeight

func (d *Decoder) GetImageHeight() int

GetImageHeight returns the image height in pixels.

func (*Decoder) GetImageWidth

func (d *Decoder) GetImageWidth() int

GetImageWidth returns the image width in pixels.

func (*Decoder) GetPrecision

func (d *Decoder) GetPrecision() int

GetPrecision returns the sample precision in bits.

func (*Decoder) GetQuantTableIDs

func (d *Decoder) GetQuantTableIDs() []int

GetQuantTableIDs returns the quantization table ID for each component. Returns a slice where index 0=Y, 1=Cb, 2=Cr.

func (*Decoder) GetQuantizationTables

func (d *Decoder) GetQuantizationTables() map[int][64]int

GetQuantizationTables returns all quantization tables.

func (*Decoder) GetSOFType

func (d *Decoder) GetSOFType() int

GetSOFType returns the detected SOF marker type. Returns 0 (SOFUnknown) if no SOF marker has been parsed yet.

func (*Decoder) GetSamplingFactors

func (d *Decoder) GetSamplingFactors() (horizontal, vertical []int)

GetSamplingFactors returns the horizontal and vertical sampling factors for each component. Returns two slices: horizontal and vertical factors, indexed by component (0=Y, 1=Cb, 2=Cr). For 4:2:0: Y=(2,2), Cb=(1,1), Cr=(1,1) For 4:2:2: Y=(2,1), Cb=(1,1), Cr=(1,1) For 4:4:4: Y=(1,1), Cb=(1,1), Cr=(1,1)

func (*Decoder) GetSpectralSelection

func (d *Decoder) GetSpectralSelection() (ss, se int)

GetSpectralSelection returns the spectral selection parameters (Ss, Se). Only meaningful for progressive JPEG (SOF2).

func (*Decoder) GetSuccessiveApproximation

func (d *Decoder) GetSuccessiveApproximation() (ah, al int)

GetSuccessiveApproximation returns the successive approximation parameters (Ah, Al). Only meaningful for progressive JPEG (SOF2).

func (*Decoder) IsDifferential

func (d *Decoder) IsDifferential() bool

IsDifferential returns true if the image uses differential encoding (SOF5-7, SOF13-15).

func (*Decoder) IsLossless

func (d *Decoder) IsLossless() bool

IsLossless returns true if the image uses lossless encoding (SOF3).

func (*Decoder) IsProgressive

func (d *Decoder) IsProgressive() bool

IsProgressive returns true if the image uses progressive DCT encoding (SOF2).

type Table

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

Table represents a Huffman table for JPEG decoding. This structure follows the JPEG/JFIF specification for Huffman table construction.

func NewTable

func NewTable(reader ByteReader) *Table

NewTable creates a new Huffman table from a byte reader. The reader parameter provides abstracted byte reading.

func (*Table) GetHuffval

func (t *Table) GetHuffval() [256]int

GetHuffval returns the HUFFVAL array.

func (*Table) GetLength

func (t *Table) GetLength() int

GetLength returns the total table length including header.

func (*Table) GetMaxcode

func (t *Table) GetMaxcode() [18]int

GetMaxcode returns the MAXCODE array.

func (*Table) GetMincode

func (t *Table) GetMincode() [17]int

GetMincode returns the MINCODE array.

func (*Table) GetValptr

func (t *Table) GetValptr() [17]int

GetValptr returns the VALPTR array.

type Validator

type Validator interface {
	// ValidateArrayIndex checks if an array index is within bounds
	ValidateArrayIndex(index int, arrayLength int) error

	// ValidateSliceAccess checks if a slice access is safe
	ValidateSliceAccess(slice []int, index int) error

	// ValidateTableIndex checks if a Huffman table index is valid
	ValidateTableIndex(tableIdx int) error

	// ValidateBitCount checks if bit count is within valid range (0-16)
	ValidateBitCount(bits int) error

	// ValidateCoefficientIndex checks if coefficient index is valid (0-63)
	ValidateCoefficientIndex(k int) error
}

Validator provides input validation and bounds checking. Single Responsibility: Only validates inputs, doesn't decode data.

type ValidatorAdapter

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

ValidatorAdapter adapts the parent package Validator to the huffman-specific Validator interface. This follows the Adapter pattern and Dependency Inversion Principle.

func NewValidatorAdapter

func NewValidatorAdapter(parentValidator interface {
	ValidatePosition(pos int, maxPos int) error
},
) *ValidatorAdapter

NewValidatorAdapter creates a new validator adapter.

func (*ValidatorAdapter) ValidateArrayIndex

func (v *ValidatorAdapter) ValidateArrayIndex(index int, arrayLength int) error

ValidateArrayIndex checks if an array index is within bounds.

func (*ValidatorAdapter) ValidateBitCount

func (v *ValidatorAdapter) ValidateBitCount(bits int) error

ValidateBitCount checks if bit count is within valid range (0-16).

func (*ValidatorAdapter) ValidateCoefficientIndex

func (v *ValidatorAdapter) ValidateCoefficientIndex(k int) error

ValidateCoefficientIndex checks if coefficient index is valid (0-63).

func (*ValidatorAdapter) ValidateSliceAccess

func (v *ValidatorAdapter) ValidateSliceAccess(slice []int, index int) error

ValidateSliceAccess checks if a slice access is safe.

func (*ValidatorAdapter) ValidateTableIndex

func (v *ValidatorAdapter) ValidateTableIndex(tableIdx int) error

ValidateTableIndex checks if a Huffman table index is valid (0-3).

Jump to

Keyboard shortcuts

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