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

  1. ; The GIMP -- an image manipulation program
  2. ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. ;
  4. ; add-bevel.scm version 1.03
  5. ; Time-stamp: <1997-12-16 09:17:24 ard>
  6. ; This program is free software; you can redistribute it and/or modify
  7. ; it under the terms of the GNU General Public License as published by
  8. ; the Free Software Foundation; either version 2 of the License, or
  9. ; (at your option) any later version.
  10. ; This program is distributed in the hope that it will be useful,
  11. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ; GNU General Public License for more details.
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with this program; if not, write to the Free Software
  16. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ;
  18. ; Copyright (C) 1997 Andrew Donkin  (ard@cs.waikato.ac.nz)
  19. ; Contains code from add-shadow.scm by Sven Neumann
  20. ; (neumanns@uni-duesseldorf.de) (thanks Sven).
  21. ;  
  22. ; Adds a bevel to an image.  See http://www.cs.waikato.ac.nz/~ard/gimp/
  23. ;
  24. ; If there is a selection, it is bevelled.
  25. ; Otherwise if there is an alpha channel, the selection is taken from it
  26. ; and bevelled.
  27. ; Otherwise the whole image is bevelled.
  28. ;
  29. ; The selection is set on exit, so Select->Invert then Edit->Clear will
  30. ; leave a cut-out.  Then use Sven's add-shadow for that
  31. ; floating-bumpmapped-texture cliche.
  32.  
  33. ;
  34. ; 1.01: now works on offset layers.
  35. ; 1.02: has crop-pixel-border option to trim one pixel off each edge of the
  36. ;       bevelled image.  Bumpmapping leaves edge pixels unchanged, which
  37. ;       looks bad.  Oddly, this is not apparant in the GIMP - you have to
  38. ;       save the image and load it into another viewer.  First noticed in
  39. ;       Nutscrape.
  40. ;       Changed path (removed "filters/").
  41. ; 1.03: adds one-pixel border before bumpmapping, and removes it after.
  42. ;    Got rid of the crop-pixel-border option (no longer reqd).
  43. ;
  44.  
  45. ;
  46. ; BUMPMAP NOTES:
  47. ;
  48. ; Bumpmap changed arguments from version 2.02 to 2.03.  If you use the
  49. ; wrong bumpmap.c this script will either bomb (good) or produce a
  50. ; naff image (bad).
  51. ;
  52. ; As distributed this script expects bumpmap 2.03 (shipped with Gimp 0.99.11)
  53. ; or later.
  54.  
  55. ;
  56. ; BUGS
  57. ;
  58. ; Doesn't allow undoing the operation.  Why not?
  59. ;
  60. ; Sometimes (and that's the scary bit) gives bogloads of GTK warnings.
  61. ;
  62.  
  63.   
  64. (define (script-fu-add-bevel img
  65.                  drawable
  66.                  thickness
  67.                  work-on-copy
  68.                  keep-bump-layer)
  69.  
  70.   (let* ((index 0)
  71.      (bevelling-whole-image FALSE)
  72.      (greyness 0)
  73.      (thickness (abs thickness))
  74.      (type (car (gimp-drawable-type-with-alpha drawable)))
  75.      (image (if (= work-on-copy TRUE) (car (gimp-channel-ops-duplicate img)) img))
  76.      (pic-layer (car (gimp-image-active-drawable image)))
  77.      (width (car (gimp-drawable-width pic-layer)))
  78.      (height (car (gimp-drawable-height pic-layer)))
  79.      (old-bg (car (gimp-palette-get-background)))
  80.      (bump-layer (car (gimp-layer-new image
  81.                       width
  82.                       height
  83.                       GRAY
  84.                       "Bumpmap"
  85.                       100
  86.                       NORMAL)))
  87.      )
  88.     ;
  89.     ; If the layer we're bevelling is offset from the image's origin, we
  90.     ; have to do the same to the bumpmap
  91.     (let ((offsets (gimp-drawable-offsets pic-layer)))
  92.       (gimp-layer-set-offsets bump-layer (car offsets) (cadr offsets))
  93.       )
  94.  
  95.     (gimp-image-undo-disable image)
  96.  
  97.     ;------------------------------------------------------------
  98.     ;
  99.     ; Set the selection to the area we want to bevel.
  100.     ;
  101.     (if (eq? 0 (car (gimp-selection-bounds image)))
  102.     (begin
  103.       (set! bevelling-whole-image TRUE) ; ...so we can restore things properly, and crop.
  104.       (gimp-image-resize image (+ width 2) (+ height 2) 1 1)
  105.       (if (not (eq? 0 (car (gimp-drawable-has-alpha pic-layer))))    ; Wish I knew Scheme
  106.           (gimp-selection-layer-alpha pic-layer)
  107.           (begin
  108.         (gimp-selection-all image)
  109.         )
  110.           )
  111.       )
  112.     )
  113.  
  114.     ; Store it for later.
  115.     (set! select (car (gimp-selection-save image)))
  116.     ; Try to lose the jaggies
  117.     (gimp-selection-feather image 2)
  118.  
  119.     ;------------------------------------------------------------
  120.     ;
  121.     ; Initialise our bumpmap
  122.     ;
  123.     (gimp-palette-set-background '(0 0 0))
  124.     (gimp-drawable-fill bump-layer BG-IMAGE-FILL)
  125.  
  126.     (set! index 1)
  127.     (while (< index thickness)
  128.        (set! greyness (/ (* index 255) thickness))
  129.        (gimp-palette-set-background (list greyness greyness greyness))
  130.        ;(gimp-selection-feather image 1) ;Stop the slopey jaggies?
  131.        (gimp-bucket-fill bump-layer BG-BUCKET-FILL NORMAL 100 0 FALSE 0 0)
  132.        (gimp-selection-shrink image 1)
  133.        (set! index (+ index 1))
  134.        )
  135.     ; Now the white interior
  136.     (gimp-palette-set-background '(255 255 255))
  137.     (gimp-bucket-fill bump-layer BG-BUCKET-FILL NORMAL 100 0 FALSE 0 0)
  138.  
  139.     ;------------------------------------------------------------
  140.     ;
  141.     ; Do the bump.
  142.     ;
  143.     (gimp-selection-none image)
  144.  
  145.     ; To further lessen jaggies?
  146.     ;(plug-in-gauss-rle 1 image bump-layer thickness TRUE TRUE)
  147.  
  148.  
  149.     ; If they're working on the original, let them undo the filter's effects.
  150.     ; This doesn't work - ideas why not?
  151.     (if (= work-on-copy FALSE) (gimp-image-undo-enable image))
  152.  
  153.     ;
  154.     ; BUMPMAP INVOCATION:
  155.     ;
  156.     ; Use the former with version 2.02 or earlier of bumpmap:
  157.     ; Use the latter with version 2.03 or later (ambient and waterlevel params)
  158. ;   (plug-in-bump-map 1 image pic-layer bump-layer 125 45 3 0 0     TRUE FALSE 1)
  159.     (plug-in-bump-map 1 image pic-layer bump-layer 125 45 3 0 0 0 0 TRUE FALSE 1)
  160.  
  161.     ;
  162.     ; Shave one pixel off each edge
  163.     ;
  164.     (if (= bevelling-whole-image TRUE)
  165.       (gimp-crop image width height 1 1)
  166.       )
  167.  
  168.     (if (= work-on-copy FALSE) (gimp-image-undo-disable image))
  169.  
  170.     ;------------------------------------------------------------
  171.     ;
  172.     ; Restore things
  173.     ;
  174.     (gimp-palette-set-background old-bg)
  175.     (if (= bevelling-whole-image TRUE)
  176.     (gimp-selection-none image)    ; No selection to start with
  177.     (gimp-selection-load select)
  178.     )
  179.     ; If they started with a selection, they can Select->Invert then
  180.     ; Edit->Clear for a cutout.
  181.  
  182.     ; clean up
  183.     (gimp-image-remove-channel image select)
  184.     (if (= keep-bump-layer TRUE)
  185.     (begin
  186.       (gimp-image-add-layer image bump-layer 1)
  187.       (gimp-layer-set-visible bump-layer 0))
  188.     (gimp-layer-delete bump-layer)
  189.     )
  190.  
  191.     (gimp-image-set-active-layer image pic-layer)
  192.  
  193.     (if (= work-on-copy TRUE) (gimp-display-new image))
  194.  
  195.     (gimp-image-undo-enable image)
  196.     (gimp-displays-flush)
  197.  
  198.     )
  199.   )
  200.  
  201. (script-fu-register "script-fu-add-bevel"
  202.             _"<Image>/Script-Fu/Decor/Add Bevel..."
  203.             "Add a bevel to an image"
  204.             "Andrew Donkin <ard@cs.waikato.ac.nz>"
  205.             "Andrew Donkin"
  206.             "1997/11/06"
  207.             "RGB* GRAY*"
  208.             SF-IMAGE "Image" 0
  209.             SF-DRAWABLE "Drawable" 0
  210.             SF-ADJUSTMENT _"Thickness"       '(5 0 30 1 2 0 0)
  211.             SF-TOGGLE     _"Work on Copy"    TRUE
  212.             SF-TOGGLE     _"Keep Bump Layer" FALSE
  213.             )
  214.