From 5b7c7a70078cb688e1c5bdf8ee9350281c2d8708 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 6 Jun 2019 06:39:52 +0200 Subject: [PATCH] gif2c: Now has an option --full-frames which encodes every frame completely, without the "leave the previous pixel intact" bits. --- src/tools/gif2c.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/tools/gif2c.rb b/src/tools/gif2c.rb index 03f5e89..ac19add 100755 --- a/src/tools/gif2c.rb +++ b/src/tools/gif2c.rb @@ -48,29 +48,26 @@ def compress(data, use_cutoff=true) return result end -unless ARGV[0] && ARGV[1] +unless ARGV[0] puts "Usage:" - puts "#{$0} " - puts "e.g. '#{$0} mario.gif mario'" + puts "#{$0} [OPTIONS]" + puts "e.g. '#{$0} mario.gif'" + puts + puts "OPTIONS:" + puts " --full-frames Don't use optimiziation; output each frame as full frame." puts puts "Resulting data format is described in animations.h" exit 1 end 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) STDERR.puts "Found #{frames.count} frames." -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 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) 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 elsif transparent.include? color data << 1 @@ -149,18 +146,21 @@ frames.each_with_index do |frame, index| p_frame = frame if DEBUG - puts "Frame ##{index}:" + STDERR.puts "Frame ##{index}:" total_y.times do |y| total_x.times do |x| - print data[x + y*total_x]==0 ? "." : "X" + STDERR.print data[x + y*total_x]==0 ? "." : "X" end - puts + STDERR.puts end end 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 @@ -172,7 +172,7 @@ 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. 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 " TOTAL: %6d bytes." % [s1+s2+s3+s5] STDERR.puts "Original size: %6d bytes." % [s4=File.new(image_file).size]