unipdf/internal/jbig2/bitmap/slices_test.go
Jacek Kucharczyk c582323a8f
JBIG2 Generic Encoder (#264)
* 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

* Working generic

* Decoded full document

* Update Jenkinsfile go version [master] (#398)

* Update Jenkinsfile go version

* 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

* Initial encoder skeleton

* 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.

* Added JBIG2 Encoder skeleton.

* Moved Document and Page to jbig2/document package. Created decoder package responsible for decoding jbig2 stream.

* Implemented raster functions.

* Added raster uni low test funcitons.

* Added raster low test functions

* untracked files on jbig2-encoder: c869089 Added raster low test functions

* index on jbig2-encoder: c869089 Added raster low test functions

* Added morph files.

* implemented jbig2 encoder basics

* JBIG2 Encoder - Generic method

* Added jbig2 image encode ttests, black/white image tests

* cleaned and tested jbig2 package

* unfinished jbig2 classified encoder

* jbig2 minor style changes

* minor jbig2 encoder changes

* prepared JBIG2 Encoder

* Style and lint fixes

* Minor changes and lints

* Fixed shift unsinged value build errors

* Minor naming change

* Added jbig2 encode, image gondels. Fixed jbig2 decode bug.

* Provided jbig2 core.DecodeGlobals function.

* Fixed JBIG2Encoder `r6` revision issues.

* Removed public JBIG2Encoder document.

* Minor style changes

* added NewJBIG2Encoder function.

* fixed JBIG2Encoder 'r9' revision issues.

* Cleared 'r9' commented code.

* Updated ACKNOWLEDGEMENETS. Fixed JBIG2Encoder 'r10' revision issues.

Co-authored-by: Gunnsteinn Hall <gunnsteinn.hall@gmail.com>
2020-03-27 11:47:41 +00:00

212 lines
5.8 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 bitmap
import (
"image"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestSelectBitmapsBySize tests the select bitmaps by size function
func TestSelectBitmapsBySize(t *testing.T) {
type size struct {
width, height int
}
t.Run("WithBoxes", func(t *testing.T) {
t.Run("AllWithin", func(t *testing.T) {
sizes := []size{{5, 8}, {6, 9}, {5, 5}, {3, 7}}
type testParam struct {
name string
width, height int
location LocationFilter
comparison SizeComparison
}
tests := []testParam{
// each width is within bounds, but height not,
{"WidthLTE", 6, 2, LocSelectWidth, SizeSelectIfLTE},
{"WidthLT", 7, 2, LocSelectWidth, SizeSelectIfLT},
{"WidthGT", 2, 10, LocSelectWidth, SizeSelectIfGT},
{"WidthGTE", 3, 10, LocSelectWidth, SizeSelectIfGTE},
// each height is within bounds but width not.
{"HeightLTE", 2, 9, LocSelectHeight, SizeSelectIfLTE},
{"HeightLT", 2, 10, LocSelectHeight, SizeSelectIfLT},
{"HeightGT", 10, 4, LocSelectHeight, SizeSelectIfGT},
{"HeightGTE", 10, 5, LocSelectHeight, SizeSelectIfGTE},
// either width or height fits the bounds.
{"EitherLTEWidth", 6, 3, LocSelectIfEither, SizeSelectIfLTE},
{"EitherLTEHeight", 4, 10, LocSelectIfEither, SizeSelectIfLTE},
{"EitherGTEWidth", 3, 10, LocSelectIfEither, SizeSelectIfGTE},
{"EitherGTEHeight", 2, 5, LocSelectIfEither, SizeSelectIfGTE},
{"EitherGTWidth", 2, 10, LocSelectIfEither, SizeSelectIfGT},
{"EitherGTHeight", 10, 4, LocSelectIfEither, SizeSelectIfGT},
{"EitherLTWidth", 7, 2, LocSelectIfEither, SizeSelectIfLT},
{"EitherLTHeight", 2, 10, LocSelectIfEither, SizeSelectIfLT},
// both must fit the bounds
{"BothLTE", 6, 9, LocSelectIfBoth, SizeSelectIfLTE},
{"BothGTE", 3, 5, LocSelectIfBoth, SizeSelectIfGTE},
{"BothLT", 7, 10, LocSelectIfBoth, SizeSelectIfLT},
{"BothGT", 2, 4, LocSelectIfBoth, SizeSelectIfGT},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
bitmaps := make([]*Bitmap, len(sizes))
boxes := make([]*image.Rectangle, len(sizes))
var rect image.Rectangle
for i, sz := range sizes {
bitmaps[i] = New(sz.width, sz.height)
rect = image.Rect(0, 0, sz.width, sz.height)
boxes[i] = &rect
}
bms := &Bitmaps{Values: bitmaps, Boxes: boxes}
selected, err := bms.SelectBySize(testCase.width, testCase.height, testCase.location, testCase.comparison)
require.NoError(t, err)
assert.Equal(t, selected, bms)
assert.Equal(t, selected.Values, bitmaps)
assert.Equal(t, selected.Boxes, boxes)
})
}
})
t.Run("PartlyWithin", func(t *testing.T) {
t.Run("BothLTE", func(t *testing.T) {
sizes := []size{{7, 8}, {6, 9}, {5, 5}, {3, 7}}
bitmaps := make([]*Bitmap, len(sizes))
boxes := make([]*image.Rectangle, len(sizes))
var rect image.Rectangle
for i, sz := range sizes {
bitmaps[i] = New(sz.width, sz.height)
rect = image.Rect(0, 0, sz.width, sz.height)
boxes[i] = &rect
}
bms := &Bitmaps{Values: bitmaps, Boxes: boxes}
selected, err := bms.SelectBySize(6, 10, LocSelectIfBoth, SizeSelectIfLTE)
require.NoError(t, err)
assert.NotEqual(t, selected, bms)
assert.Len(t, selected.Values, 3)
assert.Len(t, selected.Boxes, 3)
for i := 1; i < 4; i++ {
assert.Equal(t, bitmaps[i], selected.Values[i-1])
assert.Equal(t, boxes[i], selected.Boxes[i-1])
}
})
})
})
}
// TestBitmaps_SortByHeight tests the SortByHeight function for the bitmaps 'b'
func TestBitmaps_SortByHeight(t *testing.T) {
bms := &Bitmaps{}
bms.AddBitmap(New(10, 20))
bms.AddBitmap(New(5, 18))
bms.AddBitmap(New(2, 30))
bms.AddBitmap(New(40, 20))
bms.SortByHeight()
assert.Equal(t, 18, bms.Values[0].Height)
assert.Equal(t, 20, bms.Values[1].Height)
assert.Equal(t, 20, bms.Values[2].Height)
assert.Equal(t, 30, bms.Values[3].Height)
}
// TestBitmaps_SortByWidth tests the SortByHeight function for the bitmaps 'b'
func TestBitmaps_SortByWidth(t *testing.T) {
bms := &Bitmaps{}
bms.AddBitmap(New(10, 20))
bms.AddBitmap(New(5, 18))
bms.AddBitmap(New(2, 30))
bms.AddBitmap(New(40, 20))
bms.SortByWidth()
assert.Equal(t, 2, bms.Values[0].Width)
assert.Equal(t, 5, bms.Values[1].Width)
assert.Equal(t, 10, bms.Values[2].Width)
assert.Equal(t, 40, bms.Values[3].Width)
}
func TestBitmaps_GroupByHeight(t *testing.T) {
bms := &Bitmaps{}
bms.AddBitmap(New(10, 20))
bms.AddBitmap(New(5, 18))
bms.AddBitmap(New(2, 30))
bms.AddBitmap(New(40, 20))
bms.AddBitmap(New(2, 20))
bms.AddBitmap(New(3, 20))
bms.AddBitmap(New(3, 18))
ba, err := bms.GroupByHeight()
require.NoError(t, err)
require.Len(t, ba.Values, 3)
for i, bmst := range ba.Values {
var height, length int
switch i {
case 0:
height = 18
length = 2
case 1:
height = 20
length = 4
case 2:
height = 30
length = 1
}
assert.Len(t, bmst.Values, length)
assert.Equal(t, height, bmst.Values[0].Height)
}
}
func TestBitmaps_GroupByWidth(t *testing.T) {
bms := &Bitmaps{}
bms.AddBitmap(New(10, 20))
bms.AddBitmap(New(5, 18))
bms.AddBitmap(New(2, 30))
bms.AddBitmap(New(40, 20))
bms.AddBitmap(New(2, 20))
bms.AddBitmap(New(3, 20))
bms.AddBitmap(New(3, 18))
// 2 x 2, 2 x 3, 1 x 5, 1 x 10, 1 x 40
// there should be 5 groups.
ba, err := bms.GroupByWidth()
require.NoError(t, err)
require.Len(t, ba.Values, 5)
for i, bmst := range ba.Values {
var width, length int
switch i {
case 0:
width = 2
length = 2
case 1:
width = 3
length = 2
case 2:
width = 5
length = 1
case 3:
width = 10
length = 1
case 4:
width = 40
length = 1
}
assert.Len(t, bmst.Values, length)
assert.Equal(t, width, bmst.Values[0].Width)
}
}