...

Source file src/github.com/boombuler/barcode/aztec/errorcorrection.go

Documentation: github.com/boombuler/barcode/aztec

     1  package aztec
     2  
     3  import (
     4  	"github.com/boombuler/barcode/utils"
     5  )
     6  
     7  func bitsToWords(stuffedBits *utils.BitList, wordSize int, wordCount int) []int {
     8  	message := make([]int, wordCount)
     9  
    10  	for i := 0; i < wordCount; i++ {
    11  		value := 0
    12  		for j := 0; j < wordSize; j++ {
    13  			if stuffedBits.GetBit(i*wordSize + j) {
    14  				value |= (1 << uint(wordSize-j-1))
    15  			}
    16  		}
    17  		message[i] = value
    18  	}
    19  	return message
    20  }
    21  
    22  func generateCheckWords(bits *utils.BitList, totalBits, wordSize int) *utils.BitList {
    23  	rs := utils.NewReedSolomonEncoder(getGF(wordSize))
    24  
    25  	// bits is guaranteed to be a multiple of the wordSize, so no padding needed
    26  	messageWordCount := bits.Len() / wordSize
    27  	totalWordCount := totalBits / wordSize
    28  	eccWordCount := totalWordCount - messageWordCount
    29  
    30  	messageWords := bitsToWords(bits, wordSize, messageWordCount)
    31  	eccWords := rs.Encode(messageWords, eccWordCount)
    32  	startPad := totalBits % wordSize
    33  
    34  	messageBits := new(utils.BitList)
    35  	messageBits.AddBits(0, byte(startPad))
    36  
    37  	for _, messageWord := range messageWords {
    38  		messageBits.AddBits(messageWord, byte(wordSize))
    39  	}
    40  	for _, eccWord := range eccWords {
    41  		messageBits.AddBits(eccWord, byte(wordSize))
    42  	}
    43  	return messageBits
    44  }
    45  
    46  func getGF(wordSize int) *utils.GaloisField {
    47  	switch wordSize {
    48  	case 4:
    49  		return utils.NewGaloisField(0x13, 16, 1)
    50  	case 6:
    51  		return utils.NewGaloisField(0x43, 64, 1)
    52  	case 8:
    53  		return utils.NewGaloisField(0x012D, 256, 1)
    54  	case 10:
    55  		return utils.NewGaloisField(0x409, 1024, 1)
    56  	case 12:
    57  		return utils.NewGaloisField(0x1069, 4096, 1)
    58  	default:
    59  		return nil
    60  	}
    61  }
    62  

View as plain text