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

; Process texture files per Hootyhoo 
; (discussion @ http://gimpchat.com/viewtopic.php?f=9&t=5519)

(define (script-fu-sg-hootyhoo directory color color-level repeat)
  (gimp-context-push)
  (gimp-context-set-foreground color)
  (let loop ((files (cadr (file-glob (string-append directory DIR-SEPARATOR "*.jpg") 1))))
    (unless (null? files)
      (let ((filename (car files)))
        (unless (> (length (strbreakup filename "_LM")) 1) ; ignore _LM files
          (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
                 (layer (car (gimp-image-get-active-layer image)))
                 (height (car (gimp-drawable-height layer)))
                 (width (car (gimp-drawable-width layer))) )
            (gimp-levels layer HISTOGRAM-VALUE 0 255 1 0 25)
            (let loop ((repeat repeat))
              (unless (zero? repeat)
                (let ((overlay (car (gimp-layer-copy layer TRUE))))
                  (gimp-image-add-layer image overlay 0)
                  (gimp-drawable-fill overlay FOREGROUND-FILL)
                  (gimp-layer-set-mode overlay OVERLAY-MODE)
                  (set! layer (car (gimp-image-merge-down image overlay EXPAND-AS-NECESSARY)))
                  (loop (pred repeat)) )))
            (let* ((basename (unbreakupstr (butlast (strbreakup filename ".")) "."))
                   (layer (car (gimp-image-flatten image)))
                   (pathname (string-append basename "_LM.jpg")) )
              (file-jpeg-save RUN-NONINTERACTIVE 
                              image 
                              layer
                              pathname 
                              pathname 
                              0.93
                              0 ; smoothing 
                              1 ; optimize 
                              1 ; progressive 
                              "" ; comment 
                              0 ; subsmp (0-4)
                              1 ; baseline 
                              0 ; restart 
                              0 ;dct 
                              ))
          (gimp-image-delete image) )
          (loop (cdr files)) ))))
  (gimp-context-pop) )
      
(script-fu-register "script-fu-sg-hootyhoo"
  "Hootyhoo..."
  "Process BMP files in folder using half of image as mask"
  "Saul Goode"
  "Saul Goode"
  "Feb 2012"
  ""
  SF-DIRNAME    "Directory"    "" 
  SF-COLOR      "Overlay color" '(255 245 212)
  SF-ADJUSTMENT "Color level" '( 25 0 255 1 10 0 1 )
  SF-ADJUSTMENT "Repeat" '( 2 1 10 1 1 0 1 )
  )
  
(script-fu-menu-register "script-fu-sg-hootyhoo"
  "<Image>/Filters/Misc" )
