Class: Seadragon::Slicer
- Inherits:
-
Object
- Object
- Seadragon::Slicer
- Defined in:
- lib/seadragon.rb
Instance Attribute Summary collapse
-
#dzi_name ⇒ Object
Returns the value of attribute dzi_name.
-
#format ⇒ Object
Returns the value of attribute format.
-
#handle ⇒ Object
Returns the value of attribute handle.
-
#height ⇒ Object
Returns the value of attribute height.
-
#overlap ⇒ Object
Returns the value of attribute overlap.
-
#quality ⇒ Object
Returns the value of attribute quality.
-
#source_image ⇒ Object
Returns the value of attribute source_image.
-
#source_path ⇒ Object
Returns the value of attribute source_path.
-
#tile_size ⇒ Object
Returns the value of attribute tile_size.
-
#tiles_path ⇒ Object
Returns the value of attribute tiles_path.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Slicer
constructor
A new instance of Slicer.
-
#slice! ⇒ Object
Generates the tiles.
-
#write_dzi_specification ⇒ Object
Generates the DZI (Deep Zoom Image format) descriptor file in JSON.
Constructor Details
#initialize(attributes = {}) ⇒ Slicer
Returns a new instance of Slicer.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/seadragon.rb', line 12 def initialize(attributes = {}) raise ArgumentError.new('source and destination paths are required') unless attributes[:source_path] && attributes[:tiles_path] raise ArgumentError.new('a handle is required') unless attributes[:handle] @source_path = attributes[:source_path] @tiles_path = attributes[:tiles_path] @handle = attributes[:handle] @tile_size = attributes[:tile_size] || 512 @overlap = attributes[:overlap] || 1 @quality = attributes[:quality] || 100 @format = attributes[:format] || 'jpg' raise ArgumentError.new("source file doesn't exist") unless File.exist? @source_path @source_image = Magick::Image.read(@source_path)[0] # an Image object. @width, @height = @source_image.columns, @source_image.rows # image dims end |
Instance Attribute Details
#dzi_name ⇒ Object
Returns the value of attribute dzi_name.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def dzi_name @dzi_name end |
#format ⇒ Object
Returns the value of attribute format.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def format @format end |
#handle ⇒ Object
Returns the value of attribute handle.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def handle @handle end |
#height ⇒ Object
Returns the value of attribute height.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def height @height end |
#overlap ⇒ Object
Returns the value of attribute overlap.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def overlap @overlap end |
#quality ⇒ Object
Returns the value of attribute quality.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def quality @quality end |
#source_image ⇒ Object
Returns the value of attribute source_image.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def source_image @source_image end |
#source_path ⇒ Object
Returns the value of attribute source_path.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def source_path @source_path end |
#tile_size ⇒ Object
Returns the value of attribute tile_size.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def tile_size @tile_size end |
#tiles_path ⇒ Object
Returns the value of attribute tiles_path.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def tiles_path @tiles_path end |
#width ⇒ Object
Returns the value of attribute width.
9 10 11 |
# File 'lib/seadragon.rb', line 9 def width @width end |
Instance Method Details
#slice! ⇒ Object
Generates the tiles.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/seadragon.rb', line 33 def slice! # duplicate the source image, we'll be resizing it for each zoom layer. image = @source_image.dup # create a parent folder for all of the tiles FileUtils.mkdir_p( File.join(tiles_path, handle+"_files") ) max_level(width, height).downto(0) do |level| # get dimensions of the image (image is resized on each iteration) current_level_width, current_level_height = image.columns, image.rows current_level_dir = File.join(tiles_path, handle+"_files", level.to_s) FileUtils.mkdir_p(current_level_dir) # create dir for current level # iterate over columns x, col_count = 0, 0 while x < current_level_width # iterate over rows y, row_count = 0, 0 while y < current_level_height tile_file_path = File.join(current_level_dir, "#{col_count}_#{row_count}.#{format}") tile_width, tile_height = tile_dimensions(x, y, tile_size, overlap) save_tile(image, tile_file_path, x, y, tile_width, tile_height, quality) unless File.exist? tile_file_path y += (tile_height - (2 * overlap)) row_count += 1 end x += (tile_width - (2 * overlap)) col_count += 1 end image.resize!(0.5) end image.destroy! end |
#write_dzi_specification ⇒ Object
Generates the DZI (Deep Zoom Image format) descriptor file in JSON.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/seadragon.rb', line 71 def write_dzi_specification properties = { 'Image': { 'xmlns': "http://schemas.microsoft.com/deepzoom/2008", 'Format': format, 'Overlap': overlap.to_s, 'TileSize': tile_size.to_s, 'Size': { 'Height': height.to_s, 'Width': width.to_s } } } FileUtils.mkdir(tiles_path) unless File.exists?(tiles_path) dzi_path = File.join(tiles_path, handle + ".dzi") File.open(dzi_path, 'w') do |f| f.write(JSON.pretty_generate(properties)) end end |