Last active
January 11, 2019 03:53
-
-
Save Shinichi-Ohki/40a2d3c63b3cde0c8b6b45e1b3fb19f8 to your computer and use it in GitHub Desktop.
bmp to header file. (.bmp to .h)
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
require 'mini_magick' | |
shiftbit = 2 | |
filename = ARGV[0] | |
bname = File.basename(filename, '.*') | |
img = MiniMagick::Image.open(filename) | |
img_width = img.width | |
img_height = img.height | |
pixels = img.get_pixels | |
pix_r = Array.new(img_width) | |
pix_g = Array.new(img_width) | |
pix_b = Array.new(img_width) | |
print("#include <avr/pgmspace.h>\n") unless ARGV[1] == 'h' | |
print(format('const PROGMEM uint32_t %s[] = {', bname)) | |
print("\n") | |
img_height.times do |ypos| | |
img_width.times do |xpos| | |
pix_r[xpos] = (pixels[ypos][xpos][0] >> shiftbit).to_s(16) | |
pix_g[xpos] = (pixels[ypos][xpos][1] >> shiftbit).to_s(16) | |
pix_b[xpos] = (pixels[ypos][xpos][2] >> shiftbit).to_s(16) | |
pix_r[xpos] = '0' << pix_r[xpos] if pix_r[xpos].length == 1 | |
pix_g[xpos] = '0' << pix_g[xpos] if pix_g[xpos].length == 1 | |
pix_b[xpos] = '0' << pix_b[xpos] if pix_b[xpos].length == 1 | |
print(format('0x%s%s%s', pix_r[xpos], pix_g[xpos], pix_b[xpos])) | |
print(',') unless xpos == img_width-1 && ypos == img_height-1 | |
end | |
print("\n") | |
end | |
print("};\n") |
第2パラメータに'h'があったら#includeの行を出力しないことにした。
第1パラメータはbmpのファイル名。.bmpは省略できない。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LEDテープを光らせるためのデータを作るスクリプトとして作りました。そのため少し暗いデータにするために2bitシフトしています。
生データがほしい場合は2行目のshiftbitを0にしたらいい気がします。