Class: RubyDzi

Inherits:
Object
  • Object
show all
Includes:
Magick
Defined in:
lib/ruby_dzi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_path) ⇒ RubyDzi

Returns a new instance of RubyDzi.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_dzi.rb', line 29

def initialize(image_path)

  #set defaults
  @quality = 75
  @dir = '.'
  @tile_size = 254
  @overlap = 1
  @output_ext = 'dzi'

  @image_path = image_path

end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



27
28
29
# File 'lib/ruby_dzi.rb', line 27

def dir
  @dir
end

#formatObject

Returns the value of attribute format.



27
28
29
# File 'lib/ruby_dzi.rb', line 27

def format
  @format
end

#image_pathObject

Returns the value of attribute image_path.



27
28
29
# File 'lib/ruby_dzi.rb', line 27

def image_path
  @image_path
end

#nameObject

Returns the value of attribute name.



27
28
29
# File 'lib/ruby_dzi.rb', line 27

def name
  @name
end

#output_extObject

Returns the value of attribute output_ext.



27
28
29
# File 'lib/ruby_dzi.rb', line 27

def output_ext
  @output_ext
end

#overlapObject

Returns the value of attribute overlap.



27
28
29
# File 'lib/ruby_dzi.rb', line 27

def overlap
  @overlap
end

#qualityObject

Returns the value of attribute quality.



27
28
29
# File 'lib/ruby_dzi.rb', line 27

def quality
  @quality
end

#tile_sizeObject

Returns the value of attribute tile_size.



27
28
29
# File 'lib/ruby_dzi.rb', line 27

def tile_size
  @tile_size
end

Instance Method Details

#generate!(name, format = 'jpg') ⇒ Object



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_dzi.rb', line 42

def generate!(name, format = 'jpg')
  @name = name
  @format = format
  
  @levels_root_dir     = File.join(@dir, @name + '_files')
  @xml_descriptor_path = File.join(@dir, @name + '.' + @output_ext)

  image = get_image(@image_path)
  
  image.strip! # remove meta information

  orig_width, orig_height = image.columns, image.rows

  remove_files!

  # iterate over all levels (= zoom stages)
  max_level(orig_width, orig_height).downto(0) do |level|
    width, height = image.columns, image.rows
    puts "level #{level} is #{width} x #{height}"
    
    current_level_dir = File.join(@levels_root_dir, level.to_s)
    FileUtils.mkdir_p(current_level_dir)
    
    # iterate over columns
    x, col_count = 0, 0
    while x < width      
      # iterate over rows
      y, row_count = 0, 0
      while y < height          
        dest_path = File.join(current_level_dir, "#{col_count}_#{row_count}.#{@format}")
        tile_width, tile_height = tile_dimensions(x, y, @tile_size, @overlap)
        
        save_cropped_image(image, dest_path, x, y, tile_width, tile_height, @quality)
        
        y += (tile_height - (2 * @overlap))
        row_count += 1
      end
      x += (tile_width - (2 * @overlap))
      col_count += 1
    end
    
    image.resize!(0.5)
  end

  # generate xml descriptor and write file
  write_xml_descriptor(@xml_descriptor_path,
                       :tile_size => @tile_size,
                       :overlap   => @overlap,
                       :format    => @format,
                       :width     => orig_width,
                       :height    => orig_height)
end

#remove_files!Object



95
96
97
98
99
100
101
102
# File 'lib/ruby_dzi.rb', line 95

def remove_files!
  files_existed = (File.file?(@xml_descriptor_path) or File.directory?(@levels_root_dir))

  File.delete @xml_descriptor_path if File.file? @xml_descriptor_path
  FileUtils.remove_dir @levels_root_dir if File.directory? @levels_root_dir

  return files_existed
end