swiff 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 2Performant
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = Swiff
2
+
3
+ Description goes here.
4
+
5
+ Heavily based on swf-util[http://code.google.com/p/swf-util/] by Dennis Zhuang
6
+
7
+ == Note on Patches/Pull Requests
8
+
9
+ * Fork the project.
10
+ * Make your feature addition or bug fix.
11
+ * Add tests for it. This is important so I don't break it in a
12
+ future version unintentionally.
13
+ * Commit, do not mess with rakefile, version, or history.
14
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
15
+ * Send me a pull request. Bonus points for topic branches.
16
+
17
+ == Copyright
18
+
19
+ Copyright (c) 2010 2Perfomant. See LICENSE for details.
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "swiff"
8
+ gem.summary = %Q{SWF Information Library}
9
+ gem.description = %Q{SWF Information Library}
10
+ gem.email = "zmaxor@gmail.com"
11
+ gem.homepage = "http://github.com/2performant/swiff"
12
+ gem.authors = ["Andrei Bocan"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "swfutil #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
Binary file
Binary file
@@ -0,0 +1,26 @@
1
+ require 'zlib'
2
+ require 'swiff/header_info'
3
+ require 'swiff/compression'
4
+ require 'swiff/packed_bits'
5
+
6
+ class Swiff
7
+ include HeaderInfo
8
+ include Compression
9
+
10
+ def initialize(path)
11
+ @path = path
12
+ parse_header
13
+ end
14
+
15
+ def bytes
16
+ @file_contents ||= File.read(@path)
17
+ end
18
+
19
+ def decompressed_bytes
20
+ if is_compressed?
21
+ @decompressed_file_contents ||= self.decompress
22
+ else
23
+ @decompressed_file_contents = @file_contents
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ class Swiff
2
+
3
+ module Compression
4
+
5
+ def read_file(file,to)
6
+ buff = File.open(file,"rb") do |fin|
7
+ fin.read
8
+ end
9
+ raise RuntimeError.new,"The file have already been compressed",caller if is_compressed?(buff[0])
10
+ result = compress(buff)
11
+ File.open(to,"wb") do |fout|
12
+ fout.write(result)
13
+ end
14
+ end
15
+
16
+ def compress
17
+ compressor = Zlib::Deflate.new
18
+ data = compressor.deflate(strip_header(decompressed_bytes), Zlib::FINISH)
19
+ data = bytes[0,8] + data
20
+ data[0] = ?C
21
+ data
22
+ end
23
+
24
+ def decompress
25
+ decompressor = Zlib::Inflate.new
26
+ swf = decompressor.inflate(strip_header)
27
+ swf = bytes[0,8] + swf
28
+ swf[0] = ?F
29
+ swf
30
+ end
31
+
32
+ def read_full_size
33
+ buff = File.open(@path,"rb") do |f|
34
+ f.seek(4,IO::SEEK_CUR)
35
+ f.read 4
36
+ end
37
+ buff.unpack("L")[0]
38
+ end
39
+
40
+ def strip_header(buffer = nil)
41
+ buffer ||= bytes
42
+ buffer[8,bytes.size-8]
43
+ end
44
+
45
+ def is_compressed?
46
+ bytes[0] == ?C
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,97 @@
1
+ class Swiff
2
+
3
+ module HeaderInfo
4
+
5
+ attr_reader :signature,:compression_type,:version,:size,:nbits,:x_max,:y_max,:width,:height,:frame_rate, :frame_count
6
+ COMPRESSED = "compressed"
7
+ UNCOMPRESSED = "uncompressed"
8
+
9
+ # TODO: Clean this up
10
+ def parse_header
11
+ @nbits = ((decompressed_bytes[8]&0xff)>>3)
12
+ pbo = read_packed_bits(decompressed_bytes, 8, 5, @nbits )
13
+ pbo2 = read_packed_bits(decompressed_bytes, pbo.nextByteIndex,pbo.nextBitIndex, @nbits)
14
+ pbo3 = read_packed_bits(decompressed_bytes, pbo2.nextByteIndex,pbo2.nextBitIndex, @nbits)
15
+ pbo4 = read_packed_bits(decompressed_bytes, pbo3.nextByteIndex,pbo3.nextBitIndex, @nbits)
16
+
17
+ @x_max = pbo2.value
18
+ @y_max = pbo4.value
19
+
20
+ @width = convert_twips_to_pixels( @x_max )
21
+ @height = convert_twips_to_pixels( @y_max )
22
+
23
+ byte_pointer = pbo4.nextByteIndex + 2
24
+
25
+ @frame_rate = decompressed_bytes[byte_pointer]
26
+ byte_pointer += 1
27
+ fc1 = decompressed_bytes[byte_pointer] & 0xFF
28
+ byte_pointer += 1
29
+
30
+ fc2 = decompressed_bytes[byte_pointer] & 0xFF
31
+ byte_pointer += 1
32
+ @frame_count = (fc2<<8) + fc1
33
+ end
34
+
35
+ def is_swf?
36
+ bytes[0,3] =="FWS" || bytes[0,3] == "CWS"
37
+ end
38
+
39
+ # TODO: Clean this up
40
+ def read_packed_bits(bytes,byte_marker,bit_marker,length)
41
+ total = 0
42
+ shift = 7 - bit_marker
43
+ counter = 0
44
+ bit_index = bit_marker
45
+ byte_index = byte_marker
46
+ while counter<length
47
+ (bit_marker...8).each do |i|
48
+ bit =((bytes[byte_marker] & 0xff ) >> shift ) & 1
49
+ total = ( total << 1 ) + bit
50
+ bit_index = i
51
+ shift -= 1
52
+ counter += 1
53
+ if counter == length
54
+ break
55
+ end
56
+ end
57
+ byte_index = byte_marker
58
+ byte_marker += 1
59
+ bit_marker = 0
60
+ shift = 7
61
+ end
62
+ return PackedBits.new(bit_index, byte_index, total )
63
+ end
64
+
65
+ def convert_twips_to_pixels(twips)
66
+ twips / 20
67
+ end
68
+
69
+ def version
70
+ bytes[3]
71
+ end
72
+
73
+ def convert_pixels_to_twips( pixels )
74
+ return pixels * 20
75
+ end
76
+
77
+ def to_s
78
+ inspect
79
+ end
80
+
81
+ def inspect
82
+ "signature: "+@signature.to_s+"\n"+
83
+ "version: "+@version.to_s+"\n"+
84
+ "compression: "+@compression_type.to_s+"\n"+
85
+ "size: "+@size.to_s+"\n"+
86
+ "nbits: "+@nbits.to_s+"\n"+
87
+ "xmax: "+@xmax.to_s+"\n"+
88
+ "ymax: "+@ymax.to_s+"\n"+
89
+ "width: "+@width.to_s+"\n"+
90
+ "height: "+@height.to_s+"\n"+
91
+ "frame_rate: "+@frame_rate.to_s+"\n"+
92
+ "frame_count: "+@frame_count.to_s+"\n"
93
+ end
94
+
95
+ end
96
+
97
+ end
@@ -0,0 +1,23 @@
1
+ class Swiff
2
+ class PackedBits
3
+
4
+ attr_accessor :bitIndex,:byteIndex,:value,:nextBitIndex,:nextByteIndex,:nextByteBoundary
5
+
6
+ def initialize(bitMarker,byteMarker,decimalValue)
7
+ @bitIndex = bitMarker;
8
+ @byteIndex = byteMarker;
9
+ @value = decimalValue;
10
+ @nextBitIndex = bitMarker;
11
+ if ( bitMarker <= 7 )
12
+ @nextBitIndex+=1
13
+ @nextByteIndex =byteMarker
14
+ @nextByteBoundary = (byteMarker+=1)
15
+ else
16
+ @nextBitIndex = 0
17
+ @nextByteIndex+=1
18
+ @nextByteBoundary = @nextByteIndex;
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'swiff'
8
+
9
+ def fixture_path(filename)
10
+ File.join(File.dirname(__FILE__), '..', 'fixtures', filename)
11
+ end
12
+
13
+ class Test::Unit::TestCase
14
+ end
@@ -0,0 +1,69 @@
1
+ require 'helper'
2
+
3
+ class TestSwfutil < Test::Unit::TestCase
4
+ should "take the filename as a mandatory parameter" do
5
+ assert_nothing_raised do
6
+ swiff = Swiff.new fixture_path('clicktag.swf')
7
+ end
8
+ end
9
+
10
+ context "a file" do
11
+ setup do
12
+ @swiff = Swiff.new fixture_path('clicktag.swf')
13
+ end
14
+
15
+ should "be able to determine whether it is compressed" do
16
+ assert_equal true, @swiff.is_compressed?
17
+ end
18
+
19
+ should "be able to determine whether it is not compressed" do
20
+ @swiff = Swiff.new fixture_path('clicktag-decompressed.swf')
21
+ assert_equal false, @swiff.is_compressed?
22
+ end
23
+
24
+ should "be able to determine file is a swf" do
25
+ assert_equal true, @swiff.is_swf?
26
+
27
+ @not_swiff = Swiff.new fixture_path('smallgoat.jpg')
28
+
29
+ assert_equal false, @not_swiff.is_swf?
30
+ end
31
+
32
+ should "determine the flash version of the file" do
33
+ assert_equal 8, @swiff.version
34
+ end
35
+
36
+ should "determine width and height" do
37
+ assert_equal 468, @swiff.width
38
+ assert_equal 60, @swiff.height
39
+ end
40
+
41
+ should "determine the frame rate" do
42
+ assert_equal 24, @swiff.frame_rate
43
+ end
44
+
45
+ should "determine maximum x value" do
46
+ assert_equal 9360, @swiff.x_max
47
+ end
48
+
49
+ should "determine maximum y value" do
50
+ assert_equal 1200, @swiff.y_max
51
+ end
52
+
53
+ should "determine frame count" do
54
+ assert_equal 1, @swiff.frame_count
55
+ end
56
+
57
+ should "be able to decompress a file" do
58
+ decompressed_file = File.read(fixture_path('clicktag-decompressed.swf'))
59
+ assert_equal decompressed_file, @swiff.decompress
60
+ end
61
+
62
+ should "be able to compress a file" do
63
+ @swiff = Swiff.new fixture_path('clicktag-decompressed.swf')
64
+ compressed_file = File.read(fixture_path('clicktag.swf'))
65
+
66
+ assert_equal compressed_file, @swiff.compress
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swiff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Bocan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-17 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: SWF Information Library
26
+ email: zmaxor@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - fixtures/clicktag-decompressed.swf
42
+ - fixtures/clicktag.swf
43
+ - fixtures/smallgoat.jpg
44
+ - lib/swiff.rb
45
+ - lib/swiff/compression.rb
46
+ - lib/swiff/header_info.rb
47
+ - lib/swiff/packed_bits.rb
48
+ - test/helper.rb
49
+ - test/test_swiff.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/2performant/swiff
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.5
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: SWF Information Library
78
+ test_files:
79
+ - test/helper.rb
80
+ - test/test_swiff.rb