Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
encode.cpp
Go to the documentation of this file.
1#include <haio_codec.hpp>
2#include <haio_convert.hpp>
3#include <haio_util.hpp>
4
5namespace Haio::Codecs {
6namespace {
7
8template <Color P>
9Result<Blob> encodeKtx(Image<P> img) {
10 const Size size{img.width, img.height};
11 HAIO_TRY(expected, sizeOf(P, size));
12 if (img.data.size() != expected) HAIO_FAIL(InvalidInput, "ktx payload does not match its size");
13
14 HAIO_TRY(profile, Util::GPU::profileFor(P, size));
15 std::vector<uint8_t> out = {0xab, 0x4b, 0x54, 0x58, 0x20, 0x31, 0x31, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a};
16 for (auto v : {0x04030201u, profile.glType, Util::GPU::typeSize(profile), profile.glFormat, profile.glInternal,
17 profile.glBase, static_cast<uint32_t>(size.width), static_cast<uint32_t>(size.height),
18 0u, 0u, 1u, 1u, 0u}) {
19 Util::appendU32LE(out, v);
20 }
21 Util::appendU32LE(out, static_cast<uint32_t>(img.data.size()));
22 out.insert(out.end(), img.data.begin(), img.data.end());
23 while (out.size() % 4) out.push_back(0);
24
25 return Blob{Format::KTX, P, "image/ktx", {}, std::move(out)};
26}
27
28}
29
30/**
31 * @addtogroup encode
32 * @{
33 */
34template <>
36 return encodeKtx<Color::ETC1>(std::move(img));
37}
38/** @} */
39
40/**
41 * @addtogroup encode
42 * @{
43 */
44template <>
46 return encodeKtx<Color::RGB565>(std::move(img));
47}
48/** @} */
49
50/**
51 * @addtogroup encode
52 * @{
53 */
54template <>
56 return encodeKtx<Color::RGBA8888>(std::move(img));
57}
58/** @} */
59
60}
Result< Blob > Encode< Format::KTX, Color::ETC1 >(Image< Color::ETC1 > img)
Definition encode.cpp:35
Result< Blob > Encode< Format::KTX, Color::RGB565 >(Image< Color::RGB565 > img)
Definition encode.cpp:45
Result< Blob > Encode< Format::KTX, Color::RGBA8888 >(Image< Color::RGBA8888 > img)
Definition encode.cpp:55
#define HAIO_TRY(name, expr)
#define HAIO_FAIL(why, message)
Result< Profile > profileFor(Color color, Size size)
Definition gpu.cpp:20
uint32_t typeSize(const Profile &profile)
Definition gpu.cpp:34
void appendU32LE(std::vector< uint8_t > &out, uint32_t value)
Definition bytes.cpp:9
std::expected< T, Error > Result
Result< size_t > sizeOf(Color color, Size size)
Definition sizes.cpp:6
std::vector< uint8_t > data