swf_ruby 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -9,7 +9,7 @@ SwfRuby is a utilities to dump or manipulate a SWF with Ruby.
9
9
  * Used from 'swf_dump' command.
10
10
  * SwfRuby::SwfTamperer
11
11
  * Manipulating(replagcing) resources in SWF.
12
- * Used from 'swf_jpeg_replace' command.
12
+ * Used from 'swf_jpeg_replace' and 'swf_lossless_replace' command.
13
13
  * compatible on ruby-1.8.7 and ruby-1.9.2.
14
14
 
15
15
  Dependencies
@@ -51,6 +51,12 @@ Replacing Jpeg in SWF
51
51
  $ swf_jpeg_replace samples/sample.swf 623 samples/bg.jpg > samples/sample2.swf
52
52
  # <623> is offset to DefineBitsJPEG2 resource getting by 'swf_dump'.
53
53
 
54
+ Replacing Jpeg in SWF
55
+ ---------------------
56
+
57
+ $ swf_lossless_replace samples/sample.swf 120 samples/icon.gif > samples/sample3.swf
58
+ # <120> is offset to DefineBitsLossless2 resource getting by 'swf_dump'.
59
+
54
60
  Thanks
55
61
  ======
56
62
 
data/bin/swf_dump CHANGED
@@ -4,11 +4,7 @@
4
4
 
5
5
  require 'swf_ruby'
6
6
 
7
- if ARGV.size != 1
8
- print "Specify swf file path to dump for argument.\n"
9
- exit
10
- end
11
-
7
+ print "Specify swf file path to dump for argument." if ARGV.size != 1
12
8
  swf = SwfRuby::SwfDumper.new
13
9
  swf.open(ARGV[0])
14
10
  swf.tags.each_with_index do |tag, i|
data/bin/swf_jpeg_replace CHANGED
@@ -3,10 +3,7 @@
3
3
  # SWF中のJpeg(DefineBitsJpeg2)イメージを差し替える.
4
4
  require 'swf_ruby'
5
5
 
6
- if ARGV.size != 3
7
- print "Specify target swf path, offset to jpeg, new jpeg path for arguments.\n"
8
- exit
9
- end
6
+ print "Specify target swf path, offset to jpeg, new jpeg path for arguments." if ARGV.size != 3
10
7
 
11
8
  swf = File.open(ARGV[0], "rb").read
12
9
  st = SwfRuby::SwfTamperer.new
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: set fileencoding=utf-8 filetype=ruby ts=2 :
3
+ # SWF中のLossless(DefineBitsLossless2)イメージを差し替える.
4
+ require 'swf_ruby'
5
+
6
+ print "Specify target swf path, offset to image, new image(png,gif) path for arguments." if ARGV.size != 3
7
+
8
+ swf = File.open(ARGV[0], "rb").read
9
+ st = SwfRuby::SwfTamperer.new
10
+ image = File.open(ARGV[2], "rb").read
11
+ repl_targets = [
12
+ SwfRuby::Lossless2ReplaceTarget.new(ARGV[1].to_i, image)
13
+ ]
14
+
15
+ print st.replace(swf, repl_targets)
@@ -0,0 +1,75 @@
1
+ # vim: set fileencoding=utf-8 filetype=ruby ts=2 :
2
+
3
+ module SwfRuby
4
+ module Swf
5
+ class BitsLossless2
6
+ attr_accessor :format, :width, :height, :color_table_size, :zlib_bitmap_data
7
+
8
+ ShiftDepth = Magick::QuantumDepth - 8
9
+ MaxRGB = 2 ** Magick::QuantumDepth - 1
10
+
11
+ def initialize(image_bytearray)
12
+ image = Magick::Image.from_blob(image_bytearray).first
13
+ data = ""
14
+ data.force_encoding("ASCII-8BIT") if data.respond_to? :force_encoding
15
+ @format = nil
16
+ colormap = []
17
+ # creating colormap to check number of colors
18
+ image.get_pixels(0, 0, image.columns, image.rows).each_with_index do |pixel,i|
19
+ break if colormap.length > 255
20
+ idx = colormap.index(pixel)
21
+ if idx then
22
+ data << [idx].pack("C")
23
+ else
24
+ colormap << pixel
25
+ data << [colormap.length-1].pack("C")
26
+ end
27
+ if (i+1) % image.rows == 0 then
28
+ # padding
29
+ data += [0].pack("C") * (4-image.columns&3)
30
+ end
31
+ end
32
+
33
+ # checking image format by size of colormap
34
+ if colormap.length > 255 then
35
+ # format=5
36
+ # reset and re-build data_stream without colopmap
37
+ data = ""
38
+ image.get_pixels(0, 0, image.columns, image.rows).each_with_index do |pixel,i|
39
+ opacity = (MaxRGB-pixel.opacity) >> ShiftDepth
40
+ data += [opacity].pack("C")
41
+ data += [pixel.red >> ShiftDepth].pack("C")
42
+ data += [pixel.green >> ShiftDepth].pack("C")
43
+ data += [pixel.blue >> ShiftDepth].pack("C")
44
+ end
45
+ @format = 5
46
+ else
47
+ # format=3
48
+ # added colormap before data_stream
49
+ data = colormap.inject("") { |r,c|
50
+ opacity = (MaxRGB-c.opacity) >> ShiftDepth
51
+ if opacity == 0 then
52
+ r +=
53
+ [0].pack("C") +
54
+ [0].pack("C") +
55
+ [0].pack("C") +
56
+ [opacity].pack("C")
57
+ else
58
+ r +=
59
+ [c.red >> ShiftDepth].pack("C") +
60
+ [c.green >> ShiftDepth].pack("C") +
61
+ [c.blue >> ShiftDepth].pack("C") +
62
+ [opacity].pack("C")
63
+ end
64
+ } + data
65
+ @format = 3
66
+ @color_table_size = colormap.length-1
67
+ end
68
+
69
+ @width = image.columns
70
+ @height = image.rows
71
+ @zlib_bitmap_data = Zlib::Deflate.deflate(data)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -9,6 +9,8 @@ module SwfRuby
9
9
  case rt
10
10
  when Jpeg2ReplaceTarget
11
11
  swf = self.repl_jpeg2(swf, rt.offset, rt.jpeg)
12
+ when Lossless2ReplaceTarget
13
+ swf = self.repl_lossless2(swf, rt.offset, rt.image)
12
14
  when AsVarReplaceTarget
13
15
  swf = self.repl_action_push_string(swf, rt.do_action_offset, rt.offset, rt.str, rt.parent_sprite_offsets)
14
16
  end
@@ -85,6 +87,33 @@ module SwfRuby
85
87
  end
86
88
  swf
87
89
  end
90
+
91
+ # DefineBitsLossless2のイメージバイナリを置換.
92
+ def repl_lossless2(swf, offset, lossless)
93
+ swf.force_encoding("ASCII-8BIT") if swf.respond_to? :force_encoding
94
+
95
+ # replace lossless2 data
96
+ if lossless.format == 3
97
+ org_image_length = swf[offset+2, 4].unpack("i").first - 8
98
+ swf[offset+14, org_image_length] = lossless.zlib_bitmap_data
99
+ swf[offset+13, 1] = [lossless.color_table_size].pack("C")
100
+ swf[offset+11, 2] = [lossless.width].pack("v")
101
+ swf[offset+9, 2] = [lossless.height].pack("v")
102
+ swf[offset+8, 1] = [lossless.format].pack("C")
103
+ swf[offset+2, 4] = [lossless.zlib_bitmap_data.size + 8].pack("i")
104
+ elsif format == 5
105
+ org_image_length = swf[offset+2, 4].unpack("i").first - 7
106
+ swf[offset+13, org_image_length] = lossless.zlib_bitmap_data
107
+ swf[offset+11, 2] = [lossless.width].pack("v")
108
+ swf[offset+9, 2] = [lossless.height].pack("v")
109
+ swf[offset+8, 1] = [lossless.format].pack("C")
110
+ swf[offset+2, 4] = [lossless.zlib_bitmap_data.size + 7].pack("i")
111
+ else
112
+ raise ReplaceTargetError
113
+ end
114
+
115
+ swf
116
+ end
88
117
  end
89
118
 
90
119
  # 置換対象指定エラー.
@@ -1,3 +1,3 @@
1
1
  module SwfRuby
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/swf_ruby.rb CHANGED
@@ -1 +1,3 @@
1
+ require 'zlib'
2
+ require 'RMagick'
1
3
  Dir[File.join(File.dirname(__FILE__), 'swf_ruby', '*.rb')].sort.each { |f| require f }
data/swf_ruby.gemspec CHANGED
@@ -16,9 +16,10 @@ Gem::Specification.new do |s|
16
16
  s.rubyforge_project = "swf_ruby"
17
17
 
18
18
  s.add_dependency "bundler", ">= 1.0.0.rc.5"
19
+ s.add_dependency "rmagick", ">= 2.13.0"
19
20
 
20
21
  s.bindir = 'bin'
21
- s.executables = ['swf_dump', 'swf_jpeg_replace']
22
+ s.executables = ['swf_dump', 'swf_jpeg_replace', 'swf_lossless_replace']
22
23
 
23
24
  s.files = `git ls-files`.split("\n")
24
25
  s.require_path = 'lib'
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 2
9
- version: 0.0.2
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors: []
12
12
 
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-01 00:00:00 +09:00
17
+ date: 2011-01-27 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -34,12 +34,28 @@ dependencies:
34
34
  version: 1.0.0.rc.5
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rmagick
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ segments:
46
+ - 2
47
+ - 13
48
+ - 0
49
+ version: 2.13.0
50
+ type: :runtime
51
+ version_requirements: *id002
37
52
  description: SwfRuby is utilities to dump or manipulate Swf binary file.
38
53
  email: []
39
54
 
40
55
  executables:
41
56
  - swf_dump
42
57
  - swf_jpeg_replace
58
+ - swf_lossless_replace
43
59
  extensions: []
44
60
 
45
61
  extra_rdoc_files: []
@@ -54,6 +70,7 @@ files:
54
70
  - Rakefile
55
71
  - bin/swf_dump
56
72
  - bin/swf_jpeg_replace
73
+ - bin/swf_lossless_replace
57
74
  - lib/swf_ruby.rb
58
75
  - lib/swf_ruby/do_action_dumper.rb
59
76
  - lib/swf_ruby/replace_target.rb
@@ -61,6 +78,7 @@ files:
61
78
  - lib/swf_ruby/swf.rb
62
79
  - lib/swf_ruby/swf/action_push.rb
63
80
  - lib/swf_ruby/swf/action_record.rb
81
+ - lib/swf_ruby/swf/bits_lossless2.rb
64
82
  - lib/swf_ruby/swf/header.rb
65
83
  - lib/swf_ruby/swf/rectangle.rb
66
84
  - lib/swf_ruby/swf/tag.rb