class ShatteredMachine::Exchange

Use the exchange algorithm from pnglitch on a given png image.

Constants

ALL_FILTERS

Public Class Methods

new(options = {}) click to toggle source

@param options [Hash] options for exchange algorithm

# File lib/shattered_machine/exchange.rb, line 10
def initialize(options = {})
  @filter = define_filter(options[:filter]) || 'average'
  @random = options[:random] || false
  @range = options[:range] || 0
  @seed = options[:seed] || 'x'
end

Public Instance Methods

call(input_image, output_image) click to toggle source

@param input_image [string] path for input image @param output_image [string] path for output exchanged image @return [boolean] status of exchange

# File lib/shattered_machine/exchange.rb, line 20
def call(input_image, output_image)
  PNGlitch.open(input_image) do |png|
    filtered_glitch(png, @filter).save output_image
  end
  output_image
end

Private Instance Methods

define_filter(filter_from_options) click to toggle source
# File lib/shattered_machine/exchange.rb, line 29
def define_filter(filter_from_options)
  return filter_from_options unless filter_from_options == 'random'

  ALL_FILTERS[rand(5)]
end
exchange_data(data) click to toggle source
# File lib/shattered_machine/exchange.rb, line 48
def exchange_data(data)
  if @range.zero?
    letter = @random ? give_me_a_letter.to_s : @seed
    data.gsub(/\d/, letter)
  else
    @range.times do
      data[rand(data.size)] = give_me_a_letter.to_s
    end
    data
  end
end
filtered_glitch(png, custom_filter) click to toggle source
# File lib/shattered_machine/exchange.rb, line 39
def filtered_glitch(png, custom_filter)
  png.each_scanline do |scanline|
    scanline.change_filter custom_filter
  end
  png.glitch do |data|
    exchange_data(data)
  end
end
give_me_a_letter(index = rand(26)) click to toggle source
# File lib/shattered_machine/exchange.rb, line 35
def give_me_a_letter(index = rand(26))
  ('a'..'z').to_a[index]
end