File: perspective-shadow.scm

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (196 lines) | stat: -rw-r--r-- 6,285 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
; The GIMP -- an image manipulation program
; Copyright (C) 1995 Spencer Kimball and Peter Mattis
; 
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.  
; 
; 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.
; 
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
;
; perspective-shadow.scm   version 1.2   2000/11/08
;
; Copyright (C) 1997-2000 Sven Neumann <sven@gimp.org>
; 
;  
; Adds a perspective shadow of the current selection or alpha-channel 
; as a layer below the active layer
;
  
(define (script-fu-perspective-shadow image
				      drawable
				      alpha
				      rel-distance
				      rel-length
				      shadow-blur
				      shadow-color
				      shadow-opacity
				      interpolation
				      allow-resize)
  (let* ((shadow-blur (max shadow-blur 0))
	 (shadow-opacity (min shadow-opacity 100))
	 (shadow-opacity (max shadow-opacity 0))
	 (rel-length (abs rel-length))
	 (alpha (* (/ alpha 180) *pi*))
	 (type (car (gimp-drawable-type-with-alpha drawable)))
	 (image-width (car (gimp-image-width image)))
	 (image-height (car (gimp-image-height image)))
	 (from-selection 0)
	 (active-selection 0)
	 (shadow-layer 0))
    
  (if (= rel-distance 0) (set! rel-distance 999999))

  (gimp-context-push)
  
  (gimp-image-undo-group-start image)
  
  (gimp-layer-add-alpha drawable)
  (if (= (car (gimp-selection-is-empty image)) TRUE)
      (begin
	(gimp-selection-layer-alpha drawable)
	(set! from-selection FALSE))
      (begin
	(set! from-selection TRUE)
	(set! active-selection (car (gimp-selection-save image)))))
  
  (let* ((selection-bounds (gimp-selection-bounds image))
	 (select-offset-x (cadr selection-bounds))
	 (select-offset-y (caddr selection-bounds))
	 (select-width (- (cadr (cddr selection-bounds)) select-offset-x))
	 (select-height (- (caddr (cddr selection-bounds)) select-offset-y))
 
	 (abs-length (* rel-length select-height))
	 (abs-distance (* rel-distance select-height))
	 (half-bottom-width (/ select-width 2))
	 (half-top-width (* half-bottom-width
			  (/ (- rel-distance rel-length) rel-distance)))
  
	 (x0 (+ select-offset-x (+ (- half-bottom-width half-top-width)
				 (* (cos alpha) abs-length))))
	 (y0 (+ select-offset-y (- select-height
				 (* (sin alpha) abs-length))))
	 (x1 (+ x0 (* 2 half-top-width)))
	 (y1 y0)
	 (x2 select-offset-x)
	 (y2 (+ select-offset-y select-height))
	 (x3 (+ x2 select-width))
	 (y3 y2)

	 (shadow-width (+ (- (max x1 x3) (min x0 x2)) (* 2 shadow-blur)))
	 (shadow-height (+ (- (max y1 y3) (min y0 y2)) (* 2 shadow-blur)))
	 (shadow-offset-x (- (min x0 x2) shadow-blur))
	 (shadow-offset-y (- (min y0 y2) shadow-blur)))
	 
  
    (set! shadow-layer (car (gimp-layer-new image
					    select-width
					    select-height
					    type
					    "Perspective Shadow"
					    shadow-opacity
					    NORMAL-MODE)))


    (gimp-image-add-layer image shadow-layer -1)
    (gimp-layer-set-offsets shadow-layer select-offset-x select-offset-y)
    (gimp-drawable-fill shadow-layer TRANSPARENT-FILL)
    (gimp-context-set-background shadow-color)
    (gimp-edit-fill shadow-layer BACKGROUND-FILL)
    (gimp-selection-none image)

    (if (= allow-resize TRUE)
	(let* ((new-image-width image-width)
	       (new-image-height image-height)
	       (image-offset-x 0)
	       (image-offset-y 0))

	  (if (< shadow-offset-x 0)
	      (begin
		(set! image-offset-x (- 0 shadow-offset-x))
		(set! new-image-width (- new-image-width image-offset-x))))

	  (if (< shadow-offset-y 0)
	      (begin
		(set! image-offset-y (- 0 shadow-offset-y))
		(set! new-image-height (- new-image-height image-offset-y))))
	  
	  (if (> (+ shadow-width shadow-offset-x) new-image-width)
	      (set! new-image-width (+ shadow-width shadow-offset-x)))
	  
	  (if (> (+ shadow-height shadow-offset-y) new-image-height)
	      (set! new-image-height (+ shadow-height shadow-offset-y)))
	  (gimp-image-resize image
			     new-image-width
			     new-image-height
			     image-offset-x
			     image-offset-y)))
  
    (gimp-drawable-transform-perspective shadow-layer
					 x0 y0
					 x1 y1
					 x2 y2
					 x3 y3
					 TRANSFORM-FORWARD interpolation
					 TRUE 3 FALSE)

    (if (>= shadow-blur 1.0)
	(begin
	  (gimp-layer-set-preserve-trans shadow-layer FALSE)
	  (gimp-layer-resize shadow-layer
			     shadow-width
			     shadow-height
			     shadow-blur
			     shadow-blur)
	  (plug-in-gauss-rle 1
			     image
			     shadow-layer
			     shadow-blur
			     TRUE
			     TRUE))))

  (if (= from-selection TRUE)
      (begin
	(gimp-selection-load active-selection)
	(gimp-edit-clear shadow-layer)
	(gimp-image-remove-channel image active-selection)))

  (if (and
       (= (car (gimp-layer-is-floating-sel drawable)) 0)
       (= from-selection FALSE))
      (gimp-image-raise-layer image drawable))

  (gimp-image-set-active-layer image drawable)
  (gimp-image-undo-group-end image)
  (gimp-displays-flush)

  (gimp-context-pop)))

(script-fu-register "script-fu-perspective-shadow"
		    _"_Perspective..."
		    "Add a perspective shadow"
		    "Sven Neumann <sven@gimp.org>"
		    "Sven Neumann"
		    "2000/11/08"
		    "RGB* GRAY*"
		    SF-IMAGE       "Image"          0
		    SF-DRAWABLE    "Drawable"       0
		    SF-ADJUSTMENT _"Angle"          '(45 0 180 1 10 1 0)
		    SF-ADJUSTMENT _"Relative distance of horizon" '(5 0 24 .1 1 1 1)
		    SF-ADJUSTMENT _"Relative length of shadow" '(1 0 24 .1 1 1 1)
		    SF-ADJUSTMENT _"Blur radius"    '(3 0 1024 1 10 0 0)
		    SF-COLOR      _"Color"          '(0 0 0)
		    SF-ADJUSTMENT _"Opacity"        '(80 0 100 1 10 0 0)
		    SF-OPTION     _"Interpolation"  '(_"None" _"Linear" _"Cubic")
		    SF-TOGGLE     _"Allow resizing" FALSE)

(script-fu-menu-register "script-fu-perspective-shadow"
			 _"<Image>/Script-Fu/Shadow")