home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 June / june_2001.iso / Multimedia / Images / Gimp / setup.exe / Main / perspective-shadow.scm < prev    next >
Encoding:
GIMP Script-Fu Script  |  2000-10-23  |  6.4 KB  |  197 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. ; perspective-shadow.scm   version 1.03   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 - only call blur plugin when blut-radius >= 1.0
  23. ;
  24. ;
  25. ; Copyright (C) 1997-1999 Sven Neumann <sven@gimp.org>
  26. ;  
  27. ; Adds a perspective shadow of the current selection or alpha-channel 
  28. ; as a layer below the active layer
  29. ;
  30.   
  31. (define (script-fu-perspective-shadow image
  32.                       drawable
  33.                       alpha
  34.                       rel-distance
  35.                       rel-length
  36.                       shadow-blur
  37.                       shadow-color
  38.                       shadow-opacity
  39.                       interpolate
  40.                       allow-resize)
  41.   (let* ((shadow-blur (max shadow-blur 0))
  42.      (shadow-opacity (min shadow-opacity 100))
  43.      (shadow-opacity (max shadow-opacity 0))
  44.      (rel-length (abs rel-length))
  45.      (alpha (* (/ alpha 180) *pi*))
  46.      (type (car (gimp-drawable-type-with-alpha drawable)))
  47.      (image-width (car (gimp-image-width image)))
  48.      (image-height (car (gimp-image-height image)))
  49.      (old-bg (car (gimp-palette-get-background)))
  50.      (from-selection 0)
  51.      (active-selection 0)
  52.      (shadow-layer 0))
  53.  
  54.     
  55.   (if (= rel-distance 0) (set! rel-distance 999999))
  56.   (gimp-undo-push-group-start image)
  57.   
  58.   (gimp-layer-add-alpha drawable)
  59.   (if (= (car (gimp-selection-is-empty image)) TRUE)
  60.       (begin
  61.     (gimp-selection-layer-alpha drawable)
  62.     (set! from-selection FALSE))
  63.       (begin
  64.     (set! from-selection TRUE)
  65.     (set! active-selection (car (gimp-selection-save image)))))
  66.   
  67.   (let* ((selection-bounds (gimp-selection-bounds image))
  68.      (select-offset-x (cadr selection-bounds))
  69.      (select-offset-y (caddr selection-bounds))
  70.      (select-width (- (cadr (cddr selection-bounds)) select-offset-x))
  71.      (select-height (- (caddr (cddr selection-bounds)) select-offset-y))
  72.  
  73.      (abs-length (* rel-length select-height))
  74.      (abs-distance (* rel-distance select-height))
  75.      (half-bottom-width (/ select-width 2))
  76.      (half-top-width (* half-bottom-width
  77.               (/ (- rel-distance rel-length) rel-distance)))
  78.   
  79.      (x0 (+ select-offset-x (+ (- half-bottom-width half-top-width)
  80.                  (* (cos alpha) abs-length))))
  81.      (y0 (+ select-offset-y (- select-height
  82.                  (* (sin alpha) abs-length))))
  83.      (x1 (+ x0 (* 2 half-top-width)))
  84.      (y1 y0)
  85.      (x2 select-offset-x)
  86.      (y2 (+ select-offset-y select-height))
  87.      (x3 (+ x2 select-width))
  88.      (y3 y2)
  89.  
  90.      (shadow-width (+ (- (max x1 x3) (min x0 x2)) (* 2 shadow-blur)))
  91.      (shadow-height (+ (- (max y1 y3) (min y0 y2)) (* 2 shadow-blur)))
  92.      (shadow-offset-x (- (min x0 x2) shadow-blur))
  93.      (shadow-offset-y (- (min y0 y2) shadow-blur)))
  94.      
  95.   
  96.     (set! shadow-layer (car (gimp-layer-new image
  97.                         select-width
  98.                         select-height
  99.                         type
  100.                         "Perspective Shadow"
  101.                         shadow-opacity
  102.                         NORMAL)))
  103.     (gimp-layer-set-offsets shadow-layer select-offset-x select-offset-y)
  104.     (gimp-drawable-fill shadow-layer TRANS-IMAGE-FILL)
  105.     (gimp-palette-set-background shadow-color)
  106.     (gimp-edit-fill shadow-layer BG-IMAGE-FILL)
  107.     (gimp-selection-none image)
  108.  
  109.     (if (= allow-resize TRUE)
  110.     (let* ((new-image-width image-width)
  111.            (new-image-height image-height)
  112.            (image-offset-x 0)
  113.            (image-offset-y 0))
  114.  
  115.       (if (< shadow-offset-x 0)
  116.           (begin
  117.         (set! image-offset-x (- 0 shadow-offset-x))
  118.         (set! new-image-width (- new-image-width image-offset-x))))
  119.  
  120.       (if (< shadow-offset-y 0)
  121.           (begin
  122.         (set! image-offset-y (- 0 shadow-offset-y))
  123.         (set! new-image-height (- new-image-height image-offset-y))))
  124.       
  125.       (if (> (+ shadow-width shadow-offset-x) new-image-width)
  126.           (set! new-image-width (+ shadow-width shadow-offset-x)))
  127.       
  128.       (if (> (+ shadow-height shadow-offset-y) new-image-height)
  129.           (set! new-image-height (+ shadow-height shadow-offset-y)))
  130.       (gimp-image-resize image
  131.                  new-image-width
  132.                  new-image-height
  133.                  image-offset-x
  134.                  image-offset-y)))
  135.  
  136.     (gimp-image-add-layer image shadow-layer -1)
  137.   
  138.     (gimp-perspective shadow-layer
  139.               interpolate
  140.               x0 y0
  141.               x1 y1
  142.               x2 y2
  143.               x3 y3)
  144.  
  145.     (if (>= shadow-blur 1.0)
  146.     (begin
  147.       (gimp-layer-set-preserve-trans shadow-layer FALSE)
  148.       (gimp-layer-resize shadow-layer
  149.                  shadow-width
  150.                  shadow-height
  151.                  shadow-blur
  152.                  shadow-blur)
  153.       (plug-in-gauss-rle 1
  154.                  image
  155.                  shadow-layer
  156.                  shadow-blur
  157.                  TRUE
  158.                  TRUE))))
  159.  
  160.   (if (= from-selection TRUE)
  161.       (begin
  162.     (gimp-selection-load active-selection)
  163.     (gimp-edit-clear shadow-layer)
  164.     (gimp-image-remove-channel image active-selection)))
  165.  
  166.   (if (and
  167.        (= (car (gimp-layer-is-floating-sel drawable)) 0)
  168.        (= from-selection FALSE))
  169.       (gimp-image-raise-layer image drawable))
  170.  
  171.   (gimp-image-set-active-layer image drawable)
  172.   (gimp-palette-set-background old-bg)
  173.   (gimp-undo-push-group-end image)
  174.   (gimp-displays-flush)))
  175.  
  176. (script-fu-register "script-fu-perspective-shadow"
  177.             _"<Image>/Script-Fu/Shadow/Perspective..."
  178.             "Add a perspective shadow"
  179.             "Sven Neumann <sven@gimp.org>"
  180.             "Sven Neumann"
  181.             "1999/12/21"
  182.             "RGB* GRAY*"
  183.             SF-IMAGE "Image" 0
  184.             SF-DRAWABLE "Drawable" 0
  185.             SF-ADJUSTMENT _"Angle" '(45 0 180 1 10 1 0)
  186.             SF-ADJUSTMENT _"Relative Distance of Horizon" '(5 0 24 .1 1 1 1)
  187.             SF-ADJUSTMENT _"Relative Length of Shadow" '(1 0 24 .1 1 1 1)
  188.             SF-ADJUSTMENT _"Blur Radius" '(3 0 1024 1 10 0 0)
  189.             SF-COLOR      _"Color" '(0 0 0)
  190.             SF-ADJUSTMENT _"Opacity" '(80 0 100 1 10 0 0)
  191.             SF-TOGGLE     _"Interpolate" TRUE
  192.             SF-TOGGLE     _"Allow Resizing" FALSE)
  193.