blocks.go raw

   1  package qr
   2  
   3  type block struct {
   4  	data []byte
   5  	ecc  []byte
   6  }
   7  type blockList []*block
   8  
   9  func splitToBlocks(data <-chan byte, vi *versionInfo) blockList {
  10  	result := make(blockList, vi.NumberOfBlocksInGroup1+vi.NumberOfBlocksInGroup2)
  11  
  12  	for b := 0; b < int(vi.NumberOfBlocksInGroup1); b++ {
  13  		blk := new(block)
  14  		blk.data = make([]byte, vi.DataCodeWordsPerBlockInGroup1)
  15  		for cw := 0; cw < int(vi.DataCodeWordsPerBlockInGroup1); cw++ {
  16  			blk.data[cw] = <-data
  17  		}
  18  		blk.ecc = ec.calcECC(blk.data, vi.ErrorCorrectionCodewordsPerBlock)
  19  		result[b] = blk
  20  	}
  21  
  22  	for b := 0; b < int(vi.NumberOfBlocksInGroup2); b++ {
  23  		blk := new(block)
  24  		blk.data = make([]byte, vi.DataCodeWordsPerBlockInGroup2)
  25  		for cw := 0; cw < int(vi.DataCodeWordsPerBlockInGroup2); cw++ {
  26  			blk.data[cw] = <-data
  27  		}
  28  		blk.ecc = ec.calcECC(blk.data, vi.ErrorCorrectionCodewordsPerBlock)
  29  		result[int(vi.NumberOfBlocksInGroup1)+b] = blk
  30  	}
  31  
  32  	return result
  33  }
  34  
  35  func (bl blockList) interleave(vi *versionInfo) []byte {
  36  	var maxCodewordCount int
  37  	if vi.DataCodeWordsPerBlockInGroup1 > vi.DataCodeWordsPerBlockInGroup2 {
  38  		maxCodewordCount = int(vi.DataCodeWordsPerBlockInGroup1)
  39  	} else {
  40  		maxCodewordCount = int(vi.DataCodeWordsPerBlockInGroup2)
  41  	}
  42  	resultLen := (vi.DataCodeWordsPerBlockInGroup1+vi.ErrorCorrectionCodewordsPerBlock)*vi.NumberOfBlocksInGroup1 +
  43  		(vi.DataCodeWordsPerBlockInGroup2+vi.ErrorCorrectionCodewordsPerBlock)*vi.NumberOfBlocksInGroup2
  44  
  45  	result := make([]byte, 0, resultLen)
  46  	for i := 0; i < maxCodewordCount; i++ {
  47  		for b := 0; b < len(bl); b++ {
  48  			if len(bl[b].data) > i {
  49  				result = append(result, bl[b].data[i])
  50  			}
  51  		}
  52  	}
  53  	for i := 0; i < int(vi.ErrorCorrectionCodewordsPerBlock); i++ {
  54  		for b := 0; b < len(bl); b++ {
  55  			result = append(result, bl[b].ecc[i])
  56  		}
  57  	}
  58  	return result
  59  }
  60