Class: Seadragon::Slicer

Inherits:
Object
  • Object
show all
Defined in:
lib/seadragon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Slicer

Returns a new instance of Slicer.

Raises:

  • (ArgumentError)


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_nameObject

Returns the value of attribute dzi_name.



9
10
11
# File 'lib/seadragon.rb', line 9

def dzi_name
  @dzi_name
end

#formatObject

Returns the value of attribute format.



9
10
11
# File 'lib/seadragon.rb', line 9

def format
  @format
end

#handleObject

Returns the value of attribute handle.



9
10
11
# File 'lib/seadragon.rb', line 9

def handle
  @handle
end

#heightObject

Returns the value of attribute height.



9
10
11
# File 'lib/seadragon.rb', line 9

def height
  @height
end

#overlapObject

Returns the value of attribute overlap.



9
10
11
# File 'lib/seadragon.rb', line 9

def overlap
  @overlap
end

#qualityObject

Returns the value of attribute quality.



9
10
11
# File 'lib/seadragon.rb', line 9

def quality
  @quality
end

#source_imageObject

Returns the value of attribute source_image.



9
10
11
# File 'lib/seadragon.rb', line 9

def source_image
  @source_image
end

#source_pathObject

Returns the value of attribute source_path.



9
10
11
# File 'lib/seadragon.rb', line 9

def source_path
  @source_path
end

#tile_sizeObject

Returns the value of attribute tile_size.



9
10
11
# File 'lib/seadragon.rb', line 9

def tile_size
  @tile_size
end

#tiles_pathObject

Returns the value of attribute tiles_path.



9
10
11
# File 'lib/seadragon.rb', line 9

def tiles_path
  @tiles_path
end

#widthObject

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_specificationObject

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