Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
haio_convert.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "haio_codec.hpp"
4
5namespace Haio {
6
7/**
8 * how many bytes one image of this colour and size takes. block formats round up to
9 * whole blocks, so this is not always width times height times something.
10 */
11Result<size_t> sizeOf(Color color, Size size);
12
13namespace Codecs {
14
15/**
16 * the shape every Convert specialisation has: check, allocate, hand the work to Move.
17 * it lives here so a new colour pair is one Move and one line instead of ten.
18 */
19template <Color From, Color To>
20 requires Movable<From, To>
22 const Size size{src.width, src.height};
23 HAIO_TRY(want, sizeOf(From, size));
24 if (src.data.size() != want) {
25 HAIO_FAIL(InvalidInput, "image data does not match its own size");
26 }
27
28 HAIO_TRY(bytes, sizeOf(To, size));
29 Image<To> out{src.width, src.height, std::vector<uint8_t>(bytes)};
30 if (auto moved = Move<From, To>(src.data, out.data, size); !moved) {
31 return std::unexpected(moved.error());
32 }
33 return out;
34}
35
36}
37
38}
#define HAIO_TRY(name, expr)
#define HAIO_FAIL(why, message)
Result< Image< To > > convertVia(Image< From > src)
Result< void > Move(Bytes src, std::span< uint8_t > dst, Size size)=delete
Definition haio.hpp:11
std::expected< T, Error > Result
Result< size_t > sizeOf(Color color, Size size)
Definition sizes.cpp:6
std::vector< uint8_t > data