Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Monday 26 August 2013

Simple pixellator using a cfdg end point

In hommage to guigui's original, the output cfdg file is called haddock.cfdg (after capt haddock "tintin"), the sketch data gets shoved into data.cfdg, that is called from haddock.cfdg by the cfdg program (requires version 3.0+). To adjust to different axis conventions I could just have made y negative. But because evolution of an non deterministic design (this isn't one) is from centre out, I adjusted x and y accordingly. Tune-able value is skip 3..10 is sensible choice, you could also add a posterize filter if needed. Nice to have would be a rectangle mask to define area to pixel-ate. Where an actual image is uses as basis you might be better doing img.load_pixels and accessing the pixel array thus img.pixels[x +y * width]. However the following sketch could be readily adapted to use processing generated images.
load_library :file_chooser
attr_reader :img, :data, :skip

###########
# Example file chooser (in this case as an image file chooser).
# We delay setting size of sketch until we know image size, probably
# would not work vanilla processing. Note we can wrap much code in the
# file_chooser block, no need for reflection. As with selectInput vanilla
# processing.
###########

def setup
  color_mode(HSB, 1.0);
  file_chooser do |fc|
    fc.look_feel "Gtk+"                            # optional, default is "Metal"
    fc.set_filter "Image Files",  [".png", ".jpg"] # easily customizable chooser
    @img = load_image(fc.display)                  # fc.display returns a path String
    size(img.width, img.height)
  end
  @skip = 5
  @data = []
end

def draw
  background img                                  # img must be same size as sketch
end

def write_data name, data
  open("data.cfdg", "w") do |pw|
    pw.puts "shape #{name}{\n"
    data.each do |row|
      pw.puts "  #{row[0]}[x #{row[1]} y #{row[2]} s #{row[3]} hue #{row[4]} sat #{row[5]} brightness #{row[6]}]\n"
    end
    pw.puts "}\n"
  end
end

def write_start start, data
  open("#{start}.cfdg", "w") do |pw|
  pw.puts "CF::Background = [b -1]"
  pw.puts "startshape #{start}\n"
  pw.puts "shape dot{CIRCLE[]}\n"
  pw.puts "import data.cfdg\n"
end
write_data start, data
end

def mouse_clicked
  export = Thread.new do
    load_pixels
    shp = "dot"
    (skip ... img.width).step(skip) do |x|
      (skip ... img.height).step(skip) do |y|
        pix = pixels[x +y * width]
        sat = saturation(pix)
        hue = hue(pix)
        sz = brightness(pix) * skip
        data << [shp, -width/2 + x, height/2 - y, sz.round(2), (hue * 360).round, sat.round(4), 1] if sz > 0.03
      end
    end
    write_start "haddock", data
  end  
  export.join
  puts "done"
end

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2