mirror of
https://github.com/jwetzell/bmip10-go.git
synced 2026-08-16 04:43:30 +00:00
restructure config struct
This commit is contained in:
@@ -14,7 +14,7 @@ type Config struct {
|
||||
LossyFlag uint32
|
||||
}
|
||||
|
||||
func SetupBMIP10(sampleBits uint32, codedBits uint32) *Config {
|
||||
func setupBMIP10(sampleBits uint32, codedBits uint32) *Config {
|
||||
config := Config{}
|
||||
config.SampleStates = 1 << sampleBits
|
||||
config.CodedStates = 1 << codedBits
|
||||
@@ -32,7 +32,7 @@ func SetupBMIP10(sampleBits uint32, codedBits uint32) *Config {
|
||||
}
|
||||
|
||||
// Use the decoded sample to select the next table to use
|
||||
func NextTable(config *Config, sample int32) int32 {
|
||||
func (config *Config) NextTable(sample int32) int32 {
|
||||
if sample < int32(config.LowestTableThresh) {
|
||||
return 0
|
||||
} else if sample >= int32(config.HighestTableThresh) {
|
||||
@@ -43,7 +43,7 @@ func NextTable(config *Config, sample int32) int32 {
|
||||
}
|
||||
|
||||
// For a given sample and code table calculate the code word
|
||||
func EncodeSample(config *Config, table int32, sample int32) int32 {
|
||||
func (config *Config) EncodeSample(table int32, sample int32) int32 {
|
||||
lossless_low := table * int32(config.LossyCodeWidth)
|
||||
lossless_high := lossless_low + int32(config.LosslessCodes)
|
||||
|
||||
@@ -58,7 +58,7 @@ func EncodeSample(config *Config, table int32, sample int32) int32 {
|
||||
}
|
||||
|
||||
// For a given code word and code table decode the sample
|
||||
func DecodeSample(config *Config, table int32, code_word int32) int32 {
|
||||
func (config *Config) DecodeSample(table int32, code_word int32) int32 {
|
||||
index := code_word & ^int32(config.LossyFlag)
|
||||
if (code_word & int32(config.LossyFlag)) != 0 {
|
||||
if index < table {
|
||||
@@ -73,48 +73,48 @@ func DecodeSample(config *Config, table int32, code_word int32) int32 {
|
||||
}
|
||||
|
||||
type Encoder struct {
|
||||
Config *Config
|
||||
Table int32
|
||||
config *Config
|
||||
table int32
|
||||
}
|
||||
|
||||
func NewEncoder(sampleBits uint32, codedBits uint32) *Encoder {
|
||||
config := SetupBMIP10(sampleBits, codedBits)
|
||||
config := setupBMIP10(sampleBits, codedBits)
|
||||
return &Encoder{
|
||||
Config: config,
|
||||
Table: int32(config.DefaultTable),
|
||||
config: config,
|
||||
table: int32(config.DefaultTable),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Encoder) Encode(sample int32) int32 {
|
||||
code_word := EncodeSample(e.Config, e.Table, sample)
|
||||
decoded := DecodeSample(e.Config, e.Table, code_word)
|
||||
e.Table = NextTable(e.Config, decoded)
|
||||
code_word := e.config.EncodeSample(e.table, sample)
|
||||
decoded := e.config.DecodeSample(e.table, code_word)
|
||||
e.table = e.config.NextTable(decoded)
|
||||
return code_word
|
||||
}
|
||||
|
||||
func (e *Encoder) Reset() {
|
||||
e.Table = int32(e.Config.DefaultTable)
|
||||
e.table = int32(e.config.DefaultTable)
|
||||
}
|
||||
|
||||
type Decoder struct {
|
||||
Config *Config
|
||||
Table int32
|
||||
config *Config
|
||||
table int32
|
||||
}
|
||||
|
||||
func NewDecoder(sampleBits uint32, codedBits uint32) *Decoder {
|
||||
config := SetupBMIP10(sampleBits, codedBits)
|
||||
config := setupBMIP10(sampleBits, codedBits)
|
||||
return &Decoder{
|
||||
Config: config,
|
||||
Table: int32(config.DefaultTable),
|
||||
config: config,
|
||||
table: int32(config.DefaultTable),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode(code_word int32) int32 {
|
||||
decoded := DecodeSample(d.Config, d.Table, code_word)
|
||||
d.Table = NextTable(d.Config, decoded)
|
||||
decoded := d.config.DecodeSample(d.table, code_word)
|
||||
d.table = d.config.NextTable(decoded)
|
||||
return decoded
|
||||
}
|
||||
|
||||
func (d *Decoder) Reset() {
|
||||
d.Table = int32(d.Config.DefaultTable)
|
||||
d.table = int32(d.config.DefaultTable)
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ func TestBMIP10Setup(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, testCase := range tests {
|
||||
actual := SetupBMIP10(testCase.sampleBits, testCase.codedBits)
|
||||
actual := setupBMIP10(testCase.sampleBits, testCase.codedBits)
|
||||
|
||||
if !reflect.DeepEqual(actual, &testCase.expected) {
|
||||
t.Errorf("Test '%s' failed to setup config properly", testCase.description)
|
||||
|
||||
Reference in New Issue
Block a user