class ShatteredMachine::ChangeByte

Use the change byte algorithm from pnglitch on a given png image.

Public Class Methods

new(options = {}) click to toggle source

@param options [Hash] options for change byte algorithm

# File lib/shattered_machine/change_byte.rb, line 8
def initialize(options = {})
  @byte_numbers = options[:byte_numbers].to_i || 0
end

Public Instance Methods

call(input_image, output_image) click to toggle source

@param input_image [string] path for image @param output_image [string] path for output changed byte image @return [boolean] status of change byte

# File lib/shattered_machine/change_byte.rb, line 15
def call(input_image, output_image)
  PNGlitch.open(input_image) do |png|
    filtered_glitch(png).save output_image
  end
  output_image
end

Private Instance Methods

change_byte(scanline) click to toggle source
# File lib/shattered_machine/change_byte.rb, line 30
def change_byte(scanline)
  scanline.register_filter_encoder do |data, prev|
    data.size.times.reverse_each do |i|
      x = data.getbyte(i)
      v = prev ? prev.getbyte(i - @byte_numbers) : 0
      data.setbyte(i, (x - v) & 0xff)
    end
    data
  end
end
filtered_glitch(png) click to toggle source
# File lib/shattered_machine/change_byte.rb, line 24
def filtered_glitch(png)
  png.each_scanline do |scanline|
    change_byte(scanline)
  end
end