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

; Crops image to the selection and scales so that max dimension is 1600.
; Adds watermark and saves to a specified directory.
;
; "File->Mgwalk Save First..." prompts user for settings.
; "File->Mgwalk Save Next" execute without prompting.

(define (sg-mgwalk-save orig-image 
                        max-dimension
                        output-directory
                        filename-suffix
                        output-type ; JPG, PNG, or XCF
                        watermark-filename ; #f if unused
                        watermark-opacity
                        watermark-x
                        watermark-y
                        )
  (let ((image (car (gimp-image-duplicate orig-image))))
    (gimp-image-undo-disable image)
    (let ((x-offset 0)
          (y-offset 0)
          (width (car (gimp-image-width image)))
          (height (car (gimp-image-height image)))
          (bounds (gimp-selection-bounds image)) )
      (unless (zero? (car bounds))
        (set! x-offset (cadr bounds))
        (set! y-offset (caddr bounds))
        (set! width (- (cadddr bounds) x-offset))
        (set! height (- (car (cddddr bounds)) y-offset)) )
      (gimp-image-crop image width height x-offset y-offset)
      (gimp-selection-none image)
      (let ((aspect (/ height width)))
        (cond
          ((< width height)
            (gimp-image-scale-full image (/ max-dimension aspect) max-dimension INTERPOLATION-LANCZOS))
          ((> width height)
            (gimp-image-scale-full image max-dimension (* max-dimension aspect) INTERPOLATION-LANCZOS) ))))
    (if watermark-filename
      (let ((watermark-layer (catch #f (car (gimp-file-load-layer RUN-NONINTERACTIVE image watermark-filename)))))
        (if watermark-layer
          (begin
            (gimp-image-add-layer image watermark-layer 0)
            (gimp-layer-set-offsets watermark-layer watermark-x watermark-y)
            (gimp-layer-set-opacity watermark-layer watermark-opacity) )
          (gimp-message "Watermark image file not found!") )))
    (let ((layer (case output-type
                   ((0)
                     (car (gimp-image-flatten image)) )
                   ((1)
                     (car (gimp-image-merge-visible-layers image CLIP-TO-IMAGE )) )
                   ((2)
                     (vector-ref (cadr (gimp-image-get-layers image)) 0) ))))

      (if (> (car (gimp-image-width image)) 1600)
        (gimp-image-scale-full image 1600 1600 INTERPOLATION-CUBIC) )
      (let ((raw-filename (car (gimp-image-get-name orig-image))))
        (if (string=? raw-filename "Untitled")
          (gimp-message "File Not Saved!")
          (let* ((basename (unbreakupstr (butlast (strbreakup raw-filename ".")) "."))
                 (filename (string-append output-directory
                                          DIR-SEPARATOR 
                                          basename 
                                          filename-suffix
                                          (case output-type
                                            ((0) ".jpg") 
                                            ((1) ".png")
                                            ((2) ".xcf") ))))
            (gimp-file-save RUN-NONINTERACTIVE image layer filename raw-filename) ))))
    (gimp-image-delete image) ))

(define (script-fu-sg-mgwalk-save image 
                                  max-dimension
                                  output-directory
                                  filename-suffix
                                  output-type
                                  use-watermark
                                  watermark-filename
                                  watermark-opacity
                                  watermark-x
                                  watermark-y
                                  )
  (sg-mgwalk-save image 
                  max-dimension
                  output-directory
                  filename-suffix
                  output-type ; JPG, PNG, or XCF
                  (if (zero? use-watermark)
                    #f
                    watermark-filename )
                  watermark-opacity
                  watermark-x
                  watermark-y
                  )  
  (let ((directory-parasite (list "mgwalk-save-directory" 
                                  3 
                                  output-directory ))
        (watermark-parasite (list "mgwalk-save-watermark"
                                  3
                                  (if (zero? use-watermark)
                                    "ignore"
                                    watermark-filename )))
        (settings-parasite  (list "mgwalk-save-settings"
                                  3
                                  (string-append (number->string max-dimension)
                                                 " "
                                                 filename-suffix
                                                 " "
                                                 (number->string output-type)
                                                 " "
                                                 (number->string use-watermark)
                                                 " "
                                                 (number->string watermark-opacity)
                                                 " "
                                                 (number->string watermark-x)
                                                 " "
                                                 (number->string watermark-y) ))))
    (gimp-parasite-attach directory-parasite)
    (gimp-parasite-attach watermark-parasite)
    (gimp-parasite-attach settings-parasite) )
  (gimp-image-clean-all image)
  )

(define (script-fu-sg-mgwalk-save-next image)
  (let ((output-directory (catch #f (caddar (gimp-parasite-find "mgwalk-save-directory"))))
        (watermark-filename (catch #f (caddar (gimp-parasite-find "mgwalk-save-watermark"))))
        (settings-parasite  (catch #f (caddar (gimp-parasite-find "mgwalk-save-settings")))) )
    (when (and output-directory watermark-filename settings-parasite)
      (let ((settings (strbreakup settings-parasite " ")))
        (let ((max-dimension (string->number (car settings)))
              (filename-suffix (cadr settings))
              (output-type (string->number (caddr settings)))
              (use-watermark (string->number (cadddr settings)))
              (watermark-opacity (string->number (car (cddddr settings))))
              (watermark-x (string->number (cadr (cddddr settings))))
              (watermark-y (string->number (caddr (cddddr settings)))) )
          (sg-mgwalk-save image 
                          max-dimension
                          output-directory
                          filename-suffix
                          output-type ; JPG, PNG, or XCF
                          (if (zero? use-watermark)
                            #f
                            watermark-filename )
                          watermark-opacity
                          watermark-x
                          watermark-y
                          )))))
  (gimp-image-clean-all image)
  )

(script-fu-register "script-fu-sg-mgwalk-save"
 "Mgwalk Save First..."
 "Save image per Mgwalk"
 "Saul Goode"
 "Saul Goode"
 "Sept 2012"
 "*"
 SF-IMAGE    "Image"    0
 SF-ADJUSTMENT "Max dimension" '( 1600 10 10000 1 10 0 1 )
 SF-DIRNAME "Output directory" ""
 SF-TEXT "Filename suffix" "-RESIZED"
 SF-OPTION "Output type" '("JPG" "PNG" "XCF")
 SF-TOGGLE "Add watermark" FALSE
 SF-FILENAME "Watermark file" "watermark.png"
 SF-ADJUSTMENT "Watermark opacity" '( 50 1 100 1 10 0 1 )
 SF-ADJUSTMENT "Watermark location X" '( 20 0 10000 1 10 0 1 )
 SF-ADJUSTMENT "Watermark location Y" '( 20 0 10000 1 10 0 1 )
 )
(script-fu-menu-register "script-fu-sg-mgwalk-save"
 "<Image>/File/Save"
 )
 
(script-fu-register "script-fu-sg-mgwalk-save-next"
 "Mgwalk Save Next"
 "Save Next per Mgwalk"
 "Saul Goode"
 "Saul Goode"
 "Sept 2012"
 "*"
 SF-IMAGE    "Image"    0
 )
(script-fu-menu-register "script-fu-sg-mgwalk-save-next"
 "<Image>/File/Save"
 )
 
