きょうのglitch


# HSYNC Glitch
# inspired by http://flickr.com/photos/mikrosopht, via hysysk
# 2007-11-06 t.koyachi

require "java"
include_class "processing.core.PApplet"
include_class "processing.core.PImage"

class Sketch < PApplet
  def setup
    _height = 160
    _width = 160
    size _width, _height
    background 255
    no_stroke
    frameRate 3

    imgsrc = loadImage('utamaru160_meagne.PNG')
#    _height = imgsrc.height
#    _width = imgsrc.width

    loadPixels
    last_line = imgsrc.height
    num = Integer(rand(10)) + 1
    interval = Integer(last_line / num)
    timing = []
    line = 0
    num.times do |i|
      line += Integer(rand(interval)) + 1
      timing.push(line)
      p "timing = #{line}"
    end
    tick = 1
    offset = 0
    last_effect = 'tick'
    current_line = 0
    _height.times do |y|
      if timing[current_line] == y then
        if last_effect == 'offset' then
          tick = Integer(rand(100)) # - 5
          p "y=#{y}, tick=#{tick}"
          last_effect = 'tick'
        else
          offset = Integer(rand(20))
          p "y=#{y}, offset=#{offset}"
          last_effect = 'offset'
        end
        current_line += 1
      end
      _width.times do |x|
        current_point = y * _width + x
        src_point = current_point + tick * y + offset
        if src_point >= _height * _width then
          src_point = current_point - _width
        end
        pixels[current_point] = imgsrc.pixels[src_point]
      end
    end
    updatePixels
  end
end

JFrame = javax.swing.JFrame

def run applet
  frame = JFrame.new applet.class.to_s
  frame.content_pane.add applet
  frame.default_close_operation = JFrame::EXIT_ON_CLOSE
  applet.init
  frame.pack
  frame.visible = true
end

if $0 == __FILE__
  run Sketch.new
end

jrubyでprocessingのコードは(Processing by Ruby,その2)を参考にしました。