unipdf/internal/sampling/resample.go
Jacek Kucharczyk e85616cec2 JBIG2Decoder implementation (#67)
* Prepared skeleton and basic component implementations for the jbig2 encoding.
* Added Bitset. Implemented Bitmap.
* Decoder with old Arithmetic Decoder
* Partly working arithmetic
* Working arithmetic decoder.
* MMR patched.
* rebuild to apache.
* Working generic
* Decoded full document
* Decoded AnnexH document
* Minor issues fixed.
* Update README.md
* Fixed generic region errors. Added benchmark. Added bitmap unpadder. Added Bitmap toImage method.
* Fixed endofpage error
* Added integration test.
* Decoded all test files without errors. Implemented JBIG2Global.
* Merged with v3 version
* Fixed the EOF in the globals issue
* Fixed the JBIG2 ChocolateData Decode
* JBIG2 Added license information
* Minor fix in jbig2 encoding.
* Applied the logging convention
* Cleaned unnecessary imports
* Go modules clear unused imports
* checked out the README.md
* Moved trace to Debug. Fixed the build integrate tag in the document_decode_test.go
* Applied UniPDF Developer Guide. Fixed lint issues.
* Cleared documentation, fixed style issues.
* Added jbig2 doc.go files. Applied unipdf guide style.
* Minor code style changes.
* Minor naming and style issues fixes.
* Minor naming changes. Style issues fixed.
* Review r11 fixes.
* Integrate jbig2 tests with build system
* Added jbig2 integration test golden files.
* Minor jbig2 integration test fix
* Removed jbig2 integration image assertions
* Fixed jbig2 rowstride issue. Implemented jbig2 bit writer
* Changed golden files logic. Fixes r13 issues.
2019-07-14 21:18:40 +00:00

198 lines
4.5 KiB
Go

/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package sampling
// ResampleBytes resamples the raw data which is in 8-bit (byte) format as a different
// bit count per sample, up to 32 bits (uint32).
func ResampleBytes(data []byte, bitsPerSample int) []uint32 {
var samples []uint32
bitsLeftPerSample := bitsPerSample
var sample uint32
var remainder byte
remainderBits := 0
index := 0
i := 0
for i < len(data) {
// Start with the remainder.
if remainderBits > 0 {
take := remainderBits
if bitsLeftPerSample < take {
take = bitsLeftPerSample
}
sample = (sample << uint(take)) | uint32(remainder>>uint(8-take))
remainderBits -= take
if remainderBits > 0 {
remainder = remainder << uint(take)
} else {
remainder = 0
}
bitsLeftPerSample -= take
if bitsLeftPerSample == 0 {
//samples[index] = sample
samples = append(samples, sample)
bitsLeftPerSample = bitsPerSample
sample = 0
index++
}
} else {
// Take next byte
b := data[i]
i++
// 8 bits.
take := 8
if bitsLeftPerSample < take {
take = bitsLeftPerSample
}
remainderBits = 8 - take
sample = (sample << uint(take)) | uint32(b>>uint(remainderBits))
if take < 8 {
remainder = b << uint(take)
}
bitsLeftPerSample -= take
if bitsLeftPerSample == 0 {
//samples[index] = sample
samples = append(samples, sample)
bitsLeftPerSample = bitsPerSample
sample = 0
index++
}
}
}
// Take care of remaining samples (if enough data available).
for remainderBits >= bitsPerSample {
take := remainderBits
if bitsLeftPerSample < take {
take = bitsLeftPerSample
}
sample = (sample << uint(take)) | uint32(remainder>>uint(8-take))
remainderBits -= take
if remainderBits > 0 {
remainder = remainder << uint(take)
} else {
remainder = 0
}
bitsLeftPerSample -= take
if bitsLeftPerSample == 0 {
//samples[index] = sample
samples = append(samples, sample)
bitsLeftPerSample = bitsPerSample
sample = 0
index++
}
}
return samples
}
// ResampleUint32 resamples the raw data which is in <=32-bit (uint32) format as a different
// bit count per sample, up to 32 bits (uint32).
//
// bitsPerOutputSample is the number of bits for each output sample (up to 32)
// bitsPerInputSample is the number of bits used in each input sample (up to 32)
func ResampleUint32(data []uint32, bitsPerInputSample int, bitsPerOutputSample int) []uint32 {
var samples []uint32
bitsLeftPerSample := bitsPerOutputSample
var sample uint32
var remainder uint32
remainderBits := 0
index := 0
i := 0
for i < len(data) {
// Start with the remainder.
if remainderBits > 0 {
take := remainderBits
if bitsLeftPerSample < take {
take = bitsLeftPerSample
}
sample = (sample << uint(take)) | uint32(remainder>>uint(bitsPerInputSample-take))
remainderBits -= take
if remainderBits > 0 {
remainder = remainder << uint(take)
} else {
remainder = 0
}
bitsLeftPerSample -= take
if bitsLeftPerSample == 0 {
//samples[index] = sample
samples = append(samples, sample)
bitsLeftPerSample = bitsPerOutputSample
sample = 0
index++
}
} else {
// Take next byte
b := data[i]
i++
// 32 bits.
take := bitsPerInputSample
if bitsLeftPerSample < take {
take = bitsLeftPerSample
}
remainderBits = bitsPerInputSample - take
sample = (sample << uint(take)) | uint32(b>>uint(remainderBits))
if take < bitsPerInputSample {
remainder = b << uint(take)
}
bitsLeftPerSample -= take
if bitsLeftPerSample == 0 {
//samples[index] = sample
samples = append(samples, sample)
bitsLeftPerSample = bitsPerOutputSample
sample = 0
index++
}
}
}
// Take care of remaining samples (if enough data available).
for remainderBits >= bitsPerOutputSample {
take := remainderBits
if bitsLeftPerSample < take {
take = bitsLeftPerSample
}
sample = (sample << uint(take)) | uint32(remainder>>uint(bitsPerInputSample-take))
remainderBits -= take
if remainderBits > 0 {
remainder = remainder << uint(take)
} else {
remainder = 0
}
bitsLeftPerSample -= take
if bitsLeftPerSample == 0 {
samples = append(samples, sample)
bitsLeftPerSample = bitsPerOutputSample
sample = 0
index++
}
}
// If there are partial output samples, pad with 0s.
if bitsLeftPerSample > 0 && bitsLeftPerSample < bitsPerOutputSample {
sample <<= uint(bitsLeftPerSample)
samples = append(samples, sample)
}
return samples
}