class AverageHash::HashingStrategy

Constants

INDEX_OF_B
INDEX_OF_G
INDEX_OF_R
NTSC_COEFFICIENT_OF_B
NTSC_COEFFICIENT_OF_G
NTSC_COEFFICIENT_OF_R
PIXEL_ACQUISITION_SIZE
RESIZNING_SIZE

Public Class Methods

new(file_path) click to toggle source
# File lib/average_hash/hashing_strategy.rb, line 18
def initialize(file_path)
  @image = Magick::Image.read(file_path).first.resize(RESIZNING_SIZE, RESIZNING_SIZE)
end

Public Instance Methods

generate_hash() click to toggle source
# File lib/average_hash/hashing_strategy.rb, line 22
def generate_hash
  (image_pixel_count.times).map { |i| gray_scaled_pixel(i) > average_pixel ? '1' : '0' }.join
end

Private Instance Methods

average_pixel() click to toggle source
# File lib/average_hash/hashing_strategy.rb, line 40
def average_pixel
  gray_scaled_pixels = (image_pixel_count.times).map { |i| gray_scaled_pixel(i) }
  gray_scaled_pixels.reduce(0, :+) / image_pixel_count
end
gray_scaled_pixel(index) click to toggle source
# File lib/average_hash/hashing_strategy.rb, line 45
def gray_scaled_pixel(index)
  px = pixel_at((index % RESIZNING_SIZE), (index / RESIZNING_SIZE))
  px[INDEX_OF_R] * NTSC_COEFFICIENT_OF_R + px[INDEX_OF_G] * NTSC_COEFFICIENT_OF_G + px[INDEX_OF_B] * NTSC_COEFFICIENT_OF_B
end
image_columns() click to toggle source
# File lib/average_hash/hashing_strategy.rb, line 28
def image_columns
  @image.columns
end
image_pixel_count() click to toggle source
# File lib/average_hash/hashing_strategy.rb, line 36
def image_pixel_count
  image_columns * image_rows
end
image_rows() click to toggle source
# File lib/average_hash/hashing_strategy.rb, line 32
def image_rows
  @image.rows
end
pixel_at(offset_of_x, offset_of_y) click to toggle source
# File lib/average_hash/hashing_strategy.rb, line 50
def pixel_at(offset_of_x, offset_of_y)
  @image.export_pixels(offset_of_x, offset_of_y, PIXEL_ACQUISITION_SIZE, PIXEL_ACQUISITION_SIZE)
end