; The GIMP -- an image manipulation program 
; Copyright (C) 1995 Spencer Kimball and Peter Mattis
;
; 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 3 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.
; 
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
; http://www.gnu.org/licenses/gpl-3.0.html
; 
; Copyright (C) 2010 elsamuko <elsamuko@web.de>
; Updated by karlhof26 on 04/03/2022


(define (elsamuko-cyanotype aimg adraw
                            color1 color2
                            localcontrast 
                            overlay
                            blackwhite
                            invertimage
                            bluemix)
  (let* (
            (img (car (gimp-item-get-image adraw)))
            (owidth (car (gimp-image-width img)))
            (oheight (car (gimp-image-height img)))
            (tmplayer1 0)         
            (tmplayer2 0)         
            (contrastlayer 0)
            (bw-layer 0)
            (overlay-layer (car (gimp-layer-copy adraw FALSE)))
            (blue-layer (car (gimp-layer-new img
                                          owidth 
                                          oheight
                                          RGBA-IMAGE
                                          "Prussian Blue" 
                                          100 
                                          LAYER-MODE-NORMAL-LEGACY)))
            (blue-mask (car (gimp-layer-create-mask blue-layer ADD-MASK-WHITE)))
            (white-layer (car (gimp-layer-new img
                                           owidth 
                                           oheight
                                           RGBA-IMAGE
                                           "Aged White" 
                                           100 
                                           LAYER-MODE-NORMAL)))
            (white-mask (car (gimp-layer-create-mask white-layer ADD-MASK-WHITE)))
            (blue-overlay-layer (car (gimp-layer-new img
                                                  owidth 
                                                  oheight
                                                  RGBA-IMAGE
                                                  "Blue Overlay" 
                                                  80 
                                                  LAYER-MODE-OVERLAY)))
         )
        
        ; init
        (gimp-context-push)
        (gimp-image-undo-group-start img)
        (if (= (car (gimp-drawable-is-gray adraw )) TRUE)
            (gimp-image-convert-rgb img)
        )
        
        ;invert image
        (if (= invertimage TRUE)
            (begin
                (gimp-drawable-invert adraw FALSE)
            )
        )
        
        ;desaturate original
        (if (= blackwhite TRUE)
            (begin
                (gimp-edit-copy-visible img)
                (set! bw-layer (car (gimp-layer-new-from-visible img img "B/W")))
                (gimp-image-insert-layer img bw-layer 0 -1)
                (gimp-drawable-desaturate bw-layer DESATURATE-LIGHTNESS)
            )
        )
        
        ;enhance local contrast
        (if(> localcontrast 0)
            (begin
                (gimp-edit-copy-visible img)
                (set! tmplayer1 (car (gimp-layer-new-from-visible img img "Temp 1")))
                (set! tmplayer2 (car (gimp-layer-new-from-visible img img "Temp 2")))
                (gimp-image-insert-layer img tmplayer1 0 -1)
                (gimp-image-insert-layer img tmplayer2 0 -1)
                (plug-in-unsharp-mask 1 img tmplayer1 60 localcontrast 0)
                (gimp-layer-set-mode tmplayer2 LAYER-MODE-GRAIN-EXTRACT)
                (gimp-edit-copy-visible img)
                (set! contrastlayer (car (gimp-layer-new-from-visible img img "Local Contrast")))
                (gimp-image-insert-layer img contrastlayer 0 -1)
                (gimp-layer-set-mode contrastlayer LAYER-MODE-GRAIN-MERGE)
                (gimp-image-remove-layer img tmplayer1)
                (gimp-image-remove-layer img tmplayer2)
            )
        )
        
        ;set blue and white layers
        (gimp-image-insert-layer img blue-layer 0 -1)
        (gimp-image-insert-layer img white-layer 0 -1)
        (gimp-selection-all aimg)
        (gimp-context-set-foreground color1)
        (gimp-edit-bucket-fill blue-layer BUCKET-FILL-FG LAYER-MODE-NORMAL 100 0 FALSE 0 0)
        (gimp-context-set-foreground color2)
        (gimp-edit-bucket-fill white-layer BUCKET-FILL-FG LAYER-MODE-NORMAL 100 0 FALSE 0 0)
        
        ;add layer masks
        (gimp-edit-copy adraw)
        (gimp-layer-add-mask blue-layer blue-mask)
        (gimp-floating-sel-anchor (car (gimp-edit-paste blue-mask TRUE)))
        (gimp-drawable-invert blue-mask FALSE)
        (gimp-layer-add-mask white-layer white-mask)
        (gimp-floating-sel-anchor (car (gimp-edit-paste white-mask TRUE)))
        
        ;set overlay layer
        (if(> overlay 0)
            (begin
                (gimp-image-insert-layer img overlay-layer 0 -1)
                (gimp-item-set-name overlay-layer "Overlay")
                (gimp-layer-set-mode overlay-layer LAYER-MODE-OVERLAY)
                (gimp-layer-set-opacity overlay-layer overlay)
                (gimp-drawable-desaturate overlay-layer DESATURATE-LIGHTNESS)
            )
        )
        
        ;set blue overlay layer
        (gimp-image-insert-layer img blue-overlay-layer 0 -1)
        (gimp-selection-all aimg)
        (gimp-context-set-foreground color1)
        (gimp-edit-bucket-fill blue-overlay-layer BUCKET-FILL-FG LAYER-MODE-NORMAL 100 0 FALSE 0 0)
        (gimp-selection-none img)
        
        ;;Bluemix option added by karlhof26
        (cond ((= bluemix 0)
                (gimp-layer-set-mode blue-overlay-layer LAYER-MODE-OVERLAY)
              )
              ((= bluemix 1) 
                (gimp-layer-set-mode blue-overlay-layer LAYER-MODE-HARD-MIX)
              )
              ((= bluemix 2) 
                (gimp-layer-set-mode blue-overlay-layer LAYER-MODE-VIVID-LIGHT)
              )
              ((= bluemix 3) 
                (gimp-layer-set-mode blue-overlay-layer LAYER-MODE-DISSOLVE)
                (gimp-layer-set-opacity blue-overlay-layer 61.0)
              )
        )
        ;(gimp-layer-set-mode blue-overlay-layer LAYER-MODE-HARD-MIX)
        ; tidy up 
        (gimp-image-undo-group-end img)
        (gimp-displays-flush)
        (gimp-context-pop)
  )
)

(script-fu-register "elsamuko-cyanotype"
                    "Cyanotype"
                    "Cyanotype effect from 1842. \n file:elsamuko-cyanotype.scm"
                    "elsamuko <elsamuko@web.de>"
                    "elsamuko"
                    "14/03/10"
                    "RGB*"
                    SF-IMAGE       "Input image"          0
                    SF-DRAWABLE    "Input drawable"       0
                    SF-COLOR       "Prussian Blue"       '( 61  87 136)
                    SF-COLOR       "Aged White"          '(251 253 240)
                    SF-ADJUSTMENT  "Local Contrast"      '(0.4 0   2 0.1 0.2 1 0)
                    SF-ADJUSTMENT  "B/W Overlay"         '(50  0  100  1   5 0 0)
                    SF-TOGGLE      "Desaturate Image"     TRUE
                    SF-TOGGLE      "Invert Image"         TRUE
                    SF-OPTION      "Blue mix option"      '("Overlay" "Hard mix" "Vivid" "Dissolve")
                    )

(script-fu-menu-register "elsamuko-cyanotype" "<Toolbox>/Script-Fu/Artistic")

;end of script