Close seams where an SVG's shapes meet
Anti-aliasing leaves a partly transparent hairline where two shapes share an edge (each covers about half of those pixels), and the background shows through. After drawing an SVG, a partly covered pixel with opaque pixels on both sides of it, along any axis within two pixels, takes their color over its own and becomes opaque; outer edges keep their anti-aliasing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4141c53681
commit
a6f9b425e8
2 files changed
+69
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
#include "image.hpp"
|
#include "image.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <climits>
|
||||||
|
#include <cmath>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
@@ -50,6 +52,9 @@ namespace anm2ed::resource
|
|||||||
constexpr auto XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
|
constexpr auto XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
|
||||||
constexpr auto SVG_ID_PREFIX = "anm2ed-source-";
|
constexpr auto SVG_ID_PREFIX = "anm2ed-source-";
|
||||||
constexpr std::string_view SVG_URL_START = "url(#";
|
constexpr std::string_view SVG_URL_START = "url(#";
|
||||||
|
constexpr int ALPHA = 3; // a pixel's alpha channel
|
||||||
|
constexpr int SEAM_REACH = 2;
|
||||||
|
constexpr uint8_t SEAM_OPAQUE_ALPHA = 250;
|
||||||
// A drawing's root attributes that place it, rather than style it.
|
// A drawing's root attributes that place it, rather than style it.
|
||||||
constexpr std::string_view SVG_ROOT_ONLY_ATTRIBUTES[] = {"width", "height", "viewBox", "preserveAspectRatio",
|
constexpr std::string_view SVG_ROOT_ONLY_ATTRIBUTES[] = {"width", "height", "viewBox", "preserveAspectRatio",
|
||||||
"x", "y", "version", "id"};
|
"x", "y", "version", "id"};
|
||||||
@@ -60,6 +65,51 @@ namespace anm2ed::resource
|
|||||||
|
|
||||||
glm::vec2 Image::unit_size_get() const { return glm::vec2(size) / scale; }
|
glm::vec2 Image::unit_size_get() const { return glm::vec2(size) / scale; }
|
||||||
|
|
||||||
|
// Closes the hairline seams an SVG's anti-aliasing leaves where two shapes meet: a partly covered pixel with opaque
|
||||||
|
// pixels on both sides of it (along any axis, within SEAM_REACH) takes their color over its own and becomes opaque.
|
||||||
|
// Outer edges, with transparency on one side, keep their anti-aliasing.
|
||||||
|
void seams_fill(Image& image)
|
||||||
|
{
|
||||||
|
constexpr glm::ivec2 AXES[] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};
|
||||||
|
// Coverage as drawn (filling only ever raises it, and only on partly covered pixels, whose colors no check reads).
|
||||||
|
std::vector<uint8_t> alphas((std::size_t)image.size.x * image.size.y);
|
||||||
|
for (std::size_t i = 0; i < alphas.size(); ++i)
|
||||||
|
alphas[i] = image.pixels[i * CHANNELS + ALPHA];
|
||||||
|
auto index_get = [&](glm::ivec2 point) { return (std::size_t)point.y * image.size.x + point.x; };
|
||||||
|
auto opaque_get = [&](glm::ivec2 point, glm::ivec2 step) -> const uint8_t*
|
||||||
|
{
|
||||||
|
for (int reach = 1; reach <= SEAM_REACH; ++reach)
|
||||||
|
{
|
||||||
|
auto neighbor = point + step * reach;
|
||||||
|
if (glm::any(glm::lessThan(neighbor, glm::ivec2(0))) || glm::any(glm::greaterThanEqual(neighbor, image.size)))
|
||||||
|
return nullptr;
|
||||||
|
if (alphas[index_get(neighbor)] >= SEAM_OPAQUE_ALPHA)
|
||||||
|
return image.pixels.data() + index_get(neighbor) * CHANNELS;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int y = 0; y < image.size.y; ++y)
|
||||||
|
for (int x = 0; x < image.size.x; ++x)
|
||||||
|
{
|
||||||
|
auto alpha = alphas[index_get({x, y})];
|
||||||
|
if (alpha == 0 || alpha == UINT8_MAX) continue;
|
||||||
|
for (auto axis : AXES)
|
||||||
|
{
|
||||||
|
auto before = opaque_get({x, y}, -axis);
|
||||||
|
auto after = before ? opaque_get({x, y}, axis) : nullptr;
|
||||||
|
if (!after) continue;
|
||||||
|
auto pixel = image.pixels.data() + index_get({x, y}) * CHANNELS;
|
||||||
|
auto coverage = alpha / (float)UINT8_MAX;
|
||||||
|
for (int channel = 0; channel < ALPHA; ++channel)
|
||||||
|
pixel[channel] = (uint8_t)std::round(pixel[channel] * coverage +
|
||||||
|
(before[channel] + after[channel]) * 0.5f * (1.0f - coverage));
|
||||||
|
pixel[ALPHA] = UINT8_MAX;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// An SVG drawn at about `scale` pixels per unit (exactly, after rounding to whole pixels); nothing if it won't parse.
|
// An SVG drawn at about `scale` pixels per unit (exactly, after rounding to whole pixels); nothing if it won't parse.
|
||||||
Image svg_render(std::shared_ptr<const std::string> svg, float scale)
|
Image svg_render(std::shared_ptr<const std::string> svg, float scale)
|
||||||
{
|
{
|
||||||
@@ -70,6 +120,7 @@ namespace anm2ed::resource
|
|||||||
if (bitmap.isNull()) return {};
|
if (bitmap.isNull()) return {};
|
||||||
bitmap.convertToRGBA();
|
bitmap.convertToRGBA();
|
||||||
Image image(bitmap.data(), size, true);
|
Image image(bitmap.data(), size, true);
|
||||||
|
seams_fill(image);
|
||||||
image.svg = std::move(svg);
|
image.svg = std::move(svg);
|
||||||
image.scale = (float)size.x / document->width();
|
image.scale = (float)size.x / document->width();
|
||||||
return image;
|
return image;
|
||||||
|
|||||||
@@ -98,3 +98,21 @@ TEST_CASE("SVG spritesheets compose into SVGs that draw the same")
|
|||||||
CHECK(Image::compose({{&mixed, {}, mixed.size, {}}}, mixed.size, true).pixel_read({24.0f, 8.0f}) ==
|
CHECK(Image::compose({{&mixed, {}, mixed.size, {}}}, mixed.size, true).pixel_read({24.0f, 8.0f}) ==
|
||||||
vector.pixel_read({8.0f, 8.0f}));
|
vector.pixel_read({8.0f, 8.0f}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("an SVG's shapes meet without a seam")
|
||||||
|
{
|
||||||
|
// Two rectangles of one color meeting at x 10.5: the pixel they share is solid, while the outer edge at 20.5 keeps
|
||||||
|
// its anti-aliasing.
|
||||||
|
auto text = std::make_shared<std::string>(R"(<svg xmlns="http://www.w3.org/2000/svg" width="24" height="4">
|
||||||
|
<rect x="0" y="0" width="10.5" height="4" fill="#804020"/>
|
||||||
|
<rect x="10.5" y="0" width="10" height="4" fill="#804020"/></svg>)");
|
||||||
|
auto path = std::filesystem::temp_directory_path() / "anm2ed_seam.svg";
|
||||||
|
test::file_save(path, *text);
|
||||||
|
Image image(path);
|
||||||
|
std::filesystem::remove(path);
|
||||||
|
REQUIRE(image.is_vector());
|
||||||
|
auto seam = image.pixel_read({10.0f, 2.0f});
|
||||||
|
CHECK(seam.a == 1.0f);
|
||||||
|
CHECK(glm::distance(glm::vec3(seam), glm::vec3(image.pixel_read({5.0f, 2.0f}))) < 2.0f / 255.0f);
|
||||||
|
CHECK(image.pixel_read({20.0f, 2.0f}).a < 1.0f);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user