Преглед изворни кода

Fix symbols consisting of a single block not having their remainder bits applied.

Tom Harwood пре 11 година
родитељ
комит
4d19c4c411
3 измењених фајлова са 26 додато и 7 уклоњено
  1. 6 5
      qrcode.go
  2. 3 2
      qrcode_decode_test.go
  3. 17 0
      symbol.go

+ 6 - 5
qrcode.go

@@ -316,6 +316,12 @@ func (q *QRCode) encode(numTerminatorBits int) {
 			log.Panic(err.Error())
 		}
 
+		numEmptyModules := s.numEmptyModules()
+		if numEmptyModules != 0 {
+			log.Panicf("bug: numEmptyModules is %d (expected 0) (version=%d)",
+				numEmptyModules, q.VersionNumber)
+		}
+
 		p := s.penaltyScore()
 
 		//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())
@@ -372,11 +378,6 @@ func (q *QRCode) encodeBlocks() *bitset.Bitset {
 
 	// Interleave the blocks.
 
-	// A single block doesn't need interleaving.
-	if len(block) == 1 {
-		return block[0].data
-	}
-
 	result := bitset.New()
 
 	// Combine data blocks.

+ 3 - 2
qrcode_decode_test.go

@@ -152,8 +152,9 @@ func TestDecodeFuzz(t *testing.T) {
 
 		var content string
 		for j := 0; j < len; j++ {
-			// zbarimg has trouble with null bytes, hence start from ASCII 1.
-			content += string(1+r.Intn(254))
+			// zbarimg seems to have trouble with special characters, test printable
+			// characters only for now.
+			content += string(32+r.Intn(94))
 		}
 
 		for _, level := range []RecoveryLevel{Low, Medium, High, Highest} {

+ 17 - 0
symbol.go

@@ -73,6 +73,23 @@ func (m *symbol) empty(x int, y int) bool {
 	return !m.isUsed[y+m.quietZoneSize][x+m.quietZoneSize]
 }
 
+// numEmptyModules returns the number of empty modules.
+//
+// Initially numEmptyModules is symbolSize * symbolSize. After every module has
+// been set (to either true or false), the number of empty modules is zero.
+func (m *symbol) numEmptyModules() int {
+	var count int
+	for y := 0; y < m.symbolSize; y++ {
+		for x := 0; x < m.symbolSize; x++ {
+			if !m.isUsed[y + m.quietZoneSize][x + m.quietZoneSize] {
+				count++
+			}
+		}
+	}
+
+	return count
+}
+
 // set sets the module at (x, y) to v.
 func (m *symbol) set(x int, y int, v bool) {
 	m.module[y+m.quietZoneSize][x+m.quietZoneSize] = v