Class: RQRCodeCore::QRBitBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/rqrcode_core/qrcode/qr_bit_buffer.rb

Constant Summary collapse

PAD0 =
0xEC
PAD1 =
0x11

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ QRBitBuffer

Returns a new instance of QRBitBuffer.



10
11
12
13
14
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 10

def initialize(version)
  @version = version
  @buffer = []
  @length = 0
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



5
6
7
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 5

def buffer
  @buffer
end

Instance Method Details

#alphanumeric_encoding_start(length) ⇒ Object



55
56
57
58
59
60
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 55

def alphanumeric_encoding_start(length)

  put( QRMODE[:mode_alpha_numk], 4 )
  put(length, QRUtil.get_length_in_bits(QRMODE[:mode_alpha_numk], @version))

end

#byte_encoding_start(length) ⇒ Object



48
49
50
51
52
53
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 48

def byte_encoding_start(length)

  put( QRMODE[:mode_8bit_byte], 4 )
  put(length, QRUtil.get_length_in_bits(QRMODE[:mode_8bit_byte], @version))

end

#end_of_message(max_data_bits) ⇒ Object



82
83
84
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 82

def end_of_message(max_data_bits)
  put( 0, 4 ) unless get_length_in_bits+4 > max_data_bits
end

#get(index) ⇒ Object



17
18
19
20
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 17

def get( index )
  buf_index = (index / 8).floor
  (( (@buffer[buf_index]).rszf(7 - index % 8)) & 1) == 1
end

#get_length_in_bitsObject



30
31
32
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 30

def get_length_in_bits
  @length
end

#numeric_encoding_start(length) ⇒ Object



62
63
64
65
66
67
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 62

def numeric_encoding_start(length)

  put( QRMODE[:mode_number], 4 )
  put(length, QRUtil.get_length_in_bits(QRMODE[:mode_number], @version))

end

#pad_until(prefered_size) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 69

def pad_until(prefered_size)
  # Align on byte
  while get_length_in_bits % 8 != 0
    put_bit( false )
  end

  # Pad with padding code words
  while get_length_in_bits < prefered_size
    put( QRBitBuffer::PAD0, 8 )
    put( QRBitBuffer::PAD1, 8 ) if get_length_in_bits < prefered_size
  end
end

#put(num, length) ⇒ Object



23
24
25
26
27
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 23

def put( num, length )
  ( 0...length ).each do |i|
    put_bit((((num).rszf(length - i - 1)) & 1) == 1)
  end
end

#put_bit(bit) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rqrcode_core/qrcode/qr_bit_buffer.rb', line 35

def put_bit( bit )
  buf_index = ( @length / 8 ).floor
  if @buffer.size <= buf_index
    @buffer << 0
  end

  if bit
    @buffer[buf_index] |= ((0x80).rszf(@length % 8))
  end

  @length += 1
end