add-bevel.scm 6.74 KB
Newer Older
1
; GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3
; Copyright (C) 1995 Spencer Kimball and Peter Mattis
;
4 5
; add-bevel.scm version 1.04
; Time-stamp: <2004-02-09 17:07:06 simon>
6
;
7
; This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee committed
8
; it under the terms of the GNU General Public License as published by
9
; the Free Software Foundation; either version 3 of the License, or
Elliot Lee's avatar
Elliot Lee committed
10
; (at your option) any later version.
11
;
Elliot Lee's avatar
Elliot Lee committed
12 13 14 15
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
16
;
Elliot Lee's avatar
Elliot Lee committed
17
; You should have received a copy of the GNU General Public License
18
; along with this program.  If not, see <https://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
19 20 21 22
;
; Copyright (C) 1997 Andrew Donkin  (ard@cs.waikato.ac.nz)
; Contains code from add-shadow.scm by Sven Neumann
; (neumanns@uni-duesseldorf.de) (thanks Sven).
23
;
24
; Adds a bevel to an image.  See http://www.cs.waikato.ac.nz/~ard/gimp/
Elliot Lee's avatar
Elliot Lee committed
25 26 27 28
;
; If there is a selection, it is bevelled.
; Otherwise if there is an alpha channel, the selection is taken from it
; and bevelled.
29
; Otherwise the part of the layer inside the image boundaries is bevelled.
Elliot Lee's avatar
Elliot Lee committed
30 31 32 33 34
;
; The selection is set on exit, so Select->Invert then Edit->Clear will
; leave a cut-out.  Then use Sven's add-shadow for that
; floating-bumpmapped-texture cliche.

35 36 37 38
;
; 1.01: now works on offset layers.
; 1.02: has crop-pixel-border option to trim one pixel off each edge of the
;       bevelled image.  Bumpmapping leaves edge pixels unchanged, which
39
;       looks bad.  Oddly, this is not apparent in GIMP - you have to
40 41 42 43
;       save the image and load it into another viewer.  First noticed in
;       Nutscrape.
;       Changed path (removed "filters/").
; 1.03: adds one-pixel border before bumpmapping, and removes it after.
44 45 46 47
;       Got rid of the crop-pixel-border option (no longer reqd).
; 1.04: Fixed undo handling, ensure that bumpmap is big enough,
;       (instead of resizing the image). Removed references to outdated
;       bumpmap plugin.     (Simon)
48 49 50 51
; 1.05  When there is no selection, bevel the whole layer instead of the
;       whole image (which was broken in the first place).
;       Also fixed some bugs with setting the selection when there is no
;       initial selection.     (Barak Itkin)
52 53
;

54
(define (script-fu-add-bevel img
55 56 57 58
                             drawable
                             thickness
                             work-on-copy
                             keep-bump-layer)
Elliot Lee's avatar
Elliot Lee committed
59

60 61 62 63 64 65 66 67 68 69
  (let* (
        (index 1)
        (greyness 0)
        (thickness (abs thickness))
        (type (car (gimp-drawable-type-with-alpha drawable)))
        (image (if (= work-on-copy TRUE) (car (gimp-image-duplicate img)) img))
        (pic-layer (car (gimp-image-get-active-drawable image)))
        (offsets (gimp-drawable-offsets pic-layer))
        (width (car (gimp-drawable-width pic-layer)))
        (height (car (gimp-drawable-height pic-layer)))
70

71 72 73 74
        ; Bumpmap has a one pixel border on each side
        (bump-layer (car (gimp-layer-new image
                                         (+ width 2)
                                         (+ height 2)
75
                                         RGB-IMAGE
76
                                         _"Bumpmap"
77
                                         100
78
                                         LAYER-MODE-NORMAL)))
79 80

        (selection-exists (car (gimp-selection-bounds image)))
81
        (selection 0)
82
        )
83 84

    (gimp-context-push)
85
    (gimp-context-set-defaults)
86

87 88 89 90 91
    ; disable undo on copy, start group otherwise
    (if (= work-on-copy TRUE)
      (gimp-image-undo-disable image)
      (gimp-image-undo-group-start image)
    )
Elliot Lee's avatar
Elliot Lee committed
92

93
    (gimp-image-insert-layer image bump-layer 0 1)
94 95 96 97 98 99

    ; If the layer we're bevelling is offset from the image's origin, we
    ; have to do the same to the bumpmap
    (gimp-layer-set-offsets bump-layer (- (car offsets) 1)
                                       (- (cadr offsets) 1))

Elliot Lee's avatar
Elliot Lee committed
100 101 102 103
    ;------------------------------------------------------------
    ;
    ; Set the selection to the area we want to bevel.
    ;
104
    (if (= selection-exists 0)
105
        (gimp-image-select-item image CHANNEL-OP-REPLACE pic-layer)
106
    )
Elliot Lee's avatar
Elliot Lee committed
107 108

    ; Store it for later.
109
    (set! selection (car (gimp-selection-save image)))
Elliot Lee's avatar
Elliot Lee committed
110 111 112 113 114 115 116
    ; Try to lose the jaggies
    (gimp-selection-feather image 2)

    ;------------------------------------------------------------
    ;
    ; Initialise our bumpmap
    ;
117
    (gimp-context-set-background '(0 0 0))
118
    (gimp-drawable-fill bump-layer FILL-BACKGROUND)
Elliot Lee's avatar
Elliot Lee committed
119 120

    (while (< index thickness)
121
           (set! greyness (/ (* index 255) thickness))
122
           (gimp-context-set-background (list greyness greyness greyness))
123
           ;(gimp-selection-feather image 1) ;Stop the slopey jaggies?
124
           (gimp-drawable-edit-fill bump-layer FILL-BACKGROUND)
125 126 127
           (gimp-selection-shrink image 1)
           (set! index (+ index 1))
    )
Elliot Lee's avatar
Elliot Lee committed
128
    ; Now the white interior
129
    (gimp-context-set-background '(255 255 255))
130
    (gimp-drawable-edit-fill bump-layer FILL-BACKGROUND)
Elliot Lee's avatar
Elliot Lee committed
131 132 133 134 135 136 137 138

    ;------------------------------------------------------------
    ;
    ; Do the bump.
    ;
    (gimp-selection-none image)

    ; To further lessen jaggies?
139
    ;(plug-in-gauss-rle RUN-NONINTERACTIVE image bump-layer thickness TRUE TRUE)
Elliot Lee's avatar
Elliot Lee committed
140 141 142 143 144


    ;
    ; BUMPMAP INVOCATION:
    ;
145
    (plug-in-bump-map RUN-NONINTERACTIVE image pic-layer bump-layer 125 45 3 0 0 0 0 TRUE FALSE 1)
Elliot Lee's avatar
Elliot Lee committed
146 147 148 149 150

    ;------------------------------------------------------------
    ;
    ; Restore things
    ;
151
    (if (= selection-exists 0)
152
        (gimp-selection-none image)        ; No selection to start with
153
        (gimp-image-select-item image CHANNEL-OP-REPLACE selection)
154
    )
155 156
    ; If they started with a selection, they can Select->Invert then
    ; Edit->Clear for a cutout.
Elliot Lee's avatar
Elliot Lee committed
157 158

    ; clean up
159
    (gimp-image-remove-channel image selection)
Elliot Lee's avatar
Elliot Lee committed
160
    (if (= keep-bump-layer TRUE)
161
        (gimp-item-set-visible bump-layer 0)
162 163
        (gimp-image-remove-layer image bump-layer)
    )
Elliot Lee's avatar
Elliot Lee committed
164 165 166

    (gimp-image-set-active-layer image pic-layer)

167
    ; enable undo / end undo group
168 169 170 171 172 173 174
    (if (= work-on-copy TRUE)
      (begin
        (gimp-display-new image)
        (gimp-image-undo-enable image)
      )
      (gimp-image-undo-group-end image)
    )
175

Elliot Lee's avatar
Elliot Lee committed
176
    (gimp-displays-flush)
177 178

    (gimp-context-pop)
179 180
  )
)
Elliot Lee's avatar
Elliot Lee committed
181 182

(script-fu-register "script-fu-add-bevel"
183 184 185 186 187
  _"Add B_evel..."
  _"Add a beveled border to an image"
  "Andrew Donkin <ard@cs.waikato.ac.nz>"
  "Andrew Donkin"
  "1997/11/06"
188
  "RGB*"
189 190 191 192 193
  SF-IMAGE       "Image"           0
  SF-DRAWABLE    "Drawable"        0
  SF-ADJUSTMENT _"Thickness"       '(5 0 30 1 2 0 0)
  SF-TOGGLE     _"Work on copy"    TRUE
  SF-TOGGLE     _"Keep bump layer" FALSE
194
)
195

196
(script-fu-menu-register "script-fu-add-bevel" "<Image>/Filters/Decor")