Add a tiles effect

The two-dimensional counterpart of scanlines: rectangular tiles cut at
random positions from the source images, in three arrangements. A grid
draws its column widths and row heights once so the cells stay aligned
even when jittered; brick repacks every row on its own so the vertical
seams do not meet; scatter fills the canvas from one of the images and
drops loose tiles over it.

The plan is drawn serially from the rng as scanlines does, but scattered
tiles overlap, so they are painted in plan order on one thread: letting
threads race for the same pixels would leave the output depending on
their scheduling. A source smaller than a tile is repeated to fill the
cell, sideways and then downwards, which keeps grid and brick gapless.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Si
2026-09-18 22:10:54 +02:00
co-authored by Claude Opus 5
parent 1d0343b90a
commit 749ea239bb
6 changed files with 293 additions and 2 deletions
+85
View File
@@ -26,6 +26,7 @@
#include "effects/overlay.hpp"
#include "effects/scanlines.hpp"
#include "effects/text.hpp"
#include "effects/tiles.hpp"
namespace {
@@ -139,6 +140,15 @@ effector::Image renderText(const std::vector<effector::Image>& sources, int widt
return effect.render(sources, width, height, rng, merged);
}
effector::Image renderTiles(const std::vector<effector::Image>& sources, int width, int height,
std::uint64_t seed, const effector::ParamValues& values) {
effector::TilesEffect effect;
effector::Rng rng(seed);
auto merged = effect.defaults();
for (const auto& [key, value] : values) merged[key] = value;
return effect.render(sources, width, height, rng, merged);
}
// The three channels of one pixel, to read a placement off directly.
std::array<int, 3> pixel(const effector::Image& image, int x, int y) {
const auto* px = image.row(y) + static_cast<std::size_t>(x) * 3;
@@ -253,6 +263,81 @@ int main() {
check(threw, "no sources throws");
}
std::cout << "tiles\n";
{
std::vector<Image> sources = {gradient(1200, 800, 10), gradient(640, 480, 90),
gradient(300, 200, 200)};
for (const std::string layout : {"grid", "brick", "scatter"}) {
const ParamValues values = {{"tile_layout", layout},
{"tile_width", 70L},
{"tile_height", 50L},
{"tile_jitter", 12L}};
const auto first = renderTiles(sources, 400, 300, 1, values);
const auto again = renderTiles(sources, 400, 300, 1, values);
const auto other = renderTiles(sources, 400, 300, 2, values);
check(first.width() == 400 && first.height() == 300, layout + " has the requested size");
check(sameBytes(first, again), layout + ": same seed reproduces the image");
check(!sameBytes(first, other), layout + ": a different seed changes the image");
}
// With no jitter the grid cells line up: the gradient's red channel
// counts up with x inside a tile and only breaks at a cell boundary.
const auto aligned = renderTiles(sources, 400, 300, 5,
{{"tile_layout", std::string("grid")}, {"tile_width", 100L},
{"tile_height", 100L}});
bool onlyAtBoundaries = true;
for (int y = 0; y < aligned.height(); ++y) {
const auto* row = aligned.row(y);
for (int x = 1; x < aligned.width(); ++x) {
const bool breaks = row[x * 3] != ((row[(x - 1) * 3] + 1) & 0xFF);
if (breaks && x % 100 != 0) onlyAtBoundaries = false;
}
}
check(onlyAtBoundaries, "grid cells break only on the cell boundaries");
// One source smaller than a single tile: the cut is repeated to fill
// the cell, so the canvas is covered rather than left black.
std::vector<Image> small = {gradient(60, 40, 200)};
const auto covered = renderTiles(small, 500, 400, 9,
{{"tile_layout", std::string("brick")},
{"tile_width", 200L},
{"tile_height", 150L}});
bool filled = true;
for (int y = 0; y < covered.height(); ++y) {
const auto* row = covered.row(y);
for (int x = 0; x < covered.width(); ++x) filled = filled && row[x * 3 + 2] == 200;
}
check(filled, "a source smaller than a tile still covers the canvas");
const auto tiny = renderTiles(sources, 120, 90, 3,
{{"tile_width", 1L}, {"tile_height", 1L}});
check(tiny.width() == 120 && tiny.height() == 90, "tiles one pixel across");
const auto ordered = renderTiles(sources, 300, 200, 4,
{{"tile_width", 40L}, {"tile_keep_order", true}});
check(ordered.height() == 200, "keep_order fills the canvas");
// A single scattered tile leaves the rest of the canvas to the backdrop.
const auto sparse = renderTiles(small, 400, 300, 6,
{{"tile_layout", std::string("scatter")}, {"tile_count", 1L}});
bool backdrop = true;
for (int y = 0; y < sparse.height(); ++y) {
const auto* row = sparse.row(y);
for (int x = 0; x < sparse.width(); ++x) backdrop = backdrop && row[x * 3 + 2] == 200;
}
check(backdrop, "scattered tiles land on a filled backdrop");
bool threw = false;
try {
renderTiles({}, 100, 100, 1, {});
} catch (const EffectError&) {
threw = true;
}
check(threw, "no sources throws");
}
std::cout << "blur\n";
{
std::vector<Image> sources = {gradient(1200, 800, 10), gradient(640, 480, 90)};