class RQRCodeCore::QRCode

def create_bytes(buffer, rs_blocks) #:nodoc:

:nodoc:
def create_bytes(buffer, rs_blocks) #:nodoc:
  offset = 0
  max_dc_count = 0
  max_ec_count = 0
  dcdata = Array.new(rs_blocks.size)
  ecdata = Array.new(rs_blocks.size)
  rs_blocks.each_with_index do |rs_block, r|
    dc_count = rs_block.data_count
    ec_count = rs_block.total_count - dc_count
    max_dc_count = [max_dc_count, dc_count].max
    max_ec_count = [max_ec_count, ec_count].max
    dcdata_block = Array.new(dc_count)
    dcdata_block.size.times do |i|
      dcdata_block[i] = 0xff & buffer.buffer[i + offset]
    end
    dcdata[r] = dcdata_block
    offset += dc_count
    rs_poly = QRUtil.get_error_correct_polynomial(ec_count)
    raw_poly = QRPolynomial.new(dcdata[r], rs_poly.get_length - 1)
    mod_poly = raw_poly.mod(rs_poly)
    ecdata_block = Array.new(rs_poly.get_length - 1)
    ecdata_block.size.times do |i|
      mod_index = i + mod_poly.get_length - ecdata_block.size
      ecdata_block[i] = mod_index >= 0 ? mod_poly.get(mod_index) : 0
    end
    ecdata[r] = ecdata_block
  end
  total_code_count = rs_blocks.reduce(0) do |sum, rs_block|
    sum + rs_block.total_count
  end
  data = Array.new(total_code_count)
  index = 0
  max_dc_count.times do |i|
    rs_blocks.size.times do |r|
      if i < dcdata[r].size
        data[index] = dcdata[r][i]
        index += 1
      end
    end
  end
  max_ec_count.times do |i|
    rs_blocks.size.times do |r|
      if i < ecdata[r].size
        data[index] = ecdata[r][i]
        index += 1
      end
    end
  end
  data
end