pitrix/tools/gif2c.rb

162 lines
4.1 KiB
Ruby
Raw Normal View History

2019-05-22 06:14:13 +00:00
#!/usr/bin/env ruby
require 'rubygems'
require 'rmagick'
require 'pp'
###
# Converts a gif file to c code to enable displaying animations.
#
# The data format is described in animations.h
###
def compress(data, use_cutoff=true)
cutoff = 3
escape = 255
last = data[0]
result = []
count = 0
data.each do |x|
if x == last
count += 1
end
if x!=last || count==255
2019-05-22 06:14:13 +00:00
if use_cutoff
if count <= cutoff
count.times { result << last }
else
result << escape << count << last
end
else
result << count << last
end
count = 1
last = x
end
end
if use_cutoff
if count < cutoff
count.times { result << last }
else
result << escape << count << last
end
else
result << count << last
end
return result
end
unless ARGV[0] && ARGV[1]
puts "Usage:"
puts "#{$0} <gif_file> <variable_name>"
puts "e.g. '#{$0} mario.gif mario'"
puts
puts "Resulting data format is described in animations.h"
exit 1
end
image_file=ARGV[0]
name=ARGV[1]
frames = Magick::ImageList.new(image_file)
STDERR.puts "Found #{frames.count} frames."
2019-05-22 06:14:13 +00:00
STDERR.puts "Getting delays..."
ticks_per_second = frames.ticks_per_second
times = [] ; frames.each{|f| times << (1000.0 / ticks_per_second * f.delay).round}
individual_frame_times = true
if times.uniq.count==1
individual_frame_times = false
times = times[0, 1]
end
STDERR.print "Getting colors..."
2019-05-22 06:14:13 +00:00
colors = []
frames.each do |frame|
frame.columns.times do |x|
frame.rows.times do |y|
color = frame.pixel_color(x, y).to_color(Magick::AllCompliance, true, 8, true)
colors << color
end
end
end
colors = colors.uniq
STDERR.puts " Found #{colors.count} colors."
2019-05-22 06:14:13 +00:00
transparent = colors.select{|c| c.end_with? "00"}
STDERR.puts "#{transparent.count} color(s) being transparent."
2019-05-22 06:14:13 +00:00
# color[0] is "keep the color from the previous frame"
# color[1] is "background color"
colors = (["#00000012", "#00000013"] + (colors - transparent)).uniq
STDERR.puts "Using #{colors.count} colors."
2019-05-22 06:14:13 +00:00
raise "Number of colors has to be 255 or less!" if colors.count>255
STDERR.puts
2019-05-22 06:14:13 +00:00
puts
puts "uint32_t #{name}_colors[] = {#{colors.map{|c| "0x" + c[1, 6]}.join(", ")}};"
p_frame = nil
frames_data = []
times = []
2019-05-22 06:14:13 +00:00
frames.each_with_index do |frame, index|
data = []
if index==0 # first frame
frame.rows.times do |y|
frame.columns.times do |x|
color = frame.pixel_color(x, y).to_color(Magick::AllCompliance, true, 8, true)
if transparent.include? color
data << 1
2019-05-22 06:14:13 +00:00
else
data << colors.index(color)
end
end
end
else
frame.rows.times do |y|
frame.columns.times do |x|
color = frame.pixel_color(x, y).to_color(Magick::AllCompliance, true, 8, true)
p_color = p_frame.pixel_color(x, y).to_color(Magick::AllCompliance, true, 8, true)
if color==p_color
data << 0
elsif transparent.include? color
data << 1
else
data << colors.index(color)
end
end
end
end
time = (1000.0 / ticks_per_second * frame.delay).round
if frame==0 || data.uniq!=[0]
times << time
frames_data << data
else
old_time = times.pop
times << old_time + time
end
2019-05-22 06:14:13 +00:00
p_frame = frame
end
data = frames_data.map{|d| compress(d, true)}
puts "uint8_t #{name}_data[] = {\n #{data.map{|d| d.join(",")}.join(",\n ")}\n};"
puts "uint16_t #{name}_delays[] = {#{times.join(",")}};"
2019-05-22 06:14:13 +00:00
s=0
puts "uint16_t #{name}_offsets[] = {#{(data.map{|d| t=s; s+=d.count; t} + [s]).join(",")}};"
puts "AnimationData #{name} = {&#{name}_colors[0], &#{name}_data[0], &#{name}_offsets[0], &#{name}_delays[0], #{individual_frame_times}, #{colors.count}, #{frames_data.count}, #{frames.first.columns}, #{frames.first.rows}};"
2019-05-22 06:14:13 +00:00
puts
STDERR.puts
STDERR.puts "Space usage:"
STDERR.puts " Colors: %6d bytes." % [s1=colors.count * 4] # colors are 3-bytes, but we have to use uint32_t, which takes up 4 bytes.
STDERR.puts " Data: %6d bytes." % [s2=data.flatten.count]
STDERR.puts " Delays: %6d bytes." % [s5=times.count * 3 + 1]
STDERR.puts " Offsets: %6d bytes." % [s3=data.count * 2]
STDERR.puts " TOTAL: %6d bytes." % [s1+s2+s3+s5]
STDERR.puts "Original size: %6d bytes." % [s4=File.new(image_file).size]
STDERR.puts "Difference: %6d bytes." % [s1+s2+s3+s5 - s4]