travertine 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,41 @@
1
+ h1. travertine
2
+
3
+ Travertine (named for the tile) is a library for cutting images into square tiles used for map display. The map tiling algorithm is based on the "YM4R":http://rubyforge.org/projects/ym4r/ project.
4
+
5
+ h2. Example
6
+
7
+ <pre>
8
+ image = Magick::ImageList.new("mymap.jpg")
9
+ zoom_levels = 3
10
+
11
+ tile_sets = Travertine.cut_tiles(image, zoom_levels)
12
+
13
+ tile_sets.each_with_index do |tile_set, zoom|
14
+ coordinates = Travertine.tile_coordinates_for_zoom(zoom)
15
+ tile_set.each_with_index do |tile, i|
16
+ # Writes each tile out to a file named like 'tile_zoom_x_y.jpg'
17
+ tile.write("tile_#{zoom}_#{coordinates[i][0]}_#{coordinates[i][1]}.jpg")
18
+ end
19
+ end
20
+ </pre>
21
+
22
+
23
+ h2. Todo
24
+
25
+ * Crop and/or autofill source image to make it square prior to tiling
26
+ * Allow for stitching together multiple source images
27
+
28
+ h2. Contributing to travertine
29
+
30
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
31
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
32
+ * Fork the project
33
+ * Start a feature/bugfix branch
34
+ * Commit and push until you are happy with your contribution
35
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
36
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
37
+
38
+ h4. Copyright
39
+
40
+ Copyright (c) 2011 Micah Wedemeyer. See LICENSE.txt for
41
+ further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/lib/travertine.rb CHANGED
@@ -1,8 +1,18 @@
1
1
  class Travertine
2
-
3
2
  # This is the correct size for Google maps tiles
4
3
  DEFAULT_TILE_SIZE = 256
5
4
 
5
+ # Cuts tiles from a source image at various zoom levels.
6
+ #
7
+ # Parameters:
8
+ #
9
+ # <tt>img</tt>: A +Magick::Image+ object for the source image. For proper tiling it should be exactly square.
10
+ # <tt>max_zoom</tt>: The maximum level of zoom. max_zoom of 0 has 1 tile. Each successive level has 4 times as many tiles as the previous.
11
+ # <tt>tile_size</tt>: The size of each tile in pixels.
12
+ #
13
+ # Returns:
14
+ #
15
+ # An +Array+ of +Arrays+, where each inner array contains the tiles for a given zoom level. The first +Array+ will have 1 tile, for zoom level 0. The second will have 4 for zoom level 1, and so on.
6
16
  def self.cut_tiles(img, max_zoom, tile_size = DEFAULT_TILE_SIZE)
7
17
  tile_sets = []
8
18
  (0..max_zoom).collect.reverse.each do |zoom|
@@ -18,6 +28,18 @@ class Travertine
18
28
  tile_sets.reverse
19
29
  end
20
30
 
31
+ # Cuts a tile from the source image.
32
+ #
33
+ # Parameters:
34
+ #
35
+ # <tt>img</tt>: A +Magick::Image+ object for the source image.
36
+ # <tt>x</tt>: The x coordinate of the tile to cut. Coordinates are specified in terms of the tile grid, not pixels.
37
+ # <tt>y</tt>: The y coordinate of the tile to cut. Coordinates are specified in terms of the tile grid, not pixels.
38
+ # <tt>tile_size</tt>: The size of each tile in pixels.
39
+ #
40
+ # Returns:
41
+ #
42
+ # A +Magick::Image+ tile.
21
43
  def self.cut_tile(img, x, y, tile_size = DEFAULT_TILE_SIZE)
22
44
  tile = Magick::Image.new(tile_size, tile_size)
23
45
 
@@ -27,6 +49,15 @@ class Travertine
27
49
  tile.import_pixels(0, 0, tile_size, tile_size, "RGBA",img.export_pixels(img_x, img_y, tile_size, tile_size, "RGBA"))
28
50
  end
29
51
 
52
+ # Calculates the tile grid for a given zoom level.
53
+ #
54
+ # Parameters:
55
+ #
56
+ # <tt>zoom_level</tt>: The level of zoom to compute the tile grid for.
57
+ #
58
+ # Returns:
59
+ #
60
+ # An +Array+ of 2-element +Arrays+ representing the tile x,y coordinates. For example, the tile grid at zoom 1 is [[0,0,], [0,1], [1,0], [1,1]]
30
61
  def self.tile_coordinates_for_zoom(zoom_level)
31
62
  [].tap do |coords|
32
63
  (0..((2 ** zoom_level) - 1)).each do |x|
@@ -37,11 +68,50 @@ class Travertine
37
68
  end
38
69
  end
39
70
 
71
+ # Calculates the total tile count for a given zoom level.
72
+ #
73
+ # Parameters:
74
+ #
75
+ # <tt>zoom_level</tt>: The level of zoom to compute the tile grid for.
76
+ #
77
+ # Returns:
78
+ #
79
+ # An +Integer+ count of the number of tiles for a given zoom level.
80
+ def self.tile_count_for_zoom(zoom_level)
81
+ 4 ** zoom_level
82
+ end
83
+
84
+ # Resizes the image to the correct size for cutting tiles at the given zoom level. It does an in-place resize which modifies the passed-in image.
85
+ #
86
+ # Note: If the image is not square, tiles will probably be incorrect.
87
+ #
88
+ # Parameters:
89
+ #
90
+ # <tt>img</tt>: The source image as +Magick::Image+.
91
+ # <tt>zoom_level</tt>: The level of zoom to compute the tile grid for.
92
+ # <tt>tile_size</tt>: The size of each tile in pixels.
93
+ #
94
+ # Returns:
95
+ #
96
+ # <tt>nil</tt> - The resize is done in-place, modifying the passed in image.
40
97
  def self.resize_to_zoom_level(img, zoom_level, tile_size = DEFAULT_TILE_SIZE)
41
98
  size = zoom_image_size(zoom_level, tile_size)
42
99
  img.resize!(size, size)
43
100
  end
44
101
 
102
+ # Calculates the correct size in pixels for a source image at the given zoom level. This assumes the image
103
+ # to resize will be square.
104
+ #
105
+ # For example, at zoom_level 2 and tile_size 256, the image size is 2048px on a side.
106
+ #
107
+ # Parameters:
108
+ #
109
+ # <tt>zoom_level</tt>: The level of zoom to compute the image size for.
110
+ # <tt>tile_size</tt>: The size of each tile in pixels.
111
+ #
112
+ # Returns:
113
+ #
114
+ # The size in pixels of each side of the square image for a source image at the given zoom.
45
115
  def self.zoom_image_size(zoom_level, tile_size = DEFAULT_TILE_SIZE)
46
116
  tile_size * (2 ** zoom_level )
47
117
  end
@@ -40,6 +40,14 @@ describe Travertine do
40
40
  end
41
41
  end
42
42
 
43
+ describe "#tile_count_for_zoom" do
44
+ it "should calculate the number of tiles for a given zoom level" do
45
+ Travertine.tile_count_for_zoom(0).should == 1
46
+ Travertine.tile_count_for_zoom(2).should == 16
47
+ Travertine.tile_count_for_zoom(4).should == 256
48
+ end
49
+ end
50
+
43
51
  describe "#zoom_image_size" do
44
52
  it "should calculate the size in pixels that the source image must be resized to" do
45
53
  Travertine.zoom_image_size(0).should == Travertine::DEFAULT_TILE_SIZE
data/travertine.gemspec CHANGED
@@ -5,23 +5,23 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{travertine}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Micah Wedemeyer"]
12
- s.date = %q{2011-09-25}
12
+ s.date = %q{2011-10-02}
13
13
  s.description = %q{Image tiling with Ruby}
14
14
  s.email = %q{me@micahwedemeyer.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
- "README.rdoc"
17
+ "README.textile"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
21
  "Gemfile",
22
22
  "Gemfile.lock",
23
23
  "LICENSE.txt",
24
- "README.rdoc",
24
+ "README.textile",
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "lib/travertine.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: travertine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Micah Wedemeyer
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-25 00:00:00 -04:00
18
+ date: 2011-10-02 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -88,13 +88,13 @@ extensions: []
88
88
 
89
89
  extra_rdoc_files:
90
90
  - LICENSE.txt
91
- - README.rdoc
91
+ - README.textile
92
92
  files:
93
93
  - .document
94
94
  - Gemfile
95
95
  - Gemfile.lock
96
96
  - LICENSE.txt
97
- - README.rdoc
97
+ - README.textile
98
98
  - Rakefile
99
99
  - VERSION
100
100
  - lib/travertine.rb
data/README.rdoc DELETED
@@ -1,19 +0,0 @@
1
- = travertine
2
-
3
- Description goes here.
4
-
5
- == Contributing to travertine
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
- * Fork the project
10
- * Start a feature/bugfix branch
11
- * Commit and push until you are happy with your contribution
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2011 Micah Wedemeyer. See LICENSE.txt for
18
- further details.
19
-