ZaStoGram_desktop/Telegram/SourceFiles/test/test_ink.cpp
John Preston cee5c94af7 [ai] Add a stress kit runner and a Release test harness
Task: 2026/10/30/run-the-widget-stress-kit-at-v0-4-0-on-a-release-build-without-lto
2026-11-05 17:12:56 +04:00

3566 lines
111 KiB
C++

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#if defined _DEBUG || defined TDESKTOP_RELEASE_TEST_HARNESS
#include "test/test_ink.h"
#include "test/test_capture.h"
#include "test/test_log.h"
#include "test/test_runner.h"
#include "ui/color_contrast.h"
#include <QtGui/QPainter>
#include <algorithm>
#include <array>
#include <cmath>
#include <map>
namespace Test {
namespace {
[[nodiscard]] QString ColorHex(QColor color) {
if (!color.isValid()) {
return u"invalid"_q;
}
return u"#%1%2%3"_q
.arg(color.red(), 2, 16, QChar('0'))
.arg(color.green(), 2, 16, QChar('0'))
.arg(color.blue(), 2, 16, QChar('0'));
}
[[nodiscard]] QString CandidateName(const InkCandidate &candidate) {
return candidate.name.isEmpty()
? ColorHex(candidate.color)
: candidate.name;
}
[[nodiscard]] QString ScanReasonText(const InkScan &scan) {
return scan.reason.isEmpty()
? u"this reading was never scanned"_q
: scan.reason;
}
[[nodiscard]] double SegmentDistance(QColor p, QColor a, QColor b) {
const auto px = double(p.red());
const auto py = double(p.green());
const auto pz = double(p.blue());
const auto ax = double(a.red());
const auto ay = double(a.green());
const auto az = double(a.blue());
const auto dx = double(b.red()) - ax;
const auto dy = double(b.green()) - ay;
const auto dz = double(b.blue()) - az;
const auto len = dx * dx + dy * dy + dz * dz;
auto t = 0.;
if (len > 0.) {
t = ((px - ax) * dx + (py - ay) * dy + (pz - az) * dz) / len;
t = std::clamp(t, 0., 1.);
}
return std::max({
std::abs(px - (ax + t * dx)),
std::abs(py - (ay + t * dy)),
std::abs(pz - (az + t * dz)) });
}
[[nodiscard]] QColor RowMode(const QImage &image, QRect clip, int y) {
auto counts = std::vector<std::pair<QRgb, int>>();
for (auto x = clip.left(); x <= clip.right(); ++x) {
const auto value = image.pixel(x, y);
auto found = false;
for (auto &one : counts) {
if (one.first == value) {
++one.second;
found = true;
break;
}
}
if (!found && (counts.size() < 8192)) {
counts.push_back({ value, 1 });
}
}
auto best = QRgb(0);
auto bestCount = -1;
for (const auto &one : counts) {
if (one.second > bestCount) {
bestCount = one.second;
best = one.first;
}
}
return (bestCount > 0) ? QColor::fromRgb(best) : QColor();
}
[[nodiscard]] QRect PillBand(QRect box) {
if (box.isEmpty()) {
return box;
}
const auto radius = box.height() / 2;
const auto inner = box.adjusted(radius, 1, -radius, -1);
return (inner.width() >= 8) ? inner : box;
}
[[nodiscard]] QColor RegionMode(
const QImage &image,
QRect region,
QRect exclude) {
auto counts = std::vector<std::pair<QRgb, int>>();
const auto clip = region.intersected(image.rect());
for (auto y = clip.top(); y <= clip.bottom(); ++y) {
for (auto x = clip.left(); x <= clip.right(); ++x) {
if (exclude.contains(x, y)) {
continue;
}
const auto value = image.pixel(x, y);
auto found = false;
for (auto &one : counts) {
if (one.first == value) {
++one.second;
found = true;
break;
}
}
if (!found && (counts.size() < 8192)) {
counts.push_back({ value, 1 });
}
}
}
auto best = QRgb(0);
auto bestCount = -1;
for (const auto &one : counts) {
if (one.second > bestCount) {
bestCount = one.second;
best = one.first;
}
}
return (bestCount > 0) ? QColor::fromRgb(best) : QColor();
}
[[nodiscard]] bool SurroundingsAreFill(
const QImage &image,
QRect clip,
QColor fill) {
const auto ring = clip.adjusted(-1, -1, 1, 1).intersected(image.rect());
if (ring.isEmpty() || (ring == clip)) {
return false;
}
const auto mode = RegionMode(image, ring, clip);
return mode.isValid()
&& (ChannelDelta(mode, fill) <= kBackgroundSame);
}
// Own-colour distance to its own segment is 0, so a candidate closer than
// kInkMargin to another candidate's segment can never be the unique
// attribution. kOnLine is wider and would also refuse pixels that still
// attribute. The background is the scan's modal row colour: a row that is
// mostly ink reports that ink as its own mode and would refuse a palette
// the band still classifies.
struct CollinearPair {
int swallowed = -1;
int swallower = -1;
double distance = 0.;
};
[[nodiscard]] CollinearPair CollinearAgainstBackground(
const std::vector<InkCandidate> &candidates,
QColor background) {
auto result = CollinearPair();
if (!background.isValid()) {
return result;
}
for (auto i = 0; i != int(candidates.size()); ++i) {
for (auto j = 0; j != int(candidates.size()); ++j) {
if (i == j) {
continue;
}
const auto distance = SegmentDistance(
candidates[i].color,
background,
candidates[j].color);
if (distance >= kInkMargin) {
continue;
}
if ((result.swallowed >= 0) && (distance >= result.distance)) {
continue;
}
result.swallowed = i;
result.swallower = j;
result.distance = distance;
}
}
return result;
}
} // namespace
int ChannelDelta(QColor a, QColor b) {
if (!a.isValid() || !b.isValid()) {
return -1;
}
return std::max({
std::abs(a.red() - b.red()),
std::abs(a.green() - b.green()),
std::abs(a.blue() - b.blue()) });
}
QString InkScanStateName(InkScanState state) {
switch (state) {
case InkScanState::NotScanned:
return u"not-scanned"_q;
case InkScanState::OutsideImage:
return u"outside-image"_q;
case InkScanState::NoRowsInBand:
return u"no-rows-in-band"_q;
case InkScanState::CandidatesCollide:
return u"candidates-collide"_q;
case InkScanState::NoInk:
return u"no-ink"_q;
case InkScanState::Classified:
return u"classified"_q;
case InkScanState::BackgroundCollinear:
return u"background-collinear"_q;
}
return u"missing"_q;
}
bool Separable(const std::vector<InkCandidate> &candidates) {
for (auto i = 0; i != int(candidates.size()); ++i) {
for (auto j = i + 1; j != int(candidates.size()); ++j) {
if (ChannelDelta(candidates[i].color, candidates[j].color)
<= kSameTolerance) {
return false;
}
}
}
return true;
}
QString CollisionDump(const std::vector<InkCandidate> &candidates) {
for (auto i = 0; i != int(candidates.size()); ++i) {
for (auto j = i + 1; j != int(candidates.size()); ++j) {
const auto delta = ChannelDelta(
candidates[i].color,
candidates[j].color);
if (delta <= kSameTolerance) {
return u"%1(%2) %3(%4) delta=%5"_q
.arg(
CandidateName(candidates[i]),
ColorHex(candidates[i].color))
.arg(
CandidateName(candidates[j]),
ColorHex(candidates[j].color))
.arg(delta);
}
}
}
return u"none"_q;
}
InkCount InkScan::countAt(int index) const {
auto result = InkCount();
result.index = index;
const auto size = int(candidates.size());
const auto named = (index >= 0) && (index < size);
const auto inRange = named && (index < int(counts.size()));
if (named) {
result.name = CandidateName(candidates[index]);
result.color = candidates[index].color;
}
if ((state != InkScanState::Classified)
&& (state != InkScanState::NoInk)) {
result.refusal = u"index=%1 of %2: this reading (%3) classified "
u"nothing: %4"_q
.arg(index)
.arg(size)
.arg(InkScanStateName(state), ScanReasonText(*this));
return result;
}
if (!inRange) {
auto names = QString();
for (const auto &candidate : candidates) {
if (!names.isEmpty()) {
names += u", "_q;
}
names += CandidateName(candidate);
}
result.refusal = named
? u"index=%1 names a candidate this reading filled no count "
u"for: %2 counts for %3 candidates [%4]"_q
.arg(index)
.arg(int(counts.size()))
.arg(size)
.arg(names)
: u"index=%1 is outside this reading's %2 candidates [%3]"_q
.arg(index)
.arg(size)
.arg(names);
return result;
}
result.count = counts[index];
return result;
}
QString InkCountDetails(const InkCount &reading) {
const auto line = u"index=%1 name=%2 color=%3 count=%4"_q
.arg(reading.index)
.arg(
reading.name.isEmpty() ? u"none"_q : reading.name,
ColorHex(reading.color),
reading.read() ? QString::number(reading.count) : u"none"_q);
return reading.read()
? line
: (line + u" refusal=%1"_q.arg(reading.refusal));
}
DerivedBand DeriveBand(
const QImage &image,
QRect box,
QColor fill) {
// Keep rows whose modal background is the literal fill, take the columns
// those rows actually span, inset by the pill radius so rounded caps and
// the surface behind them are excluded, then re-assert the row background
// inside that narrower band and drop every row that no longer reads as
// the fill. A hit-box PillBand without this step inverted ink and fill.
auto result = DerivedBand();
const auto clip = box.intersected(image.rect());
if (clip.isEmpty() || !fill.isValid()) {
result.reason = u"the recovered box is empty or the fill is invalid"_q;
return result;
}
result.rowsExamined = clip.height();
auto rows = std::vector<int>();
auto left = clip.right() + 1;
auto right = clip.left() - 1;
for (auto y = clip.top(); y <= clip.bottom(); ++y) {
if (ChannelDelta(RowMode(image, clip, y), fill) > kBackgroundSame) {
continue;
}
auto first = -1;
auto last = -1;
for (auto x = clip.left(); x <= clip.right(); ++x) {
const auto delta = ChannelDelta(image.pixelColor(x, y), fill);
if (delta <= kBackgroundSame) {
if (first < 0) {
first = x;
}
last = x;
}
}
if (first < 0) {
continue;
}
rows.push_back(y);
left = std::min(left, first);
right = std::max(right, last);
}
result.fillRows = int(rows.size());
if (rows.empty() || (right < left)) {
result.reason = u"no row of the recovered box has the pill fill "
u"as its own background"_q;
return result;
}
if (SurroundingsAreFill(image, clip, fill)) {
result.reason = u"the requested fill is the image's background "
u"outside the candidate, so no band can be derived"_q;
return result;
}
result.fillRegion = QRect(
left,
rows.front(),
right - left + 1,
rows.back() - rows.front() + 1);
result.band = PillBand(result.fillRegion);
for (const auto y : rows) {
if ((y < result.band.top()) || (y > result.band.bottom())) {
continue;
}
if (ChannelDelta(RowMode(image, result.band, y), fill)
<= kBackgroundSame) {
result.rows.push_back(y);
}
}
if (result.rows.empty()) {
result.reason = u"no row of the derived band kept the pill fill "
u"as its own background"_q;
return result;
}
result.reason = u"none"_q;
result.ok = true;
return result;
}
InkScan ScanInk(
const QImage &image,
QRect band,
std::vector<InkCandidate> candidates,
const std::vector<int> &onlyRows) {
auto result = InkScan();
result.candidates = std::move(candidates);
result.counts.assign(result.candidates.size(), 0);
const auto clip = band.intersected(image.rect());
if (clip.isEmpty()) {
result.state = InkScanState::OutsideImage;
result.reason = u"the requested band [%1] does not intersect the "
u"image [%2], so no pixel was scanned"_q
.arg(RectText(band), RectText(image.rect()));
return result;
}
result.band = clip;
auto rows = std::vector<int>();
if (onlyRows.empty()) {
rows.reserve(clip.height());
for (auto y = clip.top(); y <= clip.bottom(); ++y) {
rows.push_back(y);
}
} else {
rows.reserve(onlyRows.size());
for (const auto y : onlyRows) {
if ((y >= clip.top()) && (y <= clip.bottom())) {
rows.push_back(y);
}
}
}
if (rows.empty()) {
result.state = InkScanState::NoRowsInBand;
result.reason = u"none of the %1 requested rows falls inside the "
u"band [%2], so no pixel was scanned"_q
.arg(int(onlyRows.size()))
.arg(RectText(clip));
return result;
}
result.ok = true;
result.scannedRows = int(rows.size());
auto rowBackground = std::vector<QColor>();
rowBackground.reserve(rows.size());
for (const auto y : rows) {
rowBackground.push_back(RowMode(image, clip, y));
}
auto backgroundCounts = std::vector<std::pair<QRgb, int>>();
for (const auto &one : rowBackground) {
auto found = false;
for (auto &entry : backgroundCounts) {
if (entry.first == one.rgb()) {
++entry.second;
found = true;
break;
}
}
if (!found) {
backgroundCounts.push_back({ one.rgb(), 1 });
}
}
auto bestBackground = -1;
for (const auto &entry : backgroundCounts) {
if (entry.second > bestBackground) {
bestBackground = entry.second;
result.background = QColor::fromRgb(entry.first);
}
}
const auto classify = Separable(result.candidates);
auto inkSum = std::vector<std::pair<QRgb, int>>();
auto classifiedSum = std::vector<std::pair<QRgb, int>>();
for (auto index = 0; index != int(rows.size()); ++index) {
const auto y = rows[index];
const auto background = rowBackground[index];
auto run = 0;
for (auto x = clip.left(); x <= clip.right(); ++x) {
const auto color = image.pixelColor(x, y);
const auto delta = ChannelDelta(color, background);
++result.total;
if (delta <= kBackgroundSame) {
++result.backgroundPixels;
}
run = (delta >= kInkDelta) ? (run + 1) : 0;
if (run > result.widestRun) {
result.widestRun = run;
result.widestRunRow = y - clip.top();
}
if (delta < kInkDelta) {
continue;
}
++result.inkPixels;
auto found = false;
for (auto &one : inkSum) {
if (one.first == color.rgb()) {
++one.second;
found = true;
break;
}
}
if (!found && (inkSum.size() < 8192)) {
inkSum.push_back({ color.rgb(), 1 });
}
if (!classify) {
++result.ambiguous;
continue;
}
auto best = -1;
auto bestDistance = 1e9;
auto secondDistance = 1e9;
for (auto i = 0; i != int(result.candidates.size()); ++i) {
const auto distance = SegmentDistance(
color,
background,
result.candidates[i].color);
if (distance < bestDistance) {
secondDistance = bestDistance;
bestDistance = distance;
best = i;
} else if (distance < secondDistance) {
secondDistance = distance;
}
}
const auto lone = (result.candidates.size() < 2);
if ((best >= 0)
&& (bestDistance <= kOnLine)
&& (lone || (secondDistance - bestDistance >= kInkMargin))) {
++result.counts[best];
auto seen = false;
for (auto &one : classifiedSum) {
if (one.first == color.rgb()) {
++one.second;
seen = true;
break;
}
}
if (!seen && (classifiedSum.size() < 8192)) {
classifiedSum.push_back({ color.rgb(), 1 });
}
} else {
++result.ambiguous;
}
}
}
auto bestInk = -1;
for (const auto &one : inkSum) {
if (one.second > bestInk) {
bestInk = one.second;
result.ink = QColor::fromRgb(one.first);
}
}
auto bestClassified = -1;
for (const auto &one : classifiedSum) {
if (one.second > bestClassified) {
bestClassified = one.second;
result.classifiedInk = QColor::fromRgb(one.first);
}
}
const auto collinear = CollinearAgainstBackground(
result.candidates,
result.background);
if (!classify) {
result.state = InkScanState::CandidatesCollide;
result.reason = u"the candidates do not separate from each other, "
u"so no pixel is attributed to one: %1 kSameTolerance=%2"_q
.arg(
CollisionDump(result.candidates),
QString::number(kSameTolerance));
} else if (!result.inkPixels) {
result.state = InkScanState::NoInk;
result.reason = u"no pixel of the band [%1] separated from its "
u"background %2: total=%3 kInkDelta=%4"_q
.arg(
RectText(clip),
ColorHex(result.background),
QString::number(result.total),
QString::number(kInkDelta));
} else if (collinear.swallowed >= 0) {
const auto &swallowed = result.candidates[collinear.swallowed];
const auto &swallower = result.candidates[collinear.swallower];
result.state = InkScanState::BackgroundCollinear;
result.reason = u"candidate "_q
+ CandidateName(swallowed)
+ u"("_q
+ ColorHex(swallowed.color)
+ u") lies on the segment from the measured background "_q
+ ColorHex(result.background)
+ u" to candidate "_q
+ CandidateName(swallower)
+ u"("_q
+ ColorHex(swallower.color)
+ u"), so none of its pixels can be attributed: distance="_q
+ QString::number(collinear.distance)
+ u" kInkMargin="_q
+ QString::number(kInkMargin);
} else {
result.state = InkScanState::Classified;
result.reason = u"none"_q;
}
return result;
}
float64 InkContrast(QColor a, QColor b) {
if (!a.isValid() || !b.isValid()) {
return 0.;
}
return Ui::CountContrast(a, b);
}
QString FormatInkReport(
QColor fill,
QColor ink,
float64 contrast,
int inkPixels) {
return u"fill=%1 ink=%2 contrast=%3 inkPx=%4"_q
.arg(ColorHex(fill), ColorHex(ink))
.arg(contrast)
.arg(inkPixels);
}
QString FormatInkScan(const InkScan &scan, QColor fill) {
// Every clause is resolved into its own fragment and the fragments are
// concatenated, never chained as one long .arg over a single format
// string: a chained .arg rescans text an earlier .arg already
// substituted, so a candidate name or a reason carrying its own
// %<digit> would be eaten by the next call in the chain.
const auto report = FormatInkReport(
fill,
scan.ink,
InkContrast(scan.ink, scan.background),
scan.inkPixels);
const auto measured = u" state=%1 band=%2 bg=%3 classifiedInk=%4"_q
.arg(
InkScanStateName(scan.state),
RectText(scan.band),
ColorHex(scan.background),
ColorHex(scan.classifiedInk));
const auto counted = u" total=%1 ambiguous=%2 rows=%3"_q
.arg(scan.total)
.arg(scan.ambiguous)
.arg(scan.scannedRows);
auto listed = QString();
for (auto i = 0; i != int(scan.candidates.size()); ++i) {
const auto reading = scan.countAt(i);
if (!listed.isEmpty()) {
listed += u" "_q;
}
listed += u"%1(%2)=%3"_q
.arg(
reading.name,
ColorHex(reading.color),
reading.read()
? QString::number(reading.count)
: u"none"_q);
}
const auto against = u" candidates=[%1] collision=%2"_q
.arg(listed, CollisionDump(scan.candidates));
const auto thresholds = u" kInkDelta=%1 kOnLine=%2 kInkMargin=%3 "
u"kSameTolerance=%4 kBackgroundSame=%5"_q
.arg(kInkDelta)
.arg(kOnLine)
.arg(kInkMargin)
.arg(kSameTolerance)
.arg(kBackgroundSame);
return report
+ measured
+ counted
+ against
+ thresholds
+ u" reason=%1"_q.arg(ScanReasonText(scan));
}
InkMeasure MeasurePaintedInk(
const QImage &image,
QRect box,
QColor fill,
std::vector<InkCandidate> candidates) {
auto result = InkMeasure();
result.derived = DeriveBand(image, box, fill);
result.separable = Separable(candidates);
result.collision = CollisionDump(candidates);
if (result.derived.ok) {
result.scan = ScanInk(
image,
result.derived.band,
std::move(candidates),
result.derived.rows);
result.contrast = InkContrast(
result.scan.ink,
result.scan.background);
result.report = FormatInkScan(result.scan, fill);
} else {
result.report = result.derived.reason;
}
return result;
}
namespace {
[[nodiscard]] int ChromaticSpread(QColor color) {
if (!color.isValid()) {
return 0;
}
const auto high = std::max({ color.red(), color.green(), color.blue() });
const auto low = std::min({ color.red(), color.green(), color.blue() });
return high - low;
}
[[nodiscard]] bool ExcludedColor(QColor color, QColor excluded) {
return excluded.isValid()
&& (ChannelDelta(color, excluded) <= kSameTolerance);
}
[[nodiscard]] QString ClusterRefusal(const ChromaticRaster &reading) {
if (reading.state == ChromaticRasterState::Found) {
return {};
}
const auto reason = reading.reason.isEmpty()
? u"this reading was never scanned"_q
: reading.reason;
return u"this reading (%1) found no chromatic cluster: %2"_q
.arg(ChromaticRasterStateName(reading.state), reason);
}
} // namespace
QString ChromaticRasterStateName(ChromaticRasterState state) {
switch (state) {
case ChromaticRasterState::NotScanned:
return u"not-scanned"_q;
case ChromaticRasterState::OutsideBand:
return u"raster-outside-band"_q;
case ChromaticRasterState::NoChromatic:
return u"no-chromatic"_q;
case ChromaticRasterState::BelowDensity:
return u"below-density"_q;
case ChromaticRasterState::Found:
return u"found"_q;
}
return u"missing"_q;
}
ChromaticBox ChromaticRaster::readBox() const {
auto result = ChromaticBox();
const auto refusal = ClusterRefusal(*this);
if (!refusal.isEmpty()) {
result.refusal = refusal;
return result;
}
result.box = box;
return result;
}
ChromaticMean ChromaticRaster::readMean() const {
auto result = ChromaticMean();
const auto refusal = ClusterRefusal(*this);
if (!refusal.isEmpty()) {
result.refusal = refusal;
return result;
}
result.color = mean;
return result;
}
ChromaticDensity ChromaticRaster::readDensity() const {
auto result = ChromaticDensity();
const auto refusal = ClusterRefusal(*this);
if (!refusal.isEmpty()) {
result.refusal = refusal;
return result;
}
result.density = density;
return result;
}
ChromaticRaster ReadChromaticRaster(
const QImage &image,
QRect band,
QColor pen,
QColor fill) {
auto result = ChromaticRaster();
const auto clip = band.intersected(image.rect());
if (clip.isEmpty()) {
result.state = ChromaticRasterState::OutsideBand;
result.reason = u"the requested band ["_q
+ RectText(band)
+ u"] does not intersect the image ["_q
+ RectText(image.rect())
+ u"], so no chromatic pixel was read"_q;
return result;
}
result.ok = true;
result.band = clip;
auto points = std::vector<QPoint>();
for (auto y = clip.top(); y <= clip.bottom(); ++y) {
for (auto x = clip.left(); x <= clip.right(); ++x) {
const auto color = image.pixelColor(x, y);
if (ChromaticSpread(color) < kChromaticFloor) {
continue;
}
if (ExcludedColor(color, pen) || ExcludedColor(color, fill)) {
continue;
}
points.push_back(QPoint(x, y));
}
}
result.totalChromatic = int(points.size());
if (points.empty()) {
result.state = ChromaticRasterState::NoChromatic;
result.reason = u"no pixel of the band ["_q
+ RectText(clip)
+ u"] cleared the chromatic floor "_q
+ QString::number(kChromaticFloor);
return result;
}
auto top = points.front().y();
auto bottom = top;
auto left = points.front().x();
auto right = left;
for (const auto &point : points) {
top = std::min(top, point.y());
bottom = std::max(bottom, point.y());
left = std::min(left, point.x());
right = std::max(right, point.x());
}
const auto origin = clip.left();
const auto columns = clip.width();
auto columnCount = std::vector<int>(columns, 0);
for (const auto &point : points) {
++columnCount[point.x() - origin];
}
auto prefix = std::vector<int>(columns + 1, 0);
for (auto i = 0; i != columns; ++i) {
prefix[i + 1] = prefix[i] + columnCount[i];
}
const auto window = std::max(bottom - top + 1, 1);
auto bestCount = -1;
auto bestX = left;
const auto lastStart = std::max(left, right - window + 1);
for (auto start = left; start <= lastStart; ++start) {
const auto from = start - origin;
const auto to = std::min(columns, from + window);
if ((from < 0) || (to < from)) {
continue;
}
const auto count = prefix[to] - prefix[from];
if (count > bestCount) {
bestCount = count;
bestX = start;
}
}
auto boxLeft = bestX + window;
auto boxRight = bestX - 1;
auto boxTop = bottom + 1;
auto boxBottom = top - 1;
auto sumR = qint64(0);
auto sumG = qint64(0);
auto sumB = qint64(0);
auto matched = 0;
for (const auto &point : points) {
if ((point.x() < bestX) || (point.x() >= bestX + window)) {
continue;
}
boxLeft = std::min(boxLeft, point.x());
boxRight = std::max(boxRight, point.x());
boxTop = std::min(boxTop, point.y());
boxBottom = std::max(boxBottom, point.y());
const auto color = image.pixelColor(point);
sumR += color.red();
sumG += color.green();
sumB += color.blue();
++matched;
}
if (matched <= 0) {
result.state = ChromaticRasterState::NoChromatic;
result.reason = u"no pixel of the band ["_q
+ RectText(clip)
+ u"] cleared the chromatic floor "_q
+ QString::number(kChromaticFloor);
return result;
}
const auto boxWidth = boxRight - boxLeft + 1;
const auto boxHeight = boxBottom - boxTop + 1;
const auto area = std::max(boxWidth * boxHeight, 1);
const auto density = float64(matched) / float64(area);
if (density >= kChromaticDensityFloor) {
result.state = ChromaticRasterState::Found;
result.reason = u"none"_q;
result.box = QRect(boxLeft, boxTop, boxWidth, boxHeight);
result.matched = matched;
result.density = density;
result.mean = QColor(
int(sumR / matched),
int(sumG / matched),
int(sumB / matched));
} else {
result.state = ChromaticRasterState::BelowDensity;
result.reason = u"the densest chromatic window stays under the "
"density floor: total="_q
+ QString::number(result.totalChromatic)
+ u" floor="_q
+ QString::number(kChromaticDensityFloor);
}
return result;
}
QString FormatChromaticRaster(const ChromaticRaster &reading) {
const auto box = reading.readBox();
const auto mean = reading.readMean();
const auto density = reading.readDensity();
const auto reason = reading.reason.isEmpty()
? u"this reading was never scanned"_q
: reading.reason;
return u"state="_q
+ ChromaticRasterStateName(reading.state)
+ u" band="_q
+ RectText(reading.band)
+ u" box="_q
+ (box.read() ? RectText(box.box) : u"none"_q)
+ u" matched="_q
+ (box.read() ? QString::number(reading.matched) : u"none"_q)
+ u" totalChromatic="_q
+ (reading.ok
? QString::number(reading.totalChromatic)
: u"none"_q)
+ u" density="_q
+ (density.read() ? QString::number(density.density) : u"none"_q)
+ u" mean="_q
+ (mean.read() ? ColorHex(mean.color) : u"none"_q)
+ u" reason="_q
+ reason;
}
namespace {
[[nodiscard]] QString GlyphReason(const GlyphCore &reading) {
return reading.reason.isEmpty()
? u"this reading was never scanned"_q
: reading.reason;
}
[[nodiscard]] QColor ExactModeColor(const QImage &image, QRect clip) {
auto backgroundCounts = std::vector<std::pair<QRgb, int>>();
for (auto y = clip.top(); y <= clip.bottom(); ++y) {
for (auto x = clip.left(); x <= clip.right(); ++x) {
const auto color = image.pixelColor(x, y);
const auto rgb = qRgb(color.red(), color.green(), color.blue());
auto found = false;
for (auto &one : backgroundCounts) {
if (one.first == rgb) {
++one.second;
found = true;
break;
}
}
if (!found && (backgroundCounts.size() < 8192)) {
backgroundCounts.push_back({ rgb, 1 });
}
}
}
auto bestBackground = -1;
auto backgroundRgb = QRgb(0);
for (const auto &one : backgroundCounts) {
if (one.second > bestBackground) {
bestBackground = one.second;
backgroundRgb = one.first;
}
}
return QColor::fromRgb(backgroundRgb);
}
struct GlyphInkPixel {
QRgb color = 0;
QRgb background = 0;
};
struct SurroundBackground {
int left = 0;
double topRow = 0.;
double bottomRow = 0.;
std::vector<std::array<int, 3>> top;
std::vector<std::array<int, 3>> bottom;
QColor mean;
int spread = -1;
QString refusal;
[[nodiscard]] QRgb at(int x, int y) const;
};
QRgb SurroundBackground::at(int x, int y) const {
const auto &above = top[x - left];
const auto &below = bottom[x - left];
const auto t = (y - topRow) / (bottomRow - topRow);
const auto channel = [&](int index) {
return int(base::SafeRound(
above[index] + (below[index] - above[index]) * t));
};
return qRgb(channel(0), channel(1), channel(2));
}
[[nodiscard]] int Median(std::vector<int> values) {
const auto middle = values.begin() + values.size() / 2;
std::nth_element(values.begin(), middle, values.end());
return *middle;
}
[[nodiscard]] std::vector<std::array<int, 3>> StripMedians(
const QImage &image,
QRect clip,
int firstRow) {
auto result = std::vector<std::array<int, 3>>();
result.reserve(clip.width());
auto values = std::array<std::vector<int>, 3>();
for (auto x = clip.left(); x <= clip.right(); ++x) {
for (auto &channel : values) {
channel.clear();
}
for (auto y = firstRow; y != firstRow + kGlyphSurroundStrip; ++y) {
const auto color = image.pixelColor(x, y);
values[0].push_back(color.red());
values[1].push_back(color.green());
values[2].push_back(color.blue());
}
result.push_back({
Median(values[0]),
Median(values[1]),
Median(values[2]),
});
}
return result;
}
[[nodiscard]] std::vector<std::array<int, 3>> SmoothColumns(
const std::vector<std::array<int, 3>> &columns) {
const auto count = int(columns.size());
auto result = std::vector<std::array<int, 3>>();
result.reserve(count);
auto values = std::vector<int>();
for (auto i = 0; i != count; ++i) {
const auto from = std::max(i - kGlyphSurroundSmooth, 0);
const auto till = std::min(i + kGlyphSurroundSmooth + 1, count);
auto smoothed = std::array<int, 3>();
for (auto channel = 0; channel != 3; ++channel) {
values.clear();
for (auto j = from; j != till; ++j) {
values.push_back(columns[j][channel]);
}
smoothed[channel] = Median(values);
}
result.push_back(smoothed);
}
return result;
}
// Strips are taken over |band|'s rows and |clip|'s columns, so a band cut
// by the image's top or bottom edge refuses instead of shrinking its strips.
[[nodiscard]] SurroundBackground MeasureSurround(
const QImage &image,
QRect band,
QRect clip) {
auto result = SurroundBackground();
const auto aboveFrom = band.top() - kGlyphSurroundGap - kGlyphSurroundStrip;
const auto aboveTill = band.top() - kGlyphSurroundGap;
const auto belowFrom = band.bottom() + 1 + kGlyphSurroundGap;
const auto belowTill = belowFrom + kGlyphSurroundStrip;
if ((aboveFrom < 0) || (belowTill > image.height())) {
result.refusal = u"the surround strips the background is measured "
"from lie outside the image: band="_q
+ RectText(band)
+ u" image="_q
+ RectText(image.rect())
+ u" rows above=%1..%2 below=%3..%4"_q
.arg(aboveFrom)
.arg(aboveTill - 1)
.arg(belowFrom)
.arg(belowTill - 1);
return result;
}
result.left = clip.left();
result.topRow = (aboveFrom + aboveTill - 1) / 2.;
result.bottomRow = (belowFrom + belowTill - 1) / 2.;
result.top = SmoothColumns(StripMedians(image, clip, aboveFrom));
result.bottom = SmoothColumns(StripMedians(image, clip, belowFrom));
auto sums = std::array<qint64, 3>();
auto lowest = std::array<int, 3>{ 255, 255, 255 };
auto highest = std::array<int, 3>{ 0, 0, 0 };
for (auto y = clip.top(); y <= clip.bottom(); ++y) {
for (auto x = clip.left(); x <= clip.right(); ++x) {
const auto rgb = result.at(x, y);
const int value[3] = { qRed(rgb), qGreen(rgb), qBlue(rgb) };
for (auto channel = 0; channel != 3; ++channel) {
sums[channel] += value[channel];
lowest[channel] = std::min(lowest[channel], value[channel]);
highest[channel] = std::max(highest[channel], value[channel]);
}
}
}
const auto pixels = qint64(clip.width()) * clip.height();
const auto mean = [&](int channel) {
return int((sums[channel] + pixels / 2) / pixels);
};
result.mean = QColor(mean(0), mean(1), mean(2));
result.spread = std::max({
highest[0] - lowest[0],
highest[1] - lowest[1],
highest[2] - lowest[2] });
return result;
}
} // namespace
QString GlyphBackgroundName(GlyphBackground source) {
switch (source) {
case GlyphBackground::BandMode:
return u"band-mode"_q;
case GlyphBackground::Surround:
return u"surround"_q;
}
return u"missing"_q;
}
QString GlyphCoreStateName(GlyphCoreState state) {
switch (state) {
case GlyphCoreState::Unread:
return u"core-unread"_q;
case GlyphCoreState::OutsideBand:
return u"core-outside-band"_q;
case GlyphCoreState::NoPaint:
return u"no-paint"_q;
case GlyphCoreState::NoSolidCore:
return u"no-solid-core"_q;
case GlyphCoreState::NoBackground:
return u"core-no-background"_q;
case GlyphCoreState::Measured:
return u"core-measured"_q;
}
return u"missing"_q;
}
GlyphCoreModal GlyphCore::readModal() const {
auto result = GlyphCoreModal();
if (state != GlyphCoreState::Measured) {
result.refusal = u"this reading ("_q
+ GlyphCoreStateName(state)
+ u") measured no solid core: "_q
+ GlyphReason(*this);
return result;
}
result.color = modal;
result.count = modalCount;
result.cores = cores;
return result;
}
GlyphCoreSolid GlyphCore::solidAt(int index) const {
auto result = GlyphCoreSolid();
result.index = index;
if (state != GlyphCoreState::Measured) {
result.refusal = u"index="_q
+ QString::number(index)
+ u" of "_q
+ QString::number(int(pens.size()))
+ u": this reading ("_q
+ GlyphCoreStateName(state)
+ u") measured no solid core: "_q
+ GlyphReason(*this);
return result;
}
if ((index < 0) || (index >= int(pens.size()))) {
result.refusal = u"index="_q
+ QString::number(index)
+ u" is outside this reading's "_q
+ QString::number(int(pens.size()))
+ u" pens"_q;
return result;
}
if (index >= int(solid.size())) {
result.refusal = u"index="_q
+ QString::number(index)
+ u" names a pen this reading filled no solid count for"_q;
return result;
}
result.count = solid[index];
return result;
}
GlyphCore ReadGlyphCore(
const QImage &image,
QRect band,
std::vector<InkCandidate> pens,
GlyphBackground source) {
auto result = GlyphCore();
result.source = source;
result.pens = std::move(pens);
result.solid.assign(result.pens.size(), -1);
const auto clip = band.intersected(image.rect());
if (clip.isEmpty()) {
result.state = GlyphCoreState::OutsideBand;
result.reason = u"the requested band lies outside the image, so "
"no glyph pixel was read: band="_q
+ RectText(band)
+ u" image="_q
+ RectText(image.rect());
return result;
}
const auto surround = (source == GlyphBackground::Surround);
auto measured = SurroundBackground();
if (surround) {
measured = MeasureSurround(image, band, clip);
if (!measured.refusal.isEmpty()) {
result.band = clip;
result.state = GlyphCoreState::NoBackground;
result.reason = measured.refusal;
return result;
}
}
result.ok = true;
result.band = clip;
result.total = clip.width() * clip.height();
if (surround) {
result.background = measured.mean;
result.backgroundSpread = measured.spread;
} else {
result.background = ExactModeColor(image, clip);
result.backgroundSpread = 0;
}
const auto bandBackground = result.background.rgb();
auto strongest = 0;
auto ink = std::vector<GlyphInkPixel>();
for (auto y = clip.top(); y <= clip.bottom(); ++y) {
for (auto x = clip.left(); x <= clip.right(); ++x) {
const auto color = image.pixelColor(x, y);
const auto rgb = qRgb(color.red(), color.green(), color.blue());
const auto background = surround
? measured.at(x, y)
: bandBackground;
const auto delta = ChannelDelta(
QColor::fromRgb(rgb),
QColor::fromRgb(background));
strongest = std::max(strongest, delta);
if (delta >= kInkDelta) {
ink.push_back({ rgb, background });
}
}
}
result.strongest = strongest;
result.inkPixels = int(ink.size());
if (ink.empty()) {
result.state = GlyphCoreState::NoPaint;
result.reason = u"the measured background fills the band, so there "
"is no glyph ink to read a pen from"_q;
return result;
}
int extreme[3] = { 0, 0, 0 };
int bestAbs[3] = { -1, -1, -1 };
for (const auto &pixel : ink) {
const auto rgb = pixel.color;
const auto bg = pixel.background;
const int value[3] = { qRed(rgb), qGreen(rgb), qBlue(rgb) };
const int base[3] = { qRed(bg), qGreen(bg), qBlue(bg) };
for (auto channel = 0; channel != 3; ++channel) {
const auto distance = std::abs(value[channel] - base[channel]);
if (distance > bestAbs[channel]) {
bestAbs[channel] = distance;
extreme[channel] = value[channel];
}
}
}
const auto extremeColor = QColor(extreme[0], extreme[1], extreme[2]);
auto cores = std::vector<QRgb>();
for (const auto &pixel : ink) {
if (ChannelDelta(QColor::fromRgb(pixel.color), extremeColor)
<= kGlyphCoreTolerance) {
cores.push_back(pixel.color);
}
}
if (cores.empty()) {
result.state = GlyphCoreState::NoSolidCore;
result.reason = u"the band has glyph ink but every ink pixel is a "
"fringe or partial coverage, so no solid core decides the pen"_q;
return result;
}
auto coreCounts = std::vector<std::pair<QRgb, int>>();
for (const auto rgb : cores) {
auto found = false;
for (auto &one : coreCounts) {
if (one.first == rgb) {
++one.second;
found = true;
break;
}
}
if (!found && (coreCounts.size() < 8192)) {
coreCounts.push_back({ rgb, 1 });
}
}
auto bestCore = -1;
auto modalRgb = cores.front();
for (const auto &one : coreCounts) {
if (one.second > bestCore) {
bestCore = one.second;
modalRgb = one.first;
}
}
result.modal = QColor::fromRgb(modalRgb);
result.modalCount = bestCore;
result.cores = int(cores.size());
result.state = GlyphCoreState::Measured;
result.reason = u"none"_q;
for (auto i = 0; i != int(result.pens.size()); ++i) {
auto count = 0;
for (const auto rgb : cores) {
if (ChannelDelta(QColor::fromRgb(rgb), result.pens[i].color)
<= kGlyphCoreTolerance) {
++count;
}
}
result.solid[i] = count;
}
return result;
}
QString FormatGlyphCore(const GlyphCore &reading) {
const auto measured = (reading.state == GlyphCoreState::Measured);
const auto reason = GlyphReason(reading);
const auto background = reading.background.isValid()
? ColorHex(reading.background)
: u"none"_q;
const auto strongest = (reading.strongest < 0)
? u"none"_q
: QString::number(reading.strongest);
const auto cores = measured
? QString::number(reading.cores)
: u"none"_q;
const auto modal = (measured && reading.modal.isValid())
? ColorHex(reading.modal)
: u"none"_q;
const auto share = measured
? (QString::number(reading.modalCount)
+ u"/"_q
+ QString::number(reading.cores))
: u"none"_q;
auto listed = QString();
for (auto i = 0; i != int(reading.pens.size()); ++i) {
auto name = reading.pens[i].name;
if (name.isEmpty()) {
name = u"pen"_q + QString::number(i);
}
const auto solid = reading.solidAt(i);
if (!listed.isEmpty()) {
listed += u" "_q;
}
listed += name
+ u"="_q
+ (solid.read() ? QString::number(solid.count) : u"none"_q);
}
auto text = u"state="_q
+ GlyphCoreStateName(reading.state)
+ u" band="_q
+ RectText(reading.band)
+ u" bg="_q
+ background
+ u" bgSource="_q
+ GlyphBackgroundName(reading.source);
if (reading.source == GlyphBackground::Surround) {
text += u" bgSpread="_q
+ ((reading.backgroundSpread < 0)
? u"none"_q
: QString::number(reading.backgroundSpread));
}
text += u" kGlyphCoreTolerance="_q
+ QString::number(kGlyphCoreTolerance)
+ u" strongest="_q
+ strongest
+ u" cores="_q
+ cores
+ u" modal="_q
+ modal
+ u" share="_q
+ share;
if (!listed.isEmpty()) {
text += u" "_q + listed;
}
return text + u" reason="_q + reason;
}
namespace {
constexpr auto kListedComponents = 8;
[[nodiscard]] QString IsolatedReason(const IsolatedGlyph &reading) {
return reading.reason.isEmpty()
? u"this reading was never scanned"_q
: reading.reason;
}
[[nodiscard]] bool ReachesCoverage(
QColor color,
QColor background,
QColor pen) {
const auto dr = pen.red() - background.red();
const auto dg = pen.green() - background.green();
const auto db = pen.blue() - background.blue();
const auto norm = dr * dr + dg * dg + db * db;
const auto dot = (color.red() - background.red()) * dr
+ (color.green() - background.green()) * dg
+ (color.blue() - background.blue()) * db;
return (norm > 0) && (double(dot) >= kIsolatedGlyphCoverage * norm);
}
// |mask| is band-local, row by row; the bounds are in image coordinates.
[[nodiscard]] std::vector<IsolatedGlyphComponent> ConnectedComponents(
std::vector<uchar> mask,
QRect band) {
const auto width = band.width();
const auto height = band.height();
auto result = std::vector<IsolatedGlyphComponent>();
auto pending = std::vector<int>();
for (auto start = 0; start != int(mask.size()); ++start) {
if (!mask[start]) {
continue;
}
mask[start] = 0;
pending.assign(1, start);
auto left = width;
auto top = height;
auto right = -1;
auto bottom = -1;
for (auto i = 0; i != int(pending.size()); ++i) {
const auto x = pending[i] % width;
const auto y = pending[i] / width;
left = std::min(left, x);
top = std::min(top, y);
right = std::max(right, x);
bottom = std::max(bottom, y);
for (auto dy = -1; dy <= 1; ++dy) {
for (auto dx = -1; dx <= 1; ++dx) {
const auto nx = x + dx;
const auto ny = y + dy;
if ((nx < 0)
|| (nx >= width)
|| (ny < 0)
|| (ny >= height)) {
continue;
}
const auto index = ny * width + nx;
if (mask[index]) {
mask[index] = 0;
pending.push_back(index);
}
}
}
}
result.push_back({
.bounds = QRect(
band.x() + left,
band.y() + top,
right - left + 1,
bottom - top + 1),
.pixels = int(pending.size()),
.interior = (left > 0)
&& (top > 0)
&& (right < width - 1)
&& (bottom < height - 1),
});
}
return result;
}
} // namespace
QString IsolatedGlyphStateName(IsolatedGlyphState state) {
switch (state) {
case IsolatedGlyphState::Unread:
return u"glyph-unread"_q;
case IsolatedGlyphState::BandOutsideImage:
return u"glyph-band-outside-image"_q;
case IsolatedGlyphState::NoInk:
return u"glyph-no-ink"_q;
case IsolatedGlyphState::NoPen:
return u"glyph-no-pen"_q;
case IsolatedGlyphState::Clipped:
return u"glyph-clipped"_q;
case IsolatedGlyphState::Ambiguous:
return u"glyph-ambiguous"_q;
case IsolatedGlyphState::Isolated:
return u"glyph-isolated"_q;
}
return u"missing"_q;
}
IsolatedGlyphBox IsolatedGlyph::readBounds() const {
auto result = IsolatedGlyphBox();
if (state != IsolatedGlyphState::Isolated) {
result.refusal = u"this reading ("_q
+ IsolatedGlyphStateName(state)
+ u") isolated no glyph: "_q
+ IsolatedReason(*this);
return result;
}
result.bounds = bounds;
return result;
}
IsolatedGlyph ReadIsolatedGlyph(const QImage &image, QRect band) {
auto result = IsolatedGlyph();
result.band = band;
if (band.isEmpty() || !image.rect().contains(band)) {
result.state = IsolatedGlyphState::BandOutsideImage;
result.reason = u"the band must lie wholly inside the image, so no "
"glyph pixel was read: band="_q
+ RectText(band)
+ u" image="_q
+ RectText(image.rect());
return result;
}
const auto core = ReadGlyphCore(image, band);
if (core.state == GlyphCoreState::NoPaint) {
result.state = IsolatedGlyphState::NoInk;
result.reason = u"the band-mode glyph core found no ink: "_q
+ GlyphReason(core);
return result;
}
const auto modal = core.readModal();
if (!modal.read()) {
result.state = IsolatedGlyphState::NoPen;
result.reason = u"the band-mode glyph core read no pen: "_q
+ modal.refusal;
return result;
}
result.background = core.background;
result.pen = modal.color;
auto mask = std::vector<uchar>(band.width() * band.height());
auto inkPixels = 0;
for (auto y = 0; y != band.height(); ++y) {
for (auto x = 0; x != band.width(); ++x) {
const auto color = image.pixelColor(band.x() + x, band.y() + y);
if (ReachesCoverage(color, result.background, result.pen)) {
mask[y * band.width() + x] = 1;
++inkPixels;
}
}
}
result.ok = true;
result.inkPixels = inkPixels;
if (!inkPixels) {
result.state = IsolatedGlyphState::NoInk;
result.reason = u"no band pixel reaches the declared coverage of the "
"pen"_q;
return result;
}
result.components = ConnectedComponents(std::move(mask), band);
auto largestInterior = -1;
auto largestBoundary = 0;
for (auto i = 0; i != int(result.components.size()); ++i) {
const auto &component = result.components[i];
if (!component.interior) {
largestBoundary = std::max(largestBoundary, component.pixels);
continue;
}
++result.interior;
if ((largestInterior < 0)
|| (component.pixels
> result.components[largestInterior].pixels)) {
largestInterior = i;
}
}
const auto count = int(result.components.size());
if (largestInterior < 0) {
result.state = IsolatedGlyphState::Clipped;
result.reason = u"all %1 ink components touch the band edge, so the "
"band cuts the glyph or holds only a neighbour's fragments"_q
.arg(count);
} else if (largestBoundary
>= result.components[largestInterior].pixels) {
result.state = IsolatedGlyphState::Clipped;
result.reason = u"a boundary component of %1 pixels is at least as "
"large as the largest interior one of %2 pixels, so the band "
"cuts the dominant glyph"_q
.arg(largestBoundary)
.arg(result.components[largestInterior].pixels);
} else if (result.interior > 1) {
result.state = IsolatedGlyphState::Ambiguous;
result.reason = u"%1 interior components: the band holds several "
"glyphs or a disconnected one, which this reader neither unions "
"nor chooses between"_q
.arg(result.interior);
} else {
result.state = IsolatedGlyphState::Isolated;
result.bounds = result.components[largestInterior].bounds;
result.reason = u"none"_q;
}
return result;
}
QString FormatIsolatedGlyph(const IsolatedGlyph &reading) {
const auto box = reading.readBounds();
const auto count = int(reading.components.size());
const auto measured = [&](int value) {
return reading.ok ? QString::number(value) : u"none"_q;
};
auto text = u"state="_q
+ IsolatedGlyphStateName(reading.state)
+ u" band="_q
+ RectText(reading.band)
+ u" bg="_q
+ (reading.background.isValid()
? ColorHex(reading.background)
: u"none"_q)
+ u" pen="_q
+ (reading.pen.isValid() ? ColorHex(reading.pen) : u"none"_q)
+ u" coverage="_q
+ QString::number(kIsolatedGlyphCoverage, 'f', 2)
+ u" ink="_q
+ measured(reading.inkPixels)
+ u" components="_q
+ measured(count)
+ u" interior="_q
+ measured(reading.interior)
+ u" bounds="_q
+ (box.read() ? RectText(box.bounds) : u"none"_q);
for (auto i = 0; i != std::min(count, kListedComponents); ++i) {
const auto &component = reading.components[i];
text += u" component=%1 pixels=%2 interior=%3"_q
.arg(RectText(component.bounds))
.arg(component.pixels)
.arg(component.interior ? 1 : 0);
}
if (count > kListedComponents) {
text += u" more=%1"_q.arg(count - kListedComponents);
}
return text + u" reason="_q + IsolatedReason(reading);
}
namespace {
void AppendBackgroundCollinearSelfTest(not_null<Runner*> runner) {
runner->add({
.name = u"ink scan self-test: a background-collinear candidate "
"is refused, and a separated control still classifies"_q,
.run = [] {
const auto dayFill = QColor(0xf1, 0xf1, 0xf1);
const auto controlFill = QColor(0x20, 0x40, 0x80);
const auto sub = QColor(0x99, 0x99, 0x99);
const auto fg = QColor(0x00, 0x00, 0x00);
const auto size = QSize(64, 32);
const auto band = QRect(8, 8, 48, 12);
const auto candidates = std::vector<InkCandidate>{
{ u"sub"_q, sub },
{ u"fg"_q, fg },
};
const auto colliding = std::vector<InkCandidate>{
{ u"mark"_q, QColor(0xff, 0xff, 0xff) },
{ u"mark-again"_q, QColor(0xf5, 0xf5, 0xf5) },
};
auto refusedImage = QImage(
size,
QImage::Format_ARGB32_Premultiplied);
refusedImage.fill(dayFill);
{
auto p = QPainter(&refusedImage);
p.fillRect(QRect(16, 8, 8, 12), sub);
}
auto controlImage = QImage(
size,
QImage::Format_ARGB32_Premultiplied);
controlImage.fill(controlFill);
{
auto p = QPainter(&controlImage);
p.fillRect(QRect(16, 8, 6, 12), sub);
p.fillRect(QRect(32, 8, 4, 12), fg);
}
const auto refused = ScanInk(refusedImage, band, candidates);
const auto control = ScanInk(controlImage, band, candidates);
const auto collided = ScanInk(refusedImage, band, colliding);
const auto subCount = refused.countAt(0);
const auto fgCount = refused.countAt(1);
const auto controlSub = control.countAt(0);
const auto controlFg = control.countAt(1);
const auto refusedText = FormatInkScan(refused, dayFill);
const auto controlText = FormatInkScan(control, controlFill);
const auto forbidden = std::vector<QString>{
u"candidates-collide"_q,
u"no-ink"_q,
u"outside-image"_q,
u"no-rows-in-band"_q,
u"the recovered box is empty or the fill is invalid"_q,
u"no row of the recovered box has the pill fill "
"as its own background"_q,
u"the requested fill is the image's background "
"outside the candidate, so no band can be derived"_q,
u"no row of the derived band kept the pill fill "
"as its own background"_q,
u"the candidates do not separate from each other"_q,
u"no pixel of the band"_q,
u"does not intersect the image"_q,
};
auto distinct = (InkScanStateName(refused.state)
== u"background-collinear"_q);
auto quoted = QString();
for (const auto &one : forbidden) {
if (refused.reason.contains(one)
|| (InkScanStateName(refused.state) == one)) {
distinct = false;
}
if (!quoted.isEmpty()) {
quoted += u"; "_q;
}
quoted += one;
}
const auto thresholds = std::vector<QString>{
u"kInkDelta=%1"_q.arg(kInkDelta),
u"kOnLine=%1"_q.arg(kOnLine),
u"kInkMargin=%1"_q.arg(kInkMargin),
u"kSameTolerance=%1"_q.arg(kSameTolerance),
u"kBackgroundSame=%1"_q.arg(kBackgroundSame),
};
auto thresholdsNamed = true;
for (const auto &one : thresholds) {
if (!controlText.contains(one)) {
thresholdsNamed = false;
}
}
Note(u"ink scan self-test: no window, session, chats list, "
"network, account or wallet - two synthetic images"_q);
Check(
refused.ok
&& (refused.state
== InkScanState::BackgroundCollinear)
&& (refused.state != InkScanState::Classified)
&& (refused.reason != u"none"_q)
&& refused.reason.contains(u"sub"_q)
&& refused.reason.contains(u"fg"_q)
&& refused.reason.contains(ColorHex(sub))
&& refused.reason.contains(ColorHex(fg))
&& refused.reason.contains(ColorHex(dayFill))
&& (refused.ambiguous == refused.inkPixels)
&& (refused.inkPixels > 0),
u"a candidate on the segment from the measured background "
"to another candidate is refused by name, not classified"_q,
refusedText);
Check(
!subCount.read()
&& (subCount.count == -1)
&& !fgCount.read()
&& (fgCount.count == -1)
&& InkCountDetails(subCount).contains(u"count=none"_q)
&& InkCountDetails(fgCount).contains(u"count=none"_q)
&& InkCountDetails(subCount).contains(
u"background-collinear"_q),
u"countAt on that reading refuses both candidates with "
"count=none"_q,
InkCountDetails(subCount)
+ u" | "_q
+ InkCountDetails(fgCount));
Check(
control.ok
&& (control.state == InkScanState::Classified)
&& (control.reason == u"none"_q)
&& controlSub.read()
&& (controlSub.count == 72)
&& controlFg.read()
&& (controlFg.count == 48),
u"the same two candidates against a background that does "
"not put one on the other's segment still classify"_q,
controlText
+ u" | "_q
+ InkCountDetails(controlSub)
+ u" | "_q
+ InkCountDetails(controlFg));
Check(
distinct,
u"the background-collinear name and reason differ from "
"candidates-collide, no-ink, outside-image, "
"no-rows-in-band and every DeriveBand reason"_q,
u"state=%1 reason=%2 forbidden=[%3]"_q
.arg(
InkScanStateName(refused.state),
refused.reason,
quoted));
Check(
refusedText.contains(ColorHex(sub))
&& refusedText.contains(ColorHex(fg))
&& refusedText.contains(ColorHex(dayFill))
&& refusedText.contains(u"sub"_q)
&& refusedText.contains(u"fg"_q)
&& controlText.startsWith(u"fill="_q)
&& controlText.contains(
u"sub(%1)=72"_q.arg(ColorHex(sub)))
&& controlText.contains(
u"fg(%1)=48"_q.arg(ColorHex(fg)))
&& controlText.contains(u"collision=none"_q)
&& !controlText.contains(u"background-collinear"_q)
&& thresholdsNamed,
u"the refusing format names both colours and the measured "
"background, and the control format keeps its counts and "
"thresholds"_q,
refusedText + u" || "_q + controlText);
Check(
collided.ok
&& (collided.state == InkScanState::CandidatesCollide)
&& collided.reason.contains(
u"the candidates do not separate from each other"_q)
&& (collided.state
!= InkScanState::BackgroundCollinear),
u"a pair that already collides under Separable keeps "
"CandidatesCollide and its existing text"_q,
FormatInkScan(collided, dayFill));
},
});
}
void AppendChromaticRasterSelfTest(not_null<Runner*> runner) {
runner->add({
.name = u"chromatic raster self-test: a dense mark, an equal-count "
"smear, and a flat fill"_q,
.run = [] {
const auto pen = QColor(0x11, 0x11, 0x11);
const auto fill = QColor(0xe8, 0xe4, 0xdc);
const auto red = QColor(0xff, 0x00, 0x00);
const auto blue = QColor(0x00, 0x00, 0xff);
const auto mark = QRect(8, 8, 16, 16);
const auto band = QRect(0, 0, 96, 40);
auto denseImage = QImage(
QSize(96, 40),
QImage::Format_ARGB32_Premultiplied);
denseImage.fill(fill);
auto reds = 0;
auto blues = 0;
for (auto y = mark.top(); y <= mark.bottom(); ++y) {
for (auto x = mark.left(); x <= mark.right(); ++x) {
const auto color = ((x + y) % 2) ? blue : red;
denseImage.setPixelColor(x, y, color);
if (color == red) {
++reds;
} else {
++blues;
}
}
}
const auto painted = reds + blues;
const auto expectedMean = QColor(
int((qint64(red.red()) * reds
+ qint64(blue.red()) * blues) / painted),
int((qint64(red.green()) * reds
+ qint64(blue.green()) * blues) / painted),
int((qint64(red.blue()) * reds
+ qint64(blue.blue()) * blues) / painted));
auto sparseImage = QImage(
QSize(96, 40),
QImage::Format_ARGB32_Premultiplied);
sparseImage.fill(fill);
auto sparsePainted = 0;
for (auto y = 8; y < 24; ++y) {
for (auto x = 8; x < 72; ++x) {
if ((x % 4) != 0) {
continue;
}
const auto color = ((x + y) % 2) ? blue : red;
sparseImage.setPixelColor(x, y, color);
++sparsePainted;
}
}
auto flatImage = QImage(
QSize(96, 40),
QImage::Format_ARGB32_Premultiplied);
flatImage.fill(fill);
const auto dense = ReadChromaticRaster(
denseImage,
band,
pen,
fill);
const auto sparse = ReadChromaticRaster(
sparseImage,
band,
pen,
fill);
const auto flat = ReadChromaticRaster(
flatImage,
band,
pen,
fill);
const auto outside = ReadChromaticRaster(
flatImage,
QRect(200, 200, 8, 8),
pen,
fill);
const auto box = dense.readBox();
const auto mean = dense.readMean();
const auto density = dense.readDensity();
const auto sparseBox = sparse.readBox();
const auto sparseMean = sparse.readMean();
const auto sparseDensity = sparse.readDensity();
const auto flatBox = flat.readBox();
const auto flatMean = flat.readMean();
const auto flatDensity = flat.readDensity();
const auto denseText = FormatChromaticRaster(dense);
const auto sparseText = FormatChromaticRaster(sparse);
const auto flatText = FormatChromaticRaster(flat);
const auto outsideText = FormatChromaticRaster(outside);
Note(u"chromatic raster self-test: no window, session, chats "
"list, network, account or wallet - three synthetic "
"images"_q);
Check(
dense.found()
&& box.read()
&& (box.box == mark)
&& (box.box != band)
&& band.contains(box.box)
&& density.read()
&& (density.density >= kChromaticDensityFloor)
&& mean.read()
&& (ColorHex(mean.color) == ColorHex(expectedMean))
&& (ColorHex(mean.color) != ColorHex(pen))
&& (ColorHex(mean.color) != ColorHex(fill))
&& (painted == 256),
u"a dense two-colour mark is found at its own box, with "
"the mean of its ink and neither colour the check passed "
"in"_q,
denseText
+ u" expectedMean="_q
+ ColorHex(expectedMean)
+ u" pen="_q
+ ColorHex(pen)
+ u" fill="_q
+ ColorHex(fill));
Check(
(dense.totalChromatic == sparse.totalChromatic)
&& (dense.totalChromatic == sparsePainted)
&& (dense.totalChromatic > 0)
&& (sparse.state == ChromaticRasterState::BelowDensity)
&& !sparse.found()
&& !sparseBox.read()
&& sparseBox.box.isEmpty(),
u"an equal chromatic count spread below the density floor "
"is refused and has no found box"_q,
u"denseTotal=%1 sparseTotal=%2 dense=%3 sparse=%4"_q
.arg(dense.totalChromatic)
.arg(sparse.totalChromatic)
.arg(denseText, sparseText));
Check(
flat.ok
&& (flat.state == ChromaticRasterState::NoChromatic)
&& (flat.totalChromatic == 0)
&& !flat.found()
&& !flatBox.read()
&& (ChromaticRasterStateName(flat.state)
!= ChromaticRasterStateName(sparse.state))
&& (flat.reason != sparse.reason)
&& flat.reason.contains(u"chromatic floor"_q)
&& sparse.reason.contains(u"density floor"_q),
u"a flat fill with no chromatic pixel refuses by the other "
"name and has no found box"_q,
flatText + u" || "_q + sparseText);
Check(
!sparseBox.read()
&& !sparseMean.read()
&& !sparseDensity.read()
&& !flatBox.read()
&& !flatMean.read()
&& !flatDensity.read()
&& sparseBox.refusal.contains(u"below-density"_q)
&& flatBox.refusal.contains(u"no-chromatic"_q),
u"asking a refusing raster for its box, mean or density "
"returns a refusal rather than a value"_q,
sparseBox.refusal
+ u" | "_q
+ sparseMean.refusal
+ u" | "_q
+ flatDensity.refusal);
Check(
denseText.contains(RectText(mark))
&& denseText.contains(u"matched=256"_q)
&& denseText.contains(u"totalChromatic=256"_q)
&& denseText.contains(
u"density="_q
+ QString::number(density.density)
+ u" mean="_q)
&& denseText.contains(ColorHex(expectedMean))
&& denseText.contains(u"state=found"_q)
&& !denseText.contains(ColorHex(pen))
&& !denseText.contains(ColorHex(fill))
&& sparseText.contains(u"state=below-density"_q)
&& sparseText.contains(u"box=none"_q)
&& flatText.contains(u"state=no-chromatic"_q)
&& flatText.contains(u"box=none"_q)
&& outsideText.contains(u"state=raster-outside-band"_q)
&& !outside.ok
&& (outside.state
!= ChromaticRasterState::NoChromatic)
&& (outside.state
!= ChromaticRasterState::BelowDensity),
u"the found format carries the box, both counts, the "
"density and the mean, and each refusal names itself"_q,
denseText
+ u" || "_q
+ sparseText
+ u" || "_q
+ flatText
+ u" || "_q
+ outsideText);
},
});
}
[[nodiscard]] std::vector<QString> ShippedInkStateNames() {
return {
u"not-scanned"_q,
u"outside-image"_q,
u"no-rows-in-band"_q,
u"candidates-collide"_q,
u"no-ink"_q,
u"classified"_q,
u"background-collinear"_q,
u"raster-outside-band"_q,
u"no-chromatic"_q,
u"below-density"_q,
u"found"_q,
u"missing"_q,
};
}
void AppendGlyphCoreSelfTest(not_null<Runner*> runner) {
runner->add({
.name = u"glyph-core self-test: neutral and link strokes on both "
"grounds"_q,
.run = [] {
const auto band = QRect(8, 8, 80, 32);
const auto absent = QColor(0xff, 0x00, 0x00);
const auto paint = [](
QColor ground,
QColor pen,
QColor fringeA,
QColor fringeB,
QColor fringeC) {
auto image = QImage(
QSize(96, 48),
QImage::Format_ARGB32_Premultiplied);
image.fill(ground);
for (auto y = 12; y < 28; ++y) {
for (auto x = 20; x < 36; ++x) {
image.setPixelColor(x, y, pen);
}
image.setPixelColor(17, y, fringeA);
image.setPixelColor(18, y, fringeB);
image.setPixelColor(36, y, fringeC);
}
return image;
};
const auto checkGround = [&](
const QString &ground,
QColor groundColor,
QColor neutral,
QColor link,
int neutralStrongest,
int linkStrongest,
QColor fringeA,
QColor fringeB,
QColor fringeC) {
const auto scanPens = std::vector<InkCandidate>{
{ u"neutral"_q, neutral },
{ u"link"_q, link },
};
const auto pens = std::vector<InkCandidate>{
{ u"neutral"_q, neutral },
{ u"link"_q, link },
{ u"absent"_q, absent },
};
const auto neutralImage = paint(
groundColor,
neutral,
fringeA,
fringeB,
fringeC);
const auto linkImage = paint(
groundColor,
link,
fringeA,
fringeB,
fringeC);
const auto scan = ScanInk(neutralImage, band, scanPens);
const auto scanNeutral = scan.countAt(0);
const auto scanLink = scan.countAt(1);
const auto neutralCore = ReadGlyphCore(
neutralImage,
band,
pens);
const auto linkCore = ReadGlyphCore(linkImage, band, pens);
const auto neutralModal = neutralCore.readModal();
const auto linkModal = linkCore.readModal();
const auto neutralOfNeutral = neutralCore.solidAt(0);
const auto linkOfNeutral = neutralCore.solidAt(1);
const auto absentOfNeutral = neutralCore.solidAt(2);
const auto neutralOfLink = linkCore.solidAt(0);
const auto linkOfLink = linkCore.solidAt(1);
const auto absentOfLink = linkCore.solidAt(2);
const auto neutralText = FormatGlyphCore(neutralCore);
const auto linkText = FormatGlyphCore(linkCore);
const auto quoted = neutralText
+ u" || "_q
+ linkText
+ u" || scan "_q
+ InkCountDetails(scanNeutral)
+ u" | "_q
+ InkCountDetails(scanLink);
Check(
(scan.state == InkScanState::Classified)
&& scanNeutral.read()
&& (scanNeutral.count == 256)
&& scanLink.read()
&& (scanLink.count == 48)
&& (neutralCore.state == GlyphCoreState::Measured)
&& (ColorHex(neutralCore.background)
== ColorHex(groundColor))
&& (neutralCore.strongest == neutralStrongest)
&& (neutralCore.cores == 256)
&& neutralModal.read()
&& (ColorHex(neutralModal.color) == ColorHex(neutral))
&& (neutralModal.count == 256)
&& (neutralModal.cores == 256)
&& neutralOfNeutral.read()
&& (neutralOfNeutral.count == 256)
&& linkOfNeutral.read()
&& (linkOfNeutral.count == 0)
&& absentOfNeutral.read()
&& (absentOfNeutral.count == 0)
&& (ColorHex(neutralModal.color) != ColorHex(absent))
&& neutralText.contains(RectText(band))
&& neutralText.contains(
u"bg="_q + ColorHex(groundColor))
&& neutralText.contains(u"kGlyphCoreTolerance=8"_q)
&& neutralText.contains(u"cores=256"_q)
&& neutralText.contains(
u"modal="_q + ColorHex(neutral))
&& neutralText.contains(u"share=256/256"_q)
&& neutralText.contains(u"neutral=256"_q)
&& neutralText.contains(u"link=0"_q)
&& neutralText.contains(u"absent=0"_q),
u"on the "_q
+ ground
+ u" ground a neutral stroke is classified with "
"link fringes, and its solid cores are the "
"neutral pen"_q,
quoted);
Check(
(linkCore.state == GlyphCoreState::Measured)
&& (linkCore.strongest == linkStrongest)
&& (linkCore.cores == 256)
&& linkModal.read()
&& (ColorHex(linkModal.color) == ColorHex(link))
&& (ColorHex(linkModal.color) != ColorHex(absent))
&& neutralOfLink.read()
&& (neutralOfLink.count == 0)
&& linkOfLink.read()
&& (linkOfLink.count == 256)
&& absentOfLink.read()
&& (absentOfLink.count == 0)
&& linkText.contains(u"kGlyphCoreTolerance=8"_q)
&& linkText.contains(u"cores=256"_q)
&& linkText.contains(u"modal="_q + ColorHex(link))
&& linkText.contains(u"share=256/256"_q)
&& linkText.contains(u"neutral=0"_q)
&& linkText.contains(u"link=256"_q)
&& linkText.contains(
u"bg="_q + ColorHex(groundColor)),
u"on the "_q
+ ground
+ u" ground the same stroke in the link pen has "
"that pen as its modal core"_q,
quoted);
};
Note(u"glyph-core self-test: no window, session, chats list, "
"network, account or wallet - synthetic images on two "
"grounds"_q);
checkGround(
u"day"_q,
QColor(0xff, 0xff, 0xff),
QColor(0x99, 0x99, 0x99),
QColor(0x16, 0x8a, 0xcd),
102,
233,
QColor(0xa1, 0xd0, 0xf5),
QColor(0xa1, 0xd6, 0xf2),
QColor(0xa1, 0xdb, 0xf0));
checkGround(
u"night"_q,
QColor(0x17, 0x21, 0x2b),
QColor(0x70, 0x84, 0x99),
QColor(0x6a, 0xb3, 0xf3),
110,
200,
QColor(0x38, 0x62, 0x8f),
QColor(0x34, 0x5b, 0x8b),
QColor(0x3c, 0x6a, 0x93));
},
});
runner->add({
.name = u"glyph-core self-test: no ink, no solid core, and refused "
"reads"_q,
.run = [] {
const auto band = QRect(8, 8, 80, 32);
const auto pens = std::vector<InkCandidate>{
{ u"neutral"_q, QColor(0x99, 0x99, 0x99) },
};
auto flatImage = QImage(
QSize(96, 48),
QImage::Format_ARGB32_Premultiplied);
flatImage.fill(QColor(0xf0, 0xf0, 0xf0));
const auto flat = ReadGlyphCore(flatImage, band, pens);
const auto flatModal = flat.readModal();
const auto flatSolid = flat.solidAt(0);
const auto flatText = FormatGlyphCore(flat);
auto fringeImage = QImage(
QSize(40, 16),
QImage::Format_ARGB32_Premultiplied);
fringeImage.fill(Qt::white);
for (auto x = 0; x < 40; ++x) {
fringeImage.setPixelColor(x, 0, QColor(80, 255, 255));
fringeImage.setPixelColor(x, 1, QColor(255, 80, 255));
fringeImage.setPixelColor(x, 2, QColor(255, 255, 80));
fringeImage.setPixelColor(x, 3, QColor(210, 220, 230));
}
const auto fringeBand = QRect(0, 0, 40, 16);
const auto fringe = ReadGlyphCore(fringeImage, fringeBand, pens);
const auto fringeModal = fringe.readModal();
const auto fringeSolid = fringe.solidAt(0);
const auto fringeText = FormatGlyphCore(fringe);
const auto outside = ReadGlyphCore(
flatImage,
QRect(200, 200, 8, 8),
pens);
const auto outsideModal = outside.readModal();
const auto outsideText = FormatGlyphCore(outside);
const auto shipped = ShippedInkStateNames();
const auto flatName = GlyphCoreStateName(flat.state);
const auto fringeName = GlyphCoreStateName(fringe.state);
const auto outsideName = GlyphCoreStateName(outside.state);
auto distinct = (flatName != fringeName)
&& (flat.reason != fringe.reason)
&& (flatName != outsideName)
&& (fringeName != outsideName);
auto forbidden = QString();
for (const auto &one : shipped) {
if ((flatName == one)
|| (fringeName == one)
|| (outsideName == one)
|| flat.reason.contains(one)
|| fringe.reason.contains(one)) {
distinct = false;
}
if (!forbidden.isEmpty()) {
forbidden += u", "_q;
}
forbidden += one;
}
Note(u"glyph-core self-test: no window, session, chats list, "
"network, account or wallet - a flat band and a fringe "
"band"_q);
Check(
flat.ok
&& (flat.state == GlyphCoreState::NoPaint)
&& !flatModal.read()
&& !flatModal.color.isValid()
&& (flatModal.count == -1)
&& (flatModal.cores == -1)
&& !flatSolid.read()
&& (flatSolid.count == -1)
&& flatSolid.refusal.contains(u"no-paint"_q)
&& flatModal.refusal.contains(u"no-paint"_q),
u"asking a flat band for its modal colour or a solid "
"count refuses, and the count is not a measured zero"_q,
flatModal.refusal + u" | "_q + flatSolid.refusal);
Check(
flatText.contains(u"state=no-paint"_q)
&& flatText.contains(u"modal=none"_q)
&& flatText.contains(u"cores=none"_q)
&& flatText.contains(u"share=none"_q)
&& flatText.contains(u"neutral=none"_q)
&& !flatText.contains(u"modal=#"_q),
u"the no-paint format names the refusal and carries no "
"modal colour"_q,
flatText);
Check(
fringe.ok
&& (fringe.state == GlyphCoreState::NoSolidCore)
&& (fringe.inkPixels > 0)
&& !fringeModal.read()
&& !fringeModal.color.isValid()
&& (fringeModal.count == -1)
&& !fringeSolid.read()
&& (fringeSolid.count == -1)
&& fringeText.contains(u"state=no-solid-core"_q)
&& fringeText.contains(u"modal=none"_q)
&& !fringeText.contains(u"modal=#"_q),
u"a fringe band with no full-coverage core refuses by "
"name and does not present a fringe colour as the pen"_q,
fringeText + u" | "_q + fringeModal.refusal);
Check(
!outside.ok
&& (outside.state == GlyphCoreState::OutsideBand)
&& !outsideModal.read()
&& (outsideModal.count == -1)
&& outsideText.contains(u"state=core-outside-band"_q)
&& (outside.state != GlyphCoreState::NoPaint)
&& (outside.state != GlyphCoreState::NoSolidCore),
u"a band that misses the image is not the no-paint "
"refusal"_q,
outsideText);
Check(
distinct
&& flat.reason.contains(u"no glyph ink"_q)
&& fringe.reason.contains(u"no solid core"_q),
u"the no-paint and no-solid-core names and reasons differ "
"from each other and from every state the module already "
"ships"_q,
u"flat=%1 reason=%2 fringe=%3 reason=%4 outside=%5 "
"forbidden=[%6]"_q
.arg(
flatName,
flat.reason,
fringeName,
fringe.reason,
outsideName,
forbidden));
},
});
}
struct SurroundFixture {
QImage image;
QRect band;
QColor pen;
int drawn = 0;
};
struct ExactColorCounts {
int pen = 0;
int rival = 0;
};
// Bilinear between four wallpaper colours, so it is linear in y for a fixed
// x and the surround interpolation is exact; every seventh diagonal is
// lifted 20% toward white, which stays below kInkDelta.
[[nodiscard]] QColor GradientGround(QPoint point, QSize size) {
constexpr auto kCorners = std::array<std::array<int, 3>, 4>{ {
{ 0xc7, 0xd2, 0x8b }, // top-left
{ 0xe6, 0xee, 0xdc }, // top-right, the lightest in every channel
{ 0x8b, 0xc7, 0xa0 }, // bottom-left
{ 0xb4, 0xcd, 0xd6 }, // bottom-right
} };
const auto u = point.x() / double(size.width() - 1);
const auto v = point.y() / double(size.height() - 1);
const auto lifted = (((point.x() + point.y()) % 7) == 0);
auto value = std::array<int, 3>();
for (auto channel = 0; channel != 3; ++channel) {
const auto blended = kCorners[0][channel] * (1. - u) * (1. - v)
+ kCorners[1][channel] * u * (1. - v)
+ kCorners[2][channel] * (1. - u) * v
+ kCorners[3][channel] * u * v;
value[channel] = int(base::SafeRound(blended));
if (lifted) {
value[channel] = int(base::SafeRound(
value[channel] + (255 - value[channel]) * 0.2));
}
}
return QColor(value[0], value[1], value[2]);
}
// The glyph is exact-pen strokes inside the band with no antialiasing: two
// 100x3 bars joined by five 3x16 stems, 840 pixels that never overlap.
// The band's surround strips, rows 18-25 and 54-61, hold only the ground.
[[nodiscard]] SurroundFixture PaintSurroundFixture(
Fn<QColor(QPoint, QSize)> ground) {
auto result = SurroundFixture{
.image = QImage(QSize(160, 80), QImage::Format_ARGB32_Premultiplied),
.band = QRect(16, 28, 128, 24),
.pen = QColor(0x00, 0x3a, 0x24),
};
const auto size = result.image.size();
for (auto y = 0; y != size.height(); ++y) {
for (auto x = 0; x != size.width(); ++x) {
result.image.setPixelColor(x, y, ground(QPoint(x, y), size));
}
}
auto strokes = std::vector<QRect>{
QRect(22, 29, 100, 3),
QRect(22, 48, 100, 3),
};
for (auto i = 0; i != 5; ++i) {
strokes.push_back(QRect(24 + 20 * i, 32, 3, 16));
}
for (const auto &stroke : strokes) {
for (auto y = stroke.top(); y <= stroke.bottom(); ++y) {
for (auto x = stroke.left(); x <= stroke.right(); ++x) {
result.image.setPixelColor(x, y, result.pen);
++result.drawn;
}
}
}
return result;
}
// Counted apart from ReadGlyphCore: the pen's exact count in |band| and the
// largest exact count of any other colour there.
[[nodiscard]] ExactColorCounts CountExactColors(
const QImage &image,
QRect band,
QColor pen) {
auto counts = std::map<QRgb, int>();
const auto clip = band.intersected(image.rect());
for (auto y = clip.top(); y <= clip.bottom(); ++y) {
for (auto x = clip.left(); x <= clip.right(); ++x) {
++counts[image.pixelColor(x, y).rgb()];
}
}
auto result = ExactColorCounts();
for (const auto &[rgb, count] : counts) {
if (rgb == pen.rgb()) {
result.pen = count;
} else {
result.rival = std::max(result.rival, count);
}
}
return result;
}
[[nodiscard]] bool GradientArmHolds(
const GlyphCore &reading,
QColor pen,
int penPixels) {
const auto modal = reading.readModal();
const auto solid = reading.solidAt(0);
return (reading.state == GlyphCoreState::Measured)
&& modal.read()
&& (ChannelDelta(modal.color, pen) <= kGlyphCoreTolerance)
&& solid.read()
&& (solid.count == penPixels);
}
void AppendGlyphSurroundSelfTest(not_null<Runner*> runner) {
runner->add({
.name = u"glyph-core surround self-test: a dark pen over a "
"patterned gradient"_q,
.run = [] {
const auto fixture = PaintSurroundFixture(GradientGround);
const auto pen = fixture.pen;
const auto pens = std::vector<InkCandidate>{
{ u"text"_q, pen },
};
const auto exact = CountExactColors(
fixture.image,
fixture.band,
pen);
const auto surround = ReadGlyphCore(
fixture.image,
fixture.band,
pens,
GlyphBackground::Surround);
const auto bandMode = ReadGlyphCore(
fixture.image,
fixture.band,
pens);
const auto bandModal = bandMode.readModal();
const auto surroundText = FormatGlyphCore(surround);
const auto bandText = FormatGlyphCore(bandMode);
const auto quoted = u"surround: "_q
+ surroundText
+ u" || band-mode: "_q
+ bandText;
Note(u"glyph-core surround self-test: no window, session, chats "
"list, network, account or wallet - a synthetic four-colour "
"gradient with a pattern"_q);
Check(
(exact.pen == fixture.drawn)
&& (exact.pen > exact.rival)
&& (bandMode.state == GlyphCoreState::Measured),
u"fixture gate: the drawn pen is the band's most frequent "
"exact colour and the band-mode reading measures a core"_q,
u"penPixels=%1 drawn=%2 mostFrequentOther=%3 band-mode: %4"_q
.arg(exact.pen)
.arg(fixture.drawn)
.arg(exact.rival)
.arg(bandText));
Check(
GradientArmHolds(surround, pen, fixture.drawn)
&& (surround.source == GlyphBackground::Surround)
&& (surround.backgroundSpread > 0)
&& (ChannelDelta(surround.background, pen) >= kInkDelta)
&& surroundText.contains(u"bgSource=surround"_q)
&& surroundText.contains(u"bgSpread="_q)
&& surroundText.contains(u"modal="_q + ColorHex(pen)),
u"read against its surround the gradient arm holds: the "
"modal core is the pen, every drawn pen pixel is a solid "
"core, and the background is the wallpaper's"_q,
quoted);
Check(
(bandMode.source == GlyphBackground::BandMode)
&& (ColorHex(bandMode.background) == ColorHex(pen))
&& bandModal.read()
&& (ChannelDelta(bandModal.color, pen)
> kGlyphCoreTolerance)
&& bandText.contains(u"bgSource=band-mode"_q)
&& !bandText.contains(u"bgSpread="_q)
&& !GradientArmHolds(bandMode, pen, fixture.drawn),
u"revert control: the band-mode rule reads the pen as the "
"background and a wallpaper colour as the modal, so the "
"same arm fails with the fix reverted"_q,
quoted);
},
});
runner->add({
.name = u"glyph-core surround self-test: the same glyph over a flat "
"fill reads alike from both sources"_q,
.run = [] {
const auto fill = QColor(0xc8, 0xd2, 0x8b);
const auto fixture = PaintSurroundFixture([=](QPoint, QSize) {
return fill;
});
const auto pen = fixture.pen;
const auto pens = std::vector<InkCandidate>{
{ u"text"_q, pen },
};
const auto surround = ReadGlyphCore(
fixture.image,
fixture.band,
pens,
GlyphBackground::Surround);
const auto bandMode = ReadGlyphCore(
fixture.image,
fixture.band,
pens);
const auto surroundModal = surround.readModal();
const auto bandModal = bandMode.readModal();
const auto surroundText = FormatGlyphCore(surround);
const auto bandText = FormatGlyphCore(bandMode);
Note(u"glyph-core surround self-test: no window, session, chats "
"list, network, account or wallet - a synthetic flat fill"_q);
Check(
(surround.state == GlyphCoreState::Measured)
&& (bandMode.state == GlyphCoreState::Measured)
&& surroundModal.read()
&& bandModal.read()
&& (ColorHex(surroundModal.color) == ColorHex(pen))
&& (ColorHex(bandModal.color) == ColorHex(pen))
&& (ColorHex(surround.background) == ColorHex(fill))
&& (ColorHex(bandMode.background) == ColorHex(fill))
&& (surround.backgroundSpread == 0)
&& (bandMode.backgroundSpread == 0)
&& (surround.cores == fixture.drawn)
&& (surround.cores == bandMode.cores)
&& (surround.modalCount == bandMode.modalCount)
&& (surround.strongest == bandMode.strongest)
&& surroundText.contains(u"bgSource=surround bgSpread=0"_q)
&& bandText.contains(u"bgSource=band-mode"_q),
u"over a flat fill both sources read the pen as the modal "
"core and the fill as the background, with equal cores, "
"modal count and strongest distance"_q,
u"surround: "_q
+ surroundText
+ u" || band-mode: "_q
+ bandText);
},
});
runner->add({
.name = u"glyph-core surround self-test: a surround strip outside "
"the image refuses by name"_q,
.run = [] {
const auto fixture = PaintSurroundFixture(GradientGround);
const auto band = QRect(16, 4, 128, 24);
const auto pens = std::vector<InkCandidate>{
{ u"text"_q, fixture.pen },
};
const auto refused = ReadGlyphCore(
fixture.image,
band,
pens,
GlyphBackground::Surround);
const auto refusedModal = refused.readModal();
const auto refusedSolid = refused.solidAt(0);
const auto refusedText = FormatGlyphCore(refused);
const auto bandMode = ReadGlyphCore(fixture.image, band, pens);
const auto bandText = FormatGlyphCore(bandMode);
const auto outside = ReadGlyphCore(
fixture.image,
QRect(200, 200, 8, 8),
pens,
GlyphBackground::Surround);
const auto outsideText = FormatGlyphCore(outside);
const auto earlier = std::array{
GlyphCoreState::Unread,
GlyphCoreState::OutsideBand,
GlyphCoreState::NoPaint,
GlyphCoreState::NoSolidCore,
GlyphCoreState::Measured,
};
auto shipped = ShippedInkStateNames();
for (const auto state : earlier) {
shipped.push_back(GlyphCoreStateName(state));
}
const auto name = GlyphCoreStateName(refused.state);
auto distinct = true;
auto forbidden = QString();
for (const auto &one : shipped) {
if (name == one) {
distinct = false;
}
if (!forbidden.isEmpty()) {
forbidden += u", "_q;
}
forbidden += one;
}
Note(u"glyph-core surround self-test: no window, session, chats "
"list, network, account or wallet - the gradient image read "
"through a band whose upper strip lies above it"_q);
Check(
!refused.ok
&& (refused.state == GlyphCoreState::NoBackground)
&& (refused.source == GlyphBackground::Surround)
&& !refused.background.isValid()
&& (refused.backgroundSpread == -1)
&& (refused.strongest == -1)
&& !refusedModal.read()
&& !refusedModal.color.isValid()
&& (refusedModal.count == -1)
&& (refusedModal.cores == -1)
&& refusedModal.refusal.contains(u"core-no-background"_q)
&& !refusedSolid.read()
&& (refusedSolid.count == -1)
&& refusedSolid.refusal.contains(u"core-no-background"_q)
&& refused.reason.contains(u"surround strips"_q)
&& refused.reason.contains(u"above=-6..1"_q),
u"a band whose upper surround strip lies above the image "
"refuses by name and hands back no background, colour or "
"count"_q,
refusedText
+ u" | "_q
+ refusedModal.refusal
+ u" | "_q
+ refusedSolid.refusal);
Check(
refusedText.contains(u"state=core-no-background"_q)
&& refusedText.contains(u"bg=none"_q)
&& refusedText.contains(u"bgSource=surround"_q)
&& refusedText.contains(u"bgSpread=none"_q)
&& refusedText.contains(u"strongest=none"_q)
&& refusedText.contains(u"modal=none"_q)
&& refusedText.contains(u"text=none"_q)
&& !refusedText.contains(u"modal=#"_q),
u"the core-no-background format names its source and prints "
"every refused colour and count as none"_q,
refusedText);
Check(
bandMode.ok
&& (bandMode.source == GlyphBackground::BandMode)
&& bandMode.background.isValid()
&& (bandMode.state != GlyphCoreState::NoBackground)
&& (bandMode.state != GlyphCoreState::OutsideBand),
u"control: the band-mode reading of the same band scans it "
"and establishes a background, so the refusal is the "
"surround's"_q,
bandText);
Check(
!outside.ok
&& (outside.state == GlyphCoreState::OutsideBand)
&& (outside.source == GlyphBackground::Surround)
&& outsideText.contains(u"state=core-outside-band"_q)
&& outsideText.contains(u"bgSource=surround"_q),
u"a band that misses the image answers core-outside-band "
"before any surround is measured"_q,
outsideText);
Check(
distinct
&& (name == u"core-no-background"_q)
&& (GlyphBackgroundName(GlyphBackground::BandMode)
!= GlyphBackgroundName(GlyphBackground::Surround)),
u"the core-no-background name differs from every state the "
"module already ships"_q,
u"name=%1 forbidden=[%2]"_q.arg(name, forbidden));
},
});
}
struct IsolationFixture {
QImage image;
QColor ground;
QColor pen;
};
// The night ground and neutral pen of the glyph-core stage, on 96x96.
[[nodiscard]] IsolationFixture MakeIsolationFixture() {
auto result = IsolationFixture{
.image = QImage(QSize(96, 96), QImage::Format_ARGB32_Premultiplied),
.ground = QColor(0x17, 0x21, 0x2b),
.pen = QColor(0x70, 0x84, 0x99),
};
result.image.fill(result.ground);
return result;
}
void FillRects(
QImage &image,
const std::vector<QRect> &rects,
QColor color) {
auto p = QPainter(&image);
for (const auto &rect : rects) {
p.fillRect(rect, color);
}
}
[[nodiscard]] QRect UnitedRects(const std::vector<QRect> &rects) {
auto result = QRect();
for (const auto &rect : rects) {
result = result.united(rect);
}
return result;
}
// Two 6x40 stems 34 apart joined by two diagonals of 2x4 steps that meet
// in the middle: one 8-connected component, 40x40 from |origin|.
[[nodiscard]] std::vector<QRect> CapitalM(QPoint origin) {
auto result = std::vector<QRect>{
QRect(origin, QSize(6, 40)),
QRect(origin + QPoint(34, 0), QSize(6, 40)),
};
const auto step = QSize(2, 4);
for (auto i = 0; i != 7; ++i) {
result.push_back(QRect(origin + QPoint(6 + 2 * i, 4 * i), step));
result.push_back(QRect(origin + QPoint(32 - 2 * i, 4 * i), step));
}
return result;
}
// The following glyph's body lies right of the band QRect(10, 0, 52, 88);
// only its arm and its descender foot reach the band's last column.
[[nodiscard]] std::vector<QRect> NeighbourOverhang() {
return {
QRect(64, 26, 8, 52),
QRect(61, 36, 4, 1),
QRect(61, 75, 6, 3),
};
}
[[nodiscard]] QColor Blend(QColor ground, QColor pen, double share) {
const auto channel = [&](int from, int to) {
return int(base::SafeRound(from + (to - from) * share));
};
return QColor(
channel(ground.red(), pen.red()),
channel(ground.green(), pen.green()),
channel(ground.blue(), pen.blue()));
}
// The advance-cell reader the isolation replaces: the union of every band
// pixel at the declared coverage, whichever glyph it belongs to.
[[nodiscard]] QRect ContaminatedUnion(
const QImage &image,
QRect band,
QColor background,
QColor pen) {
auto result = QRect();
for (auto y = band.top(); y <= band.bottom(); ++y) {
for (auto x = band.left(); x <= band.right(); ++x) {
if (ReachesCoverage(image.pixelColor(x, y), background, pen)) {
result = result.united(QRect(x, y, 1, 1));
}
}
}
return result;
}
[[nodiscard]] bool IsolatedRefused(
const IsolatedGlyph &reading,
IsolatedGlyphState state) {
const auto name = IsolatedGlyphStateName(state);
const auto box = reading.readBounds();
const auto text = FormatIsolatedGlyph(reading);
return (reading.state == state)
&& !box.read()
&& box.bounds.isEmpty()
&& reading.bounds.isEmpty()
&& box.refusal.contains(name)
&& text.contains(u"state="_q + name)
&& text.contains(u"bounds=none"_q);
}
[[nodiscard]] QString IsolatedRefusalText(const IsolatedGlyph &reading) {
return FormatIsolatedGlyph(reading)
+ u" | "_q
+ reading.readBounds().refusal;
}
void AppendIsolatedGlyphSelfTest(not_null<Runner*> runner) {
runner->add({
.name = u"isolated-glyph self-test: a capital beside a neighbour's "
"boundary overhang, a clean capital and the declared coverage"_q,
.run = [] {
const auto band = QRect(10, 0, 52, 88);
const auto capital = CapitalM(QPoint(16, 26));
const auto expected = UnitedRects(capital);
auto overhang = MakeIsolationFixture();
FillRects(overhang.image, capital, overhang.pen);
FillRects(overhang.image, NeighbourOverhang(), overhang.pen);
auto clean = MakeIsolationFixture();
FillRects(clean.image, capital, clean.pen);
auto coverage = MakeIsolationFixture();
const auto weak = QRect(15, 26, 1, 40);
const auto strong = QRect(56, 26, 1, 40);
FillRects(coverage.image, capital, coverage.pen);
FillRects(
coverage.image,
{ weak },
Blend(coverage.ground, coverage.pen, 0.4));
FillRects(
coverage.image,
{ strong },
Blend(coverage.ground, coverage.pen, 0.6));
const auto coverageExpected = expected.united(strong);
const auto contaminated = ContaminatedUnion(
overhang.image,
band,
overhang.ground,
overhang.pen);
const auto cleanUnion = ContaminatedUnion(
clean.image,
band,
clean.ground,
clean.pen);
const auto reading = ReadIsolatedGlyph(overhang.image, band);
const auto cleanReading = ReadIsolatedGlyph(clean.image, band);
const auto coverageReading = ReadIsolatedGlyph(
coverage.image,
band);
const auto box = reading.readBounds();
const auto cleanBox = cleanReading.readBounds();
const auto coverageBox = coverageReading.readBounds();
const auto text = FormatIsolatedGlyph(reading);
const auto cleanText = FormatIsolatedGlyph(cleanReading);
const auto coverageText = FormatIsolatedGlyph(coverageReading);
Note(u"isolated-glyph self-test: no window, session, chats list, "
"network, account or wallet - synthetic capitals on the night "
"ground"_q);
Check(
(expected == QRect(16, 26, 40, 40))
&& (contaminated == QRect(16, 26, 46, 52))
&& (contaminated != expected)
&& (cleanUnion == expected),
u"control: the contaminated all-ink union of the advance cell "
"takes in the neighbour's overhang instead of the drawn "
"capital, and on the clean capital it is exact"_q,
u"drawn=%1 contaminatedUnion=%2 cleanUnion=%3"_q.arg(
RectText(expected),
RectText(contaminated),
RectText(cleanUnion)));
Check(
reading.ok
&& (reading.state == IsolatedGlyphState::Isolated)
&& box.read()
&& (box.bounds == expected)
&& (reading.bounds == expected)
&& (int(reading.components.size()) == 3)
&& (reading.interior == 1)
&& (ColorHex(reading.background)
== ColorHex(overhang.ground))
&& (ColorHex(reading.pen) == ColorHex(overhang.pen))
&& text.contains(u"state=glyph-isolated"_q)
&& text.contains(u"coverage=0.50"_q)
&& text.contains(u"components=3 interior=1 bounds="_q
+ RectText(expected))
&& text.contains(
u"component=61,36 1x1 pixels=1 interior=0"_q)
&& text.contains(
u"component=61,75 1x3 pixels=3 interior=0"_q),
u"beside the neighbour's boundary overhang the reading "
"isolates the complete capital and reports the bounds the "
"stage drew, the overhang as boundary fragments"_q,
text);
Check(
(cleanReading.state == IsolatedGlyphState::Isolated)
&& cleanBox.read()
&& (cleanBox.bounds == expected)
&& (int(cleanReading.components.size()) == 1)
&& (cleanReading.interior == 1)
&& cleanText.contains(u"bounds="_q + RectText(expected)),
u"a clean single capital measures the bounds the stage drew"_q,
cleanText);
Check(
(coverageReading.state == IsolatedGlyphState::Isolated)
&& coverageBox.read()
&& (coverageExpected == QRect(16, 26, 41, 40))
&& (coverageBox.bounds == coverageExpected)
&& (int(coverageReading.components.size()) == 1)
&& coverageText.contains(u"coverage=0.50"_q),
u"at the fixed declared coverage a 60% blend column joins the "
"capital and a 40% blend column does not"_q,
u"expected=%1 weak=%2 strong=%3 "_q.arg(
RectText(coverageExpected),
RectText(weak),
RectText(strong))
+ coverageText);
},
});
runner->add({
.name = u"isolated-glyph self-test: named refusals with no usable "
"bounds"_q,
.run = [] {
const auto band = QRect(10, 0, 52, 88);
const auto cut = QRect(30, 0, 52, 88);
const auto capital = CapitalM(QPoint(16, 26));
const auto flat = MakeIsolationFixture();
auto single = MakeIsolationFixture();
FillRects(single.image, capital, single.pen);
auto speck = MakeIsolationFixture();
FillRects(speck.image, capital, speck.pen);
FillRects(speck.image, { QRect(70, 10, 2, 2) }, speck.pen);
auto pair = MakeIsolationFixture();
FillRects(pair.image, capital, pair.pen);
FillRects(pair.image, { QRect(66, 26, 6, 40) }, pair.pen);
auto dotted = MakeIsolationFixture();
FillRects(
dotted.image,
{ QRect(40, 30, 6, 6), QRect(40, 40, 6, 26) },
dotted.pen);
auto fragment = MakeIsolationFixture();
FillRects(fragment.image, capital, fragment.pen);
FillRects(fragment.image, { QRect(58, 70, 2, 3) }, fragment.pen);
auto fringe = MakeIsolationFixture();
FillRects(
fringe.image,
{ QRect(20, 30, 30, 2) },
QColor(200, 60, 60));
FillRects(
fringe.image,
{ QRect(20, 40, 30, 2) },
QColor(60, 200, 60));
FillRects(
fringe.image,
{ QRect(20, 50, 30, 2) },
QColor(60, 60, 200));
const auto noInk = ReadIsolatedGlyph(flat.image, band);
const auto outside = ReadIsolatedGlyph(
single.image,
QRect(60, 0, 52, 88));
const auto clipped = ReadIsolatedGlyph(single.image, cut);
const auto dominated = ReadIsolatedGlyph(speck.image, cut);
const auto twoCapitals = ReadIsolatedGlyph(
pair.image,
QRect(10, 0, 70, 88));
const auto disconnected = ReadIsolatedGlyph(dotted.image, band);
const auto contaminated = ReadIsolatedGlyph(fragment.image, band);
const auto noPen = ReadIsolatedGlyph(fringe.image, band);
Note(u"isolated-glyph self-test: no window, session, chats list, "
"network, account or wallet - synthetic bands that cannot "
"isolate one glyph"_q);
Check(
IsolatedRefused(noInk, IsolatedGlyphState::NoInk)
&& !noInk.ok
&& (noInk.inkPixels == -1)
&& noInk.reason.contains(u"no glyph ink"_q)
&& FormatIsolatedGlyph(noInk).contains(u"ink=none"_q),
u"a flat band refuses glyph-no-ink"_q,
IsolatedRefusalText(noInk));
Check(
IsolatedRefused(outside, IsolatedGlyphState::BandOutsideImage)
&& !outside.ok
&& !outside.background.isValid()
&& !outside.pen.isValid()
&& outside.reason.contains(u"wholly inside the image"_q)
&& FormatIsolatedGlyph(outside).contains(
u"bg=none pen=none"_q),
u"a band reaching past the image refuses "
"glyph-band-outside-image"_q,
IsolatedRefusalText(outside));
Check(
IsolatedRefused(clipped, IsolatedGlyphState::Clipped)
&& clipped.ok
&& (clipped.interior == 0)
&& !clipped.components.empty(),
u"a band cutting the capital refuses glyph-clipped"_q,
IsolatedRefusalText(clipped));
Check(
IsolatedRefused(dominated, IsolatedGlyphState::Clipped)
&& (dominated.interior == 1)
&& dominated.reason.contains(u"at least as large"_q),
u"the same cut beside an interior speck still refuses "
"glyph-clipped instead of taking the only interior "
"component"_q,
IsolatedRefusalText(dominated));
Check(
IsolatedRefused(twoCapitals, IsolatedGlyphState::Ambiguous)
&& (twoCapitals.interior == 2),
u"two complete capitals in one band refuse glyph-ambiguous"_q,
IsolatedRefusalText(twoCapitals));
Check(
IsolatedRefused(disconnected, IsolatedGlyphState::Ambiguous)
&& (disconnected.interior == 2),
u"a disconnected i, stem and dot, is unsupported and refuses "
"glyph-ambiguous rather than being unioned"_q,
IsolatedRefusalText(disconnected));
Check(
IsolatedRefused(contaminated, IsolatedGlyphState::Ambiguous)
&& (contaminated.interior == 2),
u"an overhang fragment clear of the band edge refuses "
"glyph-ambiguous rather than manufacturing a metric"_q,
IsolatedRefusalText(contaminated));
Check(
IsolatedRefused(noPen, IsolatedGlyphState::NoPen)
&& !noPen.ok
&& noPen.reason.contains(u"no-solid-core"_q),
u"ink with no solid core refuses glyph-no-pen"_q,
IsolatedRefusalText(noPen));
const auto states = std::array{
IsolatedGlyphState::Unread,
IsolatedGlyphState::BandOutsideImage,
IsolatedGlyphState::NoInk,
IsolatedGlyphState::NoPen,
IsolatedGlyphState::Clipped,
IsolatedGlyphState::Ambiguous,
IsolatedGlyphState::Isolated,
};
const auto cores = std::array{
GlyphCoreState::Unread,
GlyphCoreState::OutsideBand,
GlyphCoreState::NoPaint,
GlyphCoreState::NoSolidCore,
GlyphCoreState::NoBackground,
GlyphCoreState::Measured,
};
auto shipped = QStringList();
for (const auto &one : ShippedInkStateNames()) {
shipped.push_back(one);
}
for (const auto state : cores) {
shipped.push_back(GlyphCoreStateName(state));
}
auto names = QStringList();
auto distinct = true;
for (const auto state : states) {
const auto name = IsolatedGlyphStateName(state);
if (names.contains(name) || shipped.contains(name)) {
distinct = false;
}
names.push_back(name);
}
Check(
distinct,
u"every isolated-glyph state name differs from the others and "
"from every state the module already ships"_q,
u"names=[%1] shipped=[%2]"_q.arg(
names.join(u", "_q),
shipped.join(u", "_q)));
},
});
runner->add({
.name = u"isolated-glyph self-test: expected metrics stay with the "
"caller"_q,
.run = [] {
const auto band = QRect(10, 0, 52, 88);
const auto capital = CapitalM(QPoint(16, 26));
auto overhang = MakeIsolationFixture();
FillRects(overhang.image, capital, overhang.pen);
FillRects(overhang.image, NeighbourOverhang(), overhang.pen);
const auto before = ReadIsolatedGlyph(overhang.image, band);
const auto beforeText = FormatIsolatedGlyph(before);
const auto box = before.readBounds();
const auto heightAgrees = [&](int expected) {
return box.read() && (box.bounds.height() == expected);
};
const auto centreAgrees = [&](double expected) {
const auto centre = box.bounds.y() + box.bounds.height() / 2.;
return box.read() && (std::abs(centre - expected) <= 0.5);
};
const auto height40 = heightAgrees(40);
const auto height30 = heightAgrees(30);
const auto centre46 = centreAgrees(46.);
const auto centre40 = centreAgrees(40.);
const auto after = ReadIsolatedGlyph(overhang.image, band);
const auto afterText = FormatIsolatedGlyph(after);
Note(u"isolated-glyph self-test: no window, session, chats list, "
"network, account or wallet - two caller expectations over "
"one synthetic overhang band"_q);
Check(
height40 && !height30 && centre46 && !centre40,
u"control: the caller's own assertions decide the metric - "
"expected cap height 40 agrees and 30 does not, expected "
"centre 46 agrees and 40 does not"_q,
u"bounds=%1 height40=%2 height30=%3 centre46=%4 "
"centre40=%5"_q
.arg(box.read() ? RectText(box.bounds) : u"none"_q)
.arg(height40 ? 1 : 0)
.arg(height30 ? 1 : 0)
.arg(centre46 ? 1 : 0)
.arg(centre40 ? 1 : 0));
Check(
(before.state == IsolatedGlyphState::Isolated)
&& (before.bounds == UnitedRects(capital))
&& (after.bounds == before.bounds)
&& (afterText == beforeText),
u"the reading taken before and after those opposite verdicts "
"is identical: the reader takes only the image and the band, "
"no expected height, centre or metric"_q,
u"signature=ReadIsolatedGlyph(const QImage &image, QRect band) "
"before: "_q
+ beforeText
+ u" || after: "_q
+ afterText);
},
});
}
} // namespace
void AppendDeriveBandSelfTest(not_null<Runner*> runner) {
runner->add({
.name = u"derive-band self-test: underivable fill, separable "
"control, and no-rows refusal"_q,
.run = [] {
const auto fill = QColor(0x29, 0xb0, 0x71);
const auto surround = QColor(0x17, 0x21, 0x2b);
const auto size = QSize(64, 32);
const auto candidate = QRect(8, 4, 48, 24);
const auto underivableReason = u"the requested fill is the "
"image's background outside the candidate, so no band "
"can be derived"_q;
const auto noRowsReason = u"no row of the recovered box has "
"the pill fill as its own background"_q;
auto same = QImage(size, QImage::Format_ARGB32_Premultiplied);
same.fill(fill);
const auto underivable = DeriveBand(same, candidate, fill);
const auto measured = MeasurePaintedInk(
same,
candidate,
fill,
{ { u"ink"_q, QColor(255, 255, 255) } });
auto separable = QImage(
size,
QImage::Format_ARGB32_Premultiplied);
separable.fill(surround);
{
auto p = QPainter(&separable);
p.fillRect(candidate, fill);
}
const auto derived = DeriveBand(separable, candidate, fill);
const auto measuredOk = MeasurePaintedInk(
separable,
candidate,
fill,
{ { u"ink"_q, QColor(255, 255, 255) } });
auto none = QImage(size, QImage::Format_ARGB32_Premultiplied);
none.fill(surround);
const auto noRows = DeriveBand(none, candidate, fill);
Note(u"derive-band self-test: no window, session, chats list, "
"network, account or wallet - three synthetic images"_q);
Check(
!underivable.ok
&& (underivable.reason == underivableReason)
&& (underivable.fillRows > 0)
&& underivable.rows.empty(),
u"a candidate whose fill is the surrounding background "
"is refused with the underivable-band reason"_q,
u"ok=%1 fillRows=%2 rows=%3 reason=%4"_q
.arg(underivable.ok ? 1 : 0)
.arg(underivable.fillRows)
.arg(int(underivable.rows.size()))
.arg(underivable.reason));
Check(
!measured.derived.ok
&& measured.report.contains(underivableReason),
u"MeasurePaintedInk carries that reason into its report "
"rather than a bare zero"_q,
u"report=%1 derivedOk=%2"_q
.arg(measured.report)
.arg(measured.derived.ok ? 1 : 0));
Check(
derived.ok
&& (derived.reason == u"none"_q)
&& !derived.rows.empty()
&& !derived.fillRegion.isEmpty()
&& !derived.band.isEmpty(),
u"a genuinely separable band still derives with its rows "
"and region"_q,
u"ok=%1 rows=%2 fillRows=%3 fillRegion=%4x%5 band=%6x%7 "
"reason=%8"_q
.arg(derived.ok ? 1 : 0)
.arg(int(derived.rows.size()))
.arg(derived.fillRows)
.arg(derived.fillRegion.width())
.arg(derived.fillRegion.height())
.arg(derived.band.width())
.arg(derived.band.height())
.arg(derived.reason));
Check(
measuredOk.derived.ok
&& measuredOk.report.startsWith(u"fill="_q),
u"MeasurePaintedInk on a separable band still writes a "
"FormatInkReport line"_q,
u"report=%1 derivedOk=%2"_q
.arg(measuredOk.report)
.arg(measuredOk.derived.ok ? 1 : 0));
Check(
!noRows.ok
&& (noRows.reason == noRowsReason)
&& (noRows.reason != underivableReason)
&& noRows.rows.empty(),
u"an image whose rows exist but none matches the fill "
"still produces the existing no-rows refusal"_q,
u"ok=%1 fillRows=%2 reason=%3"_q
.arg(noRows.ok ? 1 : 0)
.arg(noRows.fillRows)
.arg(noRows.reason));
Check(
ChannelDelta(fill, surround) > kBackgroundSame,
u"the separable control's surround is farther from the "
"fill than kBackgroundSame"_q,
u"delta=%1 kBackgroundSame=%2"_q
.arg(ChannelDelta(fill, surround))
.arg(kBackgroundSame));
},
});
runner->add({
.name = u"ink scan self-test: a classifying band, its counts read "
"back, and an out-of-range ask refused"_q,
.run = [] {
const auto background = QColor(0x17, 0x21, 0x2b);
const auto mark = QColor(0xff, 0xff, 0xff);
const auto text = QColor(0x29, 0xb0, 0x71);
const auto size = QSize(64, 32);
const auto band = QRect(8, 8, 48, 12);
const auto candidates = std::vector<InkCandidate>{
{ u"mark"_q, mark },
{ u"text"_q, text },
};
auto painted = QImage(
size,
QImage::Format_ARGB32_Premultiplied);
painted.fill(background);
{
auto p = QPainter(&painted);
p.fillRect(QRect(16, 8, 6, 12), mark);
p.fillRect(QRect(32, 8, 4, 12), text);
}
auto blank = QImage(size, QImage::Format_ARGB32_Premultiplied);
blank.fill(background);
const auto scan = ScanInk(painted, band, candidates);
const auto blankScan = ScanInk(blank, band, candidates);
const auto lone = ScanInk(
painted,
band,
{ { u"mark"_q, mark } });
const auto markCount = scan.countAt(0);
const auto textCount = scan.countAt(1);
const auto blankCount = blankScan.countAt(0);
const auto loneCount = lone.countAt(0);
const auto pastEnd = scan.countAt(2);
const auto beforeStart = scan.countAt(-1);
const auto formatted = FormatInkScan(scan, background);
const auto thresholds = std::vector<QString>{
u"kInkDelta=%1"_q.arg(kInkDelta),
u"kOnLine=%1"_q.arg(kOnLine),
u"kInkMargin=%1"_q.arg(kInkMargin),
u"kSameTolerance=%1"_q.arg(kSameTolerance),
u"kBackgroundSame=%1"_q.arg(kBackgroundSame),
};
auto thresholdsNamed = true;
for (const auto &one : thresholds) {
if (!formatted.contains(one)) {
thresholdsNamed = false;
}
}
Note(u"ink scan self-test: no window, session, chats list, "
"network, account or wallet - two synthetic images"_q);
Check(
scan.ok
&& (scan.state == InkScanState::Classified)
&& (scan.reason == u"none"_q)
&& (scan.candidates.size() == 2)
&& (scan.counts.size() == scan.candidates.size()),
u"a band whose two candidates separate classifies, with "
"its counts vector as long as its candidates"_q,
formatted);
Check(
markCount.read()
&& (markCount.name == u"mark"_q)
&& (markCount.count == 72)
&& textCount.read()
&& (textCount.name == u"text"_q)
&& (textCount.count == 48)
&& blankCount.read()
&& (blankCount.count == 0),
u"each candidate's count reads back through countAt as the "
"ink the fixture painted, against an image that painted "
"none"_q,
InkCountDetails(markCount)
+ u" | "_q
+ InkCountDetails(textCount)
+ u" | control "_q
+ InkCountDetails(blankCount));
Check(
!pastEnd.read()
&& (pastEnd.count == -1)
&& pastEnd.refusal.contains(
u"outside this reading's 2 candidates"_q)
&& pastEnd.refusal.contains(u"mark, text"_q)
&& !beforeStart.read()
&& (beforeStart.count == -1),
u"an index this reading does not have is refused by name "
"instead of read past the end of its counts"_q,
InkCountDetails(pastEnd)
+ u" | "_q
+ InkCountDetails(beforeStart));
Check(
formatted.startsWith(u"fill="_q)
&& formatted.contains(
u"mark(%1)=72"_q.arg(ColorHex(mark)))
&& formatted.contains(
u"text(%1)=48"_q.arg(ColorHex(text)))
&& formatted.contains(u"collision=none"_q)
&& thresholdsNamed,
u"the passing reading's own text names the candidate "
"colours, their counts and every threshold it classified "
"against"_q,
formatted);
Check(
lone.ok
&& (lone.state == InkScanState::Classified)
&& loneCount.read()
&& (loneCount.count == 72)
&& (lone.ambiguous == 48),
u"the collision gate leaves a lone-candidate reading "
"untouched, which is the shape the only tracked caller "
"passes"_q,
FormatInkScan(lone, background)
+ u" | "_q
+ InkCountDetails(loneCount));
},
});
runner->add({
.name = u"ink scan self-test: four named refusals, a collision the "
"reading names, and a scanned band with no ink"_q,
.run = [] {
// The pill control's fill is the same green the band paints as
// its text ink: that control's derived band is nothing but the
// fill, so every pixel in it reads as its own background and
// the reading is a measured NoInk rather than a refusal, which
// is what keeps the existing stage's fill= report check true.
const auto surround = QColor(0x17, 0x21, 0x2b);
const auto fill = QColor(0x29, 0xb0, 0x71);
const auto mark = QColor(0xff, 0xff, 0xff);
const auto nearMark = QColor(0xf5, 0xf5, 0xf5);
const auto size = QSize(64, 32);
const auto candidate = QRect(8, 4, 48, 24);
const auto band = QRect(8, 8, 48, 12);
const auto away = QRect(96, 96, 8, 8);
const auto candidates = std::vector<InkCandidate>{
{ u"mark"_q, mark },
{ u"text"_q, fill },
};
const auto colliding = std::vector<InkCandidate>{
{ u"mark"_q, mark },
{ u"mark-again"_q, nearMark },
};
const auto emptyBoxReason = u"the recovered box is empty or "
"the fill is invalid"_q;
const auto noFillRowsReason = u"no row of the recovered box "
"has the pill fill as its own background"_q;
const auto underivableReason = u"the requested fill is the "
"image's background outside the candidate, so no band can "
"be derived"_q;
const auto noBandRowsReason = u"no row of the derived band "
"kept the pill fill as its own background"_q;
auto painted = QImage(
size,
QImage::Format_ARGB32_Premultiplied);
painted.fill(surround);
{
auto p = QPainter(&painted);
p.fillRect(QRect(16, 8, 6, 12), mark);
p.fillRect(QRect(32, 8, 4, 12), fill);
}
auto blank = QImage(size, QImage::Format_ARGB32_Premultiplied);
blank.fill(surround);
auto solid = QImage(size, QImage::Format_ARGB32_Premultiplied);
solid.fill(surround);
{
auto p = QPainter(&solid);
p.fillRect(candidate, fill);
}
const auto outside = ScanInk(painted, away, candidates);
const auto noRows = ScanInk(
painted,
band,
candidates,
{ 0, 31 });
const auto collided = ScanInk(painted, band, colliding);
const auto noInk = ScanInk(blank, band, candidates);
const auto outsideCount = outside.countAt(0);
const auto collidedCount = collided.countAt(0);
const auto noInkCount = noInk.countAt(0);
const auto outsideText = FormatInkScan(outside, surround);
const auto collidedText = FormatInkScan(collided, surround);
const auto collisionDump = CollisionDump(colliding);
const auto outsideReason = u"the requested band [%1] does not "
"intersect the image [%2], so no pixel was scanned"_q
.arg(RectText(away), RectText(painted.rect()));
const auto noRowsReason = u"none of the %1 requested rows "
"falls inside the band [%2], so no pixel was scanned"_q
.arg(2)
.arg(RectText(band));
const auto deriveReasons = std::vector<QString>{
emptyBoxReason,
noFillRowsReason,
underivableReason,
noBandRowsReason,
};
auto distinct = (outside.reason != noRows.reason);
auto deriveList = QString();
for (const auto &one : deriveReasons) {
if ((outside.reason == one) || (noRows.reason == one)) {
distinct = false;
}
if (!deriveList.isEmpty()) {
deriveList += u"; "_q;
}
deriveList += one;
}
auto attributed = 0;
for (const auto count : collided.counts) {
attributed += count;
}
const auto measuredOk = MeasurePaintedInk(
solid,
candidate,
fill,
candidates);
const auto measuredCollide = MeasurePaintedInk(
solid,
candidate,
fill,
colliding);
const auto measuredRefused = MeasurePaintedInk(
blank,
candidate,
fill,
candidates);
const auto refusedCount = measuredRefused.scan.countAt(0);
const auto thresholds = std::vector<QString>{
u"kInkDelta=%1"_q.arg(kInkDelta),
u"kOnLine=%1"_q.arg(kOnLine),
u"kInkMargin=%1"_q.arg(kInkMargin),
u"kSameTolerance=%1"_q.arg(kSameTolerance),
u"kBackgroundSame=%1"_q.arg(kBackgroundSame),
};
auto thresholdsNamed = true;
for (const auto &one : thresholds) {
if (!measuredOk.report.contains(one)) {
thresholdsNamed = false;
}
}
Note(u"ink scan self-test: no window, session, chats list, "
"network, account or wallet - three synthetic images"_q);
Check(
!outside.ok
&& (outside.state == InkScanState::OutsideImage)
&& (outside.reason == outsideReason)
&& outside.reason.contains(
u"does not intersect the image"_q)
&& (outside.band == QRect()),
u"a band that does not intersect the image refuses by name "
"and quotes the band it was asked for"_q,
outsideText);
Check(
!noRows.ok
&& (noRows.state == InkScanState::NoRowsInBand)
&& (noRows.reason == noRowsReason)
&& (noRows.band == band),
u"an explicit row list with no row inside the band refuses "
"by name and still carries the clip"_q,
FormatInkScan(noRows, surround));
Check(
distinct,
u"the two scan refusals differ from each other and from "
"all four DeriveBand reasons read back here"_q,
u"outside=%1 noRows=%2 derive=[%3]"_q
.arg(outside.reason, noRows.reason, deriveList));
Check(
(outside.candidates.size() == 2)
&& (outside.counts.size() == outside.candidates.size())
&& (outsideCount.name == u"mark"_q)
&& !outsideCount.read()
&& (outsideCount.count == -1)
&& outsideCount.refusal.contains(
u"classified nothing"_q),
u"a refused scan still names the candidates it was asked "
"to classify against and refuses every count"_q,
outsideText + u" | "_q + InkCountDetails(outsideCount));
Check(
collided.ok
&& (collided.state == InkScanState::CandidatesCollide)
&& (collided.inkPixels == 120)
&& (collided.ambiguous == collided.inkPixels)
&& !attributed
&& !collided.classifiedInk.isValid()
&& !collidedCount.read()
&& (collidedCount.name == u"mark"_q)
&& collidedText.startsWith(u"fill="_q)
&& collidedText.contains(collisionDump)
&& collidedText.contains(
u"kSameTolerance=%1"_q.arg(kSameTolerance)),
u"a colliding candidate pair is still a scanned reading "
"whose text names the collision"_q,
collidedText + u" | "_q + InkCountDetails(collidedCount));
Check(
noInk.ok
&& (noInk.state == InkScanState::NoInk)
&& !noInk.inkPixels
&& noInkCount.read()
&& (noInkCount.count == 0)
&& noInk.reason.contains(u"no pixel of the band"_q),
u"a band that was scanned and held no ink reads back a "
"real zero rather than a refusal"_q,
FormatInkScan(noInk, surround)
+ u" | "_q
+ InkCountDetails(noInkCount));
Check(
!outsideText.isEmpty()
&& outsideText.startsWith(u"fill="_q)
&& outsideText.contains(outside.reason),
u"FormatInkScan over a refused scan reading carries that "
"reading's own reason and never nothing, which is the text "
"MeasurePaintedInk's scan branch writes"_q,
outsideText);
Check(
measuredOk.derived.ok
&& measuredOk.report.startsWith(u"fill="_q)
&& measuredOk.report.contains(u"state=no-ink"_q)
&& thresholdsNamed,
u"a passing MeasurePaintedInk still starts with its "
"measured fill and now says what it was measured against"_q,
measuredOk.report);
Check(
measuredCollide.derived.ok
&& measuredCollide.report.startsWith(u"fill="_q)
&& measuredCollide.report.contains(collisionDump),
u"MeasurePaintedInk end to end on a colliding candidate "
"pair names the collision in its report"_q,
measuredCollide.report);
Check(
!measuredRefused.derived.ok
&& (measuredRefused.scan.state
== InkScanState::NotScanned)
&& !measuredRefused.report.isEmpty()
&& !refusedCount.read()
&& (refusedCount.count == -1)
&& refusedCount.refusal.contains(u"not-scanned"_q),
u"the reading a refused derivation leaves behind names itself "
"not-scanned and refuses every count instead of reading past "
"the end of a vector it never filled"_q,
measuredRefused.report
+ u" | "_q
+ InkCountDetails(refusedCount));
},
});
AppendBackgroundCollinearSelfTest(runner);
AppendChromaticRasterSelfTest(runner);
AppendGlyphCoreSelfTest(runner);
AppendGlyphSurroundSelfTest(runner);
AppendIsolatedGlyphSelfTest(runner);
}
} // namespace Test
#endif // _DEBUG || TDESKTOP_RELEASE_TEST_HARNESS