ruby-dzi 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/Manifest ADDED
@@ -0,0 +1,8 @@
1
+ LICENSE-GPL.txt
2
+ LICENSE-LGPL.txt
3
+ LICENSE-MPL.txt
4
+ README.rdoc
5
+ Rakefile
6
+ init.rb
7
+ lib/ruby_dzi.rb
8
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,31 @@
1
+ = Ruby Dzi Generator
2
+ A forked project of Deep Zoom Slicer
3
+
4
+ Ruby Dzi slices images into several tiles and creates dzi descriptor file.
5
+ Sliced images are compatible for use with OpenZoom, Deep Zoom and Seadragon.
6
+
7
+ == Additional Information
8
+
9
+ Fork Contributor: Marc Vitalis <marc.vitalis@live.com>
10
+
11
+ Fork from::
12
+
13
+ Deep Zoom Slicer
14
+ Copyright (c) 2009, MESO Web Scapes, Sascha Hanssen (hanssen@meso.net)
15
+ All rights reserved.
16
+
17
+ * Author: MESO Web Scapes (www.meso.net)
18
+ * License: MPL 1.1/GPL 3/LGPL 3
19
+ * Meso Homepage: http://www.meso.net/deep_zoom_slicer
20
+ * GitHub: http://github.com/hanssen/deep_zoom_slicer
21
+
22
+ Contributor(s):
23
+ * Sascha Hanssen <hanssen@meso.net>
24
+
25
+ Software distributed under the License is distributed on an "AS IS" basis,
26
+ WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
27
+ for the specific language governing rights and limitations under the
28
+ License.
29
+
30
+ Deep Zoom and Seadragon are trademarks or registered trademarks of Microsoft, Inc.
31
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('ruby-dzi', '0.1.0') do |p|
6
+ p.description = "Ruby Dzi slices images into several tiles and creates dzi descriptor file."
7
+ p.url = "http://github.com/netxph/ruby-dzi"
8
+ p.author = "Marc Vitalis"
9
+ p.email = "marc.vitalis@live.com"
10
+ p.ignore_pattern = ["*.jpg", "*_files/**/**", "test.rb", "*.dzi", "pkg"]
11
+ p.development_dependencies = ["rmagick"]
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
15
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'ruby_dzi'
data/lib/ruby_dzi.rb ADDED
@@ -0,0 +1,175 @@
1
+ # Ruby Dzi generator
2
+ # A fork from Deep Zoom Slicer project
3
+ #
4
+ # Ruby Dzi slices images and generates a dzi file that is compatible with DeepZoom or Seadragon
5
+ #
6
+ # Requirements: rmagick gem (tested with version 2.9.0)
7
+ #
8
+ # Contributor: Marc Vitalis <marc.vitalis@live.com>
9
+ #
10
+ # Original Author:: MESO Web Scapes (www.meso.net)
11
+ # By:: Sascha Hanssen <hanssen@meso.net>
12
+ # License:: MPL 1.1/GPL 3/LGPL 3
13
+ #
14
+ # Software distributed under the License is distributed on an "AS IS" basis,
15
+ # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16
+ # for the specific language governing rights and limitations under the
17
+ # License.
18
+ #
19
+ require 'rubygems'
20
+ require 'fileutils'
21
+ require 'RMagick'
22
+ require 'open-uri'
23
+
24
+ class RubyDzi
25
+ include Magick
26
+
27
+ attr_accessor :image_path, :name, :format, :output_ext, :quality, :dir, :tile_size, :overlap
28
+
29
+ def initialize(image_path)
30
+
31
+ #set defaults
32
+ @quality = 75
33
+ @dir = '.'
34
+ @tile_size = 254
35
+ @overlap = 1
36
+ @output_ext = 'dzi'
37
+
38
+ @image_path = image_path
39
+
40
+ end
41
+
42
+ def generate!(name, format = 'jpg')
43
+ @name = name
44
+ @format = format
45
+
46
+ @levels_root_dir = File.join(@dir, @name + '_files')
47
+ @xml_descriptor_path = File.join(@dir, @name + '.dzi')
48
+
49
+ image = get_image(@image_path)
50
+
51
+ image.strip! # remove meta information
52
+
53
+ orig_width, orig_height = image.columns, image.rows
54
+
55
+ remove_files!
56
+
57
+ # iterate over all levels (= zoom stages)
58
+ max_level(orig_width, orig_height).downto(0) do |level|
59
+ width, height = image.columns, image.rows
60
+ puts "level #{level} is #{width} x #{height}"
61
+
62
+ current_level_dir = File.join(@levels_root_dir, level.to_s)
63
+ FileUtils.mkdir_p(current_level_dir)
64
+
65
+ # iterate over columns
66
+ x, col_count = 0, 0
67
+ while x < width
68
+ # iterate over rows
69
+ y, row_count = 0, 0
70
+ while y < height
71
+ dest_path = File.join(current_level_dir, "#{col_count}_#{row_count}.#{@extension}")
72
+ tile_width, tile_height = tile_dimensions(x, y, @tile_size, @overlap)
73
+
74
+ save_cropped_image(image, dest_path, x, y, tile_width, tile_height, @quality)
75
+
76
+ y += (tile_height - (2 * @overlap))
77
+ row_count += 1
78
+ end
79
+ x += (tile_width - (2 * @overlap))
80
+ col_count += 1
81
+ end
82
+
83
+ image.resize!(0.5)
84
+ end
85
+
86
+ # generate xml descriptor and write file
87
+ write_xml_descriptor(@xml_descriptor_path,
88
+ :tile_size => @tile_size,
89
+ :overlap => @overlap,
90
+ :format => @extension,
91
+ :width => orig_width,
92
+ :height => orig_height)
93
+ end
94
+
95
+ def remove_files!
96
+ files_existed = (File.file?(@xml_descriptor_path) or File.directory?(@levels_root_dir))
97
+
98
+ File.delete @xml_descriptor_path if File.file? @xml_descriptor_path
99
+ FileUtils.remove_dir @levels_root_dir if File.directory? @levels_root_dir
100
+
101
+ return files_existed
102
+ end
103
+
104
+ protected
105
+
106
+ def tile_dimensions(x, y, tile_size, overlap)
107
+ overlapping_tile_size = tile_size + (2 * overlap)
108
+ border_tile_size = tile_size + overlap
109
+
110
+ tile_width = (x > 0) ? overlapping_tile_size : border_tile_size
111
+ tile_height = (y > 0) ? overlapping_tile_size : border_tile_size
112
+
113
+ return tile_width, tile_height
114
+ end
115
+
116
+ def max_level(width, height)
117
+ return (Math.log([width, height].max) / Math.log(2)).ceil
118
+ end
119
+
120
+ def save_cropped_image(src, dest, x, y, width, height, quality = 75)
121
+ if src.is_a? Magick::Image
122
+ img = src
123
+ else
124
+ img = Magick::Image::read(src).first
125
+ end
126
+
127
+ quality = quality * 100 if quality < 1
128
+
129
+ # The crop method retains the offset information in the cropped image.
130
+ # To reset the offset data, adding true as the last argument to crop.
131
+ cropped = img.crop(x, y, width, height, true)
132
+ cropped.write(dest) { self.quality = quality }
133
+ end
134
+
135
+
136
+ def write_xml_descriptor(path, attr)
137
+ attr = { :xmlns => 'http://schemas.microsoft.com/deepzoom/2008' }.merge attr
138
+
139
+ xml = "<?xml version='1.0' encoding='UTF-8'?>" +
140
+ "<Image TileSize='#{attr[:tile_size]}' Overlap='#{attr[:overlap]}' " +
141
+ "Format='#{attr[:format]}' xmlns='#{attr[:xmlns]}'>" +
142
+ "<Size Width='#{attr[:width]}' Height='#{attr[:height]}'/>" +
143
+ "</Image>"
144
+
145
+ open(path, "w") { |file| file.puts(xml) }
146
+ end
147
+
148
+ def split_to_filename_and_extension(path)
149
+ extension = File.extname(path).gsub('.', '')
150
+ filename = File.basename(path, '.' + extension)
151
+ return filename, extension
152
+ end
153
+
154
+ def valid_url?(urlStr)
155
+ url = URI.parse(urlStr)
156
+ Net::HTTP.start(url.host, url.port) do |http|
157
+ return http.head(url.request_uri).code == "200"
158
+ end
159
+ end
160
+
161
+ def get_image(image_path)
162
+ image = nil
163
+
164
+ if File.file?(image_path)
165
+ image = Image::read(image_path).first
166
+ elsif valid_url
167
+ f = open(image_path)
168
+ image = Image.from_blob(f.read).first
169
+ f.close
170
+ end
171
+
172
+ return image
173
+ end
174
+
175
+ end
data/ruby-dzi.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{ruby-dzi}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Marc Vitalis"]
9
+ s.date = %q{2010-07-03}
10
+ s.description = %q{Ruby Dzi slices images into several tiles and creates dzi descriptor file.}
11
+ s.email = %q{marc.vitalis@live.com}
12
+ s.extra_rdoc_files = ["LICENSE-GPL.txt", "LICENSE-LGPL.txt", "LICENSE-MPL.txt", "README.rdoc", "lib/ruby_dzi.rb"]
13
+ s.files = ["LICENSE-GPL.txt", "LICENSE-LGPL.txt", "LICENSE-MPL.txt", "README.rdoc", "Rakefile", "init.rb", "lib/ruby_dzi.rb", "Manifest", "ruby-dzi.gemspec"]
14
+ s.homepage = %q{http://github.com/netxph/ruby-dzi}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ruby-dzi", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{ruby-dzi}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Ruby Dzi slices images into several tiles and creates dzi descriptor file.}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_development_dependency(%q<rmagick>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<rmagick>, [">= 0"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<rmagick>, [">= 0"])
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-dzi
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Marc Vitalis
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-03 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rmagick
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Ruby Dzi slices images into several tiles and creates dzi descriptor file.
36
+ email: marc.vitalis@live.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE-GPL.txt
43
+ - LICENSE-LGPL.txt
44
+ - LICENSE-MPL.txt
45
+ - README.rdoc
46
+ - lib/ruby_dzi.rb
47
+ files:
48
+ - LICENSE-GPL.txt
49
+ - LICENSE-LGPL.txt
50
+ - LICENSE-MPL.txt
51
+ - README.rdoc
52
+ - Rakefile
53
+ - init.rb
54
+ - lib/ruby_dzi.rb
55
+ - Manifest
56
+ - ruby-dzi.gemspec
57
+ has_rdoc: true
58
+ homepage: http://github.com/netxph/ruby-dzi
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --line-numbers
64
+ - --inline-source
65
+ - --title
66
+ - Ruby-dzi
67
+ - --main
68
+ - README.rdoc
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 11
86
+ segments:
87
+ - 1
88
+ - 2
89
+ version: "1.2"
90
+ requirements: []
91
+
92
+ rubyforge_project: ruby-dzi
93
+ rubygems_version: 1.3.7
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Ruby Dzi slices images into several tiles and creates dzi descriptor file.
97
+ test_files: []
98
+