Making better custom filters - GreycLab/gmic-community GitHub Wiki

0-Introduction

Here, it is supposed that you read the previous article, [How to create a custom filter in the G’mic plug-in], and are ready for more. The official goal is give tricks to make slicker and more reliable filters.

1-Better preview

1.1-Default zoom

You can specify what should be the default zoom for your preview by pasting a float number between parenthesis to the preview command at the first line end of your custom filter. Ex:

#@gui Better Filter  : better_filter, better_filter_preview(0)

(0) means 1:1 preview, (1) means previewing the whole image, (2) means 1/2 image and so on…

1.2-Split preview

You must have seen that some filters propose a split preview, so you can accurately compare the existing image with the future filtered one. You can add this functionality to your filter if you:

  1. add a new choice option dedicated to the preview type and
  2. embed your filter command inside the -gimp_split_preview command.

The filter from the previous article would become:

#@gui Better Filter  : better_filter, better_filter_preview(0)
#@gui : Size = float(0.3,0,5)
#@gui : Amplitude for B&W pencil= float(60,0,200)
#@gui : Amplitude for Soft glow = float(1,0,20)
#@gui : sep = separator()
#@gui : Preview type = choice("Full","Forward horizontal","Forward vertical","Backward horizontal","Backward vertical")
better_filter :
  --gimp_pencilbw $1,$2,0,0,0
  -gimp_glow[1] $3,0,0
  -compose_multiply

better_filter_preview :
  -gimp_split_preview "-better_filter ${1--2}",$-1
img src="shaving_filter_preview.png" [Thanks to Editor B, CC-by](http://www.flickr.com/photos/editor/3370897686)

2-Multiple input management

2.1-A possible issue

As a user, if you have several layers and want to apply the filter above on each of them, you might feel natural to just set the Input layer option to All and proceed. But with the filter as it is right now, you won’t get the expected result. This is because the -gimp_pencilbw and the -compose_multiply commands are applied on every layers without discernment. To address this issue, one solution is to create a loop inside which, each layer is considered one by one.

2.2-The saving loop

For that, you create a -repeat ... -done loop that runs as many times as you have layers. Then, inside that loop, you isolate the layer that has to be processed this time with a -local ... -endlocal block. The filter then becomes:

#@gui Better Filter  : better_filter, better_filter_preview(0)
#@gui : Size = float(0.3,0,5)
#@gui : Amplitude for B&W pencil= float(60,0,200)
#@gui : Amplitude for Soft glow = float(1,0,20)
#@gui : sep = separator(), Preview type = choice("Full","Forward horizontal","Forward vertical","Backward horizontal","Backward vertical")
better_filter :
  -repeat @# -local[$>]
    --gimp_pencilbw $1,$2,0,0,0
    -gimp_glow[1] $3,0,0
    -compose_multiply
  -endlocal -done

better_filter_preview :
  -gimp_split_preview "-better_filter ${1--2}",$-1

The variable @# returns the number of layers. If you have 3 layers the code inside the loop will be run 3 times. The variable $> is the counter telling you how many times the loop has been run so far. So, for 3 layers, it takes successively the values 0,1,2. Only one layer enter the -local[$>] ... -endlocal block, and inside that block, it is considered as being number [0].

3-Sort out your filters

Having many custom filters in your .gmic file can create a mess in your plug-in window. To sort out that, you can create directories! The next line means : “everything that follows will be placed in the directory called: My Directory”

#@gui My Directory

Whereas that one means : “everything that follows will be in the parent directory”

#@gui _

So put the first one at the top of your .gmic file and the second one at the bottom and things are sorted out.

⚠️ **GitHub.com Fallback** ⚠️