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_codecs.hpp>
3#include <haio_convert.hpp>
4
5#include <string>
6
7namespace Haio::Codecs {
8
9/**
10 * @addtogroup encode
11 * @{
12 */
13/**
14 * takes rgba and drops the alpha on the way out, so the caller does not have to
15 * convert first. the header and the pixels share one buffer: Move writes straight
16 * into the tail instead of building a second image to copy from.
17 */
18template <>
20 const Size size{img.width, img.height};
21 HAIO_TRY(incoming, sizeOf(Color::RGBA8888, size));
22 if (img.data.size() != incoming) HAIO_FAIL(InvalidInput, "invalid rgba8888 image for ppm encode");
23
24 HAIO_TRY(pixels, sizeOf(Color::RGB888, size));
25 const auto header = "P6\n" + std::to_string(img.width) + " " + std::to_string(img.height) + "\n255\n";
26
27 std::vector<uint8_t> out(header.size() + pixels);
28 std::copy(header.begin(), header.end(), out.begin());
29
30 if (auto moved = Move<Color::RGBA8888, Color::RGB888>(img.data, std::span{out}.subspan(header.size()), size); !moved) {
31 return std::unexpected(moved.error());
32 }
33 return Blob{Format::PPM, Color::RGB888, "image/x-portable-pixmap", {}, std::move(out)};
34}
35/** @} */
36
37}
Result< Blob > Encode< Format::PPM, Color::RGBA8888 >(Image< Color::RGBA8888 > img)
Definition encode.cpp:19
#define HAIO_TRY(name, expr)
#define HAIO_FAIL(why, message)
Result< void > Move(Bytes src, std::span< uint8_t > dst, Size size)=delete
std::expected< T, Error > Result
Result< size_t > sizeOf(Color color, Size size)
Definition sizes.cpp:6
std::vector< uint8_t > data