home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 February / PCWorld_2001-02_cd.bin / Software / Topware / gimp / gimp-setup-20001226.exe / Main / fade-outline.scm < prev    next >
Encoding:
GIMP Script-Fu Script  |  2000-12-27  |  7.7 KB  |  204 lines

  1. ; fade-outline.scm
  2. ; version 1.1.11a
  3. ;
  4. ; This GIMP script_fu operates on a single Layer
  5. ; It blends the outline boarder from one to another transparency level
  6. ; by adding a layer_mask that follows the shape of the selection.
  7. ; usually from 100% (white is full opaque) to 0% (black is full transparent)
  8. ;
  9. ; The user can specify the thickness of the fading border
  10. ; that is used to blend from transparent to opaque
  11. ; and the Fading direction (shrink or grow).
  12. ;
  13. ; The outline is taken from the current selection
  14. ; or from the layers alpha channel if no selection is active.
  15. ;
  16. ; Optional you may keep the generated layermask or apply
  17. ; it to the layer 
  18.  
  19. ;
  20. ; The GIMP -- an image manipulation program
  21. ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
  22. ; This program is free software; you can redistribute it and/or modify
  23. ; it under the terms of the GNU General Public License as published by
  24. ; the Free Software Foundation; either version 2 of the License, or
  25. ; (at your option) any later version.
  26. ; This program is distributed in the hope that it will be useful,
  27. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  29. ; GNU General Public License for more details.
  30. ; You should have received a copy of the GNU General Public License
  31. ; along with this program; if not, write to the Free Software
  32. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33.  
  34.  
  35. ; Define the main function:
  36.  
  37. (define (script-fu-fade-outline   inImage
  38.                                   inLayer
  39.                                   inBorderSize
  40.                                   inFadeFrom
  41.                                   inFadeTo
  42.                                   inGrowingSelection
  43.                                   inApplyMask
  44.                                   inClearUnselected
  45.         )
  46.  
  47.         (let* ((l-idx 0)
  48.                (l-old-bg-color (car (gimp-palette-get-background)))
  49.                (l-has-selection TRUE)
  50.               )
  51.               
  52.     ; check Fade from and To Values (and force values from 0% to 100%)
  53.     (if (> inFadeFrom 100) (begin (set! inFadeFrom 100 )) )
  54.     (if (< inFadeFrom 0)   (begin (set! inFadeFrom 0 )) )
  55.     (if (> inFadeTo 100) (begin (set! inFadeTo 100 )) )
  56.     (if (< inFadeTo 0)   (begin (set! inFadeTo 0 )) )
  57.          
  58.     (set! l-from-gray (* inFadeFrom 255))
  59.     (set! l-to-gray (* inFadeTo 255))
  60.         (set! l-step (/  (- l-from-gray l-to-gray) (+ inBorderSize 1)))
  61.         (set! l-gray l-to-gray)
  62.  
  63.         ; do nothing if the layer is a layer mask
  64.         (if (= (car (gimp-drawable-is-layer-mask inLayer)) 0)
  65.             (begin
  66.  
  67.               (gimp-undo-push-group-start inImage)
  68.               
  69.               ; if the layer has no alpha add alpha channel
  70.               (if (= (car (gimp-drawable-has-alpha inLayer)) FALSE)
  71.                   (begin
  72.                      (gimp-layer-add-alpha inLayer)
  73.                   )
  74.                )
  75.  
  76.              ; if the layer is the floating selection convert to normal layer
  77.               ; because floating selection cant have a layer mask
  78.               (if (> (car (gimp-layer-is-floating-sel inLayer)) 0)
  79.                   (begin
  80.                      (gimp-floating-sel-to-layer inLayer)
  81.                   )
  82.                )
  83.  
  84.               ; if there is no selection we use the layers alpha channel
  85.               (if (= (car (gimp-selection-is-empty inImage)) TRUE)
  86.                   (begin
  87.                      (set! l-has-selection FALSE)
  88.                      (gimp-selection-layer-alpha inLayer)
  89.                   )
  90.                )
  91.  
  92.                ;
  93.               (gimp-selection-sharpen inImage)
  94.  
  95.                ; apply the existing mask before creating a new one
  96.               (gimp-image-remove-layer-mask inImage inLayer 0)
  97.  
  98.               (if (= inClearUnselected  TRUE)
  99.                   (begin
  100.                     (set! l-mask (car (gimp-layer-create-mask inLayer BLACK-MASK)))
  101.                    )
  102.                   (begin
  103.                     (set! l-mask (car (gimp-layer-create-mask inLayer WHITE-MASK)))
  104.                   )
  105.                )
  106.  
  107.               (gimp-image-add-layer-mask inImage inLayer l-mask)
  108.  
  109.               (if (= inGrowingSelection  TRUE)
  110.                   (begin
  111.                 (set! l-gray l-from-gray)
  112.                 (set! l-from-gray l-to-gray)
  113.                     (set! l-to-gray l-gray)
  114.                     (set! l-step (/  (- l-from-gray l-to-gray) (+ inBorderSize 1)))
  115.                     (set! l-orig-selection  (car (gimp-selection-save inImage)))
  116.                     (gimp-selection-invert inImage)
  117.                   )
  118.                )
  119.                                            
  120.               (while (<= l-idx inBorderSize)
  121.                  (if (= l-idx inBorderSize)
  122.                      (begin
  123.                         (set! l-gray l-from-gray)
  124.                       )
  125.                   )
  126.                   (gimp-palette-set-background (list (/ l-gray 100) (/ l-gray 100) (/ l-gray 100)))
  127.                   (gimp-edit-fill l-mask BG-IMAGE-FILL)
  128.                   (set! l-idx (+ l-idx 1))
  129.                   (set! l-gray (+ l-gray l-step))
  130.                   (gimp-selection-shrink inImage 1)
  131.                   ; check if selection has shrinked to none
  132.                   (if (= (car (gimp-selection-is-empty inImage)) TRUE)
  133.                       (begin
  134.                          (set! l-idx (+ inBorderSize 100))     ; break the while loop
  135.                       )
  136.                    )
  137.  
  138.               )
  139.               
  140.               (if (= inGrowingSelection  TRUE)
  141.                   (begin
  142.                     (gimp-selection-load l-orig-selection)
  143.                     (gimp-palette-set-background (list (/ l-to-gray 100) (/ l-to-gray 100) (/ l-to-gray 100)))
  144.                     (gimp-edit-fill l-mask BG-IMAGE-FILL)
  145.                     (gimp-selection-grow inImage inBorderSize)
  146.                     (gimp-selection-invert inImage)
  147.                 (if (= inClearUnselected  TRUE)
  148.                     (begin
  149.                           ;(gimp-palette-set-background (list (/ l-from-gray 100) (/ l-from-gray 100) (/ l-from-gray 100)))
  150.                           (gimp-palette-set-background (list 0 0 0))
  151.                      )
  152.                     (begin
  153.                           (gimp-palette-set-background (list 255 255 255))
  154.                     )
  155.                  )
  156.                     (gimp-edit-fill l-mask BG-IMAGE-FILL)
  157.                     (gimp-image-remove-channel inImage l-orig-selection)
  158.                   )
  159.                )
  160.  
  161.               (if (=  inApplyMask TRUE)
  162.                   (begin
  163.                     (gimp-image-remove-layer-mask inImage inLayer 0)
  164.                   )
  165.                )
  166.  
  167.               (if (= l-has-selection FALSE)
  168.                   (gimp-selection-none inImage)
  169.               )
  170.  
  171.              (gimp-palette-set-background l-old-bg-color)
  172.              (gimp-undo-push-group-end inImage)
  173.              (gimp-displays-flush)
  174.              )
  175.         ))
  176. )
  177.  
  178.  
  179.  
  180.  
  181. ; Register the function with the GIMP:
  182. ; ------------------------------------
  183.  
  184. (script-fu-register
  185.     "script-fu-fade-outline"
  186.     _"<Image>/Script-Fu/Selection/Fade Outline..."
  187.     "Blend the Layers outline border from one alpha value (opaque) to another (transparent) by generating a Layermask"
  188.     "Wolfgang Hofer <hof@hotbot.com>"
  189.     "Wolfgang Hofer"
  190.     "10 Nov 1999"
  191.     "RGB* GRAY*"
  192.     SF-IMAGE "The Image" 0
  193.     SF-DRAWABLE "The Layer" 0
  194.     SF-ADJUSTMENT _"Border Size" '(10 1 300 1 10 0 1)
  195.     SF-ADJUSTMENT _"Fade From %" '(100 0 100 1 10 0 0)
  196.     SF-ADJUSTMENT _"Fade To   %" '(0 0 100 1 10 0 0)
  197.     SF-TOGGLE _"Use Growing Selection" FALSE
  198.     SF-TOGGLE _"Apply Generated Layermask" FALSE
  199.     SF-TOGGLE _"Clear Unselected Maskarea" TRUE
  200. )
  201.