[Part I. GIMP基礎功]

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



25.6 紋理圖,修改成可「單行」執行的 Script-Fu

修改成一行一行可執行的程式碼
  • 本節要將 clothify.scm 修改成在 Console 內可一行一行執行的 Scheme 程式碼。
  • clothify.scm 完整的程式碼可參考 25.5 節
  • 也可到 GIMP 系統的 scripts 目錄下找到該檔案。
  • 最主要修改的部份是,將「對話框接收輸入變數值」修改成「直接指定輸入變數值」。
  • 先要瞭解,clothify.scm 程式碼的執行流程。


最主要修改的部份
  • 不使用 let* 改用 define
  • 原本的程式使用 let* 定義與指定區域變數值。
  • (let* (()()()...) ()() ...) 這樣的語法括號太多。
  • 若要一行一行分析程式碼的意義,let* 語法,括號有好幾層,分析時,會造成一些干擾。
  • 現在不使用 let* 改用 define 來定義與指定變數值。


捨棄函數 script-fu-clothify ,直接指定輸入變數值
  • 修改成最多的地方就是下列這部份。
  • 修改後的程式碼,不再需要預先定義函數 script-fu-clothify ,但是要預先設定變數名稱、變數值 。

((define (script-fu-clothify timg tdrawable bx by azimuth elevation depth)
(let* (
(width (car (gimp-drawable-width tdrawable)))
(height (car (gimp-drawable-height tdrawable)))
(img (car (gimp-image-new width height RGB)))
(layer-one (car (gimp-layer-new img width height RGB-IMAGE "X Dots" 100 NORMAL-MODE)))
(layer-two 0)
(bump-layer 0)
)


從下列的程式碼得知變數值的型態與大小
SF-IMAGE       "Input image"    0
SF-DRAWABLE    "Input drawable" 0
SF-ADJUSTMENT _"Blur X"         '(9 3 100 1 10 0 1)
SF-ADJUSTMENT _"Blur Y"         '(9 3 100 1 10 0 1)
SF-ADJUSTMENT _"Azimuth"        '(135 0 360 1 10 1 0)
SF-ADJUSTMENT _"Elevation"      '(45 0 90 1 10 1 0)
SF-ADJUSTMENT _"Depth"          '(3 1 50 1 10 0 1)


使用80 x 80 橘色方塊,來測試「纖維紋路」的效果
  • 原本的clothify.scm需要參考到既存的影像物件、圖層物件,才能正確運行。
  • 添加「建立 80 x 80 橘色圖層」的程式碼,這樣「纖維紋路的程式」才能正確運行 。
  • 下圖是程式碼執行後的圖案,80 x 80 橘色方塊。

(define swidth 80)
(define sheight 80)
;新增影像物件
(define square_img (car (gimp-image-new swidth sheight RGB)))
;新增圖層物件
(define square_drw (car (gimp-layer-new square_img swidth sheight RGB-IMAGE "tmp" 100 NORMAL-MODE)))
;將圖層物件加入影像物件
(gimp-image-add-layer square_img square_drw 0)

(gimp-context-set-background '(234 125 30))
(gimp-edit-fill square_drw BACKGROUND-FILL) ;填滿橘色

(gimp-display-new square_img)


修改後的程式碼,與測試的結果圖
  • 下面是修改後的程式碼。
  • 啟動 GIMP Script-fu Console
  • 將下面的 Scheme 程式碼,全部選取、複製、再貼入 Script-fu Console 文字框,按下 Enter 。
  • 過一會,就會看到結果圖,衣料的紋路。

; 要先準備好「測試用的影像與圖層」
(define timg square_img) ;指派影像物件
(define tdrawable square_drw) ;指派圖層物件
;t 表示 temporary 臨時的

(define bx 9)
(define by 9)
(define azimuth 135)
(define elevation 45)
(define depth 3)

(define width (car (gimp-drawable-width tdrawable)))
(define height (car (gimp-drawable-height tdrawable)))
(define img (car (gimp-image-new width height RGB)))
(define layer-one (car (gimp-layer-new img width height RGB-IMAGE "X Dots" 100 NORMAL-MODE)))
(define layer-two 0)
(define bump-layer 0)

(gimp-context-push)

(gimp-image-undo-disable img)

(gimp-image-add-layer img layer-one 0)

(gimp-context-set-background '(255 255 255))
(gimp-edit-fill layer-one BACKGROUND-FILL)

(plug-in-noisify RUN-NONINTERACTIVE img layer-one FALSE 0.7 0.7 0.7 0.7)

(set! layer-two (car (gimp-layer-copy layer-one 0)))
(gimp-layer-set-mode layer-two MULTIPLY-MODE)
(gimp-image-add-layer img layer-two 0)

(gimp-display-new img) ; 顯示 img 物件

(plug-in-gauss-rle RUN-NONINTERACTIVE img layer-one bx TRUE FALSE)
(plug-in-gauss-rle RUN-NONINTERACTIVE img layer-two by FALSE TRUE)
;(gimp-image-flatten img)
(set! bump-layer (car (gimp-image-get-active-layer img)))

(plug-in-c-astretch RUN-NONINTERACTIVE img bump-layer)
(plug-in-noisify RUN-NONINTERACTIVE img bump-layer FALSE 0.2 0.2 0.2 0.2)

(plug-in-bump-map RUN-NONINTERACTIVE img tdrawable bump-layer azimuth elevation depth 0 0 0 0 FALSE FALSE 0)
;(gimp-image-delete img)
(gimp-displays-flush)

(gimp-context-pop)


觀看纖維紋路的合成原理
  • 增加顯示 img 物件的程式碼 (gimp-display-new img)
  • 以分號註解下面這兩行
    • (gimp-image-flatten img)
    • (gimp-image-delete img)
  • 這樣就會看到兩個圖層疊合的效果,可參考25.3節25.4節的分析。

  • 實際產生縱橫交織的紋路時,不能註解(gimp-image-flatten img)
  • 因為,兩圖層要合併,才能讓受作用的圖案,垂直方向與水平方向,都產生紋路。