gif2c: Now has an option --full-frames which encodes every frame completely, without the "leave the previous pixel intact" bits.

This commit is contained in:
Fabian Schlenz 2019-06-06 06:39:52 +02:00
parent b2f25f933b
commit 5b7c7a7007

View File

@ -48,29 +48,26 @@ def compress(data, use_cutoff=true)
return result return result
end end
unless ARGV[0] && ARGV[1] unless ARGV[0]
puts "Usage:" puts "Usage:"
puts "#{$0} <gif_file> <variable_name>" puts "#{$0} <gif_file> [OPTIONS]"
puts "e.g. '#{$0} mario.gif mario'" puts "e.g. '#{$0} mario.gif'"
puts
puts "OPTIONS:"
puts " --full-frames Don't use optimiziation; output each frame as full frame."
puts puts
puts "Resulting data format is described in animations.h" puts "Resulting data format is described in animations.h"
exit 1 exit 1
end end
image_file=ARGV[0] image_file=ARGV[0]
name=ARGV[1] full_frames = ARGV.include?("--full-frames")
name=File.basename(image_file, File.extname(image_file))
frames = Magick::ImageList.new(image_file) frames = Magick::ImageList.new(image_file)
STDERR.puts "Found #{frames.count} frames." STDERR.puts "Found #{frames.count} frames."
STDERR.puts "Getting delays..."
ticks_per_second = frames.ticks_per_second 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
total_x, total_y = frames.first.columns, frames.first.rows total_x, total_y = frames.first.columns, frames.first.rows
@ -128,7 +125,7 @@ frames.each_with_index do |frame, index|
color = frame.pixel_color(x, y).to_color(Magick::AllCompliance, true, 8, true)[1,8].to_i(16) 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) p_color = p_frame.pixel_color(x, y).to_color(Magick::AllCompliance, true, 8, true)[1,8].to_i(16)
if color==p_color if color==p_color && !full_frames
data << 0 data << 0
elsif transparent.include? color elsif transparent.include? color
data << 1 data << 1
@ -149,18 +146,21 @@ frames.each_with_index do |frame, index|
p_frame = frame p_frame = frame
if DEBUG if DEBUG
puts "Frame ##{index}:" STDERR.puts "Frame ##{index}:"
total_y.times do |y| total_y.times do |y|
total_x.times do |x| total_x.times do |x|
print data[x + y*total_x]==0 ? "." : "X" STDERR.print data[x + y*total_x]==0 ? "." : "X"
end end
puts STDERR.puts
end end
end end
end end
data = frames_data.map{|d| compress(d, true)} 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 "uint8_t animation_#{name}_data[] PROGMEM = {\n #{data.map{|d| d.join(",")}.join(",\n ")}\n};"
puts "uint16_t animation_#{name}_delays[] = {#{times.join(",")}};" puts "uint16_t animation_#{name}_delays[] = {#{times.join(",")}};"
s=0 s=0
@ -172,7 +172,7 @@ STDERR.puts
STDERR.puts "Space usage:" 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. 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.
STDERR.puts " Data: %6d bytes." % [s2=data.flatten.count] STDERR.puts " Data: %6d bytes." % [s2=data.flatten.count]
STDERR.puts " Delays: %6d bytes." % [s5=times.count * 3 + 1] STDERR.puts " Delays: %6d bytes." % [s5=times.count * 2 + 1]
STDERR.puts " Offsets: %6d bytes." % [s3=data.count * 2] STDERR.puts " Offsets: %6d bytes." % [s3=data.count * 2]
STDERR.puts " TOTAL: %6d bytes." % [s1+s2+s3+s5] STDERR.puts " TOTAL: %6d bytes." % [s1+s2+s3+s5]
STDERR.puts "Original size: %6d bytes." % [s4=File.new(image_file).size] STDERR.puts "Original size: %6d bytes." % [s4=File.new(image_file).size]