あーさん日記

https://akkera102.sakura.ne.jp/gbadev/ の中の人

Arduboy 実験6

現在、画像変換ツールGUI環境しかないようです。
自分はそんな環境に耐えられない為、
だったら作るしかないじゃんというオチに。


python先生のおかげで超絶短いコードになっているので
適当に直して使ってください。python + pilで動作します。

# -*- coding: utf-8 -*-
#
# Arduboy Image Converter
#
#
# 256色の画像ファイルを指定してください。
# 生成ファイルは「ファイル名.h」になります。
#
# 例:
#   conv.py image.png
#   conv.py image.bmp
#
# 注意:
#   パレット255番目の色がビットあり(白色)になります。
#   縦のサイズは8の倍数にしてください。

import Image
import sys
import os

param = sys.argv

if len(param) != 2:
	print "param error"
	sys.exit(1)

im = Image.open(param[1])

if im.size[1] % 8 != 0:
	print "size error"
	sys.exit(1)


list = []
cx = im.size[0]
cy = im.size[1]

for y in range(7, cy, 8):
	for x in range(0, cx):
		bit = 0
		if im.getpixel((x, y-0)) == 255: bit += 0x80
		if im.getpixel((x, y-1)) == 255: bit += 0x40
		if im.getpixel((x, y-2)) == 255: bit += 0x20
		if im.getpixel((x, y-3)) == 255: bit += 0x10
		if im.getpixel((x, y-4)) == 255: bit += 0x08
		if im.getpixel((x, y-5)) == 255: bit += 0x04
		if im.getpixel((x, y-6)) == 255: bit += 0x02
		if im.getpixel((x, y-7)) == 255: bit += 0x01
		list.append(bit)


fname = str(os.path.splitext(param[1])[0])
s = ""

s +=  "#ifndef RES_" + fname.upper() + "_H\n"
s +=  "#define RES_" + fname.upper() + "_H\n"
s +=  "#ifdef __cplusplus\n"
s +=  "extern \"C\" {\n"
s +=  "#endif\n"
s +=  "\n"
s +=  "// " + param[1]+ " / " + str(cx) + "x" + str(cy) + "\n"
s +=  "PROGMEM const unsigned char image" + fname[0].upper() + fname[1:] + "[] = {\n"
s +=  "\t" + str(cx) + ", " + str(cy) + ","

for i in range(len(list)):
	if i == 0 or i % cx == 0:
		s +=  "\n\t"

	s +=  "0x%02X" % list[i]

	if i != len(list)-1:
		s += ","

s += "\n};\n"
s += "\n"
s += "#ifdef __cplusplus\n"
s += "}\n"
s += "#endif\n"
s += "#endif\n"


f = open(fname + ".h", 'w')
f.write(s)
f.close()

■変換画像 16x16ドット
f:id:akkera102:20151119124547p:plain

■変換結果

#ifndef RES_TEST_H
#define RES_TEST_H
#ifdef __cplusplus
extern "C" {
#endif

// test.png / 16x16
PROGMEM const unsigned char imageTest[] = {
	16, 16,
	0xFF,0x03,0x05,0x09,0x11,0x21,0x41,0x81,0x81,0x41,0x21,0x11,0x09,0x05,0x03,0xFF,
	0xFF,0xC0,0xA0,0x90,0x88,0x84,0x82,0x81,0x81,0x82,0x84,0x88,0x90,0xA0,0xC0,0xFF
};

#ifdef __cplusplus
}
#endif
#endif