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

125 lines
2.4 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 huffman
import (
"fmt"
"strings"
"github.com/unidoc/unipdf/v3/internal/jbig2/reader"
)
// Tabler is the interface for all types of the huffman tables.
type Tabler interface {
Decode(r reader.StreamReader) (int64, error)
InitTree(codeTable []*Code) error
String() string
RootNode() *InternalNode
}
// BasicTabler is the interface common for the huffman tables.
type BasicTabler interface {
HtHigh() int
HtLow() int
StreamReader() reader.StreamReader
HtPS() int
HtRS() int
HtOOB() int
}
// Code is the model for the huffman table code.
type Code struct {
prefixLength int
rangeLength int
rangeLow int
isLowerRange bool
code int
}
// String implements Stringer interface.
func (c *Code) String() string {
var temp string
if c.code != -1 {
temp = bitPattern(c.code, c.prefixLength)
} else {
temp = "?"
}
return fmt.Sprintf("%s/%d/%d/%d", temp, c.prefixLength, c.rangeLength, c.rangeLow)
}
// NewCode creates new huffman code.
func NewCode(prefixLength, rangeLength, rangeLow int, isLowerRange bool) *Code {
return &Code{
prefixLength: prefixLength,
rangeLength: rangeLength,
rangeLow: rangeLow,
isLowerRange: isLowerRange,
code: -1,
}
}
func bitPattern(v, l int) string {
var temp int
result := make([]rune, l)
for i := 1; i <= l; i++ {
temp = (v >> uint(l-i) & 1)
if temp != 0 {
result[i-1] = '1'
} else {
result[i-1] = '0'
}
}
return string(result)
}
func preprocessCodes(codeTable []*Code) {
// Annex B.3 1) build the histogram.
var maxPrefixLength int
for _, c := range codeTable {
maxPrefixLength = maxInt(maxPrefixLength, c.prefixLength)
}
lenCount := make([]int, maxPrefixLength+1)
for _, c := range codeTable {
lenCount[c.prefixLength]++
}
var curCode int
firstCode := make([]int, len(lenCount)+1)
lenCount[0] = 0
// Annex B.3 3)
for curLen := 1; curLen <= len(lenCount); curLen++ {
firstCode[curLen] = (firstCode[curLen-1] + (lenCount[curLen-1])) << 1
curCode = firstCode[curLen]
for _, c := range codeTable {
if c.prefixLength == curLen {
c.code = curCode
curCode++
}
}
}
}
func maxInt(x, y int) int {
if x > y {
return x
}
return y
}
func codeTableToString(codeTable []*Code) string {
sb := strings.Builder{}
for _, c := range codeTable {
sb.WriteString(c.String() + "\n")
}
return sb.String()
}