Create your own G’MIC filter for GIMP – Part II

In part II of this series we will finish our filter that we started in Part I by adding two more sliders for contrast and saturation. And we want that preview!

Beginning with the preview, a basic version is like so:

#@gimp My.Basics : my_basic_adjusts, my_basic_adjusts

The complete code is now:

#@gimp My.Basics : my_basic_adjusts, my_basic_adjusts
#@gimp : Brightness = int (0,-100,100)

my_basic_adjusts:
-gimp_adjust_colors $1,0,0,0,0,0

Save the .gmic file and reload the G’MIC plugin.When you set the slider to 50, the preview window shows clearly now that the image is way too bright.

brightness.preview

You can set the initial zoom ratio of the preview of a filter this way:

#@gimp My.Basics : my_basic_adjusts, my_basic_adjusts(0)

The 0 stands for a 1:1 preview, 1 shows the full image, 10 is 100% (so again 1:1), 20 is 200% and so on.

Two more sliders

We can add the contrast and saturation sliders in the same way we did with the brightness slider, just add these two lines below the brightness line:

#@gimp : Contrast = int (0,-100,100)
#@gimp : Saturation = int (0,-100,100)

We have seen that when the user changes the brightness slider, the chosen value was stored in the variable $1. Same thing for contrast and saturation, those values are stored in the variables $2 and $3. When you add more functions and sliders, their values will be stored in the variables $4, $5, $6, and so on.

In the original filter Basic adjustments there are five sliders, for brightness, contrast, gamma, hue and saturation.

5sliders

If you change the brightness slider, the first 0 in the line below will change. If you change the contrast slider, the second 0 in the line below will change. If you change the saturation slider, the fifth 0 in the line below will change.

-gimp_adjust_colors 0,0,0,0,0,0

To pass the user set values of the three sliders to our function, we say this:

-gimp_adjust_colors $1,$2,0,0,$3,0

The complete filter code is now:

#@gimp My.Basics : my_basic_adjusts,my_basic_adjusts(1)
#@gimp : Brightness = int (0,-100,100)
#@gimp : Contrast = int (0,-100,100)
#@gimp : Saturation = int (0,-100,100)

my_basic_adjusts:
-gimp_adjust_colors $1,$2,0,0,$3,0

As you can see, this is not rocket science!

3sliders

Before/After view

Sometimes you want to compare the results of a filter with the original (part of the) photo, using a Before/After split preview window. This is what the last parameter of our function is about:

-gimp_adjust_colors 0,0,0,0,0,0

To add this functionality to our filter, we first add the following line to the code, above the ‘my_basics_adjust’ function.

#@gimp : Preview = choice (“Full”,”Forward horizontal”,”Forward vertical”,”Backward horizontal”, “Backward vertical”,”Duplicate horizontal”,”Duplicate vertical”)

(See below for what this means). You are not obliged to offer all seven choices; five or three – it’s up to you!

We are going to slightly change the filter code: we make one command that ‘does the work’ (= the actual filter) and another command that handles the split preview using the gmic function -gimp_split_preview.

The complete code including the Before/After preview is this:

#@gimp My.Basics : my_basic_adjusts,my_basic_adjusts_preview(1)
#@gimp : Brightness = int (0,-100,100)
#@gimp : Contrast = int (0,-100,100)
#@gimp : Saturation = int (0,-100,100)

#@gimp : Preview = choice (“Full”,”Forward horizontal”,”Forward vertical”,”Backward horizontal”, “Backward vertical”,”Duplicate horizontal”,”Duplicate vertical”)

my_basic_adjusts:
-gimp_adjust_colors $1,$2,0,0,$3,0

my_basic_adjusts_preview:
-gimp_split_preview “my_basic_adjusts $*”,$-1

And that results in this:

before.after

(Exaggerated to show better the effect of the Before/After view).

Thanks to Mr. G’MIC David Tschumperlé to clarify and improve some code here. What happens is that our function my_basic_adjusts is fed to the preview function, including its parameters $1, $2 and $3. The $-1 corresponds to the last parameter of the my_basic_adjusts function and that is the value of the preview choice, in other words, the preview type that the user has chosen.

Full‘ means the preview shows the whole photo. The Forward and Backward choices above refer to the order the split window is shown. Forward means: show the result in the split window on the down side (Forward horizontal) or on the right side (Forward vertical). Backward means: show the result in the split window on the upper side (Backward horizontal) or on the left side (Backward vertical).

The Forward and Backward previews show a certain part of the photo: half of it is original, half of it is filtered. You can also duplicate the same area and show the effects of the filter on the right or the lower side, as is done in the screenshot above.

In the third article in this series I will group some denoise filters together. By keeping only the Strength slider (or equivalent) for each filter, you can quickly see the effects on your photo and the differences between them.

Banner at the top of the page: G’MIC filter Frame [smooth], in section Frames.

hor.bar
pm|jul16

Advertisements