[Part I. GIMP基礎功]

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



27.5 程式camo.scm分三部份

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

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

程式camo.scm分三部份:
  • 1~21行:註解,功能說明與版權聲明。
  • 22~90行:定義函式
  • 91~113行:註冊函式
第1到第21行:註解說明1: ;
2: ;
3: ;
4: ; Chris Gutteridge (cjg@ecs.soton.ac.uk)
5: ; At ECS Dept, University of Southampton, England.
6:
7: ; This program is free software; you can redistribute it and/or modify
8: ; it under the terms of the GNU General Public License as published by
9: ; the Free Software Foundation; either version 2 of the License, or
10: ; (at your option) any later version.
11: ;
12: ; This program is distributed in the hope that it will be useful,
13: ; but WITHOUT ANY WARRANTY; without even the implied warranty of
14: ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: ; GNU General Public License for more details.
16: ;
17: ; You should have received a copy of the GNU General Public License
18: ; along with this program; if not, write to the Free Software
19: ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20:
21:

第22到第90行:定義函式22: (define (script-fu-camo-pattern inSize inGrain inColor1 inColor2 inColor3 inSmooth inFlatten)
23:
24: (let* (
25: (theWidth inSize)
26: (theHeight inSize)
27: (theImage (car (gimp-image-new theWidth theHeight RGB)))
28: (baseLayer (car (gimp-layer-new theImage theWidth theHeight RGBA-IMAGE "Background" 100 NORMAL-MODE)))
29: (thickLayer 0)
30: (thinLayer 0)
31: (theBlur 0)
32: )
33:
34: (gimp-context-push)
35:
36: (gimp-image-add-layer theImage baseLayer 0)
37:
38: (set! thickLayer (car (gimp-layer-new theImage theWidth theHeight RGBA-IMAGE "Camo Thick Layer" 100 NORMAL-MODE)))
39: (gimp-image-add-layer theImage thickLayer 0)
40:
41: (set! thinLayer (car (gimp-layer-new theImage theWidth theHeight RGBA-IMAGE "Camo Thin Layer" 100 NORMAL-MODE)))
42: (gimp-image-add-layer theImage thinLayer 0)
43:
44: (gimp-selection-all theImage)
45: (gimp-context-set-background inColor1)
46: (gimp-drawable-fill baseLayer BACKGROUND-FILL)
47:
48: (plug-in-solid-noise RUN-NONINTERACTIVE
49: theImage thickLayer 1 0 (rand 65536) 1 inGrain inGrain)
50: (plug-in-solid-noise RUN-NONINTERACTIVE
51: theImage thinLayer 1 0 (rand 65536) 1 inGrain inGrain)
52: (gimp-threshold thickLayer 127 255)
53: (gimp-threshold thinLayer 145 255)
54:
55: (set! theBlur (- 16 inGrain))
56:
57: (gimp-context-set-background inColor2)
58: (gimp-by-color-select thickLayer
59: '(0 0 0) 127 CHANNEL-OP-REPLACE TRUE FALSE 0 FALSE)
60: (gimp-edit-clear thickLayer)
61: (gimp-selection-invert theImage)
62: (gimp-edit-fill thickLayer BACKGROUND-FILL)
63: (gimp-selection-none theImage)
64: (if (= inSmooth TRUE)
65: (script-fu-tile-blur theImage thickLayer theBlur TRUE TRUE FALSE)
66: )
67:
68:
69: (gimp-context-set-background inColor3)
70: (gimp-by-color-select thinLayer '(0 0 0) 127 CHANNEL-OP-REPLACE TRUE FALSE 0 FALSE)
71: (gimp-edit-clear thinLayer)
72: (gimp-selection-invert theImage)
73: (gimp-edit-fill thinLayer BACKGROUND-FILL)
74: (gimp-selection-none theImage)
75: (if (= inSmooth TRUE)
76: (script-fu-tile-blur theImage thinLayer (/ theBlur 2) TRUE TRUE FALSE)
77: )
78:
79:
80: (if (= inFlatten TRUE)
81: (gimp-image-flatten theImage)
82: )
83:
84: (gimp-display-new theImage)
85:
86: (gimp-context-pop)
87: )
88: )
89:
90:

第91到第113行:註冊函式91: ; Register the function with GIMP:
92:
93: (script-fu-register
94: "script-fu-camo-pattern"
95: _"_Camouflage..."
96: _"Create an image filled with a camouflage pattern"
97: "Chris Gutteridge: cjg@ecs.soton.ac.uk"
98: "28th April 1998"
99: "Chris Gutteridge / ECS @ University of Southampton, England"
100: ""
101: SF-ADJUSTMENT _"Image size" '(256 10 1000 1 10 0 1)
102: SF-ADJUSTMENT _"Granularity" '(7 0 15 1 1 0 0)
103: SF-COLOR _"Color 1" '(33 100 58)
104: SF-COLOR _"Color 2" '(170 170 60)
105: SF-COLOR _"Color 3" '(150 115 100)
106: SF-TOGGLE _"Smooth" FALSE
107: SF-TOGGLE _"Flatten image" TRUE
108: )
109:
110:
111: (script-fu-menu-register "script-fu-camo-pattern"
112: "<Image>/File/Create/Patterns")
113: