;  CHROME-IT
;   State of the art chrome effect for user-specified mask
;   This script requires a grayscale image containing a single layer.
;   This layer is used as the mask for the SOTA chrome effect

(define (script-fu-sota-chrome-it mask-img mask-drawable chrome-saturation
         chrome-lightness chrome-factor env-map hc cc carve-white)

  (define (set-pt a index x y)
    (begin
      (aset a (* index 2) x)
      (aset a (+ (* index 2) 1) y)
    )
  )

  (define (spline-chrome-it)
    (let* ((a (cons-array 18 'double)))
      (set-pt a 0 0.0   0.0)
      (set-pt a 1 0.125 0.9216)
      (set-pt a 2 0.25  0.0902)
      (set-pt a 3 0.375 0.9020)
      (set-pt a 4 0.5   0.0989)
      (set-pt a 5 0.625 0.9549)
      (set-pt a 6 0.75  00784)
      (set-pt a 7 0.875 0.9412)
      (set-pt a 8 1.0   0.1216)
      a
    )
  )


  (define (shadows val)
    (/ (* 0.96 val) 2.55)
  )

  (define (midtones val)
    (/ val 2.55)
  )

  (define (highlights val)
    ; The result is used as "pika-drawable-color-balance" color parameter
    ; and thus must be restricted to -100.0 <= highlights <= 100.0.
    (min (/ (* 1.108 val) 2.55) 100.0)
  )

  (define (rval col)
    (car col)
  )

  (define (gval col)
    (cadr col)
  )

  (define (bval col)
    (caddr col)
  )

  (define (sota-scale val scale chrome-factor)
    (* (sqrt val) (* scale chrome-factor))
  )

  (define (copy-layer-chrome-it dest-image dest-drawable source-image source-drawable)
    (pika-selection-all dest-image)
    (pika-drawable-edit-clear dest-drawable)
    (pika-selection-none dest-image)
    (pika-selection-all source-image)
    (pika-edit-copy 1 (vector source-drawable))
    (let* (
           (pasted (pika-edit-paste dest-drawable FALSE))
           (num-pasted (car pasted))
           (floating-sel (aref (cadr pasted) (- num-pasted 1)))
          )
     (pika-floating-sel-anchor floating-sel)
    )
  )

  (let* (
        (banding-img (car (pika-file-load RUN-NONINTERACTIVE env-map)))
        (banding-layer (aref (cadr (pika-image-get-selected-drawables banding-img)) 0))
        (banding-height (car (pika-drawable-get-height banding-layer)))
        (banding-width (car (pika-drawable-get-width banding-layer)))
        (banding-type (car (pika-drawable-type banding-layer)))
        (width (car (pika-drawable-get-width mask-drawable)))
        (height (car (pika-drawable-get-height mask-drawable)))
        (img (car (pika-image-new width height GRAY)))
        (size (min width height))
        (offx1 (sota-scale size 0.33 chrome-factor))
        (offy1 (sota-scale size 0.25 chrome-factor))
        (offx2 (sota-scale size (- 0.33) chrome-factor))
        (offy2 (sota-scale size (- 0.25) chrome-factor))
        (feather (sota-scale size 0.5 chrome-factor))
        (brush-size (sota-scale size 0.5 chrome-factor))
        (brush-name (car (pika-brush-new "Chrome It")))
        (mask (car (pika-channel-new img width height "Chrome Stencil" 50 '(0 0 0))))
        (bg-layer (car (pika-layer-new img width height GRAY-IMAGE _"Background" 100 LAYER-MODE-NORMAL)))
        (layer1 (car (pika-layer-new img banding-width banding-height banding-type _"Layer 1" 100 LAYER-MODE-NORMAL)))
        (layer2 (car (pika-layer-new img width height GRAYA-IMAGE _"Layer 2" 100 LAYER-MODE-DIFFERENCE)))
        (layer3 (car (pika-layer-new img width height GRAYA-IMAGE _"Layer 3" 100 LAYER-MODE-NORMAL)))
        (shadow (car (pika-layer-new img width height GRAYA-IMAGE _"Drop Shadow" 100 LAYER-MODE-NORMAL)))
        (layer-mask 0)
        )

    (pika-context-push)
    (pika-context-set-defaults)

    (pika-image-undo-disable img)

    (pika-image-insert-channel img mask -1 0)
    (pika-image-insert-layer img bg-layer 0 0)
    (pika-image-insert-layer img shadow 0 0)
    (pika-image-insert-layer img layer3 0 0)
    (pika-image-insert-layer img layer2 0 0)

    (pika-edit-copy 1 (vector mask-drawable))

    ; Clipboard is copy of mask-drawable.  Paste into mask, a channel, and anchor it.
    (let* (
           (pasted (pika-edit-paste mask FALSE))
           (num-pasted (car pasted))
           (floating-sel (aref (cadr pasted) (- num-pasted 1)))
          )
     (pika-floating-sel-anchor floating-sel)
    )

    (if (= carve-white FALSE)
        (pika-drawable-invert mask FALSE)
    )

    (pika-context-set-background '(255 255 255))
    (pika-selection-none img)
    (pika-drawable-edit-fill layer2 FILL-BACKGROUND)
    (pika-drawable-edit-fill layer3 FILL-BACKGROUND)
    (pika-drawable-edit-clear shadow)

    (pika-item-set-visible bg-layer FALSE)
    (pika-item-set-visible shadow FALSE)

    (pika-image-select-item img CHANNEL-OP-REPLACE mask)
    (pika-context-set-background '(0 0 0))
    (pika-selection-translate img offx1 offy1)
    (pika-selection-feather img feather)
    (pika-drawable-edit-fill layer2 FILL-BACKGROUND)
    (pika-selection-translate img (* 2 offx2) (* 2 offy2))
    (pika-drawable-edit-fill layer3 FILL-BACKGROUND)
    (pika-selection-none img)
    (set! layer2 (car (pika-image-merge-visible-layers img CLIP-TO-IMAGE)))
    (pika-drawable-invert layer2 FALSE)

    (pika-image-insert-layer img layer1 0 0)
    (copy-layer-chrome-it img layer1 banding-img banding-layer)
    (pika-image-delete banding-img)
    (pika-layer-scale layer1 width height FALSE)
    (plug-in-gauss-iir RUN-NONINTERACTIVE img layer1 10 TRUE TRUE)
    (pika-layer-set-opacity layer1 50)
    (set! layer1 (car (pika-image-merge-visible-layers img CLIP-TO-IMAGE)))
    (pika-drawable-curves-spline layer1 HISTOGRAM-VALUE 18 (spline-chrome-it))

    (set! layer-mask (car (pika-layer-create-mask layer1 ADD-MASK-BLACK)))
    (pika-layer-add-mask layer1 layer-mask)
    (pika-image-select-item img CHANNEL-OP-REPLACE mask)
    (pika-context-set-background '(255 255 255))
    (pika-drawable-edit-fill layer-mask FILL-BACKGROUND)

    (set! layer2 (car (pika-layer-copy layer1 TRUE)))
    (pika-image-insert-layer img layer2 0 0)

    (pika-brush-set-shape brush-name BRUSH-GENERATED-CIRCLE)
    (pika-brush-set-spikes brush-name 2)
    (pika-brush-set-hardness brush-name 1.0)
    (pika-brush-set-spacing brush-name 25)
    (pika-brush-set-aspect-ratio brush-name 1)
    (pika-brush-set-angle brush-name 0)
    (cond (<= brush-size 17) (pika-brush-set-radius brush-name (\ brush-size 2))
	  (else pika-brush-set-radius brush-name (\ 19 2)))
    (pika-context-set-brush brush-name)

    (pika-context-set-foreground '(255 255 255))
    (pika-drawable-edit-stroke-selection layer-mask)

    (pika-context-set-background '(0 0 0))
    (pika-selection-feather img (* feather 1.5))
    (pika-selection-translate img (* 2.5 offx1) (* 2.5 offy1))
    (pika-drawable-edit-fill shadow FILL-BACKGROUND)

    (pika-selection-all img)
    (pika-context-set-pattern "Marble #1")
    (pika-drawable-edit-fill bg-layer FILL-PATTERN)
    (pika-selection-none img)

    (pika-image-convert-rgb img)

    (pika-drawable-color-balance layer1 TRANSFER-SHADOWS TRUE
				 (shadows (rval hc))
				 (shadows (gval hc))
				 (shadows (bval hc)))
    (pika-drawable-color-balance layer1 TRANSFER-MIDTONES TRUE
				 (midtones (rval hc))
				 (midtones (gval hc))
				 (midtones (bval hc)))
    (pika-drawable-color-balance layer1 TRANSFER-HIGHLIGHTS TRUE
				 (highlights (rval hc))
				 (highlights (gval hc))
				 (highlights (bval hc)))

    (pika-drawable-color-balance layer2 TRANSFER-SHADOWS TRUE
				 (shadows (rval cc))
				 (shadows (gval cc))
				 (shadows (bval cc)))
    (pika-drawable-color-balance layer2 TRANSFER-MIDTONES TRUE
				 (midtones (rval cc))
				 (midtones (gval cc))
				 (midtones (bval cc)))
    (pika-drawable-color-balance layer2 TRANSFER-HIGHLIGHTS TRUE
				 (highlights (rval cc))
				 (highlights (gval cc))
				 (highlights (bval cc)))
    (pika-drawable-hue-saturation layer2 HUE-RANGE-ALL
				  0.0
				  chrome-lightness
				  chrome-saturation
				  0.0)

    (pika-item-set-visible shadow TRUE)
    (pika-item-set-visible bg-layer TRUE)

    (pika-item-set-name layer2 _"Chrome")
    (pika-item-set-name layer1 _"Highlight")

    (pika-image-remove-channel img mask)

    (pika-brush-delete brush-name)

    (pika-display-new img)
    (pika-image-undo-enable img)

    (pika-context-pop)
  )
)

(script-fu-register "script-fu-sota-chrome-it"
  _"Stencil C_hrome..."
  _"Add a chrome effect to the selected region (or alpha) using a specified (grayscale) stencil"
  "Spencer Kimball"
  "Spencer Kimball"
  "1997"
  "GRAY"
  SF-IMAGE       "Chrome image"      0
  SF-DRAWABLE    "Chrome mask"       0
  SF-ADJUSTMENT _"Chrome saturation" '(-80 -100 100 1 10 0 0)
  SF-ADJUSTMENT _"Chrome lightness"  '(-47 -100 100 1 10 0 0)
  SF-ADJUSTMENT _"Chrome factor"     '(0.75 0 1 0.1 0.2 2 0)
  SF-FILENAME   _"Environment map"
                (string-append pika-data-directory
                              "/scripts/images/beavis.jpg")
  SF-COLOR      _"Highlight balance"  '(211 95 0)
  SF-COLOR      _"Chrome balance"     "black"
  SF-TOGGLE     _"Chrome white areas" TRUE
)

(script-fu-menu-register "script-fu-sota-chrome-it"
                         "<Image>/Filters/Decor")
