Documentation
¶
Index ¶
- Constants
- Variables
- func ApplyPointTransform(value, pointTransform int) int
- func ExtendDifference(bits, category int) int
- func PredictorName(predictor int) string
- type Component
- type DefaultValidator
- func (*DefaultValidator) SafeAdd(a, b int) (int, error)
- func (*DefaultValidator) SafeMultiply(a, b int) (int, error)
- func (*DefaultValidator) SafeSubtract(a, b int) (int, error)
- func (*DefaultValidator) ValidateComponentCount(count int) error
- func (*DefaultValidator) ValidateDimensions(width, height int) error
- func (*DefaultValidator) ValidatePointTransform(pt int) error
- func (*DefaultValidator) ValidatePrecision(precision int) error
- func (*DefaultValidator) ValidatePredictor(predictor int) error
- type Frame
- type FrameParser
- type LosslessDecoder
- func (d *LosslessDecoder) Components() int
- func (d *LosslessDecoder) DecodeComponent(componentIdx int, differences []int) error
- func (d *LosslessDecoder) GetFlatOutput() []int
- func (d *LosslessDecoder) GetOutput(componentIdx int) ([][]int, error)
- func (d *LosslessDecoder) Height() int
- func (d *LosslessDecoder) Precision() int
- func (d *LosslessDecoder) SetScanParameters(predictor, pointTransform int) error
- func (d *LosslessDecoder) Width() int
- type Predictor
- type RowBuffer
- type SampleDecoder
- func (d *SampleDecoder) AdvanceRow()
- func (d *SampleDecoder) DecodeRow(differences []int) ([]int, error)
- func (d *SampleDecoder) DecodeSample(difference int) (int, error)
- func (d *SampleDecoder) GetMaxValue() int
- func (d *SampleDecoder) GetPrecision() int
- func (d *SampleDecoder) Reset()
- func (d *SampleDecoder) SetPointTransform(pt int) error
- func (d *SampleDecoder) SetPredictor(predictor int) error
- type Scan
- type ScanComponent
- type ScanParser
- type Validator
Constants ¶
const ( // MaxRowBufferSize is the maximum row buffer size in bytes (64MB) MaxRowBufferSize = 67108864 // MaxComponents is the maximum number of color components MaxComponents = 4 // MaxPrecision is the maximum sample precision (16 bits) MaxPrecision = 16 // MinPrecision is the minimum sample precision (2 bits) MinPrecision = 2 // MaxPointTransform is the maximum point transform value MaxPointTransform = 15 // MaxPredictor is the maximum predictor selection value MaxPredictor = 7 // MinPredictor is the minimum predictor selection value (1-7 valid) MinPredictor = 1 )
Constants for lossless JPEG limits
const ( PredictorLeft = 1 // Ra (left neighbor) PredictorAbove = 2 // Rb (above neighbor) PredictorDiagonal = 3 // Rc (upper-left diagonal) PredictorLinear = 4 // Ra + Rb - Rc PredictorLeftAvg = 5 // Ra + (Rb - Rc) / 2 PredictorAboveAvg = 6 // Rb + (Ra - Rc) / 2 PredictorAverage = 7 // (Ra + Rb) / 2 )
Predictor constants for lossless JPEG
Variables ¶
var ErrInvalidMarker = fmt.Errorf("invalid marker")
ErrInvalidMarker indicates an invalid marker was encountered during lossless JPEG parsing.
Functions ¶
func ApplyPointTransform ¶
ApplyPointTransform applies the point transform (right shift). In lossless JPEG, samples may be right-shifted before encoding and must be left-shifted after decoding.
func ExtendDifference ¶
ExtendDifference applies sign extension to a difference value. This is the same algorithm used for DC coefficient sign extension in JPEG. Per JPEG specification Figure F.12:
- If the value is less than 2^(category-1), it's negative
- Negative values are in the range [-(2^category - 1), -(2^(category-1))]
Parameters:
- bits: The raw bits read after the Huffman symbol
- category: The SSSS value (0-16) indicating bit length
Returns the signed difference value.
func PredictorName ¶
PredictorName returns the human-readable name for a predictor.
Types ¶
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)
// Note: For lossless JPEG, this is typically 0
QuantTableID int
}
Component represents a single color component in the frame.
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. Uses pre-check algorithm to detect overflow before it occurs.
func (*DefaultValidator) SafeMultiply ¶
func (*DefaultValidator) SafeMultiply(a, b int) (int, error)
SafeMultiply performs multiplication with overflow checking. Uses pre-check algorithm to detect overflow before it occurs.
func (*DefaultValidator) SafeSubtract ¶
func (*DefaultValidator) SafeSubtract(a, b int) (int, error)
SafeSubtract performs subtraction with underflow checking. Uses pre-check algorithm to detect overflow before it occurs.
func (*DefaultValidator) ValidateComponentCount ¶
func (*DefaultValidator) ValidateComponentCount(count int) error
ValidateComponentCount validates number of components.
func (*DefaultValidator) ValidateDimensions ¶
func (*DefaultValidator) ValidateDimensions(width, height int) error
ValidateDimensions validates width and height.
func (*DefaultValidator) ValidatePointTransform ¶
func (*DefaultValidator) ValidatePointTransform(pt int) error
ValidatePointTransform validates point transform (0-15).
func (*DefaultValidator) ValidatePrecision ¶
func (*DefaultValidator) ValidatePrecision(precision int) error
ValidatePrecision validates sample precision (must be 2-16 for lossless).
func (*DefaultValidator) ValidatePredictor ¶
func (*DefaultValidator) ValidatePredictor(predictor int) error
ValidatePredictor validates predictor selection (1-7).
type Frame ¶
type Frame struct {
// Precision is the sample precision in bits (2-16 for lossless)
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
}
Frame represents a lossless JPEG frame (SOF3).
type FrameParser ¶
type FrameParser struct {
// contains filtered or unexported fields
}
FrameParser parses lossless JPEG frame markers (SOF3).
func NewFrameParser ¶
func NewFrameParser(validator Validator) *FrameParser
NewFrameParser creates a new frame parser with the given validator.
type LosslessDecoder ¶
type LosslessDecoder struct {
// contains filtered or unexported fields
}
LosslessDecoder provides high-level lossless JPEG decoding functionality. It coordinates the sample decoder, Huffman decoder, and output buffer.
func NewLosslessDecoder ¶
func NewLosslessDecoder(width, height, precision, components int, validator Validator) (*LosslessDecoder, error)
NewLosslessDecoder creates a new lossless decoder for the given frame parameters.
func (*LosslessDecoder) Components ¶
func (d *LosslessDecoder) Components() int
Components returns the number of color components.
func (*LosslessDecoder) DecodeComponent ¶
func (d *LosslessDecoder) DecodeComponent(componentIdx int, differences []int) error
DecodeComponent decodes all samples for a single component from difference values. The differences slice should contain width*height values in raster order.
func (*LosslessDecoder) GetFlatOutput ¶
func (d *LosslessDecoder) GetFlatOutput() []int
GetFlatOutput returns the decoded output as a flat slice of samples. Order: component 0 all rows, component 1 all rows, etc.
func (*LosslessDecoder) GetOutput ¶
func (d *LosslessDecoder) GetOutput(componentIdx int) ([][]int, error)
GetOutput returns the decoded output for a specific component. Returns a 2D slice [row][column] of sample values.
func (*LosslessDecoder) Height ¶
func (d *LosslessDecoder) Height() int
Height returns the image height.
func (*LosslessDecoder) Precision ¶
func (d *LosslessDecoder) Precision() int
Precision returns the sample precision in bits.
func (*LosslessDecoder) SetScanParameters ¶
func (d *LosslessDecoder) SetScanParameters(predictor, pointTransform int) error
SetScanParameters sets the scan parameters for decoding.
func (*LosslessDecoder) Width ¶
func (d *LosslessDecoder) Width() int
Width returns the image width.
type Predictor ¶
type Predictor struct {
// contains filtered or unexported fields
}
Predictor calculates prediction values for lossless JPEG decoding.
func NewPredictor ¶
NewPredictor creates a new predictor with the given mode.
func (*Predictor) Decode ¶
Decode applies the difference value to get the reconstructed sample. The difference is added to the prediction, then clamped to valid range.
func (*Predictor) DecodeAndStore ¶
DecodeAndStore decodes a sample and stores it in the buffer.
func (*Predictor) DecodeWithPointTransform ¶
DecodeWithPointTransform decodes with point transform applied.
type RowBuffer ¶
type RowBuffer struct {
// Width is the number of samples per row
Width int
// Precision is the sample precision in bits
Precision int
// MaxValue is the maximum sample value (2^precision - 1)
MaxValue int
// PreviousRow holds the decoded values from the previous row
PreviousRow []int
// CurrentRow holds the decoded values from the current row
CurrentRow []int
// CurrentColumn is the current column index being decoded
CurrentColumn int
// CurrentRowIndex is the current row index being decoded
CurrentRowIndex int
// contains filtered or unexported fields
}
RowBuffer manages the previous and current row for prediction. In lossless JPEG, predictors need access to:
- Ra: left neighbor (current row, previous column)
- Rb: above neighbor (previous row, same column)
- Rc: diagonal neighbor (previous row, previous column)
func NewRowBuffer ¶
NewRowBuffer creates a new row buffer for the given dimensions.
func (*RowBuffer) GetRa ¶
GetRa returns the left neighbor (Ra) for the current position. For the first column, returns the initial prediction value.
func (*RowBuffer) GetRb ¶
GetRb returns the above neighbor (Rb) for the current position. For the first row, returns the initial prediction value.
func (*RowBuffer) GetRc ¶
GetRc returns the diagonal neighbor (Rc) for the current position. For the first row or first column, returns appropriate fallback.
func (*RowBuffer) NextRow ¶
func (rb *RowBuffer) NextRow()
NextRow advances to the next row, swapping buffers.
func (*RowBuffer) Reset ¶
func (rb *RowBuffer) Reset()
Reset resets the buffer for a new image decode.
func (*RowBuffer) SetCurrent ¶
SetCurrent sets the decoded value at the current position and advances.
type SampleDecoder ¶
type SampleDecoder struct {
// contains filtered or unexported fields
}
SampleDecoder decodes lossless JPEG samples using prediction-based decoding. In lossless JPEG, samples are encoded as differences from predicted values, where the prediction is calculated from neighboring samples.
func NewSampleDecoder ¶
func NewSampleDecoder(buffer *RowBuffer, predictor, pointTransform int, validator Validator) *SampleDecoder
NewSampleDecoder creates a new sample decoder with the given parameters. Parameters:
- buffer: RowBuffer for managing decoded samples and neighbor access
- predictor: Prediction mode (1-7 per ITU-T T.81 Table H.1)
- pointTransform: Point transform value (0-15)
- validator: Validator for parameter bounds checking
func (*SampleDecoder) AdvanceRow ¶
func (d *SampleDecoder) AdvanceRow()
AdvanceRow advances to the next row, swapping the row buffers. Must be called after decoding each complete row.
func (*SampleDecoder) DecodeRow ¶
func (d *SampleDecoder) DecodeRow(differences []int) ([]int, error)
DecodeRow decodes a complete row of samples from their difference values. Returns the slice of reconstructed sample values.
func (*SampleDecoder) DecodeSample ¶
func (d *SampleDecoder) DecodeSample(difference int) (int, error)
DecodeSample decodes a single sample from its Huffman-coded difference value. The process: 1. Apply point transform to the difference value (shift left by Pt bits) 2. Calculate prediction using the selected predictor mode 3. Add shifted difference to prediction 4. Clamp result to valid range [0, MaxValue] 5. Store the sample in the row buffer
Returns the reconstructed sample value or an error if parameters are invalid.
func (*SampleDecoder) GetMaxValue ¶
func (d *SampleDecoder) GetMaxValue() int
GetMaxValue returns the maximum sample value.
func (*SampleDecoder) GetPrecision ¶
func (d *SampleDecoder) GetPrecision() int
GetPrecision returns the sample precision in bits.
func (*SampleDecoder) Reset ¶
func (d *SampleDecoder) Reset()
Reset resets the decoder state for a new image.
func (*SampleDecoder) SetPointTransform ¶
func (d *SampleDecoder) SetPointTransform(pt int) error
SetPointTransform changes the point transform value.
func (*SampleDecoder) SetPredictor ¶
func (d *SampleDecoder) SetPredictor(predictor int) error
SetPredictor changes the predictor mode. This is useful when different scans use different predictors.
type Scan ¶
type Scan struct {
// Components lists the components in this scan
Components []ScanComponent
// Predictor is the predictor selection (Ss parameter, 1-7)
Predictor int
// PointTransform is the point transform value (Al parameter, 0-15)
PointTransform int
}
Scan represents a single scan in a lossless JPEG.
type ScanComponent ¶
type ScanComponent struct {
// ComponentSelector identifies the component (matches Component.ID)
ComponentSelector int
// DCTableSelector is the DC Huffman table selector (0-3)
// Note: For lossless JPEG, only DC tables are used
DCTableSelector int
}
ScanComponent represents a component's parameters within a scan.
type ScanParser ¶
type ScanParser struct {
// contains filtered or unexported fields
}
ScanParser parses lossless JPEG scan markers (SOS).
func NewScanParser ¶
func NewScanParser(validator Validator) *ScanParser
NewScanParser creates a new scan parser with the given validator.
func (*ScanParser) ParseSOS ¶
func (p *ScanParser) ParseSOS(r io.Reader) (*Scan, error)
ParseSOS parses a SOS (Start of Scan) marker for lossless JPEG. The reader should be positioned at the start of the marker (0xFF 0xDA). For lossless JPEG:
- Ss (spectral start) contains the predictor selection (1-7)
- Se (spectral end) must be 0
- Ah (successive approx high) must be 0
- Al (successive approx low) contains the point transform (0-15)
type Validator ¶
type Validator interface {
// ValidatePrecision validates sample precision (must be 2-16).
ValidatePrecision(precision int) error
// ValidateDimensions validates width and height.
ValidateDimensions(width, height int) error
// ValidatePredictor validates predictor selection (1-7).
ValidatePredictor(predictor int) error
// ValidatePointTransform validates point transform (0-15).
ValidatePointTransform(pt int) error
// ValidateComponentCount validates number of components.
ValidateComponentCount(count 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 lossless JPEG parameters.