[Part I. GIMP基礎功]

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



29.5 程式lava.scm分三部份

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

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

程式lava.scm分三部份:
  • 1~25行:註解,功能說明與版權聲明。
  • 26~115行:定義函式
  • 116~136行:註冊函式

第1到第25行:註解說明1: ; GIMP - The GNU Image Manipulation Program
2: ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
3: ;
4: ; Lava effect
5: ; Copyright (c) 1997 Adrian Likins
6: ; aklikins@eos.ncsu.edu
7: ;
8: ; based on a idea by Sven Riedel <lynx@heim8.tu-clausthal.de>
9: ; tweaked a bit by Sven Neumann <neumanns@uni-duesseldorf.de>
10: ;
11: ; This program is free software; you can redistribute it and/or modify
12: ; it under the terms of the GNU General Public License as published by
13: ; the Free Software Foundation; either version 2 of the License, or
14: ; (at your option) any later version.
15: ;
16: ; This program is distributed in the hope that it will be useful,
17: ; but WITHOUT ANY WARRANTY; without even the implied warranty of
18: ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: ; GNU General Public License for more details.
20: ;
21: ; You should have received a copy of the GNU General Public License
22: ; along with this program; if not, write to the Free Software
23: ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24:
25:

第26到第115行:定義函式26: (define (script-fu-lava image
27: drawable
28: seed
29: tile_size
30: mask_size
31: gradient
32: keep-selection
33: separate-layer
34: current-grad)
35: (let* (
36: (type (car (gimp-drawable-type-with-alpha drawable)))
37: (image-width (car (gimp-image-width image)))
38: (image-height (car (gimp-image-height image)))
39: (active-selection 0)
40: (selection-bounds 0)
41: (select-offset-x 0)
42: (select-offset-y 0)
43: (select-width 0)
44: (select-height 0)
45: (lava-layer 0)
46: (active-layer 0)
47: )
48:
49: (gimp-context-push)
50: (gimp-image-undo-group-start image)
51:
52: (if (= (car (gimp-drawable-has-alpha drawable)) FALSE)
53: (gimp-layer-add-alpha drawable)
54: )
55:
56: (if (= (car (gimp-selection-is-empty image)) TRUE)
57: (gimp-selection-layer-alpha drawable)
58: )
59:
60: (set! active-selection (car (gimp-selection-save image)))
61: (gimp-image-set-active-layer image drawable)
62:
63: (set! selection-bounds (gimp-selection-bounds image))
64: (set! select-offset-x (cadr selection-bounds))
65: (set! select-offset-y (caddr selection-bounds))
66: (set! select-width (- (cadr (cddr selection-bounds)) select-offset-x))
67: (set! select-height (- (caddr (cddr selection-bounds)) select-offset-y))
68:
69: (if (= separate-layer TRUE)
70: (begin
71: (set! lava-layer (car (gimp-layer-new image
72: select-width
73: select-height
74: type
75: "Lava Layer"
76: 100
77: NORMAL-MODE)))
78:
79: (gimp-image-add-layer image lava-layer -1)
80: (gimp-layer-set-offsets lava-layer select-offset-x select-offset-y)
81: (gimp-selection-none image)
82: (gimp-edit-clear lava-layer)
83:
84: (gimp-selection-load active-selection)
85: (gimp-image-set-active-layer image lava-layer)
86: )
87: )
88:
89: (set! active-layer (car (gimp-image-get-active-layer image)))
90:
91: (if (= current-grad FALSE)
92: (gimp-context-set-gradient gradient)
93: )
94:
95: (plug-in-solid-noise RUN-NONINTERACTIVE image active-layer FALSE TRUE seed 2 2 2)
96: (plug-in-cubism RUN-NONINTERACTIVE image active-layer tile_size 2.5 0)
97: (plug-in-oilify RUN-NONINTERACTIVE image active-layer mask_size 0)
98: (plug-in-edge RUN-NONINTERACTIVE image active-layer 2 0 0)
99: (plug-in-gauss-rle RUN-NONINTERACTIVE image active-layer 2 TRUE TRUE)
100: (plug-in-gradmap RUN-NONINTERACTIVE image active-layer)
101:
102: (if (= keep-selection FALSE)
103: (gimp-selection-none image)
104: )
105:
106: (gimp-image-set-active-layer image drawable)
107: (gimp-image-remove-channel image active-selection)
108:
109: (gimp-image-undo-group-end image)
110: (gimp-context-pop)
111:
112: (gimp-displays-flush)
113: )
114: )
115:

第116到第136行:註冊函式116: (script-fu-register "script-fu-lava"
117: _"_Lava..."
118: _"Fill the current selection with lava"
119: "Adrian Likins <adrian@gimp.org>"
120: "Adrian Likins"
121: "10/12/97"
122: "RGB* GRAY*"
123: SF-IMAGE "Image" 0
124: SF-DRAWABLE "Drawable" 0
125: SF-ADJUSTMENT _"Seed" '(10 1 30000 1 10 0 1)
126: SF-ADJUSTMENT _"Size" '(10 0 100 1 10 0 1)
127: SF-ADJUSTMENT _"Roughness" '(7 3 50 1 10 0 0)
128: SF-GRADIENT _"Gradient" "German flag smooth"
129: SF-TOGGLE _"Keep selection" TRUE
130: SF-TOGGLE _"Separate layer" TRUE
131: SF-TOGGLE _"Use current gradient" FALSE
132: )
133:
134: (script-fu-menu-register "script-fu-lava"
135: "<Image>/Filters/Render")
136: