Animation now uses .pia files from SPIFFS instead of hardcoded C stuff.

This commit is contained in:
2019-06-18 18:10:58 +02:00
parent 1912772da3
commit 3c0e7a116a
9 changed files with 234 additions and 286 deletions

View File

@ -21,7 +21,7 @@ def compress(data, use_cutoff=true)
if x == last
count += 1
end
if x!=last || count==255
if use_cutoff
if count <= cutoff
@ -47,7 +47,7 @@ def compress(data, use_cutoff=true)
end
return result
end
unless ARGV[0]
puts "Usage:"
puts "#{$0} <gif_file> [OPTIONS]"
@ -58,7 +58,7 @@ unless ARGV[0]
puts
puts "Resulting data format is described in animations.h"
exit 1
end
end
image_file=ARGV[0]
full_frames = ARGV.include?("--full-frames")
@ -99,8 +99,6 @@ STDERR.puts "Using #{colors.count} colors."
raise "Number of colors has to be 255 or less!" if colors.count>255
STDERR.puts
puts
puts "uint8_t animation_#{name}_colors[] PROGMEM = {#{colors[2..-1].map{|c| [c>>24 & 0xFF, c>>16 & 0xFF, c>>8 & 0xFF]}.flatten.join(", ")}};"
p_frame = nil
frames_data = []
@ -124,7 +122,7 @@ frames.each_with_index do |frame, index|
frame.columns.times do |x|
color = frame.pixel_color(x, y).to_color(Magick::AllCompliance, true, 8, true)[1,8].to_i(16)
p_color = p_frame.pixel_color(x, y).to_color(Magick::AllCompliance, true, 8, true)[1,8].to_i(16)
if color==p_color && !full_frames
data << 0
elsif transparent.include? color
@ -144,7 +142,7 @@ frames.each_with_index do |frame, index|
times << old_time + time
end
p_frame = frame
if DEBUG
STDERR.puts "Frame ##{index}:"
total_y.times do |y|
@ -158,25 +156,27 @@ end
data = frames_data.map{|d| compress(d, true)}
individual_frame_times = times.uniq.count>1
times = [times[0]] unless individual_frame_times
puts "uint8_t animation_#{name}_data[] PROGMEM = {\n #{data.map{|d| d.join(",")}.join(",\n ")}\n};"
puts "uint16_t animation_#{name}_delays[] = {#{times.join(",")}};"
s=0
puts "uint16_t animation_#{name}_offsets[] = {#{(data.map{|d| t=s; s+=d.count; t} + [s]).join(",")}};"
d = "PIA\x01__" +
[total_x, total_y].pack("C*") +
[frames_data.count, colors.count-2].pack("C*") +
colors[2..-1].map{|c| [c>>24 & 0xFF, c>>16 & 0xFF, c>>8 & 0xFF]}.flatten.pack("C*") +
times.pack("n*") +
(data.map{|d| d.count}).pack("n*") +
data.flatten.pack("C*")
dd = "PIA\x01__" +
"XY:" + [total_x, total_y].pack("C*") +
"FC,CC:" + [frames_data.count, colors.count-2].pack("C*") +
"COLORS:" + colors[2..-1].map{|c| [c>>24 & 0xFF, c>>16 & 0xFF, c>>8 & 0xFF]}.flatten.pack("C*") +
"TIME:" + times.pack("n*") +
"LENGTHS:" + (data.map{|d| d.count}).pack("n*") +
"DATA:" + data.flatten.pack("C*")
size = d.length
d[4] = (size >> 8 & 0xFF).chr
d[5] = (size & 0xFF).chr
puts "AnimationData animation_#{name} = {"
puts " &animation_#{name}_colors[0],"
puts " &animation_#{name}_data[0],"
puts " &animation_#{name}_offsets[0],"
puts " &animation_#{name}_delays[0],"
puts " #{individual_frame_times}, /* individual_frame_times */"
puts " #{colors.count-2}, /* color_count */"
puts " #{frames_data.count}, /* frames_count */"
puts " #{total_x}, #{total_y} /* width, height */"
puts "};"
puts
STDERR.puts
STDERR.puts "Space usage:"
STDERR.puts " Colors: %6d bytes." % [s1=(colors.count-2) * 3] # colors are 3-bytes, but we have to use uint32_t, which takes up 4 bytes.
@ -184,5 +184,24 @@ STDERR.puts " Data: %6d bytes." % [s2=data.flatten.count]
STDERR.puts " Delays: %6d bytes." % [s5=times.count * 2 + 1]
STDERR.puts " Offsets: %6d bytes." % [s3=data.count * 2]
STDERR.puts " TOTAL: %6d bytes." % [s1+s2+s3+s5]
STDERR.puts " REALLY: %6d bytes." % d.size
STDERR.puts "Original size: %6d bytes." % [s4=File.new(image_file).size]
STDERR.puts "Difference: %6d bytes." % [s1+s2+s3+s5 - s4]
STDERR.puts
# File format
# Bytes 0-2: P I A (magic number)
# Byte 3: Version (currently 1)
# Bytes 4-5: Total file size
# Byte 6: width
# Byte 7: height
# Byte 8: frame_count
# Byte 9: color_count
# Color data, 3*color_count bytes
# Frame times, 2*frame_count bytes
# frame offsets, 2*frame_count bytes
STDERR.puts "Writing data to #{name}.pia..."
File.new("#{name}.pia", "w").write(d)
File.new("#{name}.pia.debug", "w").write(dd)