[Part I. GIMP基礎功]

[Part II. 一種效果的誕生]
[Part III. Script-Fu的秘密]



28.5 程式circuit.scm分三部份



若要徹底瞭解circuit.scm,可仿傚第22章的方式,單步執行,來分析每一行程式碼的作用。

若用PSPad開啟程式檔(或任何文字編輯器),看到的Script-Fu程式碼,變數會對得很整齊。HTML會把大量的空白省略,因此程式碼的排版變成了下面這樣子。我替Script-Fu程式碼加上行號乎,方便讀者閱讀。



1~31:註解說明1: ; GIMP - The GNU Image Manipulation Program
2: ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
3: ;
4: ; Circuit board effect
5: ; Copyright (c) 1997 Adrian Likins
6: ; aklikins@eos.ncsu.ed
7: ;
8: ; Genrates what looks a little like the back of an old circuit board.
9: ; Looks even better when gradmapped with a suitable gradient.
10: ;
11: ; This script doesnt handle or color combos well. ie, black/black
12: ; doesnt work..
13: ; The effect seems to work best on odd shaped selections because of some
14: ; limitations in the maze codes selection handling ablity
15: ;
16: ;
17: ; This program is free software; you can redistribute it and/or modify
18: ; it under the terms of the GNU General Public License as published by
19: ; the Free Software Foundation; either version 2 of the License, or
20: ; (at your option) any later version.
21: ;
22: ; This program is distributed in the hope that it will be useful,
23: ; but WITHOUT ANY WARRANTY; without even the implied warranty of
24: ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25: ; GNU General Public License for more details.
26: ;
27: ; You should have received a copy of the GNU General Public License
28: ; along with this program; if not, write to the Free Software
29: ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30:
31:

32~140:定義函式
32: (define (script-fu-circuit image
33: drawable
34: mask-size
35: seed
36: remove-bg
37: keep-selection
38: separate-layer)
39: (let* (
40: (type (car (gimp-drawable-type-with-alpha drawable)))
41: (image-width (car (gimp-image-width image)))
42: (image-height (car (gimp-image-height image)))
43: (active-selection 0)
44: (from-selection 0)
45: (selection-bounds 0)
46: (select-offset-x 0)
47: (select-offset-y 0)
48: (select-width 0)
49: (select-height 0)
50: (effect-layer 0)
51: (active-layer 0)
52: )
53:
54: (gimp-context-push)
55:
56: (gimp-image-undo-group-start image)
57:
58: (gimp-layer-add-alpha drawable)
59:
60: (if (= (car (gimp-selection-is-empty image)) TRUE)
61: (begin
62: (gimp-selection-layer-alpha drawable)
63: (set! active-selection (car (gimp-selection-save image)))
64: (set! from-selection FALSE))
65: (begin
66: (set! from-selection TRUE)
67: (set! active-selection (car (gimp-selection-save image)))))
68:
69: (set! selection-bounds (gimp-selection-bounds image))
70: (set! select-offset-x (cadr selection-bounds))
71: (set! select-offset-y (caddr selection-bounds))
72: (set! select-width (- (cadr (cddr selection-bounds)) select-offset-x))
73: (set! select-height (- (caddr (cddr selection-bounds)) select-offset-y))
74:
75: (if (= separate-layer TRUE)
76: (begin
77: (set! effect-layer (car (gimp-layer-new image
78: select-width
79: select-height
80: type
81: "effect layer"
82: 100
83: NORMAL-MODE)))
84:
85: (gimp-image-add-layer image effect-layer -1)
86: (gimp-layer-set-offsets effect-layer select-offset-x select-offset-y)
87: (gimp-selection-none image)
88: (gimp-edit-clear effect-layer)
89: (gimp-selection-load active-selection)
90: (gimp-edit-copy drawable)
91:
92: (let ((floating-sel (car (gimp-edit-paste effect-layer FALSE))))
93: (gimp-floating-sel-anchor floating-sel)
94: )
95: (gimp-image-set-active-layer image effect-layer ))
96: (set! effect-layer drawable)
97: )
98: (set! active-layer effect-layer)
99:
100: (if (= remove-bg TRUE)
101: (gimp-context-set-foreground '(0 0 0))
102: (gimp-context-set-foreground '(14 14 14))
103: )
104:
105: (gimp-selection-load active-selection)
106: (plug-in-maze RUN-NONINTERACTIVE image active-layer 5 5 TRUE 0 seed 57 1)
107: (plug-in-oilify RUN-NONINTERACTIVE image active-layer mask-size 0)
108: (plug-in-edge RUN-NONINTERACTIVE image active-layer 2 1 0)
109: (if (= type RGBA-IMAGE)
110: (gimp-desaturate active-layer))
111:
112: (if (and
113: (= remove-bg TRUE)
114: (= separate-layer TRUE))
115: (begin
116: (gimp-by-color-select
117: active-layer
118: '(0 0 0)
119: 15
120: 2
121: TRUE
122: FALSE
123: 10
124: FALSE)
125: (gimp-edit-clear active-layer)))
126:
127: (if (= keep-selection FALSE)
128: (gimp-selection-none image))
129:
130: (gimp-image-remove-channel image active-selection)
131: (gimp-image-set-active-layer image drawable)
132:
133: (gimp-image-undo-group-end image)
134:
135: (gimp-displays-flush)
136:
137: (gimp-context-pop)
138: )
139: )
140:

141~159:註冊函式
141: (script-fu-register "script-fu-circuit"
142: _"_Circuit..."
143: _"Fill the selected region (or alpha) with traces like those on a circuit board"
144: "Adrian Likins <adrian@gimp.org>"
145: "Adrian Likins"
146: "10/17/97"
147: "RGB* GRAY*"
148: SF-IMAGE "Image" 0
149: SF-DRAWABLE "Drawable" 0
150: SF-ADJUSTMENT _"Oilify mask size" '(17 3 50 1 10 0 1)
151: SF-ADJUSTMENT _"Circuit seed" '(3 1 3000000 1 10 0 1)
152: SF-TOGGLE _"No background (only for separate layer)" FALSE
153: SF-TOGGLE _"Keep selection" TRUE
154: SF-TOGGLE _"Separate layer" TRUE
155: )
156:
157: (script-fu-menu-register "script-fu-circuit"
158: "<Image>/Filters/Render")
159: