Last active
July 8, 2016 13:17
-
-
Save aycabta/5d26037333246c7f052f7b904667a65a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BinaryDotToBraille | |
LEFT_SHIFT_TABLE = { | |
0b00000001 => 7, | |
0b00000010 => 4, | |
0b00000100 => 1, | |
0b00001000 => 3, | |
0b00010000 => 0, | |
0b00100000 => -3, | |
0b01000000 => -5, | |
0b10000000 => -7 | |
} | |
num = 0 | |
@@num_to_braille = {} | |
(0..0xF).each do |upper| | |
(0..0xF).each do |downer| | |
code = (0..7).inject(0) { |code, bit_digit| | |
mask = 1 << bit_digit | |
code |= (num & mask) << LEFT_SHIFT_TABLE[mask] | |
} | |
@@num_to_braille[code] = [(0x2800 + upper * 0x10 + downer)].pack('U*') | |
num += 1 | |
end | |
end | |
def self.convert(width, height, binary) | |
if width % 2 != 0 || height % 4 != 0 | |
nil | |
else | |
table = [] | |
vertical_char_num = height / 4 | |
horizontal_char_num = width / 2 | |
(0..(vertical_char_num - 1)).each do |v_pos| | |
row = [] | |
(0..(horizontal_char_num - 1)).each do |h_pos| | |
braille_binary = | |
binary.slice((v_pos * 4 ) * width + h_pos * 2, 2) + | |
binary.slice((v_pos * 4 + 1) * width + h_pos * 2, 2) + | |
binary.slice((v_pos * 4 + 2) * width + h_pos * 2, 2) + | |
binary.slice((v_pos * 4 + 3) * width + h_pos * 2, 2) | |
row << @@num_to_braille[braille_binary.to_i(2)] | |
end | |
table << row | |
end | |
table | |
end | |
end | |
end | |
puts BinaryDotToBraille.convert(64, 48, | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000010010101010010000000100010000101000010000000000000000000000'+ | |
'0000010010101110111010001010100001000100111001000000000000000000'+ | |
'0000010000001010110000100100000001000100010001000000000000000010'+ | |
'0000010000001010011001000110000001000100101011100000111000000100'+ | |
'0000000000001110111010001100000001000100000001000000000000001000'+ | |
'0000010000001010010000101110000001000100000001000100000001000000'+ | |
'0000000000000000000000000000000000101000000000001000000000000000'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000000000000000000000000000000000000000000000000000000000000100'+ | |
'0100010011001100001011100110111001000100000000000010000010001010'+ | |
'1010110000100010011010001000001010101010010001000100111001000010'+ | |
'1110010001000100101011001100010001000110000000001000000000100100'+ | |
'1010010010000010111000101010010010100010000000000100111001000000'+ | |
'0100111011101100001011000100010001001100010001000010000010000100'+ | |
'0000000000000000000000000000000000000000000010000000000000000000'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0100010011000110110011101110011010101110001010101000101011000100'+ | |
'1010101010101000101010001000100010100100001010101000111010101010'+ | |
'0010101011001000101011001100100010100100001011001000111010101010'+ | |
'0110111010101000101010001000101011100100001010101000101010101010'+ | |
'1010101010101000101010001000101010100100101010101000101010101010'+ | |
'0100101011000110110011101000011010101110010010101110101010100100'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'1100010011000110111010101010101010101010111001101010110001000000'+ | |
'1010101010101000010010101010101010101010001001000100010010100000'+ | |
'1010101010100100010010101010101001000100010001001110010000000000'+ | |
'1100101011000010010010101010111001000100010001000100010000000000'+ | |
'1000101010100010010010101100111010100100100001001110010000000000'+ | |
'1000010010101100010011101000101010100100111001000100010000000000'+ | |
'0000001000000000000000000000000000000000000001100000110000001110'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0100000010000000001000000110000010000100010010001100000000000000'+ | |
'0010000010000000001000000100000010000000000010000100000000000000'+ | |
'0000011011000110011001101110011011000100010010100100110011000100'+ | |
'0000101010101000101011100100101010100100010011000100111010101010'+ | |
'0000101010101000101010000100011010100100010010100100111010101010'+ | |
'0000011011000110011001100100001010100100010010100100101010100100'+ | |
'0000000000000000000000000000110000000000100000000000000000000000'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000000000000000000000000000000000000000000000100100100011100000'+ | |
'0000000000000000010000000000000000000000000001000100010000000000'+ | |
'1100011010100110111010101010101010101010111001000100010000000000'+ | |
'1010101011001100010010101010101001001010001010000100001000000000'+ | |
'1010101010000110010010101100111001000110010001000100010000000000'+ | |
'1100011010001100011011101000111010100010111001000100010000000000'+ | |
'1000001000000000000000000000000000001100000000100100100000000000' | |
).map{ |row| row.join }.join("\n") | |
puts BinaryDotToBraille.convert(64, 32, | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000000011100000000000001110000000000000000000000000000000000000'+ | |
'0000000010000010000000000010000000000000000000000000000000000000'+ | |
'0000000010000010000000001110111000100100000000100100000011101110'+ | |
'0000010010000010000001000010011001001110111011101110110001101110'+ | |
'0000101010000010100000000100010011001010010001100110010000100010'+ | |
'0000010000001110010000001000100001000010111010100100111011101100'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000111000100100000000100100010001001000000010101100111010001010'+ | |
'0000001000101110111011101110111001101110111011100000001011100110'+ | |
'1000011001001010010000100110010010101100001010101100001010100110'+ | |
'0110010011000010010001100110111000100100001000100000010010100010'+ | |
'0000010001000010010010101010010001000100001001000010010010000100'+ | |
'0000100001000100111000101010010010001000111010001100101011101000'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0100111000001110100001000000111001000010010010001110010001001110'+ | |
'0110010011100000100001000110001011100010001010000010010011100010'+ | |
'1010111011101110110011100000101000100010101011100010101001000010'+ | |
'0110010000100100101001000000010001000010101010000010101011101100'+ | |
'0100010000100100100001000000010011100100101010000100001011100100'+ | |
'1000100011001000100010001110101001001000101001101000000001000010'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000000000000000000000000000000000000000000000000000000000000000'+ | |
'0000010000101110010011001110111010100100100000001110110010100100'+ | |
'1110010010100100111001000010000010100100100011101010000000001010'+ | |
'0000010001001110011001001110111010101100100010100010000000000100'+ | |
'0110100001000100011001000010001000101100100010100010001000000000'+ | |
'0000101010100100010001000010001000101100101010100100001000000000'+ | |
'1110111010000110010011101110110001001110110011101000110000000000' | |
).map{ |row| row.join }.join("\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment