;  CARVE-IT
;   Carving, embossing, & stamping
;   Process taken from "The Photoshop 3 WOW! Book"
;   http://www.peachpit.com
;   This script requires a grayscale image containing a single layer.
;   This layer is used as the mask for the carving effect
;   NOTE: This script requires the image to be carved to either be an
;   RGB color or grayscale image with a single layer. An indexed file
;   can not be used due to the use of pika-drawable-histogram and
;   pika-drawable-levels.


(define (carve-scale val scale)
  (* (sqrt val) scale))

(define (calculate-inset-gamma img layer)
  (let* ((stats (pika-drawable-histogram layer 0 0.0 1.0))
         (mean (car stats)))
    (cond ((< mean 127) (+ 1.0 (* 0.5 (/ (- 127 mean) 127.0))))
          ((>= mean 127) (- 1.0 (* 0.5 (/ (- mean 127) 127.0)))))))


(define (copy-layer-carve-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)
  )
)



(define (script-fu-carve-it mask-img mask-drawable bg-layer carve-white)
  (let* (
        (width (car (pika-drawable-get-width mask-drawable)))
        (height (car (pika-drawable-get-height mask-drawable)))
        (type (car (pika-drawable-type bg-layer)))
        (img (car (pika-image-new width height (cond ((= type RGB-IMAGE) RGB)
                                                     ((= type RGBA-IMAGE) RGB)
                                                     ((= type GRAY-IMAGE) GRAY)
                                                     ((= type GRAYA-IMAGE) GRAY)
                                                     ((= type INDEXED-IMAGE) INDEXED)
                                                     ((= type INDEXEDA-IMAGE) INDEXED)))))
        (size (min width height))
        (offx (carve-scale size 0.33))
        (offy (carve-scale size 0.25))
        (feather (carve-scale size 0.3))
        (brush-size (carve-scale size 0.3))
        (brush-name (car (pika-brush-new "Carve It")))
        (mask (car (pika-channel-new img width height "Engraving Mask" 50 '(0 0 0))))
        (inset-gamma (calculate-inset-gamma (car (pika-item-get-image bg-layer)) bg-layer))
        (mask-fat 0)
        (mask-emboss 0)
        (mask-highlight 0)
        (mask-shadow 0)
        (shadow-layer 0)
        (highlight-layer 0)
        (cast-shadow-layer 0)
        (csl-mask 0)
        (inset-layer 0)
        (il-mask 0)
        (bg-width (car (pika-drawable-get-width bg-layer)))
        (bg-height (car (pika-drawable-get-height bg-layer)))
        (bg-type (car (pika-drawable-type bg-layer)))
        (bg-image (car (pika-item-get-image bg-layer)))
        (layer1 (car (pika-layer-new img bg-width bg-height bg-type "Layer1" 100 LAYER-MODE-NORMAL)))
        )

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

    (pika-image-undo-disable img)

    (pika-image-insert-layer img layer1 0 0)

    (pika-selection-all img)
    (pika-drawable-edit-clear layer1)
    (pika-selection-none img)
    (copy-layer-carve-it img layer1 bg-image bg-layer)

    (pika-edit-copy 1 (vector mask-drawable))
    (pika-image-insert-channel img mask -1 0)

    (plug-in-tile RUN-NONINTERACTIVE img 1 (vector layer1) width height FALSE)
    (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))

    (set! mask-fat (car (pika-channel-copy mask)))
    (pika-image-insert-channel img mask-fat -1 0)
    (pika-image-select-item img CHANNEL-OP-REPLACE mask-fat)

    (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 mask-fat)
    (pika-selection-none img)

    (set! mask-emboss (car (pika-channel-copy mask-fat)))
    (pika-image-insert-channel img mask-emboss -1 0)
    (plug-in-gauss-rle RUN-NONINTERACTIVE img mask-emboss feather TRUE TRUE)
    (plug-in-emboss RUN-NONINTERACTIVE img mask-emboss 315.0 45.0 7 TRUE)

    (pika-context-set-background '(180 180 180))
    (pika-image-select-item img CHANNEL-OP-REPLACE mask-fat)
    (pika-selection-invert img)
    (pika-drawable-edit-fill mask-emboss FILL-BACKGROUND)
    (pika-image-select-item img CHANNEL-OP-REPLACE mask)
    (pika-drawable-edit-fill mask-emboss FILL-BACKGROUND)
    (pika-selection-none img)

    (set! mask-highlight (car (pika-channel-copy mask-emboss)))
    (pika-image-insert-channel img mask-highlight -1 0)
    (pika-drawable-levels mask-highlight 0
			  0.7056 1.0 TRUE
			  1.0
			  0.0 1.0 TRUE)

    (set! mask-shadow mask-emboss)
    (pika-drawable-levels mask-shadow 0
			  0.0 0.70586 TRUE
			  1.0
			  0.0 1.0 TRUE)

    (pika-edit-copy 1 (vector mask-shadow))
    (let* (
           (pasted (pika-edit-paste layer1 FALSE))
           (num-pasted (car pasted))
           (floating-sel (aref (cadr pasted) (- num-pasted 1)))
          )
          (set! shadow-layer floating-sel)
          (pika-floating-sel-to-layer shadow-layer)
    )
    (pika-layer-set-mode shadow-layer LAYER-MODE-MULTIPLY)

    (pika-edit-copy 1 (vector mask-highlight))
    (let* (
           (pasted (pika-edit-paste shadow-layer FALSE))
           (num-pasted (car pasted))
           (floating-sel (aref (cadr pasted) (- num-pasted 1)))
          )
          (set! highlight-layer floating-sel)
          (pika-floating-sel-to-layer highlight-layer)
    )
    (pika-layer-set-mode highlight-layer LAYER-MODE-SCREEN)

    (pika-edit-copy 1 (vector mask))
    (let* (
           (pasted (pika-edit-paste highlight-layer FALSE))
           (num-pasted (car pasted))
           (floating-sel (aref (cadr pasted) (- num-pasted 1)))
          )
          (set! cast-shadow-layer floating-sel)
          (pika-floating-sel-to-layer cast-shadow-layer)
    )
    (pika-layer-set-mode cast-shadow-layer LAYER-MODE-MULTIPLY)
    (pika-layer-set-opacity cast-shadow-layer 75)

    (plug-in-gauss-rle RUN-NONINTERACTIVE img cast-shadow-layer feather TRUE TRUE)
    (pika-item-transform-translate cast-shadow-layer offx offy)

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

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

    (set! il-mask (car (pika-layer-create-mask inset-layer ADD-MASK-BLACK)))
    (pika-layer-add-mask inset-layer il-mask)
    (pika-image-select-item img CHANNEL-OP-REPLACE mask)
    (pika-context-set-background '(255 255 255))
    (pika-drawable-edit-fill il-mask FILL-BACKGROUND)
    (pika-selection-none img)
    (pika-selection-none bg-image)
    (pika-drawable-levels inset-layer 0 0.0 1.0 TRUE inset-gamma 0.0 1.0 TRUE)
    (pika-image-remove-channel img mask)
    (pika-image-remove-channel img mask-fat)
    (pika-image-remove-channel img mask-highlight)
    (pika-image-remove-channel img mask-shadow)

    (pika-item-set-name layer1 _"Carved Surface")
    (pika-item-set-name shadow-layer _"Bevel Shadow")
    (pika-item-set-name highlight-layer _"Bevel Highlight")
    (pika-item-set-name cast-shadow-layer _"Cast Shadow")
    (pika-item-set-name inset-layer _"Inset")

    (pika-brush-delete brush-name)

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

    (pika-context-pop)
  )
)

(script-fu-register "script-fu-carve-it"
    _"Stencil C_arve..."
    _"Use the specified drawable as a stencil to carve from the specified image."
    "Spencer Kimball"
    "Spencer Kimball"
    "1997"
    "GRAY"
    SF-IMAGE     "Mask image"        0
    SF-DRAWABLE  "Mask drawable"     0
    SF-DRAWABLE _"Image to carve"    0
    SF-TOGGLE   _"Carve white areas" TRUE
)

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