jpegls

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

Documentation

Overview

Package jpegls implements JPEG-LS encoding and decoding per ITU-T T.87/ISO 14495-1 with T.870 extensions for higher bit depths.

Package jpegls implements JPEG-LS encoding and decoding per ITU-T T.87/ISO 14495-1 with T.870 extensions for higher bit depths.

Index

Constants

View Source
const (
	// NumContexts is the number of regular contexts (365 total: 365 = 3^5 * 9 / 2 + 1)
	// JPEG-LS uses 365 contexts for regular mode
	NumContexts = 365

	// ResetThreshold is the reset threshold for context statistics
	ResetThreshold = 64

	// InitialA is the initial value for A (accumulated error magnitude)
	InitialA = 4

	// InitialN is the initial value for N (context occurrence count)
	InitialN = 1

	// InitialB is the initial bias value
	InitialB = 0

	// InitialC is the initial C value (correction value)
	InitialC = 0
)

Context-related constants

View Source
const (
	// NumEnhancedBuckets is the number of quantization buckets for T.870 enhanced mode
	// Standard JPEG-LS uses 9 buckets (-4 to +4), T.870 can use the same
	// but with more precise boundaries for high bit depths
	NumEnhancedBuckets = 9

	// AdaptiveFactor controls how adaptive thresholds are computed
	// Higher values create more aggressive sub-bucket boundaries
	AdaptiveFactor = 2
)

Enhanced quantization constants for T.870

View Source
const (
	MarkerSOI  = 0xFFD8 // Start of Image
	MarkerEOI  = 0xFFD9 // End of Image
	MarkerSOF  = 0xFFF7 // Start of Frame (JPEG-LS)
	MarkerLSE  = 0xFFF8 // JPEG-LS Preset Parameters
	MarkerSOS  = 0xFFDA // Start of Scan
	MarkerDNL  = 0xFFDC // Define Number of Lines
	MarkerAPP0 = 0xFFE0 // Application segment 0
)

JPEG-LS marker codes

View Source
const (
	// MaxStandardMAXVAL is the maximum MAXVAL for standard JPEG-LS (16 bits)
	MaxStandardMAXVAL = 65535

	// MaxT870MAXVAL is the maximum MAXVAL for T.870 extended mode (32 bits).
	// Per T.870 the maximum is 2^32 - 1. Kept untyped; use sites that would
	// otherwise force it to a 32-bit int (TinyGo/wasm) convert it to int64
	// explicitly.
	MaxT870MAXVAL = (1 << 32) - 1

	// MinMAXVAL is the minimum allowed MAXVAL
	MinMAXVAL = 1
)

T.870 MAXVAL limits

View Source
const (
	// DefaultRESET is the default reset threshold (from T.87)
	DefaultRESET = 64

	// MinRESET is the minimum allowed RESET value per T.870
	MinRESET = 3

	// MaxRESET is the maximum allowed RESET value per T.870
	MaxRESET = 65535
)

T.870 RESET parameter limits

View Source
const (
	// LSEMarker is the LSE (JPEG-LS Extension) marker code
	LSEMarker = 0xFFF8

	// LSEIDPresetParameters identifies preset parameters in LSE marker
	LSEIDPresetParameters = 0x01

	// LSEIDMappingTable identifies mapping table in LSE marker
	LSEIDMappingTable = 0x02

	// LSEIDMappingTableContinuation identifies mapping table continuation
	LSEIDMappingTableContinuation = 0x03

	// LSEIDExtendedPresetParameters identifies T.870 extended preset parameters
	LSEIDExtendedPresetParameters = 0x04

	// StandardLSELength is the standard LSE marker length for 16-bit values
	// Format: Length(2) + ID(1) + MAXVAL(2) + T1(2) + T2(2) + T3(2) + RESET(2) = 13 bytes
	StandardLSELength = 13

	// ExtendedLSELength is the extended LSE marker length for 32-bit values (T.870)
	// Format: Length(2) + ID(1) + MAXVAL(4) + T1(4) + T2(4) + T3(4) + RESET(4) = 23 bytes
	ExtendedLSELength = 23
)

LSE marker constants

View Source
const (
	// MinThreshold is the minimum threshold value
	MinThreshold = 1
)

T.870 threshold limits

Variables

View Source
var (
	ErrBufferOverflow = errors.New("buffer overflow")
	ErrInvalidCode    = errors.New("invalid Golomb-Rice code")
)

Errors

View Source
var (
	// ErrInvalidPresetParameters indicates invalid preset parameter values
	ErrInvalidPresetParameters = errors.New("jpegls: invalid preset parameters")

	// ErrInvalidLSEMarker indicates a malformed LSE marker segment
	ErrInvalidLSEMarker = errors.New("jpegls: invalid LSE marker")

	// ErrUnsupportedLSEID indicates an unknown or unsupported LSE ID
	ErrUnsupportedLSEID = errors.New("jpegls: unsupported LSE ID")

	// ErrTruncatedLSE indicates the LSE marker data is incomplete
	ErrTruncatedLSE = errors.New("jpegls: truncated LSE marker data")
)

T.870 specific errors

Functions

func GenerateLSEMarkerBytes

func GenerateLSEMarkerBytes(params *ExtendedPresetParameters) ([]byte, error)

GenerateLSEMarkerBytes generates the raw bytes for an LSE marker segment. This is useful when you need the bytes without writing to a stream.

func MapErrorWithPrediction

func MapErrorWithPrediction(err int, signFlip bool) int

MapErrorWithPrediction maps error value considering prediction context signFlip indicates if the context requires sign inversion

func UnmapErrorWithPrediction

func UnmapErrorWithPrediction(mapped int, signFlip bool) int

UnmapErrorWithPrediction reverses the error mapping considering prediction context

func WriteLSEToBytes

func WriteLSEToBytes(params *ExtendedPresetParameters) ([]byte, error)

WriteLSEToBytes is a convenience function that writes an LSE marker to a byte slice.

Types

type Context

type Context struct {
	A int // Accumulated prediction error magnitude (always positive)
	B int // Accumulated bias (signed)
	C int // Bias correction value
	N int // Context occurrence count
}

Context holds the state for a single JPEG-LS context

type ContextModel

type ContextModel struct {
	// Regular mode contexts
	Contexts [NumContexts]*Context

	// Run mode contexts (for run interruption samples)
	RunContexts [2]*Context

	// Quantization thresholds for gradients
	T1, T2, T3 int

	// Maximum sample value
	MaxVal int

	// Near parameter
	Near int

	// Range for error values
	Range int

	// Limit for Golomb parameter k
	Limit int

	// qbpp - bits needed to represent quantized error
	Qbpp int
}

ContextModel manages all contexts for JPEG-LS encoding/decoding

func NewContextModel

func NewContextModel(maxVal, near int) *ContextModel

NewContextModel creates a new context model for JPEG-LS

func (*ContextModel) ComputeK

func (cm *ContextModel) ComputeK(ctx *Context) int

ComputeK computes the Golomb parameter k for a context

func (*ContextModel) CorrectPrediction

func (cm *ContextModel) CorrectPrediction(predicted int, ctx *Context, signFlip bool) int

CorrectPrediction applies bias correction to a predicted value

func (*ContextModel) GetContext

func (cm *ContextModel) GetContext(idx int) *Context

GetContext returns the context for the given index

func (*ContextModel) GetContextIndex

func (cm *ContextModel) GetContextIndex(Q1, Q2, Q3 int) int

GetContextIndex computes the context index from quantized gradients Q1, Q2, Q3 are the quantized gradient values (each in [0, 8])

func (*ContextModel) GetRunContext

func (cm *ContextModel) GetRunContext(idx int) *Context

GetRunContext returns a run mode context

func (*ContextModel) QuantizeGradient

func (cm *ContextModel) QuantizeGradient(gradient int) int

QuantizeGradient quantizes a gradient value to one of 9 levels Returns a value in range [0, 8] corresponding to [-4, 4]

func (*ContextModel) Reset

func (cm *ContextModel) Reset()

Reset resets all contexts to initial state

func (*ContextModel) UpdateContext

func (cm *ContextModel) UpdateContext(ctx *Context, errVal int)

UpdateContext updates context statistics after encoding/decoding a sample

type Decoder

type Decoder struct {
	Width      int
	Height     int
	NumComps   int
	BitDepth   int
	MaxVal     int
	Near       int
	Interleave int // Interleave mode: 0=non-interleaved, 1=line interleaved, 2=sample interleaved
	// contains filtered or unexported fields
}

Decoder decodes JPEG-LS images

func NewDecoder

func NewDecoder(data []byte) *Decoder

NewDecoder creates a new JPEG-LS decoder

func (*Decoder) Decode

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

Decode decodes JPEG-LS data and returns the image samples

func (*Decoder) DecodeMultiComponent

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

DecodeMultiComponent decodes a multi-component JPEG-LS image

func (*Decoder) GetInfo

func (d *Decoder) GetInfo() (width, height, numComps, bitDepth int)

GetInfo returns image information from the decoder

type Encoder

type Encoder struct {
	// Image parameters
	Width    int
	Height   int
	NumComps int
	BitDepth int
	MaxVal   int
	Near     int // 0 = lossless, >0 = near-lossless tolerance
	// contains filtered or unexported fields
}

Encoder encodes images using JPEG-LS

func NewEncoder

func NewEncoder(width, height, numComps, bitDepth int) *Encoder

NewEncoder creates a new JPEG-LS encoder

func (*Encoder) Encode

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

Encode encodes a single-component image and returns the JPEG-LS data

func (*Encoder) EncodeMultiComponent

func (e *Encoder) EncodeMultiComponent(components [][]int) ([]byte, error)

EncodeMultiComponent encodes a multi-component image

func (*Encoder) GetExtendedParams

func (e *Encoder) GetExtendedParams() *ExtendedPresetParameters

GetExtendedParams returns the current extended parameters, or nil if not set

func (*Encoder) IsExtendedMode

func (e *Encoder) IsExtendedMode() bool

IsExtendedMode returns true if the encoder is using T.870 extended mode

func (*Encoder) SetExtendedParams

func (e *Encoder) SetExtendedParams(params *ExtendedPresetParameters) *Encoder

SetExtendedParams sets T.870 extended preset parameters. When set, an LSE marker will be written with the custom parameters. The parameters will be validated before use. Pass nil to disable extended mode and use default parameters.

func (*Encoder) SetNear

func (e *Encoder) SetNear(near int) *Encoder

SetNear sets the near-lossless parameter (0 = lossless)

func (*Encoder) WriteTo

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

WriteTo writes encoded data to an io.Writer

type ExtendedContextModel

type ExtendedContextModel struct {
	// Embed the standard context model
	*ContextModel

	// ExtendedParams contains T.870 extended preset parameters
	ExtendedParams *ExtendedPresetParameters

	// T870Mode indicates if T.870 extended mode is active
	Mode T870Mode
	// contains filtered or unexported fields
}

ExtendedContextModel extends ContextModel to support T.870 higher bit depths. It provides adjusted context quantization and threshold handling for MAXVAL > 65535.

func NewExtendedContextModel

func NewExtendedContextModel(maxVal, near int) *ExtendedContextModel

NewExtendedContextModel creates a context model with T.870 support. For standard MAXVAL (<=65535), it behaves identically to NewContextModel. For extended MAXVAL (>65535), it uses T.870 extended computations.

func NewExtendedContextModelWithParams

func NewExtendedContextModelWithParams(params *ExtendedPresetParameters, near int) *ExtendedContextModel

NewExtendedContextModelWithParams creates a context model using provided preset parameters.

func (*ExtendedContextModel) ApplyPresetParameters

func (ecm *ExtendedContextModel) ApplyPresetParameters(params *ExtendedPresetParameters) error

ApplyPresetParameters applies T.870 preset parameters to the context model.

func (*ExtendedContextModel) ComputeKExtended

func (ecm *ExtendedContextModel) ComputeKExtended(ctx *Context) int

ComputeKExtended computes Golomb parameter k with overflow protection for T.870. Uses int64 arithmetic to handle large accumulated error values.

func (*ExtendedContextModel) QuantizeGradient

func (ecm *ExtendedContextModel) QuantizeGradient(gradient int) int

QuantizeGradient overrides the base QuantizeGradient for T.870 mode. Uses int64 conversion when in extended mode to handle large gradients.

func (*ExtendedContextModel) QuantizeGradientExtended

func (ecm *ExtendedContextModel) QuantizeGradientExtended(gradient int64) int

QuantizeGradientExtended quantizes a gradient value with T.870 support. For large gradients (|gradient| > MaxInt32), it uses int64 comparisons. Uses 9 standard buckets (0-8) mapping to context values (-4 to +4).

func (*ExtendedContextModel) QuantizeGradientWithAdaptive

func (ecm *ExtendedContextModel) QuantizeGradientWithAdaptive(gradient int64) int

QuantizeGradientWithAdaptive performs enhanced quantization using adaptive boundaries. This is an optional method for applications requiring finer gradient discrimination. Returns bucket 0-8 but uses intermediate T0 and T4 for refined boundary detection.

func (*ExtendedContextModel) ResetWithThreshold

func (ecm *ExtendedContextModel) ResetWithThreshold(ctx *Context, threshold int)

ResetWithThreshold resets context statistics with a custom reset threshold. T.870 allows customizing the RESET parameter.

func (*ExtendedContextModel) UpdateContextExtended

func (ecm *ExtendedContextModel) UpdateContextExtended(ctx *Context, errVal int)

UpdateContextExtended updates context with T.870 reset threshold support.

type ExtendedPredictor

type ExtendedPredictor struct {
	*Predictor

	// Mode indicates T.870 extended mode
	Mode T870Mode

	// Extended sample range
	MaxValExtended int64
}

ExtendedPredictor wraps Predictor with T.870 support for high bit depth samples.

func NewExtendedPredictor

func NewExtendedPredictor(width, height, maxVal, near int) *ExtendedPredictor

NewExtendedPredictor creates a predictor with T.870 support.

func (*ExtendedPredictor) ClampExtended

func (ep *ExtendedPredictor) ClampExtended(value int64) int64

ClampExtended clamps a value to the valid sample range [0, MaxVal] using int64.

func (*ExtendedPredictor) ComputeGradientsExtended

func (ep *ExtendedPredictor) ComputeGradientsExtended(a, b, c, d int64) (D1, D2, D3 int64)

ComputeGradientsExtended computes gradients using int64 for T.870.

func (*ExtendedPredictor) GetNeighborsFromResult

func (ep *ExtendedPredictor) GetNeighborsFromResult(data []int, x, y, stride int) (a, b, c, d int64)

GetNeighborsFromResult returns neighbor values from an int result array.

func (*ExtendedPredictor) PredictExtended

func (ep *ExtendedPredictor) PredictExtended(a, b, c int64) int64

PredictExtended computes predicted value for high bit depth samples. Uses int64 arithmetic to prevent overflow with large sample values.

func (*ExtendedPredictor) ReduceRangeExtended

func (ep *ExtendedPredictor) ReduceRangeExtended(err int64) int64

ReduceRangeExtended applies modulo range reduction for T.870 high bit depths.

func (*ExtendedPredictor) ToStandardRange

func (ep *ExtendedPredictor) ToStandardRange(err int64) (int, error)

ToStandardRange safely converts int64 error to int for standard processing. Uses safeconv for safe type conversion.

type ExtendedPresetParameters

type ExtendedPresetParameters struct {
	// MAXVAL is the maximum sample value (1 to 2^32-1 for T.870)
	MAXVAL int

	// T1, T2, T3 are the gradient quantization thresholds
	// T1 <= T2 <= T3 <= MAXVAL
	T1 int
	T2 int
	T3 int

	// RESET is the context reset threshold (3 to 65535)
	// Controls how often context statistics are halved
	RESET int

	// ExtendedMode indicates T.870 extended mode is active
	ExtendedMode bool
}

ExtendedPresetParameters holds JPEG-LS preset parameters with T.870 extensions. These parameters can be set via LSE marker segments or programmatically.

func ParseLSEFromBuffer

func ParseLSEFromBuffer(data []byte) (*ExtendedPresetParameters, error)

ParseFromBuffer parses an LSE marker from a byte slice. The buffer should start with the LSE marker (0xFF 0xF8).

func (*ExtendedPresetParameters) ApplyDefaults

func (p *ExtendedPresetParameters) ApplyDefaults()

ApplyDefaults sets default values for zero parameters

func (*ExtendedPresetParameters) Clone

Clone returns a copy of the preset parameters

func (*ExtendedPresetParameters) ComputeDefaultThresholds

func (p *ExtendedPresetParameters) ComputeDefaultThresholds()

ComputeDefaultThresholds computes default threshold values for the given MAXVAL according to the JPEG-LS specification. This is used when T1=T2=T3=0.

func (*ExtendedPresetParameters) Validate

func (p *ExtendedPresetParameters) Validate() error

Validate checks if the preset parameters are valid per T.87/T.870 specifications

type GolombDecoder

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

GolombDecoder decodes Golomb-Rice coded values

func NewGolombDecoder

func NewGolombDecoder(data []byte, maxVal, near int) *GolombDecoder

NewGolombDecoder creates a new Golomb-Rice decoder

func (*GolombDecoder) AlignToByte

func (g *GolombDecoder) AlignToByte()

AlignToByte advances to the next byte boundary This is used when decoding multi-component images where each component starts on a byte boundary (because encoder flushes after each component)

func (*GolombDecoder) DecodeRegular

func (g *GolombDecoder) DecodeRegular(k int) (int, error)

DecodeRegular decodes a Golomb-Rice coded error value

func (*GolombDecoder) DecodeRunLength

func (g *GolombDecoder) DecodeRunLength(maxRun int) (int, error)

DecodeRunLength decodes a run length value

func (*GolombDecoder) Reset

func (g *GolombDecoder) Reset(data []byte)

Reset resets the decoder for a new decoding session

type GolombEncoder

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

GolombEncoder encodes values using Golomb-Rice coding

func NewGolombEncoder

func NewGolombEncoder(maxVal, near int) *GolombEncoder

NewGolombEncoder creates a new Golomb-Rice encoder

func (*GolombEncoder) Bytes

func (g *GolombEncoder) Bytes() []byte

Bytes returns the encoded data

func (*GolombEncoder) EncodeRegular

func (g *GolombEncoder) EncodeRegular(mappedErr, k int)

EncodeRegular encodes a mapped error value using Golomb-Rice coding mappedErr should be a non-negative value (already mapped from signed error) k is the Golomb parameter

func (*GolombEncoder) EncodeRunInterruption

func (g *GolombEncoder) EncodeRunInterruption(errVal, k int, runCtx *Context)

EncodeRunInterruption encodes the sample that ends a run

func (*GolombEncoder) EncodeRunLength

func (g *GolombEncoder) EncodeRunLength(runLen, maxRun int)

EncodeRunLength encodes a run length value For a full run (runLen >= maxRun), we write enough 1 bits so that the decoder can detect that the accumulated sum reaches maxRun. For a partial run, we write 1 bits for each run length table entry consumed, then a 0 bit, followed by the remainder bits.

func (*GolombEncoder) Flush

func (g *GolombEncoder) Flush()

Flush writes any remaining bits to the buffer

func (*GolombEncoder) Reset

func (g *GolombEncoder) Reset()

Reset resets the encoder for a new encoding session

type LSEParser

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

LSEParser parses JPEG-LS Extension (LSE) marker segments. The LSE marker (0xFFF8) contains preset parameters for JPEG-LS decoding.

func NewLSEParser

func NewLSEParser(r io.Reader) *LSEParser

NewLSEParser creates a new LSE marker parser.

func (*LSEParser) Parse

func (p *LSEParser) Parse() (*ExtendedPresetParameters, error)

Parse reads and parses an LSE marker segment from the reader. The reader should be positioned at the start of the LSE marker (0xFF 0xF8). Returns the parsed ExtendedPresetParameters or an error.

type LSEWriter

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

LSEWriter writes JPEG-LS Extension (LSE) marker segments. It supports both standard JPEG-LS (T.87) and T.870 extended preset parameters.

func NewLSEWriter

func NewLSEWriter(w io.Writer) *LSEWriter

NewLSEWriter creates a new LSE marker writer.

func (*LSEWriter) Write

func (w *LSEWriter) Write(params *ExtendedPresetParameters) error

Write writes an LSE marker segment with the given preset parameters. If ExtendedMode is true or MAXVAL > 65535, it writes the T.870 extended format (ID=0x04). Otherwise, it writes the standard format (ID=0x01).

type PredictionMode

type PredictionMode int

PredictionMode defines the prediction mode for T.870 extended support

const (
	// PredictionModeLOCO is the standard LOCO-I prediction (default)
	PredictionModeLOCO PredictionMode = iota

	// PredictionModeHorizontal uses horizontal prediction only (a)
	PredictionModeHorizontal

	// PredictionModeVertical uses vertical prediction only (b)
	PredictionModeVertical

	// PredictionModeAdaptive dynamically selects the best predictor
	PredictionModeAdaptive
)

func (PredictionMode) String

func (pm PredictionMode) String() string

String returns a string representation of the prediction mode.

type Predictor

type Predictor struct {
	// Image dimensions
	Width  int
	Height int

	// Bit depth
	MaxVal int // Maximum sample value (2^bpp - 1)

	// Near parameter for near-lossless mode (0 = lossless)
	Near int

	// T.870 prediction mode (default is LOCO-I)
	Mode PredictionMode
}

Predictor implements the LOCO-I (Low Complexity Lossless Compression for Images) predictor used in JPEG-LS encoding and decoding.

func NewPredictor

func NewPredictor(width, height, maxVal, near int) *Predictor

NewPredictor creates a new LOCO-I predictor

func NewPredictorWithMode

func NewPredictorWithMode(width, height, maxVal, near int, mode PredictionMode) *Predictor

NewPredictorWithMode creates a predictor with a specified prediction mode. This is useful for T.870 extended modes.

func (*Predictor) Clamp

func (p *Predictor) Clamp(value int) int

Clamp clamps a value to the valid sample range [0, MaxVal]

func (*Predictor) ClampExtended

func (p *Predictor) ClampExtended(value int64) int64

ClampExtended clamps an int64 value for T.870 high bit depth samples.

func (*Predictor) ComputeError

func (p *Predictor) ComputeError(actual, predicted int) int

ComputeError computes the prediction error (residual)

func (*Predictor) ComputeErrorExtended

func (p *Predictor) ComputeErrorExtended(actual, predicted int64) int64

ComputeErrorExtended computes prediction error for T.870 extended samples.

func (*Predictor) ComputeGradients

func (p *Predictor) ComputeGradients(a, b, c, d int) (D1, D2, D3 int)

ComputeGradients computes the local gradients for context determination Returns (D1, D2, D3) where: D1 = d - b (vertical gradient above b) D2 = b - c (horizontal gradient in previous row) D3 = c - a (diagonal gradient)

func (*Predictor) ComputeGradientsExtended

func (p *Predictor) ComputeGradientsExtended(a, b, c, d int64) (D1, D2, D3 int64)

ComputeGradientsExtended computes gradients for T.870 high bit depth samples. Uses int64 to handle large sample values without overflow.

func (*Predictor) DequantizeError

func (p *Predictor) DequantizeError(quantErr int) int

DequantizeError dequantizes an error value

func (*Predictor) DequantizeErrorExtended

func (p *Predictor) DequantizeErrorExtended(quantErr int64) int64

DequantizeErrorExtended dequantizes error for T.870 mode.

func (*Predictor) GetNeighbors

func (p *Predictor) GetNeighbors(data []int, x, y, stride int) (a, b, c, d int)

GetNeighbors returns the neighbor values (a, b, c, d) for position (x, y) in the image data. d is the sample above b (two rows up).

    d
c b
a x

func (*Predictor) GetNeighborsExtended

func (p *Predictor) GetNeighborsExtended(data []int64, x, y, stride int) (a, b, c, d int64)

GetNeighborsExtended returns neighbor values for T.870 high bit depth images. Uses int64 for sample values to prevent overflow with >16 bit samples.

func (*Predictor) InverseModRange

func (p *Predictor) InverseModRange(err, predicted int) int

InverseModRange reverses the modulo range reduction For near-lossless mode, err is a dequantized error value

func (*Predictor) InverseModRangeExtended

func (p *Predictor) InverseModRangeExtended(err, predicted int64) int64

InverseModRangeExtended reverses modulo range reduction for T.870 mode.

func (*Predictor) IsRunMode

func (p *Predictor) IsRunMode(D1, D2, D3 int) bool

IsRunMode checks if the encoder should switch to run mode Run mode is used when the local area is uniform (all gradients are small)

func (*Predictor) IsRunModeExtended

func (p *Predictor) IsRunModeExtended(D1, D2, D3 int64) bool

IsRunModeExtended checks run mode condition for T.870 high bit depth.

func (*Predictor) ModRange

func (p *Predictor) ModRange(err int) int

ModRange applies modulo range reduction for quantized error values For lossless mode, this operates on raw errors For near-lossless mode, errors should be reduced BEFORE quantization using ReduceRange

func (*Predictor) ModRangeExtended

func (p *Predictor) ModRangeExtended(err int64) int64

ModRangeExtended applies modulo range reduction for T.870 mode.

func (*Predictor) Predict

func (p *Predictor) Predict(a, b, c int) int

Predict computes the predicted value for a sample at position (x, y) using neighboring samples a, b, c where:

c b
a x

a = sample to the left (same row) b = sample above (previous row) c = sample above and to the left (previous row)

func (*Predictor) PredictExtended

func (p *Predictor) PredictExtended(a, b, c int64) int64

PredictExtended computes predicted value for T.870 high bit depth samples. Uses int64 arithmetic to prevent overflow with >16 bit samples.

func (*Predictor) QuantizeError

func (p *Predictor) QuantizeError(err int) int

QuantizeError quantizes the prediction error for near-lossless mode

func (*Predictor) QuantizeErrorExtended

func (p *Predictor) QuantizeErrorExtended(err int64) int64

QuantizeErrorExtended quantizes error for T.870 high bit depth. Uses int64 arithmetic to prevent overflow.

func (*Predictor) ReconstructSample

func (p *Predictor) ReconstructSample(predicted, err int) int

ReconstructSample reconstructs the original sample from prediction and error

func (*Predictor) ReconstructSampleExtended

func (p *Predictor) ReconstructSampleExtended(predicted, err int64) int64

ReconstructSampleExtended reconstructs sample for T.870 high bit depth.

func (*Predictor) ReduceRange

func (p *Predictor) ReduceRange(err int) int

ReduceRange applies modulo range reduction for RAW (unquantized) error values This maps errors to the range [-(RANGE)/2, (RANGE)/2] BEFORE quantization This is the correct approach per JPEG-LS spec for near-lossless mode

func (*Predictor) ReduceRangeExtended

func (p *Predictor) ReduceRangeExtended(err int64) int64

ReduceRangeExtended applies modulo range reduction for T.870 high bit depths.

func (*Predictor) ToInt

func (p *Predictor) ToInt(value int64) (int, error)

ToInt safely converts an int64 sample value to int using safeconv. Returns an error if the value cannot be represented as int.

type T870Decoder

type T870Decoder struct {
	*Decoder

	// T.870 mode indicator
	Mode T870Mode

	// Extended preset parameters from LSE markers
	ExtendedParams *ExtendedPresetParameters
	// contains filtered or unexported fields
}

T870Decoder extends the standard JPEG-LS decoder with T.870 support for high bit depth images (>16 bits) and extended preset parameters.

func NewT870Decoder

func NewT870Decoder(data []byte) *T870Decoder

NewT870Decoder creates a new T.870-aware JPEG-LS decoder. It wraps the standard decoder and adds T.870 extended mode support.

func (*T870Decoder) Decode

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

Decode decodes JPEG-LS data with T.870 extension support. It automatically detects T.870 extended mode from LSE markers.

func (*T870Decoder) DecodeMultiComponent

func (d *T870Decoder) DecodeMultiComponent() ([][]int, error)

DecodeMultiComponent decodes a multi-component JPEG-LS image with T.870 support.

func (*T870Decoder) GetExtendedParams

func (d *T870Decoder) GetExtendedParams() *ExtendedPresetParameters

GetExtendedParams returns the extended preset parameters if present.

func (*T870Decoder) GetInfo

func (d *T870Decoder) GetInfo() (width, height, numComps, bitDepth int, t870Mode T870Mode)

GetInfo returns image information including T.870 mode.

func (*T870Decoder) GetT870Mode

func (d *T870Decoder) GetT870Mode() T870Mode

GetT870Mode returns the detected T.870 mode.

func (*T870Decoder) IsT870Extended

func (d *T870Decoder) IsT870Extended() bool

IsT870Extended returns true if the decoder is using T.870 extended mode.

type T870Mode

type T870Mode int

T870Mode indicates which JPEG-LS mode is active

const (
	// T870Standard indicates standard JPEG-LS (T.87) mode
	T870Standard T870Mode = iota

	// T870Extended indicates T.870 extended mode for >16 bit samples
	T870Extended
)

func DetectT870Mode

func DetectT870Mode(maxval int) T870Mode

DetectT870Mode returns the appropriate T.870 mode based on MAXVAL

func (T870Mode) String

func (m T870Mode) String() string

String returns a string representation of the T870Mode

Jump to

Keyboard shortcuts

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