blocks.go 1.7 KB

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