forked from makiuchi-d/gozxing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_matrix_parser.go
206 lines (184 loc) · 5.76 KB
/
bit_matrix_parser.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package decoder
import (
"github.com/kaxap/gozxing"
)
type BitMatrixParser struct {
bitMatrix *gozxing.BitMatrix
parsedVersion *Version
parsedFormatInfo *FormatInformation
mirror bool
}
func NewBitMatrixParser(bitMatrix *gozxing.BitMatrix) (*BitMatrixParser, error) {
dimension := bitMatrix.GetHeight()
if dimension < 21 || (dimension&0x03) != 1 {
return nil, gozxing.NewFormatException("dimension = %v", dimension)
}
return &BitMatrixParser{bitMatrix: bitMatrix}, nil
}
func (this *BitMatrixParser) ReadFormatInformation() (*FormatInformation, error) {
if this.parsedFormatInfo != nil {
return this.parsedFormatInfo, nil
}
// Read top-left format info bits
formatInfoBits1 := 0
for i := 0; i < 6; i++ {
formatInfoBits1 = this.copyBit(i, 8, formatInfoBits1)
}
// .. and skip a bit in the timing pattern ...
formatInfoBits1 = this.copyBit(7, 8, formatInfoBits1)
formatInfoBits1 = this.copyBit(8, 8, formatInfoBits1)
formatInfoBits1 = this.copyBit(8, 7, formatInfoBits1)
// .. and skip a bit in the timing pattern ...
for j := 5; j >= 0; j-- {
formatInfoBits1 = this.copyBit(8, j, formatInfoBits1)
}
// Read the top-right/bottom-left pattern too
dimension := this.bitMatrix.GetHeight()
formatInfoBits2 := 0
jMin := dimension - 7
for j := dimension - 1; j >= jMin; j-- {
formatInfoBits2 = this.copyBit(8, j, formatInfoBits2)
}
for i := dimension - 8; i < dimension; i++ {
formatInfoBits2 = this.copyBit(i, 8, formatInfoBits2)
}
this.parsedFormatInfo = FormatInformation_DecodeFormatInformation(uint(formatInfoBits1), uint(formatInfoBits2))
if this.parsedFormatInfo != nil {
return this.parsedFormatInfo, nil
}
return nil, gozxing.NewFormatException("failed to parse format info")
}
func (this *BitMatrixParser) ReadVersion() (*Version, error) {
if this.parsedVersion != nil {
return this.parsedVersion, nil
}
dimension := this.bitMatrix.GetHeight()
provisionalVersion := (dimension - 17) / 4
if provisionalVersion <= 6 {
return Version_GetVersionForNumber(provisionalVersion)
}
// Read top-right version info: 3 wide by 6 tall
versionBits := 0
ijMin := dimension - 11
for j := 5; j >= 0; j-- {
for i := dimension - 9; i >= ijMin; i-- {
versionBits = this.copyBit(i, j, versionBits)
}
}
theParsedVersion, e := Version_decodeVersionInformation(versionBits)
if e == nil && theParsedVersion != nil && theParsedVersion.GetDimensionForVersion() == dimension {
this.parsedVersion = theParsedVersion
return theParsedVersion, nil
}
// Hmm, failed. Try bottom left: 6 wide by 3 tall
versionBits = 0
for i := 5; i >= 0; i-- {
for j := dimension - 9; j >= ijMin; j-- {
versionBits = this.copyBit(i, j, versionBits)
}
}
theParsedVersion, e = Version_decodeVersionInformation(versionBits)
if e == nil && theParsedVersion != nil && theParsedVersion.GetDimensionForVersion() == dimension {
this.parsedVersion = theParsedVersion
return theParsedVersion, nil
}
return nil, gozxing.WrapFormatException(e)
}
func (this *BitMatrixParser) copyBit(i, j, versionBits int) int {
var bit bool
if this.mirror {
bit = this.bitMatrix.Get(j, i)
} else {
bit = this.bitMatrix.Get(i, j)
}
if bit {
return (versionBits << 1) | 0x1
}
return versionBits << 1
}
func (this *BitMatrixParser) ReadCodewords() ([]byte, error) {
formatInfo, e := this.ReadFormatInformation()
if e != nil {
return nil, e
}
version, e := this.ReadVersion()
if e != nil {
return nil, gozxing.WrapFormatException(e)
}
// Get the data mask for the format used in this QR Code. This will exclude
// some bits from reading as we wind through the bit matrix.
dataMask := DataMaskValues[formatInfo.GetDataMask()]
dimension := this.bitMatrix.GetHeight()
dataMask.UnmaskBitMatrix(this.bitMatrix, dimension)
functionPattern, e := version.buildFunctionPattern()
if e != nil {
return nil, gozxing.WrapFormatException(e)
}
readingUp := true
result := make([]byte, version.GetTotalCodewords())
resultOffset := 0
currentByte := 0
bitsRead := 0
// Read columns in pairs, from right to left
for j := dimension - 1; j > 0; j -= 2 {
if j == 6 {
// Skip whole column with vertical alignment pattern;
// saves time and makes the other code proceed more cleanly
j--
}
// Read alternatingly from bottom to top then top to bottom
for count := 0; count < dimension; count++ {
i := count
if readingUp {
i = dimension - 1 - count
}
for col := 0; col < 2; col++ {
// Ignore bits covered by the function pattern
if !functionPattern.Get(j-col, i) {
// Read a bit
bitsRead++
currentByte <<= 1
if this.bitMatrix.Get(j-col, i) {
currentByte |= 1
}
// If we've made a whole byte, save it off
if bitsRead == 8 {
result[resultOffset] = byte(currentByte)
resultOffset++
bitsRead = 0
currentByte = 0
}
}
}
}
readingUp = !readingUp // readingUp ^= true; // switch directions
}
if resultOffset != version.GetTotalCodewords() {
return nil, gozxing.NewFormatException(
"resultOffset=%v, totalCodeWords=%v", resultOffset, version.GetTotalCodewords())
}
return result, nil
}
func (this *BitMatrixParser) Remask() {
if this.parsedFormatInfo == nil {
return // We have no format information, and have no data mask
}
dataMask := DataMaskValues[this.parsedFormatInfo.GetDataMask()]
dimension := this.bitMatrix.GetHeight()
dataMask.UnmaskBitMatrix(this.bitMatrix, dimension)
}
func (this *BitMatrixParser) SetMirror(mirror bool) {
this.parsedVersion = nil
this.parsedFormatInfo = nil
this.mirror = mirror
}
func (this *BitMatrixParser) Mirror() {
for x := 0; x < this.bitMatrix.GetWidth(); x++ {
for y := x + 1; y < this.bitMatrix.GetHeight(); y++ {
if this.bitMatrix.Get(x, y) != this.bitMatrix.Get(y, x) {
this.bitMatrix.Flip(y, x)
this.bitMatrix.Flip(x, y)
}
}
}
}