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

; Produce layers that allow subtracting particular hues.
; Command is in "Filters->Misc->De-mirror"

(define (script-fu-sg-de-mirror image orig-layer)
  (let ((hue-names (vector "ALL-HUES" "RED-HUES" "YELLOW-HUES" "GREEN-HUES" "CYAN-HUES" "BLUE-HUES" "MAGENTA-HUES")))
    (gimp-image-undo-group-start image)
    (map 
      (lambda (hue-range)
        (let ((layer (car (gimp-layer-copy orig-layer FALSE))))
          (gimp-image-insert-layer image layer 0 0)
          (gimp-hue-saturation layer hue-range 0 0 100)
          (gimp-layer-set-mode layer DIFFERENCE-MODE)
          (let ((new-layer (car (gimp-layer-new-from-visible image image (vector-ref hue-names hue-range)))))
            (gimp-image-insert-layer image new-layer 0 0)
            (gimp-image-remove-layer image layer)
            (plug-in-normalize RUN-NONINTERACTIVE image new-layer)
            (gimp-item-set-visible new-layer FALSE)
            (gimp-layer-set-mode new-layer SUBTRACT-MODE)
            (gimp-layer-set-opacity new-layer 100) )))
      (list MAGENTA-HUES BLUE-HUES CYAN-HUES GREEN-HUES YELLOW-HUES RED-HUES) )
    (gimp-image-undo-group-end image) ))

(script-fu-register "script-fu-sg-de-mirror"
  "De-mirror"
  _"Create adjustment layers to remove old-photo mirroring"
  "Saul Goode"
  "Saul Goode"
  "June 2014"
  "RGB*"
  SF-IMAGE "Image" 0
  SF-DRAWABLE "Drawable" 0
  )

(script-fu-menu-register "script-fu-sg-de-mirror"
  "<Image>/Filters/Misc/"
  )
