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

; This script is an experimental work in progress!

(define (script-fu-sg-technicolor-3 image orig-layer)
  ; multiply all layers in given list to produce a new layer
  ; new layer is positioned above the 
  ;
  (define (multiply-layers image lis)
    (let* ((new-layers (map (lambda (x) (car (gimp-layer-copy x FALSE)))
                            lis )) )
      (gimp-image-add-layer image (car new-layers) -1)
      (let loop ((layers (cdr new-layers))
                 (new-layer (car new-layers)) )
        (if (null? layers)
          new-layer
          (begin
            (gimp-image-add-layer image (car layers) -1)
            (gimp-layer-set-mode (car layers) MULTIPLY-MODE)
            (loop (cdr layers)
                  (car (gimp-image-merge-down image
                                              (car layers)
                                              EXPAND-AS-NECESSARY ))))))))
  (let* ((red-matte 0)
         (green-matte 0)
         (blue-matte 0)
         (tmp-layer 0)
         (orig-sel 0)
         (orig-vis (car (gimp-drawable-get-visible orig-layer))) )
    (gimp-image-undo-group-start image)
    (set! orig-sel (car (gimp-selection-save image)))
    (gimp-selection-none image)
    (gimp-drawable-set-visible orig-layer TRUE)                                           
    (set! red-matte (car (gimp-layer-copy orig-layer FALSE)))
    (gimp-image-add-layer image red-matte -1)
    (plug-in-colors-channel-mixer RUN-NONINTERACTIVE
                                  image
                                  red-matte
                                  FALSE
                                   1.0 -0.5 -0.5
                                   1.0 -0.5 -0.5
                                   1.0 -0.5 -0.5 )
    (gimp-invert red-matte)
    (gimp-drawable-set-name red-matte "Red matte")

    (set! green-matte (car (gimp-layer-copy orig-layer FALSE)))
    (gimp-image-add-layer image green-matte -1)
    (plug-in-colors-channel-mixer RUN-NONINTERACTIVE
                                  image
                                  green-matte
                                  FALSE
                                  -0.5  1.0 -0.5
                                  -0.5  1.0 -0.5
                                  -0.5  1.0 -0.5 )
    (gimp-invert green-matte)
    (gimp-drawable-set-name green-matte "Green matte")

    (set! blue-matte (car (gimp-layer-copy orig-layer FALSE)))
    (gimp-image-add-layer image blue-matte -1)
    (plug-in-colors-channel-mixer RUN-NONINTERACTIVE
                                  image
                                  blue-matte
                                  FALSE
                                  -0.5 -0.5  1.0
                                  -0.5 -0.5  1.0
                                  -0.5 -0.5  1.0 )
    (gimp-invert blue-matte)
    (gimp-drawable-set-name blue-matte "Blue matte")

    (let ((red 0)
          (green 0)
          (blue 0) )
      (set! red (multiply-layers image (list orig-layer green-matte blue-matte)))
      (plug-in-colors-channel-mixer RUN-NONINTERACTIVE
                                    image
                                    red
                                    FALSE
                                     1.0  0.0  0.0
                                     0.0  0.0  0.0
                                     0.0  0.0  0.0 )
      (set! green (multiply-layers image (list orig-layer red-matte blue-matte)))
      (plug-in-colors-channel-mixer RUN-NONINTERACTIVE
                                    image
                                    green
                                    FALSE
                                     0.0  0.0  0.0
                                     0.0  1.0  0.0
                                     0.0  0.0  0.0 )
      (gimp-layer-set-mode green ADDITION-MODE)
      (gimp-image-merge-down image green EXPAND-AS-NECESSARY)
      (set! blue (multiply-layers image (list orig-layer red-matte green-matte)))
      (plug-in-colors-channel-mixer RUN-NONINTERACTIVE
                                    image
                                    blue
                                    FALSE
                                     0.0  0.0  0.0
                                     0.0  0.0  0.0
                                     0.0  0.0  1.0 )
      (gimp-layer-set-mode blue ADDITION-MODE)
      (set! tmp-layer (car (gimp-image-merge-down image blue EXPAND-AS-NECESSARY)))
      (gimp-drawable-set-name tmp-layer "Technicolor")
      )
    (gimp-image-remove-layer image red-matte)
    (gimp-image-remove-layer image green-matte)
    (gimp-image-remove-layer image blue-matte)
    (gimp-selection-load orig-sel)
    (when (zero? (car (gimp-selection-is-empty image)))
      (gimp-selection-invert image)
      (gimp-edit-clear tmp-layer) )
    (set! orig-layer (car (gimp-image-merge-down image tmp-layer EXPAND-AS-NECESSARY)))
    (gimp-selection-load orig-sel)
    (gimp-drawable-set-visible orig-layer orig-vis)
    (gimp-image-remove-channel image orig-sel)
    (gimp-image-undo-group-end image)
    (gimp-displays-flush)                                        
    )
  )
  
(script-fu-register "script-fu-sg-technicolor-3"
		    "3 Strip Technicolor"
		    "Simulate color crosstalk as in Technicolor 3-strip process"
		    "Saul Goode"
		    "Saul Goode"
		    "April 2011"
		    "RGB*"
		    SF-IMAGE "Image" 0
		    SF-DRAWABLE "Drawable" 0
		    )

(script-fu-menu-register "script-fu-sg-technicolor-3"
  "<Image>/Filters/Artistic" )

  
  
  
  
  
