Class: SVGProfiler::Histogram

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(png) ⇒ Histogram

Returns a new instance of Histogram.



7
8
9
10
# File 'lib/svg_profiler/histogram.rb', line 7

def initialize(png)
  check_for_imagemagick
  @image = Magick::Image.read(png.path).first
end

Class Method Details

.for(png, threshold) ⇒ Object



3
4
5
# File 'lib/svg_profiler/histogram.rb', line 3

def self.for(png, threshold)
  new(png).histogram(threshold)
end

Instance Method Details

#histogram(threshold) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/svg_profiler/histogram.rb', line 12

def histogram(threshold)
  @image.quantize(8) # 00-FF is 8-bits.

  histogram = @image.color_histogram
  frequencies = histogram.inject({}) do |hash, (p, f)|
    hash.merge(to_hex(p) => f)
  end

  ratios = normalize(frequencies)
  ratios.reject! { |_, v| v < threshold }

  # Re-normalize after thresholding.
  normalize(ratios)
end