Writing an effect plugin
An effect does not have to be built into Tessel. A plugin is a shared library holding one class, and the effects inside it are declared exactly the way the forty built in ones are: a lambda and a list of parameters. Tessel reads the plugin directories at startup, and whatever loads appears in the Effects menu under the submenu it asked for, with the same generated dialog and the same live preview.
Where plugins live
Tessel scans a plugins directory in each of these places, in this order,
and loads every shared library it finds there. Nothing is scanned later: a plugin
dropped in while the program is running appears the next time it starts.
- the per user application data directory, which on Linux is
~/.local/share/Tessel/Tessel/plugins - the system wide ones, such as
/usr/share/Tessel/Tessel/plugins pluginsbeside the executable, which is what makesbuild/pluginswork in a build tree
The exact list is not worth memorising, because the program prints it: open Effects > About the Plugins. That dialog is the answer to both "where does this go" and "why did mine not load", and it lists every plugin that loaded, the effects each one brought, and every file that was refused with the reason it was refused.
Anything in those directories that is not a shared library is ignored without complaint, so a licence file or a readme can sit next to the plugin.
The interface
The whole contract is src/effects/EffectPlugin.h. Include it, derive one
class from QObject and tessel::EffectPlugin, and implement
three things:
TESSEL_DECLARE_EFFECT_PLUGIN()- A macro that records which version of the interface the plugin was compiled
against. The host refuses a plugin built against any other version, because
everything crossing the library boundary (
Effect,ParamSpec,EffectContext,Image) is a C++ object whose layout has to match on both sides. Rebuilding against the current source is the fix. QString name() const- The name of the plugin, shown in About the Plugins. There are optional
version()andauthor()beside it. void registerEffects(tessel::EffectRegistry&)- Called once at startup, after the built in effects are in place. Add effects with
fx::add(registry, id, name, category, params, body), which is the same one liner the built in modules use. The registry is handed over rather than looked up, so a plugin never touches the host's singleton.
The class also needs the two Qt macros: Q_PLUGIN_METADATA(IID
TesselEffectPluginIid), which is what makes the library a Qt plugin at all,
and Q_INTERFACES(tessel::EffectPlugin), which is what lets Tessel cast
the loaded object to the interface. Leave either of them out and the library is
refused as not being a Tessel effect plugin.
Two details decide where the effect turns up. The id has to be unique
across the whole program, so give it a prefix of your own; an id that is already taken
is ignored. The category is the Effects submenu: an existing name such
as Blurs or Stylize joins that submenu, a new name gets a
submenu of its own after the built in ones, and Adjustments puts the
effect in the Adjustments menu instead.
The example, in full
plugins/example in the repository is a working plugin with nothing left
out. It adds one effect, Scanlines, with three parameters, which is enough to see the
generated dialog and the live preview at work. Build it with
-DTESSEL_BUILD_EXAMPLE_PLUGIN=ON and it is written into
build/plugins, where a ./build/tessel finds it.
ExamplePlugin.h
// An effect plugin, complete, in two files. Build it with
// -DTESSEL_BUILD_EXAMPLE_PLUGIN=ON and it lands in build/plugins, which is one
// of the directories Tessel reads at startup.
#pragma once
#include "effects/EffectPlugin.h"
namespace tessel_example {
class ScanlinesPlugin : public QObject, public tessel::EffectPlugin {
Q_OBJECT
Q_PLUGIN_METADATA(IID TesselEffectPluginIid)
Q_INTERFACES(tessel::EffectPlugin)
public:
TESSEL_DECLARE_EFFECT_PLUGIN()
QString name() const override { return QStringLiteral("Scanlines Example"); }
QString version() const override { return QStringLiteral("1.0"); }
QString author() const override { return QStringLiteral("The Tessel authors"); }
void registerEffects(tessel::EffectRegistry& registry) override;
};
} // namespace tessel_example
ExamplePlugin.cpp
#include "ExamplePlugin.h"
#include <algorithm>
#include "core/Blend.h"
namespace tessel_example {
using namespace tessel;
void ScanlinesPlugin::registerEffects(EffectRegistry& registry)
{
// The same one liner the built in effects use. The three parameters are
// what the dialog is built from, in this order, and the body below runs on
// every preview as the user drags them.
fx::add(registry, "example_scanlines", "Scanlines", "Examples",
{
ParamSpec::intParam("spacing", "Line spacing", 2, 64, 4, " px"),
ParamSpec::doubleParam("strength", "Strength", 0.0, 1.0, 0.6, 0.05, 2),
ParamSpec::colorParam("tint", "Line colour", Rgba(0, 0, 0, 255)),
},
[](const EffectContext& context, const ParamValues& values) {
const int spacing = std::max(2, values.intValue("spacing", 4));
const Rgba tint = values.colorValue("tint", Rgba(0, 0, 0, 255));
// The tint's own alpha scales the strength, so a half
// transparent colour draws a fainter line without touching the
// slider.
const float weight = float(values.doubleValue("strength", 0.6))
* float(tint.a) / 255.0f;
// mapPixels copies the source, rewrites the region and feeds
// the progress channel, which is what makes a slow effect
// cancellable from the dialog.
return fx::mapPixels(context, [&](Rgba c, int, int y) {
if (y % spacing != 0)
return c;
const float keep = 1.0f - weight;
return Rgba(toByte((c.r * keep + tint.r * weight) / 255.0f),
toByte((c.g * keep + tint.g * weight) / 255.0f),
toByte((c.b * keep + tint.b * weight) / 255.0f), c.a);
});
});
}
} // namespace tessel_example
That is the entire plugin. The three ParamSpec entries become two
sliders, each with a number box beside it, and a colour button, in that order, and the
dialog previews the effect on the canvas as they are dragged. The other kinds are
boolParam, choiceParam, angleParam and
offsetParam; an effect with no parameters at all is applied straight away
with no dialog.
Building one
Out of tree, a plugin needs the Tessel headers to compile and the core archive to
link, because fx::add and the parameter specs live there:
cmake_minimum_required(VERSION 3.21)
project(ScanlinesPlugin LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
find_package(Qt6 6.4 REQUIRED COMPONENTS Core Gui)
qt_standard_project_setup()
qt_add_plugin(scanlines SHARED CLASS_NAME ScanlinesPlugin)
target_sources(scanlines PRIVATE ExamplePlugin.h ExamplePlugin.cpp)
# The Tessel source tree, for the headers, and its core archive, for fx::add
# and the parameter specs.
target_include_directories(scanlines PRIVATE /path/to/tessel/src)
target_link_libraries(scanlines PRIVATE /path/to/tessel/build/libtessel_core.a
Qt6::Core Qt6::Gui)
Copy the resulting library into one of the directories above and restart Tessel. The plugin must be built with the same compiler and the same Qt as the Tessel it is loaded into: it shares C++ objects with the program, not a C interface.
What an effect body may do
The body is a plain function of its inputs, and the preview relies on that:
- It runs on a worker thread, possibly many times a second while the dialog is open, so it must not touch the interface or any shared state of its own.
- It must be deterministic for a given context and parameter set. A random effect
takes its seed from
context.seed, which is why the previews do not flicker. - It returns a full size image, and leaves the pixels outside
context.regionas they were: the caller mixes the result back through the selection mask.fx::mapPixelsdoes both for you. - It should report progress through
context.progressand give up when that channel says it was cancelled, whichfx::forEachRowandfx::mapPixelsalready do. An effect that ignores it still works, it just cannot be stopped early.
Everything an effect is allowed to look at is in EffectContext: the source
image, the selection mask, the region, the two palette colours, the seed and the
document.
Because it is a plain function of its inputs, an effect from a plugin can be used everywhere a built in one can: applied to a layer from the menu, held live by an adjustment layer, or kept as an entry in a layer's effect stack. A document that stores such an entry records the effect's id, not a pointer into the registry, so it still opens and composites on a machine where the plugin is not installed: the entry passes the pixels through and says (not installed) until the plugin comes back.
When one does not load
A plugin that fails is never silent and never fatal. The status bar says so for a few seconds at startup, and Effects > About the Plugins keeps the reason for as long as the session lasts. The reasons are short and there are not many of them:
- the library could not be opened
- Qt's own message, which usually names a dependency that is missing or a Qt version that does not match.
- a Qt plugin, but not a Tessel effect plugin
- The id in
Q_PLUGIN_METADATAis notorg.tessel.EffectPlugin, orQ_INTERFACESwas left out. - built against effect plugin interface version N
- The plugin was compiled against a different
EffectPlugin.h. Rebuild it against this version of Tessel.
A plugin that loaded stays loaded for as long as the program runs, because the effects it registered are code inside its library. One that was refused is unloaded again immediately, having registered nothing.