teaching machines

CS 330 Lecture 36 – GIMP example, macros

May 2, 2012 by . Filed under cs330, lectures, spring 2012.

Agenda

Program this

Sketch out the pseudocode for implementing a paintbucket operation in an image editor.

Script-Fu template

(script-fu-register
  "floodfill"
  "Hackbucket"
  "Performs the hackbucket operation very slowly"
  "Chris Johnson"
  "You wrote this code, not me."
  "May 2, 2012"
  ""
  ARGS
  ...
)

(script-fu-menu-register "floodfill" "<Image>/CS330")

Code

hackbucket.scm

(define floodfill
  (lambda (image color x y)
    (let
      ((layer (car (gimp-image-get-active-layer image)))
       (fill-color (cons-array 3 'byte)))
         ; check for fill != init
         (floodfill-helper layer (car (cdr (gimp-drawable-get-pixel layer x y))) fill-color x y))))

(define floodfill-helper
  (lambda (layer init-color fill-color x y)
    (cond
      ; check for canvas drop off
      ((< x 0) '())
      ((< y 0) '())
      ; TODO

      ((not (equal? (car (cdr (gimp-drawable-get-pixel layer x y))) init-color)) '())

      ; general case
      (else 
        (begin
          (color-pixel-at layer fill-color x y)
          (floodfill-helper layer init-color fill-color (+ x 1) y)
          (floodfill-helper layer init-color fill-color (- x 1) y)
          (floodfill-helper layer init-color fill-color x (+ y 1))
          (floodfill-helper layer init-color fill-color x (- y 1)))))))

(define color-pixel-at
  (lambda (layer color x y)
    (gimp-drawable-set-pixel layer x y 3 color)
    (gimp-displays-flush)
    (gimp-drawable-update layer x y 1 1)))

(script-fu-register
  "floodfill"
  "Hackbucket"
  "Performs the hackbucket operation very slowly"
  "Chris Johnson"
  "You wrote this code, not me."
  "May 2, 2012"
  ""
  SF-IMAGE "Image" 0
  SF-COLOR "Color" '(0 0 0)
  SF-VALUE "X" "0"
  SF-VALUE "Y" "0"
)

(script-fu-menu-register "floodfill" "<Image>/CS330")

Haiku

Plugins tweak software.
But what about appliances?
Blenders need Whip-Fu.