Tip #18 - part twoAs anticipated, now we go one level down, from the "end user" view (how to use the filter)
to the coder view (how the filter is written in G'MIC language).
To analyse the code, first I show it, expanded for better understanding of the single commands, that is:
- one command per line
- no short format of the commands
#@gimp Stained glass : gimp_stained_glass, gimp_stained_glass_preview(0)
#@gimp : Edges = float(20,0,100)
#@gimp : Shading = float(0.1,0,0.5)
#@gimp : Thin separators = bool(1)
#@gimp : Sep = separator()
#@gimp : Equalize = bool(1)
#@gimp : Colors = float(1,0,3)
#@gimp : Gamma = float(0,-1.2,1.2)
#@gimp : Contrast = float(1,0,4)
#@gimp : Brightness = float(0,-255,255)
#@gimp : Sep = separator(), Preview type = choice("Full","Forward horizontal","Forward vertical","Backward horizontal","Backward vertical","Duplicate horizontal","Duplicate vertical")
#@gimp : Sep = separator(), note = note("<small>Author: <i>David Tschumperlé</i>. Latest update: <i>2011/03/18</i>.</small>")
gimp_stained_glass :
-repeat @#
-local[$>]
-split_opacity #separate colors from opacity
-local[0] #work only on colors
-stained_glass $1,$2,$3
-normalize 0,255
-if $4
-equalize[-1]
-endif
-rgb2lab[-1]
-shared[-1] 1,2
-mul[-1] $5
-remove[-1]
-lab2rgb[-1]
-apply_gamma[-1] {10^$6}
-sub[-1] 128
-mul[-1] $7
-add[-1] {128+$8}
-cut[-1] 0,255
-endlocal
-append c
-endlocal
-done
gimp_stained_glass_preview :
-gimp_split_preview "-gimp_stained_glass $*",$-1
All the lines at the beginning, starting with
#@gimp, are definitions: name of the command, parameters, etc.
The last lines specify the preview:
-preview the command with all the parameters (
'$*' is substituted by a copy of the specified string of arguments)
-preview starts with no split (
'$-1')
About the latter: did you ever play with the preview type? It's very nice the possibility to have the effect shown only on a half of the image (in many ways).Now the body of the filter.
-repeat @# (
@#’ is substituted by the current number of images in the list)
("-repeat" starts a loop which ends at the line "-done")
the initial line asks G'mic to apply the filter to ALL the images referred to in input;
well, one says: there is always only ONE image on which you can call a G'MIC filter from Gimp;yes, that's true in Gimp language, not in G'mic language, because a Gimp image can have more than one layer and each layer is considered as an image by G'mic.
If you call G'mic starting from an image with two (or more) layers, then select "All" in Input/Output,
when running the filter all the layers are modified according to the filter
(in our case: all the layers will be transformed in a stained glass image).
Try:
1-open an image
2-duplicate the layer
3-select the filter (Pattern/Stained Glass)
4-run it (press OK) with all the default values (including the default Input/Output values)
result is: your image with ONLY the top layer transformed into a stained glass.
Now:
5-Ctrl+Z to reset your image at the status before calling the filter
6-reselect the filter (Filters/Re-Show "G'MIC")
7-change the option in Input Layers to "All"
result is: both layers of your image have been transformed into a stained glass.
-local[$>] ($> refers to the indice of the running ’repeat..done’ loop
("-local" start a capsule of code which end at "-endlocal")
all code that is encapsulated between a -local and a -endlocal is applied to the currently active image (layer in terms of Gimp) in the stack of images (layers).
-split_opacity this command splits colour and opacity parts of the image;
this is necessary because the next command (-stained_glass, which performs the main function of the filter) requires to operate on a RGB image, without the A channel (without the transparency layer, in Gimp terms).
The user could have called the filter having in input RGBA layer(s)
(for instance, when experimenting with more layers than just the background).
Omitting this command, could therefore lead to abort the filter.
There are other commands which could have been used to avoid the opacity layer being on the game, for instance
-remove_opacitybut then the source image(s) would have been altered.
Instead, the -split_opacity keeps aside the opacity layer during the process but is then rejoined to the final image(s) by using the command
-append c, at the end.
-local[0] starts a new capsule of code, which operates only on the colours, ignoring the opacity
this capsule will terminate with the -endlocal command before the -append c command, which restores the opacity.
-stained_glass $1,$2,$3this is the real centre of the function. This command will be studied in detail on a next post.
Now, we continue with the next lines.
-normalize 0,255we know very well already this command, so no need to re-discuss it.
-if $4
-equalize[-1]
-endif Now the filter takes care of the fourth user parameter, which is a boolean value.
Thus the meaning of -if $4 is (as for many languages using boolean values): if the parameter was set to 1 (or true) ...
The equalisation command operates much like the Gimp Colors>Auto>Equalize
(from Gimp documentation: "The Equalize command automatically adjusts the brightness of colors across the active layer so that the histogram for the Value channel is as nearly flat as possible, that is, so that each possible brightness value appears at about the same number of pixels as every other value")-rgb2lab[-1]this command transforms the image from the RGB colour space to the Lab colour space.
As we know, RGB is composed by three "channels": Red, Green and Blue.
Lab also is composed by three "channels", which are: Lightness, "a" and "b";
"a" and "b" are arbitrary names, they could have been named as well Greta and Bob, but probably
better names could have been GM and BY, at least they would have been more mnemonic;
in fact, the "a" channel keeps the balance between Green and Magenta, while the "b" channel
keeps the balance between Blue and Yellow.
I stop here for now, the analysis will continue next days...