- commit
- f3168efedf8714b39ed1bb317c087505c8d4aaa6
- parent
- 6b7676c4bd82a6d57c640eca7e08e9547b3da7b0
- Author
- Tom Harwood <tfh@skip.org>
- Date
- 2014-04-17 00:28
Improve penalty calculations, still incomplete.
Diffstat
| M | qrcode.go | 4 | ++++ |
| M | qrcode_decode_test.go | 5 | +++++ |
| M | qrcode_test.go | 17 | ++++++++++++++--- |
| M | symbol.go | 16 | ++++++++++++++-- |
4 files changed, 37 insertions, 5 deletions
diff --git a/qrcode.go b/qrcode.go
@@ -101,6 +101,7 @@ type QRCode struct {
101 101
102 102 data *bitset.Bitset
103 103 symbol *symbol
-1 104 mask int
104 105 }
105 106
106 107 // New constructs a QRCode.
@@ -318,8 +319,11 @@ func (q *QRCode) encode(numTerminatorBits int) {
318 319
319 320 var p int = s.penaltyScore()
320 321
-1 322 //log.Printf("mask=%d p=%3d p1=%3d p2=%3d p3=%3d p4=%d\n", mask, p, s.penalty1(), s.penalty2(), s.penalty3(), s.penalty4())
-1 323
321 324 if q.symbol == nil || p < penalty {
322 325 q.symbol = s
-1 326 q.mask = mask
323 327 penalty = p
324 328 }
325 329 }
diff --git a/qrcode_decode_test.go b/qrcode_decode_test.go
@@ -59,6 +59,11 @@ func TestDecodeBasic(t *testing.T) {
59 59 1,
60 60 Highest,
61 61 },
-1 62 {
-1 63 "01234567",
-1 64 1,
-1 65 Medium,
-1 66 },
62 67 }
63 68
64 69 for _, test := range tests {
diff --git a/qrcode_test.go b/qrcode_test.go
@@ -136,9 +136,20 @@ func TestQRCodeVersionCapacity(t *testing.T) {
136 136 }
137 137 }
138 138
139 -1 func BenchmarkQRCodeMinimumSize(b *testing.B) {
140 -1 for n := 0; n < b.N; n++ {
141 -1 New("1", Low)
-1 139 func TestQRCodeISOAnnexIExample(t *testing.T) {
-1 140 var q *QRCode
-1 141 q, err := New("01234567", Medium)
-1 142
-1 143 if err != nil {
-1 144 t.Fatalf("Error producing ISO Annex I Example: %s, expected success",
-1 145 err.Error())
-1 146 }
-1 147
-1 148 const expectedMask int = 2
-1 149
-1 150 if q.mask != 2 {
-1 151 t.Errorf("ISO Annex I example mask got %d, expected %d\n", q.mask,
-1 152 expectedMask)
142 153 }
143 154 }
144 155
diff --git a/symbol.go b/symbol.go
@@ -220,7 +220,7 @@ func (m *symbol) penalty3() int {
220 220 var penalty int = 0
221 221
222 222 for y := 0; y < m.symbolSize; y++ {
223 -1 var bitBuffer int16 = 0xFF
-1 223 var bitBuffer int16 = 0x00
224 224
225 225 for x := 0; x < m.symbolSize; x++ {
226 226 bitBuffer <<= 1
@@ -233,12 +233,18 @@ func (m *symbol) penalty3() int {
233 233 // 0x05d or 0x5d0
234 234 case 0x05d, 0x5d0:
235 235 penalty += penaltyWeight3
-1 236 bitBuffer = 0xFF
-1 237 default:
-1 238 if x == m.symbolSize - 1 && (bitBuffer & 0x7f) == 0x5d {
-1 239 penalty += penaltyWeight3
-1 240 bitBuffer = 0xFF
-1 241 }
236 242 }
237 243 }
238 244 }
239 245
240 246 for x := 0; x < m.symbolSize; x++ {
241 -1 var bitBuffer int16 = 0xFF
-1 247 var bitBuffer int16 = 0x00
242 248
243 249 for y := 0; y < m.symbolSize; y++ {
244 250 bitBuffer <<= 1
@@ -251,6 +257,12 @@ func (m *symbol) penalty3() int {
251 257 // 0x05d or 0x5d0
252 258 case 0x05d, 0x5d0:
253 259 penalty += penaltyWeight3
-1 260 bitBuffer = 0xFF
-1 261 default:
-1 262 if y == m.symbolSize - 1 && (bitBuffer & 0x7f) == 0x5d {
-1 263 penalty += penaltyWeight3
-1 264 bitBuffer = 0xFF
-1 265 }
254 266 }
255 267 }
256 268 }