lib/thwomp/animation_preview.rb in thwomp-0.0.6 vs lib/thwomp/animation_preview.rb in thwomp-0.1.0

- old
+ new

@@ -1,58 +1,27 @@ require 'tmpdir' module Thwomp - class AnimationPreview < Struct.new(:renderer) + class AnimationPreview - attr_accessor :renderer, :max_width, :max_height, :max_frames + # TODO: (optional) use Thumbnail class to make png's from the + # rendered frames, instead of passing the frames directly to 'convert' - DEFAULT_OPTIONS = { :max_width => 128, - :max_height => 128, - :max_frames => 10 } + attr_reader :frames - # - # Creates an GIF animation from a given SWF - # - # Options - # :max_width Maximum width of GIF - # :max_height Maximum height of GIF - # - def initialize(renderer, options = {}) - DEFAULT_OPTIONS.merge(options).each { |k,v| send(:"#{k}=", v) if v } - @renderer = renderer + def initialize(frames) + @frames = frames end - # returns the gif binary data of generated animation - def gif_data - File.open(filename, 'rb') { |f| f.read } - end - - # returns temp filename of generated animation - def filename - @filename ||= generate_gif! - end - - private - - def thumbnail - @thumbnail ||= Thumbnail.new(renderer, :max_width => max_width, :max_height => max_height) - end - - # returns all filenames of the seperate frames in the flash animation - def frame_filenames - unless @frame_filenames - frames = (0..max_frames).to_a + ['last'] - renderer.render_batch!(frames) - @frame_filenames = frames.map { |frame| thumbnail.filename(frame) }.compact - end - - @frame_filenames - end - - def generate_gif! + def generate! filename = "#{Dir.tmpdir}/animation_#{Time.now.to_i}.gif" - `convert #{frame_filenames.join(' ')} #{filename}` + Command.exec("convert #{frames.join(' ')} #{filename}") filename + end + + # returns the gif binary data of generated animation + def gif_data + File.open(generate!, 'rb') { |f| f.read } end end end