-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
116 lines (105 loc) · 2.57 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package classer
import (
"image"
"math"
"github.com/dlocmelis/pdfreader/common"
"github.com/dlocmelis/pdfreader/internal/jbig2/bitmap"
"github.com/dlocmelis/pdfreader/internal/jbig2/errors"
)
// JbAddedPixels is the size of the border added around pix of each c.c. for further processing.
const JbAddedPixels = 6
// For PixHausTest, PixRankHausTest and PixCorrelationScore
// the values should be or greater.
const (
MaxDiffWidth = 2
MaxDiffHeight = 2
)
const (
// MaxConnCompWidth is the default max cc width.
MaxConnCompWidth = 350
// MaxCharCompWidth is the default max char width.
MaxCharCompWidth = 350
// MaxWordCompWidth is the default max word width.
MaxWordCompWidth = 1000
// MaxCompHeight is the default max component height.
MaxCompHeight = 120
)
// finalAlignmentPositioning gets the best match position for the provided arguments.
func finalAlignmentPositioning(s *bitmap.Bitmap, x, y, iDelX, iDelY int, t *bitmap.Bitmap) (pt image.Point, err error) {
const processName = "finalAlignmentPositioning"
if s == nil {
return pt, errors.Error(processName, "source not provided")
}
if t == nil {
return pt, errors.Error(processName, "template not provided")
}
w, h := t.Width, t.Height
bx, by := x-iDelX-JbAddedPixels, y-iDelY-JbAddedPixels
common.Log.Trace("x: '%d', y: '%d', w: '%d', h: '%d', bx: '%d', by: '%d'", x, y, w, h, bx, by)
box, err := bitmap.Rect(bx, by, w, h)
if err != nil {
return pt, errors.Wrap(err, processName, "")
}
d, _, err := s.ClipRectangle(box)
if err != nil {
common.Log.Error("Can't clip rectangle: %v", box)
return pt, errors.Wrap(err, processName, "")
}
r := bitmap.New(d.Width, d.Height)
minCount := math.MaxInt32
var i, j, count, minX, minY int
for i = -1; i <= 1; i++ {
for j = -1; j <= 1; j++ {
if _, err = bitmap.Copy(r, d); err != nil {
return pt, errors.Wrap(err, processName, "")
}
if err = r.RasterOperation(j, i, w, h, bitmap.PixSrcXorDst, t, 0, 0); err != nil {
return pt, errors.Wrap(err, processName, "")
}
count = r.CountPixels()
if count < minCount {
minX = j
minY = i
minCount = count
}
}
}
pt.X = minX
pt.Y = minY
return pt, nil
}
// Method is the encoding method used enum.
type Method int
// enum definitions of the encoding methods.
const (
RankHaus Method = iota
Correlation
)
// TwoByTwoWalk is the slice of values for classified encoding.
var TwoByTwoWalk = []int{
0, 0,
0, 1,
-1, 0,
0, -1,
1, 0,
-1, 1,
1, 1,
-1, -1,
1, -1,
0, -2,
2, 0,
0, 2,
-2, 0,
-1, -2,
1, -2,
2, -1,
2, 1,
1, 2,
-1, 2,
-2, 1,
-2, -1,
-2, -2,
2, -2,
2, 2,
-2, 2,
}