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

; Scales the layer to match the selection size while maintaining
; original aspect ration. If no selection is present, scales 
; layer to image size. Note: if the layer is floated (for example,
; after an Edit->Paste has been performed) there is no selection
; present even though there are marching ants around the originally
; selected region -- if this script is run without first making
; a new selection then the floating selection will be scaled to
; the image size. 

(define (script-fu-sg-kumar-kovuru dirname bg-color)
  (gimp-context-push)
  (let loop ((filenames (append (cadr (file-glob (string-append dirname 
                                                                DIR-SEPARATOR
                                                                "*.png" ) 1))
                                (cadr (file-glob (string-append dirname 
                                                                DIR-SEPARATOR
                                                                "*.PNG" ) 1))
                                (cadr (file-glob (string-append dirname 
                                                                DIR-SEPARATOR
                                                                "*.jpg" ) 1))
                                (cadr (file-glob (string-append dirname 
                                                                DIR-SEPARATOR
                                                                "*.JPG" ) 1)))))
    (unless (null? filenames)
      (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE 
                                         (car filenames)
                                         (car filenames) )))
             (layer (car (gimp-image-get-active-layer image)))
             (width (car (gimp-drawable-width layer)))
             (height (car (gimp-drawable-height layer)))
             (aspect (/ height width)) )
        (gimp-image-undo-disable image)
        (if (< (/ 300 width) (/ 300 height))
            (gimp-layer-scale layer
                              300
                              (* 300 aspect) 
                              TRUE)
            (gimp-layer-scale layer 
                              (/ 300 aspect)
                              300 
                              TRUE ))
        (gimp-context-set-background bg-color)
        (gimp-layer-resize layer 
                           320 
                           320 
                           (/ (- 320 (car (gimp-drawable-width layer))) 2) 
                           (/ (- 320 (car (gimp-drawable-height layer))) 2))
        (gimp-layer-flatten layer)
        (gimp-image-resize-to-layers image) 
        (gimp-file-save RUN-NONINTERACTIVE image layer (car filenames) (car filenames))
        (gimp-image-delete image)
        (loop (cdr filenames)) )))
  (gimp-context-pop)
  )
(script-fu-register "script-fu-sg-kumar-kovuru"
 "Process images per Kumar Kovuru..."
 "For each file in directory, fit in 300x300 and add border"
 "Saul Goode"
 "Saul Goode"
 "September 2013"
 ""
 SF-DIRNAME "Directory" "Pick A Directory"
 SF-COLOR "Border color" '(0 0 0)
 )

(script-fu-menu-register "script-fu-sg-kumar-kovuru"
 "<Image>/Filters/Misc/"
 )
 
