; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

; Scale each of the rows of the current layer to fit the selection
;

(define (script-fu-sg-contour-fill image drawable)
  (let  ((bounds (gimp-selection-bounds image)))
    (unless (zero? (car bounds))
      (gimp-image-undo-group-start image)
      (set! bounds (cdr bounds))
      (let* ((contour-layer (car (gimp-layer-copy drawable TRUE)))
             (orig-sel (car (gimp-selection-save image)))
             (offset-y (cadr bounds))
             (offset-x (car bounds))
             (width (- (caddr bounds) (car bounds)))
             (height (- (cadddr bounds) (cadr bounds)))
             )
        (gimp-image-add-layer image contour-layer -1)
        (gimp-layer-scale-full contour-layer 
                               width
                               height
                               TRUE
                               INTERPOLATION-CUBIC )
        (gimp-layer-set-offsets contour-layer 0 0)
        (let loop ((row height))
          (unless (zero? row)
            (gimp-selection-load orig-sel)
            (gimp-rect-select image offset-x (- (+ offset-y row) 1) width 1 CHANNEL-OP-INTERSECT 0 0)
            (set! bounds (gimp-selection-bounds image))
            (gimp-rect-select image 0 (- row 1) width 1 CHANNEL-OP-REPLACE 0 0)
            (if (zero? (car bounds))
              (gimp-edit-clear contour-layer)
              (unless (zero? (car (gimp-drawable-mask-intersect contour-layer)))
                (let ((floating-sel (car (gimp-selection-float contour-layer 0 0))))
                  (gimp-layer-scale-full floating-sel 
                                         (- (cadddr bounds) (cadr bounds) )
                                         1
                                         FALSE
                                         INTERPOLATION-CUBIC )
                  (gimp-layer-set-offsets floating-sel (- (cadr bounds) offset-x) (- row 1))
                  (gimp-floating-sel-anchor floating-sel) )))
            (loop (- row 1)) ))
        (gimp-layer-set-offsets contour-layer offset-x offset-y)
        (gimp-selection-load orig-sel)
        (gimp-selection-invert image)
        (gimp-edit-clear contour-layer)
        (gimp-selection-invert image)
        (gimp-image-remove-channel image orig-sel)
        (gimp-image-undo-group-end image)
        (gimp-displays-flush) ))))

(script-fu-register "script-fu-sg-contour-fill"
  "Contour Fill Selection"
  "Scale each row of layer to fit selection"
  "Saul Goode"
  "Saul Goode"
  "Feb 2012"
  "RGB*"
  SF-IMAGE    "Image"    0
  SF-DRAWABLE "Drawable"  0 )
  
(script-fu-menu-register "script-fu-sg-contour-fill"
  "<Image>/Filters/Distorts" )
