class PhotoMosaic::Mosaic

Public Class Methods

new(original_image, tile_images, tile_width, tile_height) click to toggle source
# File lib/photo_mosaic/mosaic.rb, line 5
def initialize(original_image, tile_images, tile_width, tile_height)
  @tile_width = tile_width
  @tile_height = tile_height
  @original_image = original_image.shave(original_image.width % tile_width, original_image.height % tile_height)
  @tile_images = tile_images.map { |img| img.fit(tile_width, tile_height) }
end

Public Instance Methods

height() click to toggle source
# File lib/photo_mosaic/mosaic.rb, line 25
def height
  @original_image.height / @tile_height
end
join_tiles() click to toggle source
# File lib/photo_mosaic/mosaic.rb, line 12
def join_tiles
  rows = []
  tiles.each_with_index do |tile, index|
    row_index = index / width
    rows[row_index] = if rows[row_index].nil?
      tile.pixels
    else
      merge_pixels(rows[row_index], tile.pixels)
    end
  end
  Image.new(rows.reduce(&:+))
end
size() click to toggle source
# File lib/photo_mosaic/mosaic.rb, line 29
def size
  width * height
end
tiles() click to toggle source
# File lib/photo_mosaic/mosaic.rb, line 33
def tiles
  squares = @original_image.squares(@tile_width, @tile_height)
  Enumerator.new do |yielder|
    squares.each do |square|
      yielder << square.find_match(@tile_images)
      broadcast(:image_find_match, {broadcaster: self, total: size})
    end
  end
end
width() click to toggle source
# File lib/photo_mosaic/mosaic.rb, line 43
def width
  @original_image.width / @tile_width
end

Private Instance Methods

merge_pixels(pixels_a, pixels_b) click to toggle source
# File lib/photo_mosaic/mosaic.rb, line 49
def merge_pixels(pixels_a, pixels_b)
  pixels_a.each_with_index.map do |row, index|
    row + (pixels_b[index])
  end
end