tily.rb 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 689971c694082323bbab56cf08dfc2e3ef225329
4
- data.tar.gz: 0c577325a87b38f3a19ef015c8d043371879df38
3
+ metadata.gz: e43703a29d2e8c65d37d9263ca47884a855b3936
4
+ data.tar.gz: dd16597b6d806cb4faa7ad822c65e41b5da12232
5
5
  SHA512:
6
- metadata.gz: 3c4565fcdc74901586f014f5ad2cd62ca647361a9673559723463b49d4f48616ec1b2decfab909f62ee05ddda546d24afff0dc3a5859a3ba4ccfec050a551d5d
7
- data.tar.gz: d6967f36285375f5384dcb3cca3a94459e4eb2bf025817eb1987e6602eb61e2eb3ae66f6781821cd1b91b770c4f970ba6827ef705064211d8e779fee71e5836a
6
+ metadata.gz: b1a702b645b1ab26ecda344ce8e7b12b4954fbb5e3a48d5f05aaf34051c7e4ce9fc56562b3d87b23a762401773603c234fde1b891b062b79cb37777f6bcd2e41
7
+ data.tar.gz: e2ae5fb85a1cac7f050d5e9368c5498f4b657a376b3088d7ba2f7b0d75df9a29c0696e2b5a73303756a78c2f0ff33a4d1bd8fccea96f865c07eb40e3363fef83
data/README.md CHANGED
@@ -1,4 +1,8 @@
1
1
  tily.rb
2
2
  =======
3
3
 
4
+ ## Status
5
+ [![Build Status](https://travis-ci.org/void-main/tily.rb.png?branch=master)](https://travis-ci.org/void-main/tily.rb)
6
+ [![Gem Version](https://badge.fury.io/rb/tily.rb.png)](http://badge.fury.io/rb/tily.rb)
7
+
4
8
  Map tile generator with ruby.
data/bin/tily ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'trollop'
4
+ require '../lib/tily'
5
+ require '../lib/tily/version'
6
+
7
+ opts = Trollop::options do
8
+ version "Tily #{Tily::VERSION} (c) 2013 Peng Sun"
9
+ banner <<-EOS
10
+ A simple map tile generator that divides huge images into classifed levels, with small tile images.
11
+
12
+ Usage:
13
+ tily [options]
14
+ where [options] are:
15
+ EOS
16
+
17
+ opt :img_path, "Path to the huge image", :long => "src", :short => "s", :type => :string
18
+ opt :output_path, "Root path to the output folder", :long => "dest", :short => "d", :type => :string
19
+ opt :unit_size, "Size of the unit tile", :long => "unitsize", :short => "u", :type => :int, :default => 256
20
+ opt :bg_color, "Color of the expanded part of the image", :long => "background", :short => "b", :type => :string, :default => "grey"
21
+ end
22
+
23
+ Trollop::die :img_path, "must not be null" unless opts[:img_path]
24
+ Trollop::die :img_path, "must exists" unless File.exist?(opts[:img_path])
25
+ Trollop::die :output_path, "must not be null" unless opts[:output_path]
26
+
27
+ tily = Tily::Tily.new opts
28
+ tily.gen_tiles
data/lib/tily.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'rmagick'
3
3
  require 'FileUtils'
4
- require './tily/utils/tile_system'
4
+ require 'json'
5
+ require_relative './tily/utils/tile_system'
5
6
 
6
7
  include Magick
7
8
 
@@ -12,12 +13,19 @@ module Tily
12
13
  attr_accessor :ts
13
14
  attr_accessor :background_color
14
15
 
15
- def initialize img_path, output_path, unit_size = 256, background_color = "grey"
16
+ def initialize opts
17
+ img_path = opts[:img_path]
18
+ output_path = opts[:output_path]
19
+ unit_size = opts[:unit_size] || 256
20
+ background_color = opts[:bg_color] || "grey"
21
+
16
22
  @raw_image = ImageList.new img_path
17
23
  @output_path = output_path
18
24
  @ts = TileSystem.new unit_size
19
25
  @ts.read_raw_dimension @raw_image.columns, @raw_image.rows
20
26
  @background_color = background_color
27
+
28
+ FileUtils.mkdir_p @output_path unless Dir.exists? @output_path
21
29
  end
22
30
 
23
31
  def gen_tiles
@@ -25,6 +33,10 @@ module Tily
25
33
  base_img = Image.new(norm_size, norm_size) { self.background_color = "grey" }
26
34
  base_img = base_img.composite(@raw_image, GravityType::CenterGravity, CompositeOperator::CopyCompositeOp)
27
35
 
36
+ puts "Generating meta-data..."
37
+ File.open("#{@output_path}/meta.json", "w") {|f| f.write(@ts.meta.to_json) }
38
+ puts "Done."
39
+
28
40
  puts "Generating images..."
29
41
 
30
42
  @ts.each_level do |level|
@@ -33,7 +45,7 @@ module Tily
33
45
  level_folder = "#{@output_path}/#{level}"
34
46
  FileUtils.mkdir_p level_folder unless Dir.exists? level_folder
35
47
 
36
- level_size = @ts.tile_size level
48
+ level_size = @ts.level_size level
37
49
  level_img = base_img.resize level_size, level_size
38
50
 
39
51
  @ts.each_tile_with_index(level) do |x, y, index|
@@ -56,9 +68,3 @@ module Tily
56
68
  end
57
69
  end
58
70
  end
59
-
60
-
61
- #img_path = "/Users/voidmain/Desktop/el-mundo.jpg"
62
- #target_path = "#{Dir.home()}/Desktop/el-mundo"
63
- #tily = Tily::Tily.new img_path, target_path
64
- #tily.gen_tiles
@@ -44,6 +44,11 @@ module Tily
44
44
  2 ** level
45
45
  end
46
46
 
47
+ # Returns the size of the level image
48
+ def level_size level
49
+ @unit_size * tile_size(level)
50
+ end
51
+
47
52
  # Returns the pixel offset in the image
48
53
  def tile_offset index
49
54
  @unit_size * index
@@ -85,12 +90,21 @@ module Tily
85
90
  (0...size).each do |y|
86
91
  (0...size).each do |x|
87
92
  yield(x, y, idx) if block_given?
88
- $stdout.flush
89
93
  idx += 1
90
94
  end
91
95
  end
92
96
  end
93
97
 
98
+ # Generates meta info for current TileSystem
99
+ def meta
100
+ {
101
+ total_level: max_level,
102
+ unit_size: @unit_size,
103
+ raw_width: @raw_width,
104
+ raw_height: @raw_height
105
+ }
106
+ end
107
+
94
108
  private
95
109
  # Calculate level size from raw size with the following fomula:
96
110
  # level = ceil(log2(raw size / unit_size))
data/lib/tily/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tily
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -23,6 +23,10 @@ describe Tily::TileSystem do
23
23
  @ts.tile_size(4).should == 2 ** 4
24
24
  end
25
25
 
26
+ it 'should calculate level size correctly' do
27
+ @ts.level_size(4).should == 4096
28
+ end
29
+
26
30
  it 'should calculate tile offset correctly' do
27
31
  @ts.tile_offset(2).should == 512
28
32
  end
data/tily-rb.gemspec CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.test_files = Dir['spec/**/*.rb']
18
18
 
19
19
  s.add_dependency('rmagick', '>= 2.13.2')
20
+ s.add_dependency('trollop', '>=2.0')
20
21
 
21
22
  s.add_development_dependency 'rake', '~> 0.9.0'
22
23
  s.add_development_dependency 'rspec', '~> 2.6.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tily.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peng Sun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-19 00:00:00.000000000 Z
11
+ date: 2013-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rmagick
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.13.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: trollop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +81,7 @@ files:
67
81
  - LICENSE
68
82
  - README.md
69
83
  - Rakefile
84
+ - bin/tily
70
85
  - lib/tily.rb
71
86
  - lib/tily/utils/tile_system.rb
72
87
  - lib/tily/version.rb