Module: Blockhash

Defined in:
lib/blockhash.rb,
lib/blockhash/version.rb

Overview

Blockhash module

Constant Summary collapse

DEFAULT_BITS =
16
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.calculate(img, bits = DEFAULT_BITS) ⇒ Object



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
# File 'lib/blockhash.rb', line 43

def self.calculate(img, bits = DEFAULT_BITS)
  even_x = (img.columns % bits).zero?
  even_y = (img.rows % bits).zero?
  return calculate_fast(img, bits) if even_x && even_y

  if img.alpha?
    total_value = method(:total_value_rgba)
    img_data = img.export_pixels(0, 0, img.columns, img.rows, "RGBA")
  else
    total_value = method(:total_value_rgb)
    img_data = img.export_pixels(0, 0, img.columns, img.rows, "RGB")
  end

  blocks = Array.new(bits**2, 0)
  block_width = img.columns / bits.to_f
  block_height = img.rows / bits.to_f

  (0...img.rows).each do |y|
    b_top, b_bottom, w_top, w_bottom = block_overlaps(y, img.rows, block_height, even_y)
    top_index = b_top * bits
    bottom_index = b_bottom * bits
    (0...img.columns).each do |x|
      value = total_value.call(img_data, img.columns, x, y)
      b_left, b_right, w_left, w_right = block_overlaps(x, img.columns, block_width, even_x)
      blocks[top_index + b_left] += value * w_top * w_left
      blocks[top_index + b_right] += value * w_top * w_right
      blocks[bottom_index + b_left] += value * w_bottom * w_left
      blocks[bottom_index + b_right] += value * w_bottom * w_right
    end
  end
  bits_to_hexhash(blocks_to_bits(blocks, block_width * block_height))
end

.calculate_fast(img, bits = DEFAULT_BITS) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blockhash.rb', line 17

def self.calculate_fast(img, bits = DEFAULT_BITS)
  if img.alpha?
    total_value = method(:total_value_rgba)
    img_data = img.export_pixels(0, 0, img.columns, img.rows, "RGBA")
  else
    total_value = method(:total_value_rgb)
    img_data = img.export_pixels(0, 0, img.columns, img.rows, "RGB")
  end
  blocksize_y = img.rows / bits
  blocksize_x = img.columns / bits
  # Loop through each block
  result = (0...bits).flat_map do |y|
    (0...bits).map do |x|
      # Sum all pixels in block
      (0...blocksize_y).sum do |iy|
        cy = (y * blocksize_y) + iy
        (0...blocksize_x).sum do |ix|
          cx = (x * blocksize_x) + ix
          total_value.call(img_data, img.columns, cx, cy)
        end
      end
    end
  end
  bits_to_hexhash(blocks_to_bits(result, blocksize_y * blocksize_x))
end

.calculate_from_path(path, bits = DEFAULT_BITS) ⇒ Object



76
77
78
# File 'lib/blockhash.rb', line 76

def self.calculate_from_path(path, bits = DEFAULT_BITS)
  calculate(Magick::Image.read(path).first, bits)
end

.distance(hash1, hash2) ⇒ Object



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

def self.distance(hash1, hash2)
  (hash1.to_i(16) ^ hash2.to_i(16)).to_s(2).count("1")
end

.similar?(hash1, hash2, thresh = 10) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/blockhash.rb', line 13

def self.similar?(hash1, hash2, thresh = 10)
  distance(hash1, hash2) < thresh
end