home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / Topware / gimp / gimp-setup-20001226.exe / Main / drop-shadow.scm < prev    next >
Encoding:
GIMP Script-Fu Script  |  2000-12-27  |  5.8 KB  |  173 lines

  1. ; The GIMP -- an image manipulation program
  2. ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. ; This program is free software; you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public License as published by
  5. ; the Free Software Foundation; either version 2 of the License, or
  6. ; (at your option) any later version.  
  7. ; This program is distributed in the hope that it will be useful,
  8. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. ; GNU General Public License for more details.
  11. ; You should have received a copy of the GNU General Public License
  12. ; along with this program; if not, write to the Free Software
  13. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. ;
  15. ;
  16. ; drop-shadow.scm   version 1.04   1999/12/21 
  17. ;
  18. ; CHANGE-LOG:
  19. ; 1.00 - initial release
  20. ; 1.01 - fixed the problem with a remaining copy of the selection
  21. ; 1.02 - some code cleanup, no real changes
  22. ; 1.03 - can't call gimp-edit-fill until layer is added to image!
  23. ;
  24. ;
  25. ; Copyright (C) 1997-1999 Sven Neumann <sven@gimp.org>
  26. ;  
  27. ; Adds a drop-shadow of the current selection or alpha-channel. 
  28. ;
  29. ; This script is derived from my script add-shadow, which has become 
  30. ; obsolete now. Thanks to Andrew Donkin (ard@cs.waikato.ac.nz) for his 
  31. ; idea to add alpha-support to add-shadow.
  32.  
  33.   
  34. (define (script-fu-drop-shadow image
  35.                    drawable
  36.                    shadow-transl-x
  37.                    shadow-transl-y
  38.                    shadow-blur
  39.                    shadow-color
  40.                    shadow-opacity
  41.                    allow-resize)
  42.   (let* ((shadow-blur (max shadow-blur 0))
  43.      (shadow-opacity (min shadow-opacity 100))
  44.      (shadow-opacity (max shadow-opacity 0))
  45.      (type (car (gimp-drawable-type-with-alpha drawable)))
  46.      (image-width (car (gimp-image-width image)))
  47.      (image-height (car (gimp-image-height image)))
  48.      (old-bg (car (gimp-palette-get-background)))
  49.      (from-selection 0)
  50.      (active-selection 0)
  51.      (shadow-layer 0))
  52.  
  53.   (gimp-undo-push-group-start image)
  54.   
  55.   (gimp-layer-add-alpha drawable)
  56.   (if (= (car (gimp-selection-is-empty image)) TRUE)
  57.       (begin
  58.     (gimp-selection-layer-alpha drawable)
  59.     (set! from-selection FALSE))
  60.       (begin
  61.     (set! from-selection TRUE)
  62.     (set! active-selection (car (gimp-selection-save image)))))
  63.   
  64.   (let* ((selection-bounds (gimp-selection-bounds image))
  65.      (select-offset-x (cadr selection-bounds))
  66.      (select-offset-y (caddr selection-bounds))
  67.      (select-width (- (cadr (cddr selection-bounds)) select-offset-x))
  68.      (select-height (- (caddr (cddr selection-bounds)) select-offset-y))
  69.   
  70.      (shadow-width (+ select-width (* 2 shadow-blur)))
  71.      (shadow-height (+ select-height (* 2 shadow-blur)))
  72.  
  73.      (shadow-offset-x (- select-offset-x shadow-blur))
  74.      (shadow-offset-y (- select-offset-y shadow-blur)))
  75.  
  76.     (if (= allow-resize TRUE)
  77.     (let* ((new-image-width image-width)
  78.            (new-image-height image-height)
  79.            (image-offset-x 0)
  80.            (image-offset-y 0))
  81.  
  82.       (if (< (+ shadow-offset-x shadow-transl-x) 0)
  83.           (begin
  84.         (set! image-offset-x (- 0 (+ shadow-offset-x
  85.                          shadow-transl-x)))
  86.         (set! shadow-offset-x (- 0 shadow-transl-x))
  87.         (set! new-image-width (- new-image-width image-offset-x))))
  88.  
  89.       (if (< (+ shadow-offset-y shadow-transl-y) 0)
  90.           (begin
  91.         (set! image-offset-y (- 0 (+ shadow-offset-y
  92.                          shadow-transl-y)))
  93.         (set! shadow-offset-y (- 0 shadow-transl-y))
  94.         (set! new-image-height (- new-image-height image-offset-y))))
  95.  
  96.       (if (> (+ (+ shadow-width shadow-offset-x) shadow-transl-x)
  97.          new-image-width)
  98.           (set! new-image-width
  99.             (+ (+ shadow-width shadow-offset-x) shadow-transl-x)))
  100.  
  101.       (if (> (+ (+ shadow-height shadow-offset-y) shadow-transl-y)
  102.          new-image-height)
  103.           (set! new-image-height
  104.             (+ (+ shadow-height shadow-offset-y) shadow-transl-y)))
  105.  
  106.       (gimp-image-resize image
  107.                  new-image-width
  108.                  new-image-height
  109.                  image-offset-x
  110.                  image-offset-y)))
  111.  
  112.  
  113.     (set! shadow-layer (car (gimp-layer-new image
  114.                         shadow-width
  115.                         shadow-height
  116.                         type
  117.                         "Drop-Shadow"
  118.                         shadow-opacity
  119.                         NORMAL)))
  120.    (gimp-image-add-layer image shadow-layer -1)
  121.     (gimp-layer-set-offsets shadow-layer
  122.                 shadow-offset-x
  123.                 shadow-offset-y))
  124.  
  125.   (gimp-drawable-fill shadow-layer TRANS-IMAGE-FILL)
  126.   (gimp-palette-set-background shadow-color)
  127.   (gimp-edit-fill shadow-layer BG-IMAGE-FILL)
  128.   (gimp-selection-none image)
  129.   (gimp-layer-set-preserve-trans shadow-layer FALSE)
  130.   (if (>= shadow-blur 1.0) (plug-in-gauss-rle 1
  131.                           image
  132.                           shadow-layer
  133.                           shadow-blur
  134.                           TRUE
  135.                           TRUE))
  136.   (gimp-layer-translate shadow-layer shadow-transl-x shadow-transl-y)
  137.  
  138.   (if (= from-selection TRUE)
  139.       (begin
  140.     (gimp-selection-load active-selection)
  141.     (gimp-edit-clear shadow-layer)
  142.     (gimp-image-remove-channel image active-selection)))
  143.  
  144.   (if (and
  145.        (= (car (gimp-layer-is-floating-sel drawable)) 0)
  146.        (= from-selection FALSE))
  147.       (gimp-image-raise-layer image drawable))
  148.  
  149.   (gimp-image-set-active-layer image drawable)
  150.   (gimp-palette-set-background old-bg)
  151.   (gimp-undo-push-group-end image)
  152.   (gimp-displays-flush)))
  153.  
  154. (script-fu-register "script-fu-drop-shadow"
  155.             _"<Image>/Script-Fu/Shadow/Drop-Shadow..."
  156.             "Add a drop-shadow of the current selection or alpha-channel"
  157.             "Sven Neumann <sven@gimp.org>"
  158.             "Sven Neumann"
  159.             "1999/12/21"
  160.             "RGB* GRAY*"
  161.             SF-IMAGE "Image" 0
  162.             SF-DRAWABLE "Drawable" 0
  163.             SF-ADJUSTMENT _"Offset X" '(8 -4096 4096 1 10 0 1)
  164.             SF-ADJUSTMENT _"Offset Y" '(8 -4096 4096 1 10 0 1)
  165.             SF-ADJUSTMENT _"Blur Radius" '(15 0 1024 1 10 0 1)
  166.             SF-COLOR      _"Color" '(0 0 0)
  167.             SF-ADJUSTMENT _"Opacity" '(80 0 100 1 10 0 0)
  168.             SF-TOGGLE     _"Allow Resizing" TRUE)
  169.