Liquify keeps pixels sharp, Smudge without ghost copies, Blur's own Radius, the largest Smear commits (13.16, part one)
This commit is contained in:
1 parent
b50657bcdb
commit
cbd8612014
15 files changed
+543
-51
No files matched your search
@@ -0,0 +1,55 @@
|
||||
#include "BrushFixtures.h"
|
||||
#include "Document/EditorSession.h"
|
||||
|
||||
// Swift's BlurBrushTests: Radius sets the reach, whatever the size.
|
||||
namespace {
|
||||
// A hard edge blurred along: row 50's gray columns.
|
||||
int spread(double radius, double diameter = 60)
|
||||
{
|
||||
EditorSession session;
|
||||
session.createDocument(200, 100);
|
||||
QImage image = BrushRaster::context(200, 100, false);
|
||||
image.fill(Qt::black);
|
||||
QPainter(&image).fillRect(QRect(100, 0, 100, 100), Qt::white);
|
||||
session.insert(ImportedImage(image, image, QStringLiteral("Edge")));
|
||||
session.selectTool(NavigationTool::blur);
|
||||
session.setBlurMode(BlurToolMode::blur);
|
||||
BrushSettings settings = session.brushSettings();
|
||||
settings.diameter = diameter;
|
||||
settings.hardness = 1;
|
||||
settings.blurRadius = radius;
|
||||
session.setBrushSettings(settings);
|
||||
session.beginBrush(QPointF(100, 20));
|
||||
session.continueBrush(QPointF(100, 80));
|
||||
session.finishBrushImmediately();
|
||||
const QImage result = session.activeLayer().value().asset.value().image().convertToFormat(QImage::Format_RGBA8888_Premultiplied);
|
||||
int gray = 0;
|
||||
for (int x = 0; x < 200; ++x) {
|
||||
const int value = result.constScanLine(50)[x * 4];
|
||||
gray += value > 10 && value < 245;
|
||||
}
|
||||
return gray;
|
||||
}
|
||||
}
|
||||
|
||||
class BlurBrushTests : public QObject {
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void radiusSetsHowFarItSoftens();
|
||||
};
|
||||
|
||||
void BlurBrushTests::radiusSetsHowFarItSoftens()
|
||||
{
|
||||
const int narrow = spread(2), wide = spread(12);
|
||||
QVERIFY2(wide > narrow * 2, qPrintable(QString("%1 against %2").arg(narrow).arg(wide)));
|
||||
QCOMPARE(narrow, 6);
|
||||
QCOMPARE(wide, 42);
|
||||
// The brush size leaves the reach alone.
|
||||
QCOMPARE(spread(12, 80), wide);
|
||||
// Past its ends, Radius holds to 0.5 and 50.
|
||||
QCOMPARE(spread(0.1), spread(0.5));
|
||||
QCOMPARE(spread(80), spread(50));
|
||||
}
|
||||
|
||||
QTEST_GUILESS_MAIN(BlurBrushTests)
|
||||
#include "BlurBrushTests.moc"
|
||||
+10
-9
@@ -54,7 +54,7 @@ private slots:
|
||||
void aPointSpreadsAlongTheBellCurve();
|
||||
void clearEdgesFadeAndClampedEdgesHold();
|
||||
void grayBlursAndBadInputIsRefused();
|
||||
void theSampleFollowsTheBrushSize();
|
||||
void theSampleFollowsTheRadius();
|
||||
void aMaskSampleKeepsItsEdgeToneAndPlacement();
|
||||
void aBlurStrokeSoftensUnderTheBrushAsOneStep();
|
||||
void aBlurStrokeSoftensAMask();
|
||||
@@ -127,16 +127,17 @@ void BlurToolTests::grayBlursAndBadInputIsRefused()
|
||||
QVERIFY_THROWS_EXCEPTION(std::logic_error, PixelAdjust::gaussianBlur(step.convertToFormat(QImage::Format_RGB32), 2, true));
|
||||
}
|
||||
|
||||
void BlurToolTests::theSampleFollowsTheBrushSize()
|
||||
void BlurToolTests::theSampleFollowsTheRadius()
|
||||
{
|
||||
EditorSession session;
|
||||
blurring(session, BrushSettings{.diameter = 60});
|
||||
const CanvasDocument document = session.document().value();
|
||||
const QImage sharp = edge(60, 40);
|
||||
// A tenth of the size, within 1.5 and 30.
|
||||
for (const auto &[diameter, sigma] : {std::pair(60.0, 6.0), std::pair(5.0, 1.5), std::pair(900.0, 30.0)}) {
|
||||
// The bar's Radius, within 0.5 and 50, whatever the size.
|
||||
for (const auto &[radius, sigma] : {std::pair(6.0, 6.0), std::pair(0.1, 0.5), std::pair(90.0, 50.0)}) {
|
||||
BrushSettings settings = session.brushSettings();
|
||||
settings.diameter = diameter;
|
||||
settings.blurRadius = radius;
|
||||
settings.diameter = 3 * radius;
|
||||
session.setBrushSettings(settings);
|
||||
QCOMPARE(session.blurSample(document).value(), PixelAdjust::gaussianBlur(sharp, sigma, false));
|
||||
}
|
||||
@@ -149,7 +150,7 @@ void BlurToolTests::theSampleFollowsTheBrushSize()
|
||||
void BlurToolTests::aMaskSampleKeepsItsEdgeToneAndPlacement()
|
||||
{
|
||||
EditorSession session;
|
||||
blurring(session, BrushSettings{.diameter = 20});
|
||||
blurring(session, BrushSettings{.diameter = 20, .blurRadius = 2});
|
||||
const QUuid id = session.activeLayerID().value();
|
||||
// A white mask with a black bar, placed mid-layer.
|
||||
QImage mask(20, 20, QImage::Format_Grayscale8);
|
||||
@@ -185,7 +186,7 @@ void BlurToolTests::aMaskSampleKeepsItsEdgeToneAndPlacement()
|
||||
record(snapshot, wideID).maskPlacement = LayerTransform{.origin = {30, 10}, .size = {40, 40}, .rotation = 30};
|
||||
record(snapshot, wideID).maskLinked = false;
|
||||
});
|
||||
turned.setBrushSettings(BrushSettings{.diameter = 5});
|
||||
turned.setBrushSettings(BrushSettings{.diameter = 5, .blurRadius = 1.5});
|
||||
const QImage soft = turned.blurSample(turned.document().value(), true).value();
|
||||
// Past the true box, inside one turned the other way.
|
||||
QVERIFY2(soft.constScanLine(36)[73] > 240, qPrintable(QString::number(soft.constScanLine(36)[73])));
|
||||
@@ -195,7 +196,7 @@ void BlurToolTests::aMaskSampleKeepsItsEdgeToneAndPlacement()
|
||||
void BlurToolTests::aBlurStrokeSoftensUnderTheBrushAsOneStep()
|
||||
{
|
||||
EditorSession session;
|
||||
blurring(session, BrushSettings{.diameter = 20, .hardness = 1});
|
||||
blurring(session, BrushSettings{.diameter = 20, .hardness = 1, .blurRadius = 2});
|
||||
const int steps = session.history.undoCount();
|
||||
session.beginBrush(QPointF(30, 12));
|
||||
QVERIFY(session.brushStroke() && session.brushStroke()->isBlur && !session.warpStroke());
|
||||
@@ -225,7 +226,7 @@ void BlurToolTests::aBlurStrokeSoftensUnderTheBrushAsOneStep()
|
||||
void BlurToolTests::aBlurStrokeSoftensAMask()
|
||||
{
|
||||
EditorSession session;
|
||||
blurring(session, BrushSettings{.diameter = 20, .hardness = 1});
|
||||
blurring(session, BrushSettings{.diameter = 20, .hardness = 1, .blurRadius = 2});
|
||||
const QUuid id = session.activeLayerID().value();
|
||||
QImage mask(60, 40, QImage::Format_Grayscale8);
|
||||
for (int y = 0; y < 40; ++y)
|
||||
|
||||
@@ -39,6 +39,7 @@ private slots:
|
||||
void cloneStampShowsAlignedSampleAndTheSourceNote();
|
||||
void theSmearShowsItsThreeModes();
|
||||
void smoothingShowsWithTheBrushAlone();
|
||||
void blurHasItsOwnRadius();
|
||||
};
|
||||
|
||||
void BrushControlsTests::theTitleAndModesFollowTheSession()
|
||||
@@ -306,5 +307,86 @@ void BrushControlsTests::smoothingShowsWithTheBrushAlone()
|
||||
QVERIFY(field.isVisible());
|
||||
}
|
||||
|
||||
// Swift's Radius: Blur alone, apart from Strength.
|
||||
void BrushControlsTests::blurHasItsOwnRadius()
|
||||
{
|
||||
EditorSession session;
|
||||
session.createDocument(8, 8, true);
|
||||
session.selectTool(NavigationTool::blur);
|
||||
BrushControls bar(session);
|
||||
bar.show();
|
||||
auto &field = find<QLineEdit>(bar, "blurRadius");
|
||||
auto &slider = find<QSlider>(bar, "blurRadiusSlider");
|
||||
QVERIFY(!field.isVisible() && !slider.isVisible());
|
||||
session.setBlurMode(BlurToolMode::blur);
|
||||
QVERIFY(field.isVisible() && slider.isVisible());
|
||||
QCOMPARE(session.brushSettings().blurRadius, 5.0);
|
||||
QCOMPARE(field.text(), QString("5"));
|
||||
QCOMPARE(field.toolTip(), QString("How far the blur softens, in pixels"));
|
||||
QCOMPARE(field.accessibleName(), QString("Radius"));
|
||||
QVERIFY(slider.minimum() == 50 && slider.maximum() == 2000 && slider.value() == 500 && slider.width() == 100 && field.width() == 42);
|
||||
slider.setValue(1250);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 12.5);
|
||||
QCOMPARE(field.text(), QString("12.5"));
|
||||
// Typed, it applies on Return, clamped, past the slider's end.
|
||||
type(field, "33.33");
|
||||
QCOMPARE(session.brushSettings().blurRadius, 12.5);
|
||||
QTest::keyClick(&field, Qt::Key_Return);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 33.33);
|
||||
QCOMPARE(field.text(), QString("33.3"));
|
||||
QCOMPARE(slider.value(), 2000);
|
||||
// Return on what it shows rounds nothing.
|
||||
QTest::keyClick(&field, Qt::Key_Return);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 33.33);
|
||||
type(field, "90");
|
||||
QTest::keyClick(&field, Qt::Key_Return);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 50.0);
|
||||
type(field, "0");
|
||||
QTest::keyClick(&field, Qt::Key_Return);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 0.5);
|
||||
QCOMPARE(field.text(), QString("0.5"));
|
||||
type(field, "abc");
|
||||
QTest::keyClick(&field, Qt::Key_Return);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 0.5);
|
||||
QCOMPARE(field.text(), QString("0.5"));
|
||||
// Arrows step a pixel, Shift ten, within bounds.
|
||||
QTest::keyClick(&field, Qt::Key_Up);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 1.5);
|
||||
QCOMPARE(field.text(), QString("1.5"));
|
||||
QTest::keyClick(&field, Qt::Key_Up, Qt::ShiftModifier);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 11.5);
|
||||
QTest::keyClick(&field, Qt::Key_Down, Qt::ShiftModifier);
|
||||
QTest::keyClick(&field, Qt::Key_Down, Qt::ShiftModifier);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 0.5);
|
||||
// While typed in, other changes leave the typing.
|
||||
type(field, "7");
|
||||
BrushSettings settings = session.brushSettings();
|
||||
settings.blurRadius = 3;
|
||||
session.setBrushSettings(settings);
|
||||
QCOMPARE(field.text(), QString("7"));
|
||||
QTest::keyClick(&field, Qt::Key_Return);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 7.0);
|
||||
// The label scrubs a tenth a point.
|
||||
QLabel *radius = nullptr;
|
||||
for (QLabel *label : bar.findChildren<QLabel *>())
|
||||
if (label->text() == QLatin1String("Radius"))
|
||||
radius = label;
|
||||
QVERIFY(radius && radius->isVisible());
|
||||
QMouseEvent press(QEvent::MouseButtonPress, QPointF(2, 2), radius->mapToGlobal(QPointF(2, 2)), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
|
||||
QApplication::sendEvent(radius, &press);
|
||||
QMouseEvent move(QEvent::MouseMove, QPointF(22, 2), radius->mapToGlobal(QPointF(22, 2)), Qt::NoButton, Qt::LeftButton, Qt::NoModifier);
|
||||
QApplication::sendEvent(radius, &move);
|
||||
QCOMPARE(session.brushSettings().blurRadius, 9.0);
|
||||
QCOMPARE(field.text(), QString("9"));
|
||||
QMouseEvent release(QEvent::MouseButtonRelease, QPointF(22, 2), radius->mapToGlobal(QPointF(22, 2)), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
|
||||
QApplication::sendEvent(radius, &release);
|
||||
// Smudge, Liquify and the other tools have none.
|
||||
session.setBlurMode(BlurToolMode::smudge);
|
||||
QVERIFY(!field.isVisible());
|
||||
session.setBlurMode(BlurToolMode::blur);
|
||||
session.selectTool(NavigationTool::brush);
|
||||
QVERIFY(!field.isVisible() && !slider.isVisible());
|
||||
}
|
||||
|
||||
QTEST_MAIN(BrushControlsTests)
|
||||
#include "BrushControlsTests.moc"
|
||||
@@ -305,7 +305,9 @@ void BrushTests::masksPaintInGrayAndCommitWhole()
|
||||
|
||||
void BrushTests::wildSettingsAndPointsAreRefused()
|
||||
{
|
||||
for (const BrushSettings &settings : {red(0), red(2001), red(NAN), red(20, 1.1), red(20, -0.1), brush(20, 1, 1, 0, 0, 0.001), brush(20, 1, 1, 0, 0, INFINITY)})
|
||||
// Past the bar's 2000, the app's own commits reach 2100.
|
||||
QCOMPARE(BrushStroke(blankLayer(600, 80), false, red(2100), QSizeF(600, 80)).settings.diameter, 2100.0);
|
||||
for (const BrushSettings &settings : {red(0), red(2100.5), red(NAN), red(20, 1.1), red(20, -0.1), brush(20, 1, 1, 0, 0, 0.001), brush(20, 1, 1, 0, 0, INFINITY)})
|
||||
QVERIFY_THROWS_EXCEPTION(ProjectError, BrushStroke(blankLayer(600, 80), false, settings, QSizeF(600, 80)));
|
||||
// A billion-pixel grid is refused while still a double.
|
||||
QImage strip = BrushRaster::context(30000, 1, false);
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
#include "BrushFixtures.h"
|
||||
#include "Document/EditorSession.h"
|
||||
#include "IO/ImageExporter.h"
|
||||
#include "SessionFixtures.h"
|
||||
#include <cmath>
|
||||
|
||||
// Swift's LargeCanvasBrushTests: the largest Smear on a big canvas.
|
||||
namespace {
|
||||
// Eight coloured bands across a square canvas.
|
||||
void bands(EditorSession &session, int side)
|
||||
{
|
||||
session.createDocument(side, side);
|
||||
QImage image = BrushRaster::context(side, side, false);
|
||||
{
|
||||
QPainter painter(&image);
|
||||
for (int band = 0; band < 8; ++band)
|
||||
painter.fillRect(QRect(band * side / 8, 0, side / 8 + 1, side), QColor::fromRgbF(band / 7.0, 0.4, 1 - band / 7.0));
|
||||
}
|
||||
session.insert(ImportedImage(image, image, QStringLiteral("Bands")));
|
||||
session.selectTool(NavigationTool::blur);
|
||||
BrushSettings settings = session.brushSettings();
|
||||
settings.diameter = 2000;
|
||||
settings.hardness = 0.5;
|
||||
session.setBrushSettings(settings);
|
||||
}
|
||||
|
||||
void stroke(EditorSession &session, int side)
|
||||
{
|
||||
const double y = side / 2.0;
|
||||
session.beginBrush(QPointF(1000, y));
|
||||
for (int step = 1; step <= 20; ++step)
|
||||
session.continueBrush(QPointF(1000 + step * 100.0, y + (step % 3) * 40.0));
|
||||
session.finishBrushImmediately();
|
||||
}
|
||||
}
|
||||
|
||||
class LargeCanvasBrushTests : public QObject {
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void largestBrushCommits_data();
|
||||
void largestBrushCommits();
|
||||
void aThinnedCommitLandsAllTheWarpMoved();
|
||||
};
|
||||
|
||||
void LargeCanvasBrushTests::largestBrushCommits_data()
|
||||
{
|
||||
QTest::addColumn<BlurToolMode>("mode");
|
||||
QTest::newRow("liquify") << BlurToolMode::liquify;
|
||||
QTest::newRow("smudge") << BlurToolMode::smudge;
|
||||
}
|
||||
|
||||
// The commit's tip runs past Size's 2000, once refused.
|
||||
void LargeCanvasBrushTests::largestBrushCommits()
|
||||
{
|
||||
QFETCH(BlurToolMode, mode);
|
||||
EditorSession session;
|
||||
bands(session, 5000);
|
||||
session.setBlurMode(mode);
|
||||
const ImageIdentity before = session.activeLayer().value().asset.value().identity();
|
||||
stroke(session, 5000);
|
||||
QCOMPARE(session.brushError(), std::optional<QString>());
|
||||
QVERIFY(session.activeLayer().value().asset.value().identity() != before);
|
||||
QCOMPARE(session.history.undoName(), rawValue(mode));
|
||||
}
|
||||
|
||||
// A point every twentieth of the tip covers the warp.
|
||||
void LargeCanvasBrushTests::aThinnedCommitLandsAllTheWarpMoved()
|
||||
{
|
||||
EditorSession session;
|
||||
bands(session, 400);
|
||||
session.setBlurMode(BlurToolMode::liquify);
|
||||
BrushSettings settings = session.brushSettings();
|
||||
settings.diameter = 160;
|
||||
settings.hardness = 1;
|
||||
session.setBrushSettings(settings);
|
||||
// Hard to its rim, ending a rim before a band.
|
||||
session.beginBrush(QPointF(60, 200));
|
||||
session.continueBrush(QPointF(273, 213));
|
||||
const QImage live = session.warpStroke()->image().copy();
|
||||
QCOMPARE(int(session.warpStroke()->points().size()), 54);
|
||||
session.finishBrush();
|
||||
const QImage landed = ImageExporter::render(session.projectSnapshot().value()).image.convertToFormat(QImage::Format_RGBA8888_Premultiplied);
|
||||
int apart = 0;
|
||||
for (int y = 0; y < 400; ++y)
|
||||
apart += std::memcmp(landed.constScanLine(y), live.constScanLine(y), 400 * 4) != 0;
|
||||
QCOMPARE(apart, 0);
|
||||
QCOMPARE(session.history.undoName(), QString("Liquify"));
|
||||
// A tight circle: chords between kept points stay close.
|
||||
session.beginBrush(QPointF(260, 200));
|
||||
for (int step = 1; step <= 72; ++step)
|
||||
session.continueBrush(QPointF(200 + 60 * std::cos(step * M_PI / 36), 200 + 60 * std::sin(step * M_PI / 36)));
|
||||
const QImage circled = session.warpStroke()->image().copy();
|
||||
session.finishBrush();
|
||||
const QImage turned = ImageExporter::render(session.projectSnapshot().value()).image.convertToFormat(QImage::Format_RGBA8888_Premultiplied);
|
||||
for (int y = 0; y < 400; ++y)
|
||||
apart += std::memcmp(turned.constScanLine(y), circled.constScanLine(y), 400 * 4) != 0;
|
||||
QCOMPARE(apart, 0);
|
||||
}
|
||||
|
||||
QTEST_GUILESS_MAIN(LargeCanvasBrushTests)
|
||||
#include "LargeCanvasBrushTests.moc"
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "SessionFixtures.h"
|
||||
#include <cmath>
|
||||
|
||||
// The Smear's warps against Swift's own arithmetic, replayed here.
|
||||
// The Smear against Swift's arithmetic and shader, replayed here.
|
||||
namespace {
|
||||
// Swift's WarpStroke, written again from SmudgeLiquify.swift.
|
||||
struct Oracle {
|
||||
@@ -15,6 +15,9 @@ struct Oracle {
|
||||
bool smudging;
|
||||
std::vector<float> carried, scratch;
|
||||
std::optional<QPointF> last;
|
||||
// Liquify, as MetalWarp's shader: the untouched pixels and offsets.
|
||||
std::vector<uchar> original;
|
||||
std::vector<float> offsets;
|
||||
|
||||
int radius() const { return int(std::ceil(diameter / 2)); }
|
||||
float weight(float u) const
|
||||
@@ -57,9 +60,9 @@ struct Oracle {
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
uchar &p = pixels[size_t((y * width + x) * 4 + k)];
|
||||
float &carry = carried[size_t(((dy + r) * side + dx + r) * 4 + k)];
|
||||
const float under = float(p), painted = under + (carry - under) * w;
|
||||
const float under = float(p), painted = under + (carry - under) * w * keep;
|
||||
p = uchar(std::max(0.0f, std::min(255.0f, std::round(painted))));
|
||||
carry = painted + (carry - painted) * keep;
|
||||
carry = painted;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,12 +77,17 @@ struct Oracle {
|
||||
if (x0 > x1 || y0 > y1)
|
||||
return;
|
||||
const int cw = x1 - x0 + 1, ch = y1 - y0 + 1;
|
||||
scratch.assign(size_t(cw * ch * 4), 0);
|
||||
if (original.empty()) {
|
||||
original = pixels;
|
||||
offsets.assign(size_t(width * height * 2), 0);
|
||||
}
|
||||
scratch.assign(size_t(cw * ch * 2), 0);
|
||||
for (int y = 0; y < ch; ++y)
|
||||
for (int x = 0; x < cw; ++x)
|
||||
for (int k = 0; k < 4; ++k)
|
||||
scratch[size_t((y * cw + x) * 4 + k)] = float(pixels[size_t(((y + y0) * width + x + x0) * 4 + k)]);
|
||||
for (int k = 0; k < 2; ++k)
|
||||
scratch[size_t((y * cw + x) * 2 + k)] = offsets[size_t(((y + y0) * width + x + x0) * 2 + k)];
|
||||
const float invR = 1 / float(diameter / 2);
|
||||
const auto mix = [](float a, float b, float t) { return a + (b - a) * t; };
|
||||
for (int dy = -r; dy <= r; ++dy)
|
||||
for (int dx = -r; dx <= r; ++dx) {
|
||||
const int y = cy + dy, x = cx + dx;
|
||||
@@ -94,11 +102,19 @@ struct Oracle {
|
||||
if (ix < 0 || iy < 0)
|
||||
continue;
|
||||
const float fx = sx - float(ix), fy = sy - float(iy);
|
||||
const auto at = [&](int ox, int oy, int k) { return scratch[size_t(((iy + oy) * cw + ix + ox) * 4 + k)]; };
|
||||
const auto at = [&](int ox, int oy, int k) { return scratch[size_t(((iy + oy) * cw + ix + ox) * 2 + k)]; };
|
||||
float moved[2];
|
||||
for (int k = 0; k < 2; ++k)
|
||||
moved[k] = mix(mix(at(0, 0, k), at(1, 0, k), fx), mix(at(0, 1, k), at(1, 1, k), fx), fy) - (k == 0 ? mx : my) * w;
|
||||
offsets[size_t((y * width + x) * 2)] = moved[0];
|
||||
offsets[size_t((y * width + x) * 2 + 1)] = moved[1];
|
||||
const float px = std::clamp(float(x) + moved[0], 0.0f, float(width - 1)), py = std::clamp(float(y) + moved[1], 0.0f, float(height - 1));
|
||||
const int jx = std::min(int(px), width - 2), jy = std::min(int(py), height - 2);
|
||||
const float gx = px - float(jx), gy = py - float(jy);
|
||||
const auto c = [&](int ox, int oy, int k) { return float(original[size_t(((jy + oy) * width + jx + ox) * 4 + k)]) / 255; };
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
const float top = at(0, 0, k) + (at(1, 0, k) - at(0, 0, k)) * fx;
|
||||
const float bottom = at(0, 1, k) + (at(1, 1, k) - at(0, 1, k)) * fx;
|
||||
pixels[size_t((y * width + x) * 4 + k)] = uchar(std::max(0.0f, std::min(255.0f, std::round(top + (bottom - top) * fy))));
|
||||
const float colour = mix(mix(c(0, 0, k), c(1, 0, k), gx), mix(c(0, 1, k), c(1, 1, k), gx), gy);
|
||||
pixels[size_t((y * width + x) * 4 + k)] = uchar(std::clamp(std::round(colour * 255), 0.0f, 255.0f));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,7 +128,7 @@ struct Oracle {
|
||||
}
|
||||
const QPointF from = *last;
|
||||
const double distance = std::hypot(point.x() - from.x(), point.y() - from.y());
|
||||
const double spacing = std::max(1.0, diameter * (smudging ? 0.08 : 0.025));
|
||||
const double spacing = std::max(1.0, diameter * (smudging ? 0.005 : 0.025));
|
||||
if (distance < spacing)
|
||||
return;
|
||||
const int steps = int(std::ceil(distance / spacing));
|
||||
@@ -147,7 +163,7 @@ Oracle oracle(const QImage &image, const BrushSettings &settings, bool smudging)
|
||||
for (int y = 0; y < image.height(); ++y)
|
||||
std::memcpy(bytes.data() + size_t(y * image.width() * 4), image.constScanLine(y), size_t(image.width() * 4));
|
||||
return Oracle{bytes, image.width(), image.height(), std::max(2.0, settings.diameter), std::min(0.98, std::max(0.0, settings.hardness)),
|
||||
std::min(1.0, std::max(0.01, settings.opacity)), smudging, {}, {}, std::nullopt};
|
||||
std::min(1.0, std::max(0.01, settings.opacity)), smudging, {}, {}, std::nullopt, {}, {}};
|
||||
}
|
||||
|
||||
// Every byte the stroke made, against the oracle's.
|
||||
@@ -181,6 +197,7 @@ class SmudgeLiquifyTests : public QObject {
|
||||
private slots:
|
||||
void liquifyAndSmudgeFollowSwiftsArithmetic_data();
|
||||
void liquifyAndSmudgeFollowSwiftsArithmetic();
|
||||
void liquifyHoldsItsSourcesToTheEdges();
|
||||
void theSettingsAreClampedAsSwiftClampsThem();
|
||||
void liquifyCommitsAlongThePathAsOneStep();
|
||||
void theCommitLandsAllTheWarpMoved();
|
||||
@@ -217,9 +234,9 @@ void SmudgeLiquifyTests::liquifyAndSmudgeFollowSwiftsArithmetic()
|
||||
ImageLayer(ImportedImage(image, image, QStringLiteral("Scene")), QPointF(0, 0)).transform, QSizeF(80, 40),
|
||||
smudging ? BlurToolMode::smudge : BlurToolMode::liquify, settings);
|
||||
Oracle expected = oracle(image, settings, smudging);
|
||||
// Along, back across, off the canvas and in again.
|
||||
// Along, back, off the canvas, then in from edges.
|
||||
for (const QPointF point : {QPointF(30.6, 20.2), QPointF(47.7, 23.1), QPointF(47.9, 23.3), QPointF(48.4, 23.3), QPointF(20, 31.5), QPointF(-6, 37),
|
||||
QPointF(12, 9.5)}) {
|
||||
QPointF(12, 9.5), QPointF(79.4, 38.6), QPointF(64, 30.2), QPointF(70, 0.3), QPointF(66.5, 12)}) {
|
||||
stroke.append(point);
|
||||
expected.append(point);
|
||||
QCOMPARE(differences(stroke, expected), 0);
|
||||
@@ -228,6 +245,26 @@ void SmudgeLiquifyTests::liquifyAndSmudgeFollowSwiftsArithmetic()
|
||||
QVERIFY(stroke.points().size() > 10);
|
||||
}
|
||||
|
||||
// Pushed in from each edge, sources clamp at the ramps.
|
||||
void SmudgeLiquifyTests::liquifyHoldsItsSourcesToTheEdges()
|
||||
{
|
||||
QImage ramps = BrushRaster::context(80, 40, false);
|
||||
for (int y = 0; y < 40; ++y)
|
||||
for (int x = 0; x < 80; ++x)
|
||||
ramps.setPixel(x, y, qRgba(x * 3, y * 6, 90, 255));
|
||||
const ImageLayer layer(ImportedImage(ramps, ramps, QStringLiteral("Ramps")), QPointF(0, 0));
|
||||
const BrushSettings settings = brush(30, 0.8, 0, 0, 0, 1);
|
||||
WarpStroke stroke(layer, ramps, layer.transform, QSizeF(80, 40), BlurToolMode::liquify, settings);
|
||||
Oracle expected = oracle(ramps, settings, false);
|
||||
for (const QPointF point : {QPointF(79.5, 20), QPointF(62, 20), QPointF(40, 39.5), QPointF(40, 24), QPointF(0.5, 20), QPointF(16, 20),
|
||||
QPointF(40, 0.5), QPointF(40, 15)}) {
|
||||
stroke.append(point);
|
||||
expected.append(point);
|
||||
QCOMPARE(differences(stroke, expected), 0);
|
||||
}
|
||||
QVERIFY(stroke.image() != ramps);
|
||||
}
|
||||
|
||||
void SmudgeLiquifyTests::theSettingsAreClampedAsSwiftClampsThem()
|
||||
{
|
||||
const QImage image = scene(20, 20);
|
||||
@@ -282,7 +319,7 @@ void SmudgeLiquifyTests::theCommitLandsAllTheWarpMoved()
|
||||
for (int y = 0; y < 40; ++y)
|
||||
apart += std::memcmp(landed.constScanLine(y), live.constScanLine(y), 80 * 4) != 0;
|
||||
QCOMPARE(apart, 0);
|
||||
QVERIFY(pixel(live, 41, 19) != pixel(scene(80, 40), 41, 19));
|
||||
QVERIFY(pixel(live, 41, 15) != pixel(scene(80, 40), 41, 15));
|
||||
// Shift's line starts where the warp ended, on the pixels.
|
||||
QCOMPARE(session.shiftLineStart().value(), to);
|
||||
}
|
||||
@@ -296,10 +333,13 @@ void SmudgeLiquifyTests::smudgeCarriesColourAndNamesItsStep()
|
||||
session.continueBrush(QPointF(50, 12));
|
||||
session.finishBrush();
|
||||
QCOMPARE(session.history.undoName(), QString("Smudge"));
|
||||
// The core carries red all the way; its flanks fade.
|
||||
QCOMPARE(exported(session, 49, 12), (std::vector<int>{255, 0, 0, 255}));
|
||||
const std::vector<int> near = exported(session, 42, 15), far = exported(session, 49, 15);
|
||||
QVERIFY2(near[0] > far[0] + 20 && far[0] > 0 && far[2] > 0, qPrintable(QString("%1 %2 %3").arg(near[0]).arg(far[0]).arg(far[2])));
|
||||
// Red drags into the blue, fading along the way.
|
||||
QCOMPARE(exported(session, 41, 12), (std::vector<int>{233, 0, 22, 255}));
|
||||
QCOMPARE(exported(session, 45, 12), (std::vector<int>{94, 0, 161, 255}));
|
||||
QCOMPARE(exported(session, 49, 12), (std::vector<int>{9, 0, 246, 255}));
|
||||
// Its flanks less so; the rim not at all.
|
||||
QCOMPARE(exported(session, 42, 15), (std::vector<int>{61, 0, 194, 255}));
|
||||
QCOMPARE(exported(session, 49, 15), (std::vector<int>{0, 0, 255, 255}));
|
||||
QCOMPARE(exported(session, 75, 35), (std::vector<int>{0, 0, 255, 255}));
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,9 @@ compositor_test(SpotHealingTests)
|
||||
compositor_test(CloneStampTests)
|
||||
compositor_test(CloneStampCanvasTests)
|
||||
compositor_test(SmudgeLiquifyTests)
|
||||
compositor_test(WarpStrokeTests)
|
||||
compositor_test(LargeCanvasBrushTests)
|
||||
compositor_test(BlurBrushTests)
|
||||
compositor_test(BlurToolTests)
|
||||
compositor_test(SmearCanvasTests)
|
||||
compositor_test(GradientTests)
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
#include "BrushFixtures.h"
|
||||
#include "Document/SmudgeLiquify.h"
|
||||
#include <cmath>
|
||||
|
||||
// Swift's MetalWarpTests: Liquify stays sharp, Smudge leaves no ghosts.
|
||||
namespace {
|
||||
WarpStroke warp(const QImage &image, BlurToolMode mode, const BrushSettings &settings)
|
||||
{
|
||||
const ImageLayer layer(ImportedImage(image, image, QStringLiteral("Picture")), QPointF(0, 0));
|
||||
return WarpStroke(layer, image, layer.transform, QSizeF(image.size()), mode, settings);
|
||||
}
|
||||
}
|
||||
|
||||
class WarpStrokeTests : public QObject {
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void liquifyStaysSharp();
|
||||
void liquifyDrawsFromTheUntouchedLayer();
|
||||
void smudgeLeavesOneFadingTrail();
|
||||
};
|
||||
|
||||
// Pushed across and back, the layer ends nearly unchanged.
|
||||
void WarpStrokeTests::liquifyStaysSharp()
|
||||
{
|
||||
// Swift's photo: two ramps, a checkerboard, a half-clear strip.
|
||||
QImage image = BrushRaster::context(300, 200, false);
|
||||
for (int y = 0; y < 200; ++y) {
|
||||
uchar *row = image.scanLine(y);
|
||||
for (int x = 0; x < 300; ++x) {
|
||||
const int a = x < 250 ? 255 : 128;
|
||||
const int check = (x / 12 + y / 12) % 2 == 0 ? 220 : 40;
|
||||
row[x * 4] = uchar(x * 255 / 300 * a / 255);
|
||||
row[x * 4 + 1] = uchar(y * 255 / 200 * a / 255);
|
||||
row[x * 4 + 2] = uchar(check * a / 255);
|
||||
row[x * 4 + 3] = uchar(a);
|
||||
}
|
||||
}
|
||||
WarpStroke stroke = warp(image, BlurToolMode::liquify, brush(60, 0.3, 0, 0, 0, 0.7));
|
||||
for (double x = 60; x <= 200; x += 4)
|
||||
stroke.append(QPointF(x, 100));
|
||||
for (double x = 200; x >= 60; x -= 4)
|
||||
stroke.append(QPointF(x, 100));
|
||||
double change = 0;
|
||||
for (int y = 0; y < 200; ++y)
|
||||
for (int x = 0; x < 300 * 4; ++x)
|
||||
change += std::abs(int(stroke.image().constScanLine(y)[x]) - int(image.constScanLine(y)[x]));
|
||||
change /= 300.0 * 200 * 4;
|
||||
// Swift measured 0.49 sampled once, 2.34 resampled per dab.
|
||||
QVERIFY2(change < 1, qPrintable(QString::number(change)));
|
||||
QVERIFY(change > 0);
|
||||
}
|
||||
|
||||
// Half-pixel pushes add up to one: stripes stay crisp.
|
||||
void WarpStrokeTests::liquifyDrawsFromTheUntouchedLayer()
|
||||
{
|
||||
QImage image = BrushRaster::context(60, 20, false);
|
||||
for (int x = 0; x < 60; ++x)
|
||||
for (int y = 0; y < 20; ++y)
|
||||
image.setPixel(x, y, qRgba(x % 2 ? 200 : 0, 0, 0, 255));
|
||||
WarpStroke stroke = warp(image, BlurToolMode::liquify, brush(10, 0.98, 0, 0, 0, 0.5));
|
||||
stroke.append(QPointF(20, 10));
|
||||
stroke.append(QPointF(21, 10));
|
||||
stroke.append(QPointF(22, 10));
|
||||
// Resampled per dab these read 100; now each, one stripe.
|
||||
QCOMPARE(pixel(stroke.image(), 22, 10), (std::vector<int>{200, 0, 0, 255}));
|
||||
QCOMPARE(pixel(stroke.image(), 21, 10), (std::vector<int>{0, 0, 0, 255}));
|
||||
QCOMPARE(pixel(stroke.image(), 23, 10), (std::vector<int>{0, 0, 0, 255}));
|
||||
QCOMPARE(pixel(stroke.image(), 40, 10), (std::vector<int>{0, 0, 0, 255}));
|
||||
QCOMPARE(pixel(stroke.image(), 41, 10), (std::vector<int>{200, 0, 0, 255}));
|
||||
}
|
||||
|
||||
void WarpStrokeTests::smudgeLeavesOneFadingTrail()
|
||||
{
|
||||
QImage image = BrushRaster::context(300, 100, false);
|
||||
image.fill(QColor::fromRgbF(0.1, 0.1, 0.1));
|
||||
{
|
||||
QPainter painter(&image);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.setBrush(Qt::white);
|
||||
painter.drawEllipse(QRectF(52, 42, 16, 16));
|
||||
}
|
||||
WarpStroke stroke = warp(image, BlurToolMode::smudge, brush(40, 0.5, 0, 0, 0, 0.6));
|
||||
for (double x = 60; x <= 240; x += 3)
|
||||
stroke.append(QPointF(x, 50));
|
||||
std::vector<int> row;
|
||||
for (int x = 70; x < 240; ++x)
|
||||
row.push_back(stroke.image().constScanLine(50)[x * 4]);
|
||||
// A ghost is a bump: brighter than two either side.
|
||||
int peaks = 0;
|
||||
for (size_t i = 2; i + 2 < row.size(); ++i)
|
||||
peaks += row[i] > row[i - 2] + 2 && row[i] > row[i + 2] + 2;
|
||||
QCOMPARE(peaks, 0);
|
||||
QVERIFY2(row.front() > row.back() + 20, qPrintable(QString("%1 to %2").arg(row.front()).arg(row.back())));
|
||||
}
|
||||
|
||||
QTEST_GUILESS_MAIN(WarpStrokeTests)
|
||||
#include "WarpStrokeTests.moc"
|
||||
Reference in new issue
Block a user