Class: SwfUtil::SWFHeader
- Inherits:
-
SWFCompression
- Object
- SWFCompression
- SwfUtil::SWFHeader
- Defined in:
- lib/swfheader/swf-header.rb
Constant Summary collapse
- UNCOMPRESSED =
"uncompressed"
Instance Method Summary collapse
- #convert_pixels_to_twips(pixels) ⇒ Object
- #convert_twips_to_pixels(twips) ⇒ Object
-
#initialize(file) ⇒ SWFHeader
constructor
A new instance of SWFHeader.
- #inspect ⇒ Object
- #is_swf?(bytes) ⇒ Boolean
- #parse_header(file) ⇒ Object
- #read_packed_bits(bytes, byte_marker, bit_marker, length) ⇒ Object
- #to_s ⇒ Object
Methods inherited from SWFCompression
#is_compressed?, #read_full_size, #strip_header
Constructor Details
#initialize(file) ⇒ SWFHeader
Returns a new instance of SWFHeader.
4 5 6 |
# File 'lib/swfheader/swf-header.rb', line 4 def initialize(file) parse_header(file) end |
Instance Method Details
#convert_pixels_to_twips(pixels) ⇒ Object
82 83 84 |
# File 'lib/swfheader/swf-header.rb', line 82 def convert_pixels_to_twips( pixels ) return pixels * 20 end |
#convert_twips_to_pixels(twips) ⇒ Object
79 80 81 |
# File 'lib/swfheader/swf-header.rb', line 79 def convert_twips_to_pixels(twips) twips / 20 end |
#inspect ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/swfheader/swf-header.rb', line 88 def inspect "signature: "+@signature.to_s+"\n"+ "version: "+@version.to_s+"\n"+ "compression: "+@compression_type.to_s+"\n"+ "size: "+@size.to_s+"\n"+ "nbits: "+@nbits.to_s+"\n"+ "xmax: "+@xmax.to_s+"\n"+ "ymax: "+@ymax.to_s+"\n"+ "width: "+@width.to_s+"\n"+ "height: "+@height.to_s+"\n"+ "frame_rate: "+@frame_rate.to_s+"\n"+ "frame_count: "+@frame_count.to_s+"\n" end |
#is_swf?(bytes) ⇒ Boolean
52 53 54 |
# File 'lib/swfheader/swf-header.rb', line 52 def is_swf?(bytes) bytes[0,3]=="FWS" or bytes[0,3]=="CWS" end |
#parse_header(file) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/swfheader/swf-header.rb', line 10 def parse_header(file) @size=read_full_size(file) buffer=File.open(file,"rb") do |f| f.read end if !is_swf?(buffer) raise RuntimeError.new,"File does not appear to be a swf file",caller else @signature=buffer[0,3] end if is_compressed?(buffer[0]) buffer=SWFDecompressor.new.uncompress(buffer) @compression_type=COMPRESSED else @compression_type=UNCOMPRESSED end @version=buffer[3] @nbits = ((buffer[8]&0xff)>>3) pbo = read_packed_bits( buffer, 8, 5, @nbits ) pbo2 = read_packed_bits( buffer, pbo.nextByteIndex,pbo.nextBitIndex, @nbits ) pbo3 = read_packed_bits( buffer, pbo2.nextByteIndex,pbo2.nextBitIndex, @nbits ) pbo4 = read_packed_bits( buffer, pbo3.nextByteIndex,pbo3.nextBitIndex, @nbits ) @xmax = pbo2.value @ymax = pbo4.value @width = convert_twips_to_pixels( @xmax ) @height = convert_twips_to_pixels( @ymax ) byte_pointer = pbo4.nextByteIndex + 2 @frame_rate = buffer[byte_pointer] byte_pointer+=1 fc1 = buffer[byte_pointer] & 0xFF byte_pointer+=1 fc2 = buffer[byte_pointer] & 0xFF byte_pointer+=1 @frame_count=(fc2<<8)+fc1 buffer=nil end |
#read_packed_bits(bytes, byte_marker, bit_marker, length) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/swfheader/swf-header.rb', line 55 def read_packed_bits(bytes,byte_marker,bit_marker,length) total = 0 shift = 7 - bit_marker counter = 0 bit_index = bit_marker byte_index = byte_marker while counter<length (bit_marker...8).each do |i| bit =((bytes[byte_marker] & 0xff ) >> shift ) & 1 total = ( total << 1 ) + bit bit_index = i shift-=1 counter+=1 if counter==length break end end byte_index = byte_marker byte_marker+=1 bit_marker = 0 shift = 7 end return PackedBitObj.new(bit_index, byte_index, total ) end |
#to_s ⇒ Object
85 86 87 |
# File 'lib/swfheader/swf-header.rb', line 85 def to_s inspect end |