#!/usr/bin/env python
from gi.repository import Gegl; Gegl.init(None)

def connect (*args):
  last = args[0]
  
  for node in args[1:]:
    last.connect_to("output", node, "input")
    last = node
  return last

for gegl_format in ['RGBA']:
  output = Gegl.Buffer.new("RGBA float", 0, 0, 1000, 1000)
  
  gegl    = Gegl.Node()

  node_uni  = gegl.create_child('gegl:checkerboard')
  
  node_crop = gegl.create_child('gegl:crop')
  node_crop.set_property("x", 0)
  node_crop.set_property("y", 0)
  node_crop.set_property("width", 1000)
  node_crop.set_property("height", 1000)

  node_convert = gegl.create_child('gegl:convert-format')
  node_convert.set_property("format", gegl_format + " float")
  
  node_blur    = gegl.create_child('gegl:gaussian-blur')
  #node_blur    = gegl.create_child('gegl:edge-sobel') # OK
  #node_blur    = gegl.create_child('gegl:dot') # OK
  #node_blur    = gegl.create_child('gegl:cubism') # Also broken
  #node_blur    = gegl.create_child('gegl:emboss') # OK
  
  node_write = gegl.create_child('gegl:write-buffer')
  node_write.set_property("buffer", output)
  
  node_save = gegl.create_child('gegl:png-save')
  node_save.set_property("path", 'process-save-'+gegl_format+'.png')
  
  graph = connect(node_uni, node_crop, node_convert, node_blur, node_save)
  r = node_save.get_bounding_box()
  print r.x, r.y, r.width, r.height
  
  if False:
    graph.process()
  else:
    step_size = int(r.height / 4)
    dr = Gegl.Rectangle.new (r.x, r.y, r.width, step_size)
    for i in range(4):
      print "PyRender", dr.x, dr.y, dr.width, dr.height
      node_blur.blit_buffer(None, dr)
      dr.y += step_size
    
    graph.blit_buffer(None, None)
