; 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.

(define (script-fu-sg-dump-path-colors image drawable output-dir basename precision)
  (let* ((path (car (gimp-image-get-active-vectors image)))
         (strokes (vector->list (cadr (gimp-vectors-get-strokes path)))))
    (let loop-strokes ((strokes strokes)
                       (stroke-id (if (= (length strokes) 1) 0 1)))
      (if (null? strokes)
        #t
        (let ((filename (string-append output-dir 
                                       (if (string=? output-dir "")
                                         ""
                                         DIR-SEPARATOR)
                                       basename
                                       (if (zero? stroke-id)
                                         ""
                                         (string-append "-" (number->string stroke-id)))
                                       ".csv")))
          (with-output-to-file filename 
            (lambda ()
              (let ((all-pts (vector->list (cadr (gimp-vectors-stroke-interpolate path 
                                                                                       (car strokes)
                                                                                       (min 0.05 precision))))))
                (let loop-pts ((pts all-pts)
                               (lastx (car all-pts)) 
                               (lasty (cadr all-pts)))
                  (unless (null? pts) 
                    (let ((x (car pts))
                          (y (cadr pts)))
                      (unless (and (= x lastx) 
                                   (= y lasty))
                        (display x) (display ",") 
                        (display y) 
                        (let loop-colors ((colors (vector->list (cadr (gimp-drawable-get-pixel drawable x y)))))
                          (unless (null? colors)
                            (display ",") (display (car colors))
                            (loop-colors (cdr colors)))))
                    (newline)
                    (loop-pts (cddr pts)
                              x
                              y)))))
          ))
        (loop-strokes (cdr strokes)
                      (+ stroke-id 1)))))))
                          
(script-fu-register "script-fu-sg-dump-path-colors"
 "Dump Path Colors..."
 "Sample colors along path and save to CSV file"
 "Saul Goode"
 "Saul Goode"
 "December 2014"
 "RGB*,GRAY*"
 SF-IMAGE    "Image"    0
 SF-DRAWABLE "Drawable" 0
 SF-DIRNAME "Output Folder" ""
 SF-STRING "Filename (.csv will be appended)" "path-colors"
 SF-ADJUSTMENT "Precision" '(0.5 0.1 10 .1 0 1)
 )

(script-fu-menu-register "script-fu-sg-dump-path-colors"
 "<Image>/Filters/Misc"
 )
