;;; The GIMP -- an image manipulation program
;;; Copyright (C) 1995 Spencer Kimball and Peter Mattis
;;; 
;;;  Meta-xstitch  script  for GIMP 1.2
;;; Copyright (C) 2002 Lasm <lasm@rocketmail.com>
;;;  http://www.godsimmediatecontact.com
;;;  http://www.godsdirectcontact.org
;;;
;;; meta-xstitch.scm - A color enhancement script
;;;
;;; Welcome to the Meta-Xstitch script.
;;;  This script requires xstitch.scm written by Jeff Trefftzs
;;;  Dedication - to my mother (1917-2002) in loving memory.
;;;
;;; --------------------------------------------------------------------
;;; version 1.0  by Lasm 2002/02/02 <lasm@rocketmail.com>
;;;     - Initial relase
;;;
;;; --------------------------------------------------------------------
;;;
;;; 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.
;;; 
;;; 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.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;   Notes on this script
;;;  1. This script was inspired by Jeff's Cross Stitch script  xstitch.scm
;;   2. By observing the reversible properties of the cross stitch script,
;;       this script produces color enhancement of the original photo
;;   3. This script can be used to enhance lightness, contrast, saturation and to add texture.
;;   
;; Another Grandmother Coffee House production.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;          Set register common information
(set! STYLE-LIST        (list "Light Texture" "Heavy Texture" "Lightness" "Contrast" "Saturation"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;  Helper function to create names of flattened layer
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (meta-xstitch-name stmode)
    (string-append
         "MetaXstitch-"
         (cond 
            ((= stmode 0) "Light Texture")
            ((= stmode 1) "Heavy Texture")
            ((= stmode 2) "Lightness")
            ((= stmode 3) "Contrast")
            ((= stmode 4) "Saturation")))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;  Helper function to create a new layer
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (copylayer layer layername)
  (set! new (car(gimp-layer-copy layer 1))) ; Add an alpha channel
  (gimp-layer-set-name new layername)
  new
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;    Meta-xstitch function:
;;
;;   Requires:
;;        script-fu-xstitch written by Jeff Trefftzs
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (script-fu-meta-xstitch img inLayer flatten? stmode)
  (let*
      (
       (width (car (gimp-drawable-width inLayer)))
       (height (car (gimp-drawable-height inLayer)))
       (color-layer1 (copylayer inLayer "Color Layer 1"))
       (old-fg (car (gimp-palette-get-foreground)))
       (old-bg (car (gimp-palette-get-background)))       
       ) ; end variable definition
	      
  (gimp-undo-push-group-start img)

;; Real work goes in here

      (set! stitch-layer (car (script-fu-xstitch img inLayer '("Circle (01)" 100 25 0) 4 32 "Bears" '(0 0 0) 0 14 14)))
      (gimp-image-merge-visible-layers img EXPAND-AS-NECESSARY)
      (gimp-convert-rgb img)
      (gimp-image-set-active-layer img inLayer)
      (set! stitch-bg (car (gimp-image-flatten img)))
      (set! stitch-layer1 (copylayer  stitch-bg "Xstitch Layer 1"))
      (gimp-image-add-layer img stitch-layer1 -1)
      (gimp-layer-set-mode stitch-layer1 OVERLAY-MODE)
      (set! stitch-layer2 (copylayer stitch-layer1 "Xstitch Layer 2"))
      (gimp-image-add-layer img stitch-layer2 -1)
      (gimp-image-add-layer img color-layer1 -1)
      (gimp-layer-set-mode color-layer1 SCREEN-MODE)
      (set! color-layer2 (copylayer color-layer1 "Color Layer 2"))
      (gimp-image-add-layer img color-layer2 -1)

      (cond
            ((= stmode 0)                       ;; Light Texture
                 (gimp-layer-set-mode color-layer1 DARKEN-ONLY-MODE))
            ((= stmode 1)                       ;; Heavy Texture
                 (gimp-layer-set-mode color-layer1 MULTIPLY-MODE))
            ((= stmode 2)                       ;; Lightness
                 (begin
                 (gimp-layer-set-mode color-layer1 DISSOLVE-MODE)
                 (gimp-layer-set-mode color-layer2 SCREEN-MODE)))
            ((= stmode 3)                       ;; Contrast 
                 (begin
                 (gimp-layer-set-mode color-layer1 DISSOLVE-MODE)
                 (gimp-layer-set-mode color-layer2 OVERLAY-MODE)))
            ((= stmode 4)                       ;; Saturation 
                 (begin
                 (gimp-layer-set-mode color-layer1 DISSOLVE-MODE)
                 (gimp-layer-set-mode color-layer2 MULTIPLY-MODE))))

   (if  (eqv? flatten? TRUE) 
       (begin
       (set! merged-layer
               (car (gimp-image-merge-visible-layers img EXPAND-AS-NECESSARY)))
        (gimp-layer-set-name merged-layer (meta-xstitch-name stmode)))
    )

  (gimp-undo-push-group-end img)
  (gimp-displays-flush)

  (gimp-palette-set-background old-bg)
  (gimp-palette-set-foreground old-fg)

  )  ;; end of let *
)    ;; end of define


(script-fu-register
 "script-fu-meta-xstitch"
 _"<Image>/Script-Fu/Lasm's FX Effects/Enhance/Meta-Xstitch"
 "Lasm's famous Color Enhancement effect for photographs. This works on any RGB image."
 "lasm"
 "Copyright 2002, lasm"
 "Feb 2, 2002"
 "RGB*"
 SF-IMAGE "The Image" 	 0
 SF-DRAWABLE "The Layer" 	 0
 SF-TOGGLE        "Flatten Image"	 TRUE
 SF-OPTION     _"Image Enhancement"    STYLE-LIST
)
