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