diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2015-03-16 19:30:08 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2015-03-17 12:30:40 +0100 |
commit | 48c55af5c1d39e9a29cdb2e0c8203e6767f76c57 (patch) | |
tree | b81115238586a172be3acc487822cd27f12efe79 /test | |
parent | 0b1faba36920be2c9343c912b4817448f5659f71 (diff) | |
download | qtlocation-mapboxgl-48c55af5c1d39e9a29cdb2e0c8203e6767f76c57.tar.gz |
fix sprites for pixel ratios that are not 1 and 2
- OpenGL ES 2 doesn't allow NPOT textures with wrap-around
- The Sprite object reported the map's pixelRatio, even though it loaded @2x assets
- Copying icons from the sprite into the atlas now uses bilinear scaling to scale up to the actual size
Diffstat (limited to 'test')
-rw-r--r-- | test/fixtures/sprites/atlas_reference.bin | bin | 0 -> 8137 bytes | |||
-rw-r--r-- | test/fixtures/sprites/atlas_reference.png | bin | 0 -> 9109 bytes | |||
-rw-r--r-- | test/fixtures/sprites/bright.bin | bin | 0 -> 68824 bytes | |||
-rw-r--r-- | test/fixtures/sprites/convert_sprite.js | 19 | ||||
-rw-r--r-- | test/miscellaneous/bilinear.cpp | 53 | ||||
-rw-r--r-- | test/test.gyp | 1 |
6 files changed, 73 insertions, 0 deletions
diff --git a/test/fixtures/sprites/atlas_reference.bin b/test/fixtures/sprites/atlas_reference.bin Binary files differnew file mode 100644 index 0000000000..57eb28bd93 --- /dev/null +++ b/test/fixtures/sprites/atlas_reference.bin diff --git a/test/fixtures/sprites/atlas_reference.png b/test/fixtures/sprites/atlas_reference.png Binary files differnew file mode 100644 index 0000000000..86fad30983 --- /dev/null +++ b/test/fixtures/sprites/atlas_reference.png diff --git a/test/fixtures/sprites/bright.bin b/test/fixtures/sprites/bright.bin Binary files differnew file mode 100644 index 0000000000..3aee130074 --- /dev/null +++ b/test/fixtures/sprites/bright.bin diff --git a/test/fixtures/sprites/convert_sprite.js b/test/fixtures/sprites/convert_sprite.js new file mode 100644 index 0000000000..ba4ff5c29f --- /dev/null +++ b/test/fixtures/sprites/convert_sprite.js @@ -0,0 +1,19 @@ +// Converts a PNG image to a custom "image format" that has a uint32_t width/height prefix and then +// raw RGBA data. We can't use the built-in PNG reading routines because they are reading +// premultiplied images by default. + +var fs = require('fs'); +var zlib = require('zlib'); +var PNG = require('png-js'); +var png = PNG.load('styles/sprites/bright.png'); +png.decodePixels(function(data) { + var result = new Buffer(8 + data.length); + result.writeUInt32BE(png.width, 0); + result.writeUInt32BE(png.height, 4); + data.copy(result, 8); + + zlib.deflate(result, function(err, data) { + if (err) throw err; + fs.writeFileSync('test/fixtures/sprites/bright.bin', data); + }); +}); diff --git a/test/miscellaneous/bilinear.cpp b/test/miscellaneous/bilinear.cpp new file mode 100644 index 0000000000..b7730303a0 --- /dev/null +++ b/test/miscellaneous/bilinear.cpp @@ -0,0 +1,53 @@ +#include "../fixtures/util.hpp" +#include <mbgl/util/compression.hpp> +#include <mbgl/util/scaling.hpp> +#include <mbgl/util/image.hpp> +#include <mbgl/util/io.hpp> +#include <mbgl/util/std.hpp> + +#include <algorithm> +#include <cstring> + +using namespace mbgl; + +TEST(Bilinear, Scaling) { + // We're reading from a custom "image format" that has a uint32_t width/height prefix and then + // raw RGBA data. We can't use the built-in PNG reading routines because they are reading + // premultiplied images by default. + const std::string sprite = util::decompress(util::read_file("test/fixtures/sprites/bright.bin")); + const uint8_t *src = reinterpret_cast<const uint8_t *>(sprite.data()); + ASSERT_GT(sprite.length(), 8u); + const uint32_t width = src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3]; + const uint32_t height = src[4] << 24 | src[5] << 16 | src[6] << 8 | src[7]; + ASSERT_EQ(sprite.length(), 2 * sizeof(uint32_t) + width * height * sizeof(uint32_t)); + + const uint32_t *srcData = reinterpret_cast<const uint32_t *>(src + 8); + const vec2<uint32_t> srcSize { width, height }; + const vec2<uint32_t> dstSize { 128, 128 }; + auto dst = util::make_unique<uint32_t[]>(dstSize.x * dstSize.y); + uint32_t *dstData = dst.get(); + std::fill(dstData, dstData + dstSize.x * dstSize.y, 0xFFFF00FF); + + util::bilinearScale(srcData, srcSize, { 0, 0, 24, 24 }, dstData, dstSize, { 8, 8, 24, 24 }); + util::bilinearScale(srcData, srcSize, { 26, 0, 24, 24 }, dstData, dstSize, { 0, 40, 48, 48 }); + util::bilinearScale(srcData, srcSize, { 26, 26, 24, 24 }, dstData, dstSize, { 52, 40, 36, 36 }); + util::bilinearScale(srcData, srcSize, { 26, 26, 24, 24 }, dstData, dstSize, { 52, 40, 36, 36 }); + util::bilinearScale(srcData, srcSize, { 104, 0, 24, 24 }, dstData, dstSize, { 96, 0, 48, 48 }); + util::bilinearScale(srcData, srcSize, { 52, 260, 24, 24 }, dstData, dstSize, { 108, 108, 38, 38 }); + util::bilinearScale(srcData, srcSize, { 380, 0, 24, 24 }, dstData, dstSize, { 36, 0, 24, 24 }); + util::bilinearScale(srcData, srcSize, { 396, 396, 24, 24 }, dstData, dstSize, { 0, 0, 50, 50 }); + util::bilinearScale(srcData, srcSize, { 380, 182, 12, 12 }, dstData, dstSize, { 52, 80, 24, 24 }); + + // From the bottom + util::bilinearScale(srcData, srcSize, { 252, 380, 12, 12 }, dstData, dstSize, { 0, 90, 12, 12 }); + util::bilinearScale(srcData, srcSize, { 252, 380, 12, 12 }, dstData, dstSize, { 18, 90, 24, 24 }); + + const std::string data { reinterpret_cast<char *>(dstData), dstSize.x * dstSize.y * sizeof(uint32_t) }; + util::write_file("test/fixtures/sprites/atlas_actual.png", util::compress_png(dstSize.x, dstSize.y, dstData)); + util::write_file("test/fixtures/sprites/atlas_actual.bin", util::compress(data)); + + const std::string reference = util::decompress(util::read_file("test/fixtures/sprites/atlas_reference.bin")); + + EXPECT_EQ(reference.size(), data.size()); + EXPECT_TRUE(0 == std::memcmp(data.data(), reference.data(), data.size())); +} diff --git a/test/test.gyp b/test/test.gyp index 719207a02e..c333a40064 100644 --- a/test/test.gyp +++ b/test/test.gyp @@ -38,6 +38,7 @@ 'headless/headless.cpp', 'miscellaneous/clip_ids.cpp', + 'miscellaneous/bilinear.cpp', 'miscellaneous/comparisons.cpp', 'miscellaneous/enums.cpp', 'miscellaneous/functions.cpp', |