mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-02 22:17:06 +08:00

* 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.
115 lines
2.4 KiB
Go
115 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 reader
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestReader tests the reader Read methods.
|
|
func TestReader(t *testing.T) {
|
|
data := []byte{3, 255, 0xcc, 0x1a, 0xbc, 0xde, 0x80, 0x01, 0x02, 0xf8, 0x08, 0xf0}
|
|
|
|
r := New(data)
|
|
b, err := r.ReadByte()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, byte(3), b)
|
|
|
|
bits, err := r.ReadBits(8)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, uint64(255), bits)
|
|
|
|
bits, err = r.ReadBits(4)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, uint64(0xc), bits)
|
|
|
|
bits, err = r.ReadBits(8)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, uint64(0xc1), bits)
|
|
|
|
bits, err = r.ReadBits(20)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, uint64(0xabcde), bits)
|
|
|
|
bl, err := r.ReadBool()
|
|
require.NoError(t, err)
|
|
assert.True(t, bl)
|
|
|
|
bl, err = r.ReadBool()
|
|
require.NoError(t, err)
|
|
assert.False(t, bl)
|
|
|
|
assert.Equal(t, byte(6), r.Align())
|
|
|
|
s := make([]byte, 2)
|
|
_, err = r.Read(s)
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, bytes.Equal(s, []byte{0x01, 0x02}))
|
|
|
|
bits, err = r.ReadBits(4)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, uint64(0xf), bits)
|
|
|
|
s = make([]byte, 2)
|
|
_, err = r.Read(s)
|
|
require.NoError(t, err)
|
|
assert.True(t, bytes.Equal(s, []byte{0x80, 0x8f}))
|
|
}
|
|
|
|
// TestSeeker test the Reader Seek methods.
|
|
func TestSeeker(t *testing.T) {
|
|
data := []byte{3, 255, 0xcc, 0x1a, 0xbc, 0xde, 0x80, 0x01, 0x02, 0xf8, 0x08, 0xf0}
|
|
|
|
t.Run("SeekStart", func(t *testing.T) {
|
|
t.Run("Valid", func(t *testing.T) {
|
|
r := New(data)
|
|
r.Seek(3, io.SeekStart)
|
|
b, err := r.ReadByte()
|
|
assert.Equal(t, nil, err)
|
|
assert.Equal(t, data[3], b)
|
|
})
|
|
t.Run("Invalid", func(t *testing.T) {
|
|
r := New(data)
|
|
_, err := r.Seek(int64(len(data)+1), io.SeekStart)
|
|
assert.Equal(t, nil, err)
|
|
|
|
_, err = r.ReadByte()
|
|
assert.Equal(t, io.EOF, err)
|
|
})
|
|
})
|
|
t.Run("SeekCurrent", func(t *testing.T) {
|
|
r := New(data)
|
|
|
|
_, err := r.ReadByte()
|
|
assert.Equal(t, nil, err)
|
|
|
|
_, err = r.Seek(int64(2), io.SeekCurrent)
|
|
assert.Equal(t, nil, err)
|
|
|
|
b, err := r.ReadByte()
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, data[2+1], b)
|
|
})
|
|
t.Run("SeekEnd", func(t *testing.T) {
|
|
r := New(data)
|
|
|
|
_, err := r.Seek(int64(-1), io.SeekEnd)
|
|
assert.Equal(t, nil, err)
|
|
|
|
b, err := r.ReadByte()
|
|
assert.Equal(t, nil, err)
|
|
assert.Equal(t, data[len(data)-1], b)
|
|
})
|
|
|
|
}
|