mirror of
https://github.com/jwetzell/bmip10-go.git
synced 2026-09-10 08:39:31 +00:00
restructure config struct
This commit is contained in:
@@ -14,7 +14,7 @@ type Config struct {
|
|||||||
LossyFlag uint32
|
LossyFlag uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupBMIP10(sampleBits uint32, codedBits uint32) *Config {
|
func setupBMIP10(sampleBits uint32, codedBits uint32) *Config {
|
||||||
config := Config{}
|
config := Config{}
|
||||||
config.SampleStates = 1 << sampleBits
|
config.SampleStates = 1 << sampleBits
|
||||||
config.CodedStates = 1 << codedBits
|
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
|
// 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) {
|
if sample < int32(config.LowestTableThresh) {
|
||||||
return 0
|
return 0
|
||||||
} else if sample >= int32(config.HighestTableThresh) {
|
} 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
|
// 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_low := table * int32(config.LossyCodeWidth)
|
||||||
lossless_high := lossless_low + int32(config.LosslessCodes)
|
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
|
// 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)
|
index := code_word & ^int32(config.LossyFlag)
|
||||||
if (code_word & int32(config.LossyFlag)) != 0 {
|
if (code_word & int32(config.LossyFlag)) != 0 {
|
||||||
if index < table {
|
if index < table {
|
||||||
@@ -73,48 +73,48 @@ func DecodeSample(config *Config, table int32, code_word int32) int32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Encoder struct {
|
type Encoder struct {
|
||||||
Config *Config
|
config *Config
|
||||||
Table int32
|
table int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEncoder(sampleBits uint32, codedBits uint32) *Encoder {
|
func NewEncoder(sampleBits uint32, codedBits uint32) *Encoder {
|
||||||
config := SetupBMIP10(sampleBits, codedBits)
|
config := setupBMIP10(sampleBits, codedBits)
|
||||||
return &Encoder{
|
return &Encoder{
|
||||||
Config: config,
|
config: config,
|
||||||
Table: int32(config.DefaultTable),
|
table: int32(config.DefaultTable),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) Encode(sample int32) int32 {
|
func (e *Encoder) Encode(sample int32) int32 {
|
||||||
code_word := EncodeSample(e.Config, e.Table, sample)
|
code_word := e.config.EncodeSample(e.table, sample)
|
||||||
decoded := DecodeSample(e.Config, e.Table, code_word)
|
decoded := e.config.DecodeSample(e.table, code_word)
|
||||||
e.Table = NextTable(e.Config, decoded)
|
e.table = e.config.NextTable(decoded)
|
||||||
return code_word
|
return code_word
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) Reset() {
|
func (e *Encoder) Reset() {
|
||||||
e.Table = int32(e.Config.DefaultTable)
|
e.table = int32(e.config.DefaultTable)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
Config *Config
|
config *Config
|
||||||
Table int32
|
table int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(sampleBits uint32, codedBits uint32) *Decoder {
|
func NewDecoder(sampleBits uint32, codedBits uint32) *Decoder {
|
||||||
config := SetupBMIP10(sampleBits, codedBits)
|
config := setupBMIP10(sampleBits, codedBits)
|
||||||
return &Decoder{
|
return &Decoder{
|
||||||
Config: config,
|
config: config,
|
||||||
Table: int32(config.DefaultTable),
|
table: int32(config.DefaultTable),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Decode(code_word int32) int32 {
|
func (d *Decoder) Decode(code_word int32) int32 {
|
||||||
decoded := DecodeSample(d.Config, d.Table, code_word)
|
decoded := d.config.DecodeSample(d.table, code_word)
|
||||||
d.Table = NextTable(d.Config, decoded)
|
d.table = d.config.NextTable(decoded)
|
||||||
return decoded
|
return decoded
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Reset() {
|
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 {
|
for _, testCase := range tests {
|
||||||
actual := SetupBMIP10(testCase.sampleBits, testCase.codedBits)
|
actual := setupBMIP10(testCase.sampleBits, testCase.codedBits)
|
||||||
|
|
||||||
if !reflect.DeepEqual(actual, &testCase.expected) {
|
if !reflect.DeepEqual(actual, &testCase.expected) {
|
||||||
t.Errorf("Test '%s' failed to setup config properly", testCase.description)
|
t.Errorf("Test '%s' failed to setup config properly", testCase.description)
|
||||||
|
|||||||
Reference in New Issue
Block a user