Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Let us say with petals
#3
Lightbulb 
This article is not about registering in Script-fu.

2. Optimizing Script-Fu

[Image: attachment.php?aid=6895]

.webp   make-flowerPetal4.webp (Size: 6.16 KB / Downloads: 297)
Since the flower is drawn in 1 s 196 ms 518 µs according to the CPU, the purpose of this chapter is not to optimize the speed.
It is enough fast for an interpreted language.

We wish to reduce the number of lines in order to better understand the architecture of the program and how to draw such a flower.

Download the supplied make-flower.zip 3 Kb.

.zip   make-flower.zip (Size: 2.67 KB / Downloads: 131)
Unzip the new make-flower.scm 9 Kb.
The number of lines has been optimized to 130 lines thanks to 8 nested defines inside the main define.
There are 23 lines of comment so 17.6% and one blank line.

Script-Fu console: 
Code:
(make-flower       500  1         0        60        0        5        '(130  65  66)  '( 24 86  46)    '(  9  29  16))
Press ENTER
;-> The new version draws another flower with 4 petals.

Click the Browse button at the right of the input area of the Script-Fu console
Search: "gimp-image-add-layer"
The PDB announces: "Deprecated: Use 'gimp-image-insert-layer' instead."
So the new version replaces the legacy:
Code:
(gimp-image-add-layer theImage base-layer 0)

with parent=0:
Code:
(gimp-image-insert-layer image layer 0 -1)

Contrary to the original version, pay attention that the new version introduces the alignement of parenthesis --typical of the Gimphried's style--.
We are using the full length of modern screen with in line 74 the longest line of code with its comment ended at the 165th character.

We introduce the case statement.

For the purpose of the exercice of the optimization of the number of lines, Gimphried resumed the entire case statement on only one line:
Code:
(case petalDir    ((0) (set! varX1 8) (set! varY1 4)) ((1) (set! varX1 4) (set! varY1 8)) ((2) (set! varX1 8) (set! varY1 8))) ; Cloud Layer X and Y
However don't do like Gimphried.

The optimization of if + begin by and with multiple parameters could be more relevant:
Since there is more than one statement in the then part, we need to group them inside a begin statement:
Code:
(if (> centreBump 0) ; Only bumpmap if the Centre Bump setting was not at zero. Ignore if set to zero.
    (begin
        (gimp-context-set-pattern "Leather")
        (gimp-edit-bucket-fill layerMap 2 0 100 255 FALSE 0 0) ; Add pattern for bump map purposes
        (gimp-selection-none image)
        (gimp-image-set-active-layer image layerCentre)
        (plug-in-bump-map 1 image layerCentre layerMap 135 45 centreBump 0 0 0 0 1 0 0) ; then apply bump map. 
        (gimp-selection-layer-alpha layerMap)
)    ) ; Remove bump map layer and merge remaining 2 layers.

The above if snippet can be optimized with the logical operateur and accepting an unlimited number of parameters
if and only if each statement of the then part --regrouped inside the begin-- never fails!
Code:
(and (> centreBump 0) ; Only bumpmap if the Centre Bump setting was not at zero. Ignore if set to zero.
    (gimp-context-set-pattern "Leather")
    (gimp-edit-bucket-fill layerMap 2 0 100 255 FALSE 0 0) ; Add pattern for bump map purposes
    (gimp-selection-none image)
    (gimp-image-set-active-layer image layerCentre)
    (plug-in-bump-map 1 image layerCentre layerMap 135 45 centreBump 0 0 0 0 1 0 0) ; then apply bump map. 
    (gimp-selection-layer-alpha layerMap)
)    ; Remove bump map layer and merge remaining 2 layers.

The new version is more readable reducing the number of hard-coded constants:
Compare the legacy:
Code:
(gimp-drawable-transform-rotate petal-layer 1.05 FALSE (/ measure 2) (/ measure 2) 0 2 FALSE 3 1)
with the modern:
Code:
(gimp-drawable-transform-rotate layerNext angle-rotation FALSE side/2 side/2 0 2 FALSE 3 1)
with the following variables that are constant in the context of given input parameters:
Code:
(define side/2 (/ side 2))
(define angle-rotation (/ *pi* 3))

If you run the definition of the angle-rotation constant in the Script-Fu console:
Code:
(define angle-rotation (/ *pi* 3))
angle-rotation
;-> 1.047197551

We introduce the convention of the green arrow comment meaning that Script-Fu returns the result after this arrow.

We retrieve the legacy angle 1.05 in radian 
since the global constant *pi* is defined in line 350 of C:\Program Files\GIMP 2\share\gimp\2.0\scripts\script-fu-compat.init

Code:
(define *pi*
  (* 4 (atan 1.0))
)
*pi*
;-> 3.141592654

Script-Fu console:
Code:
(make-flower       500  0         1        60        0        5        '(228 163 255)  '(105 10 144)    '(192 225 255))
;-> Let us say it with 6 petals
[Image: attachment.php?aid=6896]

.webp   make-flowerPetal6.webp (Size: 12.58 KB / Downloads: 350)
Regards.
Reply


Messages In This Thread
Let us say with petals - by Gimphried - 10-21-2021, 09:15 PM
RE: Let us say with petals - by Ofnuts - 10-21-2021, 10:38 PM
Optimizing Script-Fu - by Gimphried - 10-22-2021, 08:57 PM

Forum Jump: