LoginSignup
0
1

More than 5 years have passed since last update.

swfファイルのバイナリからRubyで縦横のサイズを取り出す

Last updated at Posted at 2016-05-10

http://doruby.kbmj.com/hal_on_rails/20100411/SWF_ を参考にコードを書いてみたのですが、使う機会がなかったのでここに残しておきます。

swf_file.rb
class SwfFile
  # @example:
  #   SwdFile.new("foo.swf")
  def initialize(path)
    @path = path
  end

  # @example:
  #   x_min, x_max, y_min, y_max = swf.stage_size
  def stage_size
    File.open(@path) do |io|
      io.read(8) # header

      # どんなに長くても 5+31*4 bitなので16byteで十分
      buf = io.read(16).bytes.map { |i|
        "%08d" % i.to_s(2)
      }.join

      # 最初の5bitが以降の区切りの長さ(bit)
      l = buf[0, 5].to_i(2)

      # 最初の5bitを消しておく
      buf[0, 5] = ""

      [
        buf[0, l], # x_min
        buf[l, l], # x_max
        buf[l * 2, l], # y_min
        buf[l * 3, l], # y_max
      ].map { |i|
        # 1pt=20twips
        i.to_i(2) / 20
      }
    end
  end
end
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1