-
Notifications
You must be signed in to change notification settings - Fork 2
/
img2ard.rb
executable file
·213 lines (178 loc) · 4.4 KB
/
img2ard.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env ruby
require 'rubygems'
require 'chunky_png'
class AssetFile
attr :name
attr :files
def initialize(name)
@name = name
@files = []
end
def filename
"#{name}.h"
end
def to_define_name
name.upcase + "_H"
end
def to_s
header = "#ifndef #{to_define_name}\n#define #{to_define_name}\n\n"
footer = "#endif\n"
core = @files.map(&:to_s).join("")
header + core + footer
end
end
class ImageCharArray
PER_LINE = 10
attr :name
attr :width
attr :height
attr :data
attr :mask_data
def initialize(img, name)
@width = img.width
@height = img.height
@mask = false
@name = name
@data = []
@mask_data = []
autodetect_frames
end
DIMENSION_REGEX = /[-_](\d+)x(\d+)/
def autodetect_frames
@frames = 1
return unless name =~ DIMENSION_REGEX
x = $1.to_i
y = $2.to_i
if x != width
fail "X size given in filename must match horizontal width of image"
end
if (height % y) != 0
fail "Y size given in filename must divide evenly into total image height"
end
# remove the resolution from the soruce name in our header file
name.gsub! DIMENSION_REGEX, ""
@frames = height / y
@height = y
end
def variable_name
File.basename(name,".*").gsub("-","_")
end
def mask_name
variable_name + "_mask"
end
def plus_mask_name
variable_name + "_plus_mask"
end
def masked!
@mask = true
end
def mask?
@mask
end
def frame_data(data)
return [data] if @frames == 1
groups = []
frame_size = data.size / @frames
@frames.times do |i|
groups << data.slice(i * frame_size, frame_size)
end
groups
end
def image_data(all_data)
core = ''
core << resolution_data
frame_data(all_data).each_with_index do |data, fc|
core << "// frame #{fc}\n" if @frames > 1
data.each_with_index do |x, i|
hex = x.to_s(16).upcase
core << "0x" + (hex.length == 1 ? "0" : "") + hex
core << ", " unless i == @data.size - 1
core << "\n" if (i + 1) % PER_LINE == 0
end
core << "\n"
end
core
end
def resolution_data
"// width, height\n" \
"#{width}, #{height},\n"
end
def frame_count_code
return "" unless @frames > 1
"// #{@frames} frames\n"
end
def interlace(image, mask)
a = []
while (image.size > 0)
a << image.shift
a << mask.shift
end
a
end
def code_header
o = "// #{File.basename(name)}\n"
o << frame_count_code
o << "// #{width}x#{height}\n"
o
end
def to_s
o = code_header
o << "PROGMEM const unsigned char #{variable_name}[] = {\n"
o << image_data(@data)
o << "\n};\n\n"
if mask?
o << "PROGMEM const unsigned char #{mask_name}[] = {\n"
o << image_data(@mask_data)
o << "\n};\n\n"
o << "PROGMEM const unsigned char #{plus_mask_name}[] = {\n"
o << image_data(interlace(@data, @mask_data))
o << "\n};\n\n"
end
o
end
end
resource = AssetFile.new("assets")
files = Dir.glob("./assets/**/*.png")
files.each do |file|
img = ChunkyPNG::Image.from_file(file)
# puts img.inspect
out = ImageCharArray.new(img, file)
puts "#{file}: #{img.width}x#{img.height}"
bits_last_page = img.height % 8
bytes_high = img.height / 8
bytes_high +=1 if bits_last_page>0
(0..bytes_high - 1).each do |ypage|
(0..img.width - 1).each do |x|
# how many bits does this line hold
bits = 8
# if we've reached the bottom there are fewer bits to load
bits = bits_last_page if bytes_high-1==ypage and bits_last_page > 0
byte = 0
alpha_byte = 0
(0..bits-1).each do |bit_height|
px = img[x, ypage*8 + bit_height]
# print ChunkyPNG::Color.to_hex(px)
# right now we only care about black/white so convert to greyscale
c = ChunkyPNG::Color.grayscale_teint(px)
alpha = ChunkyPNG::Color.a(px)
# puts "#{c} #{alpha}"
# puts("#{file} #{x}, #{ypage}, #{c} #{px}")
if c > 128 and not alpha < 128
byte += (1 << (bit_height))
end
if alpha > 128 # visible
alpha_byte += (1 << (bit_height))
out.masked!
end
end
out.mask_data << (alpha_byte)
out.data << byte
end
end
# puts out.inspect
resource.files << out
end
File.open(resource.filename,"w") do |f|
f.write resource.to_s
end
puts "\n#{resource.filename} compiled."