Class: FlashHeader
- Inherits:
-
Object
- Object
- FlashHeader
- Defined in:
- lib/flash_header.rb
Overview
Read and parses the headers of an SWF file.
Examples:
header = FlashHeader.new('dog_on_skateboard.swf')
header.size #=> 5234
header = FlashHeader.new
# You can feed it any object that supports the read method
header.file = StringIO.new(flash_data)
header.read_header
header.width #=> 640
header.height #=> 480
Instance Attribute Summary collapse
-
#file ⇒ Object
An open IO object to read the headers from.
-
#header ⇒ Object
readonly
Reader for the instance variable that holds all the header values.
Instance Method Summary collapse
-
#compressed? ⇒ Boolean
Returns true if the file was compressed, otherwise it returns false.
-
#initialize(filename = nil) ⇒ FlashHeader
constructor
Initialize a new FlashHeader instance.
-
#method_missing(m, *a) ⇒ Object
Adds method access to the headers; signature, version, size, width and height.
-
#read_header ⇒ Object
Read the header of the initialized file.
Constructor Details
#initialize(filename = nil) ⇒ FlashHeader
Initialize a new FlashHeader instance. This reads the file and parses the header.
filename
: A filename for a shockwave flash file (optional).
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/flash_header.rb', line 26 def initialize(filename=nil) @header = {} unless filename.nil? @file = File.open filename begin read_header ensure @file.close end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *a) ⇒ Object
Adds method access to the headers; signature, version, size, width and height.
73 74 75 76 77 78 79 |
# File 'lib/flash_header.rb', line 73 def method_missing(m, *a) if @header.has_key?(m.to_sym) @header[m.to_sym] else super end end |
Instance Attribute Details
#file ⇒ Object
An open IO object to read the headers from.
19 20 21 |
# File 'lib/flash_header.rb', line 19 def file @file end |
#header ⇒ Object (readonly)
Reader for the instance variable that holds all the header values
21 22 23 |
# File 'lib/flash_header.rb', line 21 def header @header end |
Instance Method Details
#compressed? ⇒ Boolean
Returns true if the file was compressed, otherwise it returns false
68 69 70 |
# File 'lib/flash_header.rb', line 68 def compressed? @header[:signature][0..0] == 'C' end |
#read_header ⇒ Object
Read the header of the initialized file
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/flash_header.rb', line 39 def read_header temp = @file.read # Signature is three ASCII bytes, CWS or FWS @header[:signature] = temp[0..2] # Version is one byte version number @header[:version] = temp[3] # Size is LE byte order BE bit order @header[:size] = temp[4..7].unpack('C*').reverse.inject(0) do |t,i| (t << 8) + i end # Chop off the header part we just parsed if compressed? temp = Zlib::Inflate.inflate temp[8..-1] else temp = temp[8..-1] end return if temp.empty? # First 5 bits are the number of bits for the RECT structure nbits = temp[0] >> 3 # 5 bits + 4 times nbits, byte aligned nbytes = ((5 + nbits*4) / 8.0).ceil bytes = BitUnpacker.uunpack(temp[0..nbytes], nbits, 4, 5) @header[:width] = bytes[1] / 20 @header[:heigth] = bytes[3] / 20 # TODO: support for framerate and the other thing end |