/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include /**< @todo remove once Debug is stream-free */ #include #include #include #include /**< @todo remove once Debug is stream-free */ #include #include "Magnum/Image.h" #include "Magnum/ImageView.h" #include "Magnum/PixelFormat.h" #include "Magnum/DebugTools/CompareImage.h" #ifdef MAGNUM_TARGET_GLES #include "Magnum/DebugTools/TextureImage.h" #endif #include "Magnum/Math/Range.h" #include "Magnum/GL/OpenGLTester.h" #include "Magnum/GL/Extensions.h" #include "Magnum/GL/PixelFormat.h" #include "Magnum/Text/DistanceFieldGlyphCache.h" #include "Magnum/Trade/AbstractImporter.h" #include "Magnum/Trade/ImageData.h" #include "configure.h" namespace Magnum { namespace Text { namespace Test { namespace { struct DistanceFieldGlyphCacheGLTest: GL::OpenGLTester { explicit DistanceFieldGlyphCacheGLTest(); void initialize(); void setImage(); void setDistanceFieldImage(); void setDistanceFieldImageOutOfRange(); PluginManager::Manager _manager{"nonexistent"}; }; const struct { const char* name; Vector2i sourceSize, size, sourceOffset; Containers::Size2D offset; } SetImageData[]{ {"", {256, 256}, {64, 64}, {}, {}}, {"upload with offset", {512, 384}, {128, 96}, {256, 128}, {128/4, 256/4}}, }; DistanceFieldGlyphCacheGLTest::DistanceFieldGlyphCacheGLTest() { addTests({&DistanceFieldGlyphCacheGLTest::initialize}); addInstancedTests({&DistanceFieldGlyphCacheGLTest::setImage}, Containers::arraySize(SetImageData)); addTests({&DistanceFieldGlyphCacheGLTest::setDistanceFieldImage, &DistanceFieldGlyphCacheGLTest::setDistanceFieldImageOutOfRange}); /* Load the plugin directly from the build tree. Otherwise it's either static and already loaded or not present in the build tree */ #ifdef ANYIMAGEIMPORTER_PLUGIN_FILENAME CORRADE_INTERNAL_ASSERT_OUTPUT(_manager.load(ANYIMAGEIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); #endif #ifdef TGAIMPORTER_PLUGIN_FILENAME CORRADE_INTERNAL_ASSERT_OUTPUT(_manager.load(TGAIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); #endif } void DistanceFieldGlyphCacheGLTest::initialize() { DistanceFieldGlyphCache cache{{1024, 2048}, {128, 256}, 16}; MAGNUM_VERIFY_NO_GL_ERROR(); CORRADE_COMPARE(cache.size(), (Vector3i{1024, 2048, 1})); CORRADE_COMPARE(cache.distanceFieldTextureSize(), (Vector2i{128, 256})); #ifndef MAGNUM_TARGET_GLES CORRADE_COMPARE(cache.texture().imageSize(0), (Vector2i{128, 256})); #endif } void DistanceFieldGlyphCacheGLTest::setImage() { auto&& data = SetImageData[testCaseInstanceId()]; setTestCaseDescription(data.name); Containers::Pointer importer; if(!(importer = _manager.loadAndInstantiate("TgaImporter"))) CORRADE_SKIP("TgaImporter plugin not found."); CORRADE_VERIFY(importer->openFile(Utility::Path::join(TEXTURETOOLS_DISTANCEFIELDGLTEST_DIR, "input.tga"))); CORRADE_COMPARE(importer->image2DCount(), 1); Containers::Optional inputImage = importer->image2D(0); CORRADE_VERIFY(inputImage); CORRADE_COMPARE(inputImage->format(), PixelFormat::R8Unorm); CORRADE_COMPARE(inputImage->size(), (Vector2i{256, 256})); DistanceFieldGlyphCache cache{data.sourceSize, data.size, 32}; Containers::StridedArrayView3D src = inputImage->pixels(); /* Test also uploading under an offset. The cache might be three-component in some cases, slice the destination view to just the first component */ /** @todo actually the input can be just luminance, only the destination cannot -- fix by dropping the dependency on GlyphCache and creating the texture directly */ Utility::copy(src, cache.image().pixels()[0].sliceSize({ std::size_t(data.sourceOffset.y()), std::size_t(data.sourceOffset.x()), 0}, src.size())); cache.flushImage(Range2Di::fromSize(data.sourceOffset, inputImage->size())); MAGNUM_VERIFY_NO_GL_ERROR(); /* On GLES processedImage() isn't implemented as it'd mean creating a temporary framebuffer. Do it via DebugTools here instead, we cannot really verify that the size matches, but at least something. */ #ifndef MAGNUM_TARGET_GLES Image3D actual3 = cache.processedImage(); /** @todo ugh have slicing on images directly already */ MutableImageView2D actual{actual3.format(), actual3.size().xy(), actual3.data()}; #else /* Pick a format that matches the internal texture format. This is rather shitty, TBH. */ #if !(defined(MAGNUM_TARGET_GLES) && defined(MAGNUM_TARGET_GLES2)) const GL::PixelFormat format = GL::PixelFormat::Red; #else GL::PixelFormat format; #ifndef MAGNUM_TARGET_WEBGL if(GL::Context::current().isExtensionSupported()) { format = GL::PixelFormat::Red; } else #endif { format = GL::PixelFormat::RGB; } #endif Image2D actual = DebugTools::textureSubImage(cache.texture(), 0, {{}, data.size}, {format, GL::PixelType::UnsignedByte}); #endif MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); /* On GLES2 if EXT_unpack_subimage isn't supported or on WebGL 1, the whole texture gets uploaded and processed every time. Which circumvents this problem. */ #if defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL) CORRADE_EXPECT_FAIL_IF(GL::Context::current().isExtensionSupported() && !data.sourceOffset.isZero(), "The distance field tool is currently broken if a non-zero offset is used."); #elif !defined(MAGNUM_TARGET_GLES2) CORRADE_EXPECT_FAIL_IF(!data.sourceOffset.isZero(), "The distance field tool is currently broken if a non-zero offset is used."); #endif /* The format may be three-component, consider just the first channel */ Containers::StridedArrayView3D pixels = actual.pixels(); CORRADE_COMPARE_WITH((Containers::arrayCast<2, const UnsignedByte>(pixels.prefix({pixels.size()[0], pixels.size()[1], 1})).exceptPrefix(data.offset)), Utility::Path::join(TEXTURETOOLS_DISTANCEFIELDGLTEST_DIR, "output.tga"), /* Same threshold as in TextureTools DistanceFieldGLTest */ (DebugTools::CompareImageToFile{_manager, 1.0f, 0.178f})); } void DistanceFieldGlyphCacheGLTest::setDistanceFieldImage() { DistanceFieldGlyphCache cache({64, 32}, {16, 8}, 16); /* Pick a format that matches the internal texture format. This is rather shitty, TBH. */ #if !(defined(MAGNUM_TARGET_GLES) && defined(MAGNUM_TARGET_GLES2)) const GL::PixelFormat format = GL::PixelFormat::Red; #else GL::PixelFormat format; #ifndef MAGNUM_TARGET_WEBGL if(GL::Context::current().isExtensionSupported()) { format = GL::PixelFormat::Red; } else #endif { /* Ugh, don't want to bother implementing this */ CORRADE_SKIP("A three-component input is expected on ES2, skipping due to developer laziness."); } #endif /* Clear the texture first, as it'd have random garbage otherwise */ UnsignedByte zeros[16*8]{}; cache.setDistanceFieldImage({}, ImageView2D{format, GL::PixelType::UnsignedByte, {16, 8}, zeros}); MAGNUM_VERIFY_NO_GL_ERROR(); UnsignedByte data[]{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, }; cache.setDistanceFieldImage({8, 4}, ImageView2D{format, GL::PixelType::UnsignedByte, {8, 4}, data}); MAGNUM_VERIFY_NO_GL_ERROR(); /* On GLES processedImage() isn't implemented as it'd mean creating a temporary framebuffer. Do it via DebugTools here instead, we cannot really verify that the size matches, but at least something. */ #ifndef MAGNUM_TARGET_GLES Image3D actual3 = cache.processedImage(); /** @todo ugh have slicing on images directly already */ MutableImageView2D actual{actual3.format(), actual3.size().xy(), actual3.data()}; #else Image2D actualGL = DebugTools::textureSubImage(cache.texture(), 0, {{}, {16, 8}}, {format, GL::PixelType::UnsignedByte}); ImageView2D actual{*GL::genericPixelFormat(format, GL::PixelType::UnsignedByte), actualGL.size(), actualGL.data()}; #endif MAGNUM_VERIFY_NO_GL_ERROR(); UnsignedByte expected[]{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0, 0, 0, 0, 0, 0, 0, 0, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0, 0, 0, 0, 0, 0, 0, 0, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, }; CORRADE_COMPARE_AS(actual, (ImageView2D{PixelFormat::R8Unorm, {16, 8}, expected}), DebugTools::CompareImage); } void DistanceFieldGlyphCacheGLTest::setDistanceFieldImageOutOfRange() { CORRADE_SKIP_IF_NO_ASSERT(); DistanceFieldGlyphCache cache{{200, 300}, {100, 200}, 4}; /* This is fine. Not testing on ES2 as there it would need the complicated format logic from above. */ #if !(defined(MAGNUM_TARGET_GLES) && defined(MAGNUM_TARGET_GLES2)) cache.setDistanceFieldImage({80, 175}, ImageView2D{PixelFormat::R8Unorm, {20, 25}}); #endif std::ostringstream out; Error redirectError{&out}; cache.setDistanceFieldImage({81, 175}, ImageView2D{PixelFormat::R8Unorm, {20, 25}}); cache.setDistanceFieldImage({80, 176}, ImageView2D{PixelFormat::R8Unorm, {20, 25}}); cache.setDistanceFieldImage({-1, 175}, ImageView2D{PixelFormat::R8Unorm, {20, 25}}); cache.setDistanceFieldImage({80, -1}, ImageView2D{PixelFormat::R8Unorm, {20, 25}}); CORRADE_COMPARE_AS(out.str(), "Text::DistanceFieldGlyphCache::setDistanceFieldImage(): Range({81, 175}, {101, 200}) out of range for texture size Vector(100, 200)\n" "Text::DistanceFieldGlyphCache::setDistanceFieldImage(): Range({80, 176}, {100, 201}) out of range for texture size Vector(100, 200)\n" "Text::DistanceFieldGlyphCache::setDistanceFieldImage(): Range({-1, 175}, {19, 200}) out of range for texture size Vector(100, 200)\n" "Text::DistanceFieldGlyphCache::setDistanceFieldImage(): Range({80, -1}, {100, 24}) out of range for texture size Vector(100, 200)\n", TestSuite::Compare::String); } }}}} CORRADE_TEST_MAIN(Magnum::Text::Test::DistanceFieldGlyphCacheGLTest)