# $p0 - Horizontal pieces
# $p1 - Vertical pieces
# $p2 - Curved edges (boolean)
# $p3 - Clipboard position (normal, centered, scaled)

&rfx_sendmsg (
  qq{
    (begin
      (when (= $frame $start)
        (define rfx-jigsaw-pieces '())
        (define rfx-jigsaw-keyframes '())
        (let* ((jigsaw-image (car (gimp-image-new $width $height RGB)))
               (layer (car (gimp-layer-new jigsaw-image 
                                           $width 
                                           $height 
                                           RGBA-IMAGE
                                           "jigsaw"
                                           100
                                           NORMAL-MODE ))))
          (gimp-image-add-layer jigsaw-image layer 0)
          (gimp-drawable-fill layer WHITE-FILL)
          (plug-in-jigsaw RUN-NONINTERACTIVE jigsaw-image layer $p0 $p1 $p2 0 0)
          (let row-loop ((row $p1))
            (unless (zero? row)
              (let col-loop ((col $p0))
                (if (zero? col)
                  (row-loop (pred row))
                  (begin
                    (gimp-fuzzy-select layer 
                                       (- (* (/ $width $p0) col) (/ $width $p0 2))
                                       (- (* (/ $height $p1) row) (/ $height $p1 2))
                                       128 
                                       CHANNEL-OP-REPLACE
                                       FALSE
                                       FALSE
                                       0
                                       FALSE )
                    (plug-in-sel2path RUN-NONINTERACTIVE jigsaw-image layer)
                    (set! rfx-jigsaw-pieces 
                          (cons (car (gimp-vectors-export-to-string 
                                         jigsaw-image 
                                         (car (gimp-image-get-active-vectors jigsaw-image)) ))
                                rfx-jigsaw-pieces ))
                    (col-loop (pred col)) )))))
          (gimp-image-delete jigsaw-image) 
          )
        ;; randomize the list of pieces
        (let ((number-of-pieces (* $p0 $p1)))
          (set! rfx-jigsaw-pieces 
                (let loop ((lis rfx-jigsaw-pieces)
                           (k number-of-pieces) )
                  (if (zero? k)
                    lis
                    (let ((index (random number-of-pieces)))
                      (loop (append (list-tail lis index)
                                    (list-tail (reverse lis) (- (length lis)  index)) )
                            (pred k) ))))))
        (let ((next-keyframe (make-progressor $start (/ (- $end $start) (succ (* $p0 $p1))))))
          (next-keyframe) ; start with fully shown clipboard
          (set! rfx-jigsaw-keyframes (map next-keyframe rfx-jigsaw-pieces))
          )
        )
      (let* ((input-file (string-append "$curtmpdir" DIR-SEPARATOR "$in"))
             (image (car (gimp-file-load RUN-NONINTERACTIVE input-file input-file)))
             (layer (car (gimp-image-get-active-layer image)))
             (input-file2 (string-append "$in2"))
             (image2 (car (gimp-file-load RUN-NONINTERACTIVE input-file2 input-file2)))
             (b-layer (car (gimp-layer-new-from-drawable 
                                (car (gimp-image-get-active-layer image2)) 
                                image ))) )
        (gimp-image-undo-disable image)
        (gimp-image-add-layer image b-layer 1)
        (cond 
          ((= $p3 1) ; centered
            (gimp-layer-set-offsets b-layer 
                                    (/ (- $width (car (gimp-drawable-width b-layer))) 2)
                                    (/ (- $height (car (gimp-drawable-height b-layer))) 2) ))
          ((= $p3 2) ; scaled
            (gimp-layer-scale-full b-layer $width $height TRUE INTERPOLATION-CUBIC)
            (gimp-layer-set-offsets b-layer 0 0) ))
        
        (gimp-image-delete image2)

        (gimp-layer-add-alpha layer)
        (gimp-selection-none image)

        (when (and (pair? rfx-jigsaw-keyframes)
                   (>= $frame (car rfx-jigsaw-keyframes)) )
          (set! rfx-jigsaw-keyframes (cdr rfx-jigsaw-keyframes)) )

        (let loop ((number-of-keyframes (length rfx-jigsaw-keyframes))
                   (pieces rfx-jigsaw-pieces) )
          (unless (zero? number-of-keyframes)
            (let ((path (vector-ref (cadr (gimp-vectors-import-from-string image 
                                                              (car pieces)
                                                              -1
                                                              TRUE
                                                              FALSE ))
                                    0 )))
              (gimp-vectors-to-selection path CHANNEL-OP-ADD TRUE FALSE 0 0)
              (loop (pred number-of-keyframes)
                    (cdr pieces) ))))
        (gimp-selection-grow image 1)
        (if (pair? rfx-jigsaw-keyframes)
          (gimp-edit-clear layer) )
        (gimp-image-merge-visible-layers image CLIP-TO-IMAGE)
        (rfx-save-frame image "$out") 
        )
      )
    }
  );
